anomalib.models.dfkde.torch_model¶
Normality model of DFKDE.
Module Contents¶
Classes¶
Normality Model for the DFKDE algorithm. |
Attributes¶
- class anomalib.models.dfkde.torch_model.DfkdeModel(layers: List[str], backbone: str, pre_trained: bool = True, 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.ModuleNormality 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.
- get_features(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
- pre_process(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_lenis not provided, the length is calculated from thefeature_stack. Defaults to None.
- Returns
Stacked features and length
- Return type
(Tuple)
- fit(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.
- compute_kde_scores(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)
- compute_probabilities(scores: torch.Tensor) torch.Tensor[source]¶
Converts density scores to anomaly probabilities (see https://www.desmos.com/calculator/ifju7eesg7).
- Parameters
scores (Tensor) – density of an image.
- Returns
probability that image with {density} is anomalous