Classification#
Classification modules for anomaly detection.
This module provides classification components used in anomaly detection models.
- Classes:
- KDEClassifier: Kernel Density Estimation based classifier for anomaly
detection.
- FeatureScalingMethod: Enum class defining feature scaling methods for
KDE classifier.
Example
>>> from anomalib.models.components.classification import KDEClassifier
>>> from anomalib.models.components.classification import FeatureScalingMethod
>>> # Create KDE classifier with min-max scaling
>>> classifier = KDEClassifier(
... scaling_method=FeatureScalingMethod.MIN_MAX
... )
- class anomalib.models.components.classification.FeatureScalingMethod(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#
-
Feature scaling methods for KDE classifier.
The scaling method determines how feature embeddings are normalized before being passed to the KDE model.
- NORM#
Scale features to unit vector length (L2 normalization)
- SCALE#
Scale features by maximum length observed during training (preserves relative magnitudes)
- class anomalib.models.components.classification.KDEClassifier(n_pca_components=16, feature_scaling_method=FeatureScalingMethod.SCALE, max_training_points=40000)#
Bases:
Module
Classification module for KDE-based anomaly detection.
This classifier uses kernel density estimation to model the distribution of normal samples in feature space. It first applies dimensionality reduction via PCA, then fits a Gaussian KDE model to the reduced features.
- Parameters:
n_pca_components (
int
) – Number of PCA components to retain. Lower values reduce computational cost but may lose information. Defaults to 16.feature_scaling_method (
FeatureScalingMethod
) – Method used to scale features before KDE. Options arenorm
(unit vector) orscale
(max length). Defaults toFeatureScalingMethod.SCALE
.max_training_points (
int
) – Maximum number of points used to fit the KDE model. If more points are provided, a random subset is selected. Defaults to 40000.
- pca_model#
PCA model for dimensionality reduction
- kde_model#
Gaussian KDE model for density estimation
- max_length#
Maximum feature length observed during training
- compute_kde_scores(features, as_log_likelihood=False)#
Compute KDE scores for input features.
Transforms features via PCA and scaling, then computes KDE scores.
- Parameters:
- Returns:
KDE scores of shape (N,)
- Return type:
- static compute_probabilities(scores)#
Convert density scores to anomaly probabilities.
Uses sigmoid function to map scores to [0,1] range. See https://www.desmos.com/calculator/ifju7eesg7
- Parameters:
scores (
Tensor
) – Density scores of shape (N,)- Returns:
Anomaly probabilities of shape (N,)
- Return type:
- fit(embeddings)#
Fit the KDE classifier to training embeddings.
Applies PCA, scales the features, and fits the KDE model.
- Parameters:
embeddings (
Tensor
) – Training embeddings of shape (N, D)- Returns:
True if fitting succeeded, False if insufficient samples
- Return type:
Example
>>> classifier = KDEClassifier() >>> embeddings = torch.randn(1000, 512) >>> success = classifier.fit(embeddings) >>> assert success
- forward(features)#
Forward pass of the classifier.
Equivalent to calling
predict()
.- Parameters:
features (
Tensor
) – Input features of shape (N, D)- Returns:
Anomaly probabilities of shape (N,)
- Return type:
- pre_process(feature_stack, max_length=None)#
Pre-process feature embeddings before KDE.
Scales the features according to the specified scaling method.
- Parameters:
- Returns:
(scaled_features, max_length)
- Return type:
- Raises:
RuntimeError – If unknown scaling method is specified
- predict(features)#
Predict anomaly probabilities for input features.
Computes KDE scores and converts them to probabilities.
- Parameters:
features (
Tensor
) – Input features of shape (N, D)- Returns:
Anomaly probabilities of shape (N,)
- Return type:
Example
>>> classifier = KDEClassifier() >>> features = torch.randn(10, 512) >>> classifier.fit(features) >>> probs = classifier.predict(features) >>> assert probs.shape == (10,) >>> assert (probs >= 0).all() and (probs <= 1).all()