anomalib.models.dfkde.torch_model

Normality model of DFKDE.

Module Contents

Classes

DfkdeModel

Normality Model for the DFKDE algorithm.

Attributes

anomalib.models.dfkde.torch_model.logger[source]
class anomalib.models.dfkde.torch_model.DfkdeModel(backbone: str, n_comps: int = 16, pre_processing: str = 'scale', filter_count: int = 40000, threshold_steepness: float = 0.05, threshold_offset: float = 12.0)[source]

Bases: torch.nn.Module

Normality Model for the DFKDE algorithm.

Parameters
  • backbone (str) – Pre-trained model 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.

get_features(self, batch: torch.Tensor) torch.Tensor[source]

Extract features from the pretrained network.

Parameters

batch (Tensor) – Image batch.

Returns

Tensor containing extracted features.

Return type

Tensor

fit(self, embeddings: List[torch.Tensor]) bool[source]

Fit a kde model to embeddings.

Parameters

embeddings (Tensor) – Input embeddings to fit the model.

Returns

Boolean confirming whether the training is successful.

preprocess(self, feature_stack: torch.Tensor, max_length: Optional[torch.Tensor] = None) Tuple[torch.Tensor, torch.Tensor][source]

Pre-process the CNN features.

Parameters
  • feature_stack (Tensor) – Features extracted from CNN

  • max_length (Optional[Tensor]) – Used to unit normalize the feature_stack vector. If max_len is not provided, the length is calculated from the feature_stack. Defaults to None.

Returns

Stacked features and length

Return type

(Tuple)

evaluate(self, features: torch.Tensor, as_log_likelihood: Optional[bool] = False) torch.Tensor[source]

Compute the KDE scores.

The scores calculated from the KDE model are converted to densities. If as_log_likelihood is set to true then

the log of the scores are calculated.

Parameters
  • features (Tensor) – Features to which the PCA model is fit.

  • as_log_likelihood (Optional[bool], optional) – If true, gets log likelihood scores. Defaults to False.

Returns

Score

Return type

(Tensor)

predict(self, features: torch.Tensor) torch.Tensor[source]

Predicts the probability that the features belong to the anomalous class.

Parameters

features (Tensor) – Feature from which the output probabilities are detected.

Returns

Detection probabilities

to_probability(self, densities: torch.Tensor) torch.Tensor[source]

Converts density scores to anomaly probabilities (see https://www.desmos.com/calculator/ifju7eesg7).

Parameters

densities (Tensor) – density of an image.

Returns

probability that image with {density} is anomalous

forward(self, batch: torch.Tensor) torch.Tensor[source]

Prediction by normality model.

Parameters

batch (Tensor) – Input images.

Returns

Predictions

Return type

Tensor