GANomaly¶
This is the implementation of the GANomaly paper.
Description¶
GANomaly uses the conditional GAN approach to train a Generator to produce images of the normal data. This Generator consists of an encoder-decoder-encoder architecture to generate the normal images. The distance between the latent vector $z$ between the first encoder-decoder and the output vector $hat{z}$ is minimized during training.
The key idea here is that, during inference, when an anomalous image is passed through the first encoder the latent vector $z$ will not be able to capture the data correctly. This would leave to poor reconstruction $hat{x}$ thus resulting in a very different $hat{z}$. The difference between $z$ and $hat{z}$ gives the anomaly score.
Usage¶
$ python tools/train.py --model ganomaly
Torch models defining encoder, decoder, Generator and Discriminator.
Code adapted from https://github.com/samet-akcay/ganomaly.
- class anomalib.models.ganomaly.torch_model.Decoder(input_size: tuple[int, int], latent_vec_size: int, num_input_channels: int, n_features: int, extra_layers: int = 0)[source]¶
Bases:
Module
Decoder Network.
- Parameters:
input_size (tuple[int, int]) – Size of input image
latent_vec_size (int) – Size of latent vector z
num_input_channels (int) – Number of input channels in the image
n_features (int) – Number of features per convolution layer
extra_layers (int) – Number of extra layers since the network uses only a single encoder layer by default. Defaults to 0.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- training: bool¶
- class anomalib.models.ganomaly.torch_model.Discriminator(input_size: tuple[int, int], num_input_channels: int, n_features: int, extra_layers: int = 0)[source]¶
Bases:
Module
Discriminator.
Made of only one encoder layer which takes x and x_hat to produce a score.
- Parameters:
input_size (tuple[int, int]) – Input image size.
num_input_channels (int) – Number of image channels.
n_features (int) – Number of feature maps in each convolution layer.
extra_layers (int, optional) – Add extra intermediate layers. Defaults to 0.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- training: bool¶
- class anomalib.models.ganomaly.torch_model.Encoder(input_size: tuple[int, int], latent_vec_size: int, num_input_channels: int, n_features: int, extra_layers: int = 0, add_final_conv_layer: bool = True)[source]¶
Bases:
Module
Encoder Network.
- Parameters:
input_size (tuple[int, int]) – Size of input image
latent_vec_size (int) – Size of latent vector z
num_input_channels (int) – Number of input channels in the image
n_features (int) – Number of features per convolution layer
extra_layers (int) – Number of extra layers since the network uses only a single encoder layer by default. Defaults to 0.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- training: bool¶
- class anomalib.models.ganomaly.torch_model.GanomalyModel(input_size: tuple[int, int], num_input_channels: int, n_features: int, latent_vec_size: int, extra_layers: int = 0, add_final_conv_layer: bool = True)[source]¶
Bases:
Module
Ganomaly Model.
- Parameters:
input_size (tuple[int, int]) – Input dimension.
num_input_channels (int) – Number of input channels.
n_features (int) – Number of features layers in the CNNs.
latent_vec_size (int) – Size of autoencoder latent vector.
extra_layers (int, optional) – Number of extra layers for encoder/decoder. Defaults to 0.
add_final_conv_layer (bool, optional) – Add convolution layer at the end. Defaults to True.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(batch: Tensor) tuple[Tensor, Tensor, Tensor, Tensor] | Tensor [source]¶
Get scores for batch.
- Parameters:
batch (Tensor) – Images
- Returns:
Regeneration scores.
- Return type:
Tensor
- static weights_init(module: Module) None [source]¶
Initialize DCGAN weights.
- Parameters:
module (nn.Module) – [description]
- training: bool¶
- class anomalib.models.ganomaly.torch_model.Generator(input_size: tuple[int, int], latent_vec_size: int, num_input_channels: int, n_features: int, extra_layers: int = 0, add_final_conv_layer: bool = True)[source]¶
Bases:
Module
Generator model.
Made of an encoder-decoder-encoder architecture.
- Parameters:
input_size (tuple[int, int]) – Size of input data.
latent_vec_size (int) – Dimension of latent vector produced between the first encoder-decoder.
num_input_channels (int) – Number of channels in input image.
n_features (int) – Number of feature maps in each convolution layer.
extra_layers (int, optional) – Extra intermediate layers in the encoder/decoder. Defaults to 0.
add_final_conv_layer (bool, optional) – Add a final convolution layer in the decoder. Defaults to True.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_tensor: Tensor) tuple[Tensor, Tensor, Tensor] [source]¶
Return generated image and the latent vectors.
- training: bool¶
GANomaly: Semi-Supervised Anomaly Detection via Adversarial Training.
https://arxiv.org/abs/1805.06725
- class anomalib.models.ganomaly.lightning_model.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)[source]¶
Bases:
AnomalyModule
PL Lightning Module for the GANomaly Algorithm.
- Parameters:
batch_size (int) – Batch size.
input_size (tuple[int, int]) – Input dimension.
n_features (int) – Number of features layers in the CNNs.
latent_vec_size (int) – Size of autoencoder latent vector.
extra_layers (int, optional) – Number of extra layers for encoder/decoder. Defaults to 0.
add_final_conv_layer (bool, optional) – Add convolution layer at the end. Defaults to True.
wadv (int, optional) – Weight for adversarial loss. Defaults to 1.
wcon (int, optional) – Image regeneration weight. Defaults to 50.
wenc (int, optional) – Latent vector encoder weight. Defaults to 1.
- configure_optimizers() list[optim.Optimizer] [source]¶
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
- Return type:
Optimizer
- test_epoch_end(outputs: List[Union[Tensor, Dict[str, Any]]]) List[Union[Tensor, Dict[str, Any]]] [source]¶
Normalize outputs based on min/max values.
- test_step(batch: dict[str, str | Tensor], batch_idx: int, *args, **kwargs) STEP_OUTPUT [source]¶
Update min and max scores from the current step.
- training_step(batch: dict[str, str | Tensor], batch_idx: int, optimizer_idx: int) STEP_OUTPUT [source]¶
Training step.
- Parameters:
batch (dict[str, str | Tensor]) – Input batch containing images.
batch_idx (int) – Batch index.
optimizer_idx (int) – Optimizer which is being called for current training step.
- Returns:
Loss
- Return type:
STEP_OUTPUT