anomalib.models¶
Load Anomaly Model.
Subpackages¶
anomalib.models.cflowanomalib.models.componentsanomalib.models.dfkdeanomalib.models.dfmanomalib.models.draemanomalib.models.fastflowanomalib.models.ganomalyanomalib.models.padimanomalib.models.patchcoreanomalib.models.reverse_distillationanomalib.models.stfpm
Package Contents¶
Classes¶
PL Lightning Module for the CFLOW algorithm. |
|
DFKDE: Deep Feature Kernel Density Estimation. |
|
DFM: Deep Featured Kernel Density Estimation. |
|
DRÆM: A discriminatively trained reconstruction embedding for surface anomaly detection. |
|
PL Lightning Module for the FastFlow algorithm. |
|
PL Lightning Module for the GANomaly Algorithm. |
|
PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization. |
|
PatchcoreLightning Module to train PatchCore algorithm. |
|
PL Lightning Module for Reverse Distillation Algorithm. |
|
PL Lightning Module for the STFPM algorithm. |
- class anomalib.models.Cflow(input_size: Tuple[int, int], backbone: str, layers: List[str], fiber_batch_size: int = 64, decoder: str = 'freia-cflow', condition_vector: int = 128, coupling_blocks: int = 8, clamp_alpha: float = 1.9, permute_soft: bool = False, lr: float = 0.0001)¶
Bases:
anomalib.models.components.AnomalyModulePL Lightning Module for the CFLOW algorithm.
- configure_optimizers(self) 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
- Return type
Optimizer
- training_step(self, batch, _)¶
Training Step of CFLOW.
For each batch, decoder layers are trained with a dynamic fiber batch size. Training step is performed manually as multiple training steps are involved
per batch of input images
- Parameters
batch – Input batch
_ – Index of the batch.
- Returns
Loss value for the batch
- validation_step(self, batch, _)¶
Validation Step of CFLOW.
Similar to the training step, encoder features are extracted from the CNN for each batch, and anomaly map is computed.
- Parameters
batch – Input batch
_ – Index of the batch.
- Returns
Dictionary containing images, anomaly maps, true labels and masks. These are required in validation_epoch_end for feature concatenation.
- class anomalib.models.Dfkde(backbone: str, max_training_points: int = 40000, pre_processing: str = 'scale', n_components: int = 16, threshold_steepness: float = 0.05, threshold_offset: int = 12)¶
Bases:
anomalib.models.components.AnomalyModuleDFKDE: Deep Feature Kernel Density Estimation.
- Parameters
backbone (str) – Pre-trained model backbone.
max_training_points (int, optional) – Number of training points to fit the KDE model. Defaults to 40000.
pre_processing (str, optional) – Preprocess features before passing to KDE. Options are between norm and scale. Defaults to “scale”.
n_components (int, optional) – Number of PCA components. Defaults to 16.
threshold_steepness (float, optional) – Controls how quickly the value saturates around zero. Defaults to 0.05.
threshold_offset (float, optional) – Offset of the density function from 0. Defaults to 12.0.
- static configure_optimizers()¶
DFKDE doesn’t require optimization, therefore returns no optimizers.
- training_step(self, batch, _batch_idx)¶
Training Step of DFKDE. For each batch, features are extracted from the CNN.
- Parameters
batch (Dict[str, Any]) – Batch containing image filename, image, label and mask
_batch_idx – Index of the batch.
- Returns
Deep CNN features.
- on_validation_start(self) None¶
Fit a KDE Model to the embedding collected from the training set.
- validation_step(self, batch, _)¶
Validation Step of DFKDE.
Similar to the training step, features are extracted from the CNN for each batch.
- Parameters
batch – Input batch
- Returns
Dictionary containing probability, prediction and ground truth values.
- class anomalib.models.Dfm(backbone: str, layer: str, pooling_kernel_size: int = 4, pca_level: float = 0.97, score_type: str = 'fre')¶
Bases:
anomalib.models.components.AnomalyModuleDFM: Deep Featured Kernel Density Estimation.
- Parameters
backbone (str) – Backbone CNN network
layer (str) – Layer to extract features from the backbone CNN
pooling_kernel_size (int, optional) – Kernel size to pool features extracted from the CNN. Defaults to 4.
pca_level (float, optional) – Ratio from which number of components for PCA are calculated. Defaults to 0.97.
score_type (str, optional) – Scoring type. Options are fre and nll. Defaults to “fre”.
nll – for Gaussian modeling, fre: pca feature reconstruction error
- static configure_optimizers() None¶
DFM doesn’t require optimization, therefore returns no optimizers.
- training_step(self, batch, _)¶
Training Step of DFM.
For each batch, features are extracted from the CNN.
- Parameters
batch (Dict[str, Tensor]) – Input batch
_ – Index of the batch.
- Returns
Deep CNN features.
- on_validation_start(self) None¶
Fit a PCA transformation and a Gaussian model to dataset.
- validation_step(self, batch, _)¶
Validation Step of DFM.
Similar to the training step, features are extracted from the CNN for each batch.
- Parameters
batch (List[Dict[str, Any]]) – Input batch
- Returns
Dictionary containing FRE anomaly scores and ground-truth.
- class anomalib.models.Draem(anomaly_source_path: Optional[str] = None)¶
Bases:
anomalib.models.components.AnomalyModuleDRÆM: A discriminatively trained reconstruction embedding for surface anomaly detection.
- Parameters
anomaly_source_path (Optional[str]) – Path to folder that contains the anomaly source images. Random noise will be used if left empty.
- training_step(self, batch, _)¶
Training Step of DRAEM.
Feeds the original image and the simulated anomaly image through the network and computes the training loss.
- Parameters
batch (Dict[str, Any]) – Batch containing image filename, image, label and mask
- Returns
Loss dictionary
- validation_step(self, batch, _)¶
Validation step of DRAEM. The Softmax predictions of the anomalous class are used as anomaly map.
- Parameters
batch – Batch of input images
- Returns
Dictionary to which predicted anomaly maps have been added.
- class anomalib.models.Fastflow(input_size: Tuple[int, int], backbone: str, flow_steps: int, conv3x3_only: bool = False, hidden_ratio: float = 1.0)¶
Bases:
anomalib.models.components.AnomalyModulePL Lightning Module for the FastFlow algorithm.
- Parameters
input_size (Tuple[int, int]) – Model input size.
backbone (str) – Backbone CNN network
flow_steps (int) – Flow steps.
conv3x3_only (bool, optinoal) – Use only conv3x3 in fast_flow model. Defaults to False.
hidden_ratio (float, optional) – Ratio to calculate hidden var channels. Defaults to 1.0.
- training_step(self, batch, _)¶
Forward-pass input and return the loss.
- Parameters
batch (Tensor) – Input batch
_batch_idx – Index of the batch.
- Returns
Dictionary containing the loss value.
- Return type
STEP_OUTPUT
- validation_step(self, batch, _)¶
Forward-pass the input and return the anomaly map.
- Parameters
batch (Tensor) – Input batch
_batch_idx – Index of the batch.
- Returns
batch dictionary containing anomaly-maps.
- Return type
dict
- class anomalib.models.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:
anomalib.models.components.AnomalyModulePL 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.
- _reset_min_max(self)¶
Resets min_max scores.
- 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
- Return type
Optimizer
- training_step(self, batch, _, optimizer_idx)¶
Training step.
- Parameters
batch (Dict) – Input batch containing images.
optimizer_idx (int) – Optimizer which is being called for current training step.
- Returns
Loss
- Return type
Dict[str, Tensor]
- on_validation_start(self) None¶
Reset min and max values for current validation epoch.
- validation_step(self, batch, _) Dict[str, torch.Tensor]¶
Update min and max scores from the current step.
- Parameters
batch (Dict[str, Tensor]) – Predicted difference between z and z_hat.
- Returns
batch
- Return type
Dict[str, Tensor]
- validation_epoch_end(self, outputs)¶
Normalize outputs based on min/max values.
- on_test_start(self) None¶
Reset min max values before test batch starts.
- test_step(self, batch, _)¶
Update min and max scores from the current step.
- test_epoch_end(self, outputs)¶
Normalize outputs based on min/max values.
- _normalize(self, scores: torch.Tensor) torch.Tensor¶
Normalize the scores based on min/max of entire dataset.
- Parameters
scores (Tensor) – Un-normalized scores.
- Returns
Normalized scores.
- Return type
Tensor
- class anomalib.models.Padim(layers: List[str], input_size: Tuple[int, int], backbone: str)¶
Bases:
anomalib.models.components.AnomalyModulePaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization.
- Parameters
layers (List[str]) – Layers to extract features from the backbone CNN
input_size (Tuple[int, int]) – Size of the model input.
backbone (str) – Backbone CNN network
- static configure_optimizers()¶
PADIM doesn’t require optimization, therefore returns no optimizers.
- training_step(self, batch, _batch_idx)¶
Training Step of PADIM. For each batch, hierarchical features are extracted from the CNN.
- Parameters
batch (Dict[str, Any]) – Batch containing image filename, image, label and mask
_batch_idx – Index of the batch.
- Returns
Hierarchical feature map
- on_validation_start(self) None¶
Fit a Gaussian to the embedding collected from the training set.
- validation_step(self, batch, _)¶
Validation Step of PADIM.
Similar to the training step, hierarchical features are extracted from the CNN for each batch.
- Parameters
batch – Input batch
_ – Index of the batch.
- Returns
Dictionary containing images, features, true labels and masks. These are required in validation_epoch_end for feature concatenation.
- class anomalib.models.Patchcore(input_size: Tuple[int, int], backbone: str, layers: List[str], coreset_sampling_ratio: float = 0.1, num_neighbors: int = 9)¶
Bases:
anomalib.models.components.AnomalyModulePatchcoreLightning Module to train PatchCore algorithm.
- Parameters
input_size (Tuple[int, int]) – Size of the model input.
backbone (str) – Backbone CNN network
layers (List[str]) – Layers to extract features from the backbone CNN
coreset_sampling_ratio (float, optional) – Coreset sampling ratio to subsample embedding. Defaults to 0.1.
num_neighbors (int, optional) – Number of nearest neighbors. Defaults to 9.
- configure_optimizers(self) None¶
Configure optimizers.
- Returns
Do not set optimizers by returning None.
- Return type
None
- training_step(self, batch, _batch_idx)¶
Generate feature embedding of the batch.
- Parameters
batch (Dict[str, Any]) – Batch containing image filename, image, label and mask
_batch_idx (int) – Batch Index
- Returns
Embedding Vector
- Return type
Dict[str, np.ndarray]
- on_validation_start(self) None¶
Apply subsampling to the embedding collected from the training set.
- validation_step(self, batch, _)¶
Get batch of anomaly maps from input image batch.
- Parameters
batch (Dict[str, Any]) – Batch containing image filename, image, label and mask
_ (int) – Batch Index
- Returns
Image filenames, test images, GT and predicted label/masks
- Return type
Dict[str, Any]
- class anomalib.models.ReverseDistillation(input_size: Tuple[int, int], backbone: str, layers: List[str], anomaly_map_mode: str, lr: float, beta1: float, beta2: float)¶
Bases:
anomalib.models.components.AnomalyModulePL Lightning Module for Reverse Distillation Algorithm.
- Parameters
input_size (Tuple[int, int]) – Size of model input
backbone (str) – Backbone of CNN network
layers (List[str]) – Layers to extract features from the backbone CNN
- configure_optimizers(self)¶
Configures optimizers for decoder and bottleneck.
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
- training_step(self, batch, _) Dict[str, torch.Tensor]¶
Training Step of Reverse Distillation Model.
Features are extracted from three layers of the Encoder model. These are passed to the bottleneck layer that are passed to the decoder network. The loss is then calculated based on the cosine similarity between the encoder and decoder features.
- Parameters
batch (Tensor) – Input batch
_ – Index of the batch.
- Returns
Feature Map
- validation_step(self, batch, _)¶
Validation Step of Reverse Distillation Model.
Similar to the training step, encoder/decoder features are extracted from the CNN for each batch, and anomaly map is computed.
- Parameters
batch (Tensor) – Input batch
_ – Index of the batch.
- Returns
Dictionary containing images, anomaly maps, true labels and masks. These are required in validation_epoch_end for feature concatenation.
- class anomalib.models.Stfpm(input_size: Tuple[int, int], backbone: str, layers: List[str])¶
Bases:
anomalib.models.components.AnomalyModulePL Lightning Module for the STFPM algorithm.
- Parameters
input_size (Tuple[int, int]) – Size of the model input.
backbone (str) – Backbone CNN network
layers (List[str]) – Layers to extract features from the backbone CNN
- training_step(self, batch, _)¶
Training Step of STFPM.
For each batch, teacher and student and teacher features are extracted from the CNN.
- Parameters
batch (Tensor) – Input batch
_ – Index of the batch.
- Returns
Hierarchical feature map
- validation_step(self, batch, _)¶
Validation Step of STFPM.
Similar to the training step, student/teacher features are extracted from the CNN for each batch, and anomaly map is computed.
- Parameters
batch (Tensor) – Input batch
_ – Index of the batch.
- Returns
Dictionary containing images, anomaly maps, true labels and masks. These are required in validation_epoch_end for feature concatenation.