anomalib.models.dfm.torch_model¶
PyTorch model for DFM model implementation.
Module Contents¶
Classes¶
Model Gaussian distribution over a set of points. |
|
Model for the DFM algorithm. |
- class anomalib.models.dfm.torch_model.SingleClassGaussian[source]¶
Bases:
anomalib.models.components.DynamicBufferModuleModel Gaussian distribution over a set of points.
- fit(dataset: torch.Tensor) None[source]¶
Fit a Gaussian model to dataset X.
Covariance matrix is not calculated directly using:
C = X.X^TInstead, it is represented in terms of the Singular Value Decomposition of X:X = U.S.V^THence,C = U.S^2.U^TThis simplifies the calculation of the log-likelihood without requiring full matrix inversion.- Parameters
dataset (Tensor) – Input dataset to fit the model.
- class anomalib.models.dfm.torch_model.DFMModel(backbone: str, layer: str, pre_trained: bool = True, pooling_kernel_size: int = 4, n_comps: float = 0.97, score_type: str = 'fre')[source]¶
Bases:
torch.nn.ModuleModel for the DFM algorithm.
- Parameters
backbone (str) – Pre-trained model backbone.
layer (str) – Layer from which to extract features.
pre_trained (bool, optional) – Boolean to check whether to use a pre_trained backbone.
pooling_kernel_size (int, optional) – Kernel size to pool features extracted from the CNN.
n_comps (float, optional) – Ratio from which number of components for PCA are calculated. Defaults to 0.97.
score_type (str, optional) – Scoring type. Options are fre and nll. Defaults to “fre”.
- fit(dataset: torch.Tensor) None[source]¶
Fit a pca transformation and a Gaussian model to dataset.
- Parameters
dataset (Tensor) – Input dataset to fit the model.
- score(features: torch.Tensor) torch.Tensor[source]¶
Compute scores.
Scores are either PCA-based feature reconstruction error (FRE) scores or the Gaussian density-based NLL scores
- Parameters
features (torch.Tensor) – semantic features on which PCA and density modeling is performed.
- Returns
numpy array of scores
- Return type
score (Tensor)