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:
- static configure_optimizers()#
DFKDE doesn’t require optimization, therefore returns no optimizers.
- Return type:
- property learning_type: LearningType#
Get the learning type.
- Returns:
Learning type of the model (ONE_CLASS).
- Return type:
LearningType
- 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:
- 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)
- 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:
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)
whereD
is the total feature dimension.
- Return type:
Example
>>> batch = torch.randn(32, 3, 224, 224) >>> features = model.get_features(batch) >>> features.shape torch.Size([32, 512]) # Depends on backbone and layers