DFKDE

Model Type: Classification

Description

Fast anomaly classification algorithm that consists of a deep feature extraction stage followed by anomaly classification stage consisting of PCA and Gaussian Kernel Density Estimation.

Feature Extraction

Features are extracted by feeding the images through a ResNet50 backbone, which was pre-trained on ImageNet. The output of the penultimate layer (average pooling layer) of the network is used to obtain a semantic feature vector with a fixed length of 2048.

Anomaly Detection

In the anomaly classification stage, the features are first reduced to the first 16 principal components. Gaussian Kernel Density is then used to obtain an estimate of the probability density of new examples, based on the collection of training features obtained during the training phase.

Usage

python tools/train.py --model dfkde

Normality model of DFKDE.

class anomalib.models.dfkde.torch_model.DfkdeModel(layers: list[str], backbone: str, pre_trained: bool = True, n_pca_components: int = 16, feature_scaling_method: FeatureScalingMethod = FeatureScalingMethod.SCALE, max_training_points: int = 40000)[source]

Bases: Module

Normality Model for the DFKDE algorithm.

Parameters:
  • backbone (str) – Pre-trained model backbone.

  • pre_trained (bool, optional) – Boolean to check whether to use a pre_trained backbone.

  • n_comps (int, optional) – Number of PCA components. Defaults to 16.

  • pre_processing (str, optional) – Preprocess features before passing to KDE. Options are between norm and scale. Defaults to “scale”.

  • filter_count (int, optional) – Number of training points to fit the KDE model. Defaults to 40000.

  • 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.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(batch: Tensor) Tensor[source]

Prediction by normality model.

Parameters:

batch (Tensor) – Input images.

Returns:

Predictions

Return type:

Tensor

get_features(batch: Tensor) Tensor[source]

Extract features from the pretrained network.

Parameters:

batch (Tensor) – Image batch.

Returns:

Tensor containing extracted features.

Return type:

Tensor

training: bool

DFKDE: Deep Feature Kernel Density Estimation.

class anomalib.models.dfkde.lightning_model.Dfkde(layers: list[str], backbone: str, pre_trained: bool = True, n_pca_components: int = 16, feature_scaling_method: FeatureScalingMethod = FeatureScalingMethod.SCALE, max_training_points: int = 40000)[source]

Bases: AnomalyModule

DFKDE: Deep Feature Kernel Density Estimation.

Parameters:
  • backbone (str) – Pre-trained model backbone.

  • pre_trained (bool, optional) – Boolean to check whether to use a pre_trained 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() None[source]

DFKDE doesn’t require optimization, therefore returns no optimizers.

on_validation_start() None[source]

Fit a KDE Model to the embedding collected from the training set.

training_step(batch: dict[str, str | Tensor], *args, **kwargs) None[source]

Training Step of DFKDE. For each batch, features are extracted from the CNN.

Parameters:

(batch (batch) – dict[str, str | Tensor]): Batch containing image filename, image, label and mask

Returns:

Deep CNN features.

validation_step(batch: dict[str, str | Tensor], *args, **kwargs) STEP_OUTPUT[source]

Validation Step of DFKDE.

Similar to the training step, features are extracted from the CNN for each batch.

Parameters:

batch (dict[str, str | Tensor]) – Input batch

Returns:

Dictionary containing probability, prediction and ground truth values.

class anomalib.models.dfkde.lightning_model.DfkdeLightning(hparams: DictConfig | ListConfig)[source]

Bases: Dfkde

DFKDE: Deep Feature Kernel Density Estimation.

Parameters:

hparams (DictConfig | ListConfig) – Model params