DFKDE#

DFKDE: Deep Feature Kernel Density Estimation.

This module provides a PyTorch Lightning implementation of the DFKDE model for anomaly detection. The model extracts deep features from images using a pre-trained CNN backbone and fits a kernel density estimation on these features to model the distribution of normal samples.

Example

>>> from anomalib.models.image import Dfkde
>>> model = Dfkde(
...     backbone="resnet18",
...     layers=("layer4",),
...     pre_trained=True
... )

Notes

The model uses a pre-trained backbone to extract features and fits a KDE classifier on the embeddings during training. No gradient updates are performed on the backbone.

See also

anomalib.models.image.dfkde.torch_model.DfkdeModel:

PyTorch implementation of the DFKDE model.

class anomalib.models.image.dfkde.lightning_model.Dfkde(backbone='resnet18', layers=('layer4',), pre_trained=True, n_pca_components=16, feature_scaling_method=FeatureScalingMethod.SCALE, max_training_points=40000, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#

Bases: MemoryBankMixin, AnomalibModule

DFKDE Lightning Module.

Parameters:
  • backbone (str) – Name of the backbone CNN to use for feature extraction. Defaults to "resnet18".

  • layers (Sequence[str]) – Layers from which to extract features. Defaults to ("layer4",).

  • pre_trained (bool) – Whether to use pre-trained weights. Defaults to True.

  • n_pca_components (int) – Number of principal components for dimensionality reduction. Defaults to 16.

  • feature_scaling_method (FeatureScalingMethod) – Method to scale features. Defaults to FeatureScalingMethod.SCALE.

  • max_training_points (int) – Maximum number of points to use for KDE fitting. Defaults to 40000.

  • pre_processor (PreProcessor | bool) – Pre-processor object or flag. Defaults to True.

  • post_processor (PostProcessor | bool) – Post-processor object or flag. Defaults to True.

  • evaluator (Evaluator | bool) – Evaluator object or flag. Defaults to True.

  • visualizer (Visualizer | bool) – Visualizer object or flag. Defaults to True.

Example

>>> from anomalib.models.image import Dfkde
>>> from anomalib.models.components.classification import (
...     FeatureScalingMethod
... )
>>> model = Dfkde(
...     backbone="resnet18",
...     layers=("layer4",),
...     feature_scaling_method=FeatureScalingMethod.SCALE
... )
static configure_evaluator()#

Configure the default evaluator for DFKDE.

Returns:

Evaluator object with image-level AUROC and F1 metrics.

Return type:

Evaluator

static configure_optimizers()#

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

Return type:

None

fit()#

Fit KDE model to collected embeddings from the training set.

Return type:

None

property learning_type: LearningType#

Get the learning type.

Returns:

Learning type of the model (ONE_CLASS).

Return type:

LearningType

property trainer_arguments: dict[str, Any]#

Get DFKDE-specific trainer arguments.

Returns:

Trainer arguments
  • gradient_clip_val: 0 (no gradient clipping needed)

  • max_epochs: 1 (single pass through training data)

  • num_sanity_val_steps: 0 (skip validation sanity checks)

  • devices: 1 (only single gpu supported)

Return type:

dict[str, Any]

training_step(batch, *args, **kwargs)#

Extract features from the CNN for each training batch.

Parameters:
  • batch (Batch) – Input batch containing images and metadata.

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

Dummy tensor for Lightning compatibility.

Return type:

torch.Tensor

validation_step(batch, *args, **kwargs)#

Perform validation by computing anomaly scores.

Parameters:
  • batch (Batch) – Input batch containing images and metadata.

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

Dictionary containing predictions and batch info.

Return type:

STEP_OUTPUT

PyTorch model for Deep Feature Kernel Density Estimation (DFKDE).

This module provides a PyTorch implementation of the DFKDE model for anomaly detection. The model extracts deep features from images using a pre-trained CNN backbone and fits a kernel density estimation on these features to model the distribution of normal samples.

Example

>>> import torch
>>> from anomalib.models.image.dfkde.torch_model import DfkdeModel
>>> model = DfkdeModel(
...     backbone="resnet18",
...     layers=["layer4"],
...     pre_trained=True
... )
>>> batch = torch.randn(32, 3, 224, 224)
>>> features = model(batch)  # Returns features during training
>>> predictions = model(batch)  # Returns scores during inference

Notes

The model uses a pre-trained backbone to extract features and fits a KDE classifier on the embeddings during training. No gradient updates are performed on the backbone.

class anomalib.models.image.dfkde.torch_model.DfkdeModel(backbone, layers, pre_trained=True, n_pca_components=16, feature_scaling_method=FeatureScalingMethod.SCALE, max_training_points=40000)#

Bases: Module

Deep Feature Kernel Density Estimation model for anomaly detection.

The model extracts deep features from images using a pre-trained CNN backbone and fits a kernel density estimation on these features to model the distribution of normal samples.

Parameters:
  • backbone (str) – Name of the pre-trained model backbone from timm.

  • layers (Sequence[str]) – Names of layers to extract features from.

  • pre_trained (bool, optional) – Whether to use pre-trained backbone weights. Defaults to True.

  • n_pca_components (int, optional) – Number of components for PCA dimension reduction. Defaults to 16.

  • feature_scaling_method (FeatureScalingMethod, optional) – Method used to scale features before KDE. Defaults to FeatureScalingMethod.SCALE.

  • max_training_points (int, optional) – Maximum number of points used to fit the KDE model. Defaults to 40000.

Example

>>> import torch
>>> model = DfkdeModel(
...     backbone="resnet18",
...     layers=["layer4"],
...     pre_trained=True
... )
>>> batch = torch.randn(32, 3, 224, 224)
>>> features = model(batch)
fit()#

Fits the classifier using the current contents of the memory bank.

This method is typically called after the memory bank has been populated during training.

After fitting, the memory bank is cleared to reduce GPU memory usage.

Raises:

ValueError – If the memory bank is empty.

Return type:

None

forward(batch)#

Extract features during training or compute anomaly scores during inference.

Parameters:

batch (torch.Tensor) – Batch of input images with shape (N, C, H, W).

Returns:

During training, returns extracted

features as a tensor. During inference, returns an InferenceBatch containing anomaly scores.

Return type:

torch.Tensor | InferenceBatch

Example

>>> batch = torch.randn(32, 3, 224, 224)
>>> # Training mode
>>> model.train()
>>> features = model(batch)
>>> # Inference mode
>>> model.eval()
>>> predictions = model(batch)
>>> predictions.pred_score.shape
torch.Size([32])
get_features(batch)#

Extract features from the pre-trained backbone network.

Parameters:

batch (torch.Tensor) – Batch of input images with shape (N, C, H, W).

Returns:

Concatenated features from specified layers, flattened

to shape (N, D) where D is the total feature dimension.

Return type:

torch.Tensor

Example

>>> batch = torch.randn(32, 3, 224, 224)
>>> features = model.get_features(batch)
>>> features.shape
torch.Size([32, 512])  # Depends on backbone and layers