:py:mod:`anomalib.models.ganomaly.lightning_model` ================================================== .. py:module:: anomalib.models.ganomaly.lightning_model .. autoapi-nested-parse:: GANomaly: Semi-Supervised Anomaly Detection via Adversarial Training. https://arxiv.org/abs/1805.06725 Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.ganomaly.lightning_model.Ganomaly anomalib.models.ganomaly.lightning_model.GanomalyLightning Attributes ~~~~~~~~~~ .. autoapisummary:: anomalib.models.ganomaly.lightning_model.logger .. py:data:: logger .. py:class:: Ganomaly(batch_size: int, input_size: Tuple[int, int], n_features: int, latent_vec_size: int, extra_layers: int = 0, add_final_conv_layer: bool = True, wadv: int = 1, wcon: int = 50, wenc: int = 1, lr: float = 0.0002, beta1: float = 0.5, beta2: float = 0.999) Bases: :py:obj:`anomalib.models.components.AnomalyModule` PL Lightning Module for the GANomaly Algorithm. :param batch_size: Batch size. :type batch_size: int :param input_size: Input dimension. :type input_size: Tuple[int,int] :param n_features: Number of features layers in the CNNs. :type n_features: int :param latent_vec_size: Size of autoencoder latent vector. :type latent_vec_size: int :param extra_layers: Number of extra layers for encoder/decoder. Defaults to 0. :type extra_layers: int, optional :param add_final_conv_layer: Add convolution layer at the end. Defaults to True. :type add_final_conv_layer: bool, optional :param wadv: Weight for adversarial loss. Defaults to 1. :type wadv: int, optional :param wcon: Image regeneration weight. Defaults to 50. :type wcon: int, optional :param wenc: Latent vector encoder weight. Defaults to 1. :type wenc: int, optional .. py:method:: _reset_min_max(self) Resets min_max scores. .. py:method:: configure_optimizers(self) -> List[torch.optim.Optimizer] Configures optimizers for each decoder. .. note:: This method is used for the existing CLI. When PL CLI is introduced, configure optimizers method will be deprecated, and optimizers will be configured from either config.yaml file or from CLI. :returns: Adam optimizer for each decoder :rtype: Optimizer .. py:method:: training_step(self, batch, _, optimizer_idx) Training step. :param batch: Input batch containing images. :type batch: Dict :param optimizer_idx: Optimizer which is being called for current training step. :type optimizer_idx: int :returns: Loss :rtype: Dict[str, Tensor] .. py:method:: on_validation_start(self) -> None Reset min and max values for current validation epoch. .. py:method:: validation_step(self, batch, _) -> Dict[str, torch.Tensor] Update min and max scores from the current step. :param batch: Predicted difference between z and z_hat. :type batch: Dict[str, Tensor] :returns: batch :rtype: Dict[str, Tensor] .. py:method:: validation_epoch_end(self, outputs) Normalize outputs based on min/max values. .. py:method:: on_test_start(self) -> None Reset min max values before test batch starts. .. py:method:: test_step(self, batch, _) Update min and max scores from the current step. .. py:method:: test_epoch_end(self, outputs) Normalize outputs based on min/max values. .. py:method:: _normalize(self, scores: torch.Tensor) -> torch.Tensor Normalize the scores based on min/max of entire dataset. :param scores: Un-normalized scores. :type scores: Tensor :returns: Normalized scores. :rtype: Tensor .. py:class:: GanomalyLightning(hparams: Union[omegaconf.DictConfig, omegaconf.ListConfig]) Bases: :py:obj:`Ganomaly` PL Lightning Module for the GANomaly Algorithm. :param hparams: Model params :type hparams: Union[DictConfig, ListConfig] .. py:method:: configure_callbacks(self) Configure model-specific callbacks. .. note:: This method is used for the existing CLI. When PL CLI is introduced, configure callback method will be deprecated, and callbacks will be configured from either config.yaml file or from CLI.