anomalib.models.components

Components used within the models.

Subpackages

Package Contents

Classes

AnomalyModule

AnomalyModule to train, validate, predict and test images.

DynamicBufferModule

Torch module that allows loading variables from the state dict even in the case of shape mismatch.

PCA

Principle Component Analysis (PCA).

SparseRandomProjection

Sparse Random Projection using PyTorch operations.

FeatureExtractor

Extract features from a CNN.

KCenterGreedy

Implements k-center-greedy method.

GaussianKDE

Gaussian Kernel Density Estimation.

MultiVariateGaussian

Multi Variate Gaussian Distribution.

class anomalib.models.components.AnomalyModule

Bases: pytorch_lightning.LightningModule, abc.ABC

AnomalyModule to train, validate, predict and test images.

Acts as a base class for all the Anomaly Modules in the library.

forward(self, batch)

Forward-pass input tensor to the module.

Parameters

batch (Tensor) – Input Tensor

Returns

Output tensor from the model.

Return type

Tensor

abstract validation_step(self, batch, batch_idx) dict

To be implemented in the subclasses.

predict_step(self, batch: Any, batch_idx: int, _dataloader_idx: Optional[int] = None) Any

Step function called during predict().

By default, it calls forward(). Override to add any processing logic.

Parameters
  • batch (Tensor) – Current batch

  • batch_idx (int) – Index of current batch

  • _dataloader_idx (int) – Index of the current dataloader

Returns

Predicted output

test_step(self, batch, _)

Calls validation_step for anomaly map/score calculation.

Parameters
  • batch (Tensor) – Input batch

  • _ – Index of the batch.

Returns

Dictionary containing images, features, true labels and masks. These are required in validation_epoch_end for feature concatenation.

validation_step_end(self, val_step_outputs)

Called at the end of each validation step.

test_step_end(self, test_step_outputs)

Called at the end of each test step.

validation_epoch_end(self, outputs)

Compute threshold and performance metrics.

Parameters

outputs – Batch of outputs from the validation step

test_epoch_end(self, outputs)

Compute and save anomaly scores of the test set.

Parameters

outputs – Batch of outputs from the validation step

_compute_adaptive_threshold(self, outputs)
_collect_outputs(self, image_metric, pixel_metric, outputs)
_post_process(self, outputs)

Compute labels based on model predictions.

_outputs_to_cpu(self, output)
_log_metrics(self)

Log computed performance metrics.

class anomalib.models.components.DynamicBufferModule

Bases: abc.ABC, torch.nn.Module

Torch module that allows loading variables from the state dict even in the case of shape mismatch.

get_tensor_attribute(self, attribute_name: str) torch.Tensor

Get attribute of the tensor given the name.

Parameters

attribute_name (str) – Name of the tensor

Raises

ValueErrorattribute_name is not a torch Tensor

Returns

Tensor attribute

Return type

Tensor

_load_from_state_dict(self, state_dict: dict, prefix: str, *args)

Resizes the local buffers to match those stored in the state dict.

Overrides method from parent class.

Parameters
  • state_dict (dict) – State dictionary containing weights

  • prefix (str) – Prefix of the weight file.

  • *args

class anomalib.models.components.PCA(n_components: Union[float, int])

Bases: anomalib.models.components.base.DynamicBufferModule

Principle Component Analysis (PCA).

Parameters

n_components (float) – Number of components. Can be either integer number of components or a ratio between 0-1.

fit(self, dataset: torch.Tensor) None

Fits the PCA model to the dataset.

Parameters

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

fit_transform(self, dataset: torch.Tensor) torch.Tensor

Fit and transform PCA to dataset.

Parameters

dataset (Tensor) – Dataset to which the PCA if fit and transformed

Returns

Transformed dataset

transform(self, features: torch.Tensor) torch.Tensor

Transforms the features based on singular vectors calculated earlier.

Parameters

features (Tensor) – Input features

Returns

Transformed features

inverse_transform(self, features: torch.Tensor) torch.Tensor

Inverses the transformed features.

Parameters

features (Tensor) – Transformed features

Returns: Inverse features

forward(self, features: torch.Tensor) torch.Tensor

Transforms the features.

Parameters

features (Tensor) – Input features

Returns

Transformed features

class anomalib.models.components.SparseRandomProjection(eps: float = 0.1, random_state: Optional[int] = None)

Sparse Random Projection using PyTorch operations.

Parameters
  • eps (float, optional) – Minimum distortion rate parameter for calculating Johnson-Lindenstrauss minimum dimensions. Defaults to 0.1.

  • random_state (Optional[int], optional) – Uses the seed to set the random state for sample_without_replacement function. Defaults to None.

_sparse_random_matrix(self, n_features: int)

Random sparse matrix. Based on https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf.

Parameters

n_features (int) – Dimentionality of the original source space

Returns

Sparse matrix of shape (n_components, n_features).

The generated Gaussian random matrix is in CSR (compressed sparse row) format.

Return type

Tensor

johnson_lindenstrauss_min_dim(self, n_samples: int, eps: float = 0.1)

Find a ‘safe’ number of components to randomly project to.

Ref eqn 2.1 https://cseweb.ucsd.edu/~dasgupta/papers/jl.pdf

Parameters
  • n_samples (int) – Number of samples used to compute safe components

  • eps (float, optional) – Minimum distortion rate. Defaults to 0.1.

fit(self, embedding: torch.Tensor) SparseRandomProjection

Generates sparse matrix from the embedding tensor.

Parameters

embedding (Tensor) – embedding tensor for generating embedding

Returns

Return self to be used as >>> generator = SparseRandomProjection() >>> generator = generator.fit()

Return type

(SparseRandomProjection)

transform(self, embedding: torch.Tensor) torch.Tensor

Project the data by using matrix product with the random matrix.

Parameters

embedding (Tensor) – Embedding of shape (n_samples, n_features) The input data to project into a smaller dimensional space

Returns

Sparse matrix of shape

(n_samples, n_components) Projected array.

Return type

projected_embedding (Tensor)

class anomalib.models.components.FeatureExtractor(backbone: torch.nn.Module, layers: Iterable[str])

Bases: torch.nn.Module

Extract features from a CNN.

Parameters
  • backbone (nn.Module) – The backbone to which the feature extraction hooks are attached.

  • layers (Iterable[str]) – List of layer names of the backbone to which the hooks are attached.

Example

>>> import torch
>>> import torchvision
>>> from anomalib.core.model.feature_extractor import FeatureExtractor
>>> model = FeatureExtractor(model=torchvision.models.resnet18(), layers=['layer1', 'layer2', 'layer3'])
>>> input = torch.rand((32, 3, 256, 256))
>>> features = model(input)
>>> [layer for layer in features.keys()]
    ['layer1', 'layer2', 'layer3']
>>> [feature.shape for feature in features.values()]
    [torch.Size([32, 64, 64, 64]), torch.Size([32, 128, 32, 32]), torch.Size([32, 256, 16, 16])]
get_features(self, layer_id: str) Callable

Get layer features.

Parameters

layer_id (str) – Layer ID

Returns

Layer features

forward(self, input_tensor: torch.Tensor) Dict[str, torch.Tensor]

Forward-pass input tensor into the CNN.

Parameters

input_tensor (Tensor) – Input tensor

Returns

Feature map extracted from the CNN

class anomalib.models.components.KCenterGreedy(embedding: torch.Tensor, sampling_ratio: float)

Implements k-center-greedy method.

Parameters
  • embedding (Tensor) – Embedding vector extracted from a CNN

  • sampling_ratio (float) – Ratio to choose coreset size from the embedding size.

Example

>>> embedding.shape
torch.Size([219520, 1536])
>>> sampler = KCenterGreedy(embedding=embedding)
>>> sampled_idxs = sampler.select_coreset_idxs()
>>> coreset = embedding[sampled_idxs]
>>> coreset.shape
torch.Size([219, 1536])
reset_distances(self) None

Reset minimum distances.

update_distances(self, cluster_centers: List[int]) None

Update min distances given cluster centers.

Parameters

cluster_centers (List[int]) – indices of cluster centers

get_new_idx(self) int

Get index value of a sample.

Based on minimum distance of the cluster

Returns

Sample index

Return type

int

select_coreset_idxs(self, selected_idxs: Optional[List[int]] = None) List[int]

Greedily form a coreset to minimize the maximum distance of a cluster.

Parameters

selected_idxs – index of samples already selected. Defaults to an empty set.

Returns

indices of samples selected to minimize distance to cluster centers

sample_coreset(self, selected_idxs: Optional[List[int]] = None) torch.Tensor

Select coreset from the embedding.

Parameters

selected_idxs – index of samples already selected. Defaults to an empty set.

Returns

Output coreset

Return type

Tensor

Example

>>> embedding.shape
torch.Size([219520, 1536])
>>> sampler = KCenterGreedy(...)
>>> coreset = sampler.sample_coreset()
>>> coreset.shape
torch.Size([219, 1536])
class anomalib.models.components.GaussianKDE(dataset: Optional[torch.Tensor] = None)

Bases: anomalib.models.components.base.DynamicBufferModule

Gaussian Kernel Density Estimation.

Parameters

dataset (Optional[Tensor], optional) – Dataset on which to fit the KDE model. Defaults to None.

forward(self, features: torch.Tensor) torch.Tensor

Get the KDE estimates from the feature map.

Parameters

features (Tensor) – Feature map extracted from the CNN

Returns: KDE Estimates

fit(self, dataset: torch.Tensor) None

Fit a KDE model to the input dataset.

Parameters

dataset (Tensor) – Input dataset.

Returns

None

static cov(tensor: torch.Tensor) torch.Tensor

Calculate the unbiased covariance matrix.

Parameters

tensor (Tensor) – Input tensor from which covariance matrix is computed.

Returns

Output covariance matrix.

class anomalib.models.components.MultiVariateGaussian(n_features, n_patches)

Bases: torch.nn.Module

Multi Variate Gaussian Distribution.

static _cov(observations: torch.Tensor, rowvar: bool = False, bias: bool = False, ddof: Optional[int] = None, aweights: torch.Tensor = None) torch.Tensor

Estimates covariance matrix like numpy.cov.

Parameters
  • observations (Tensor) – A 1-D or 2-D array containing multiple variables and observations. Each row of m represents a variable, and each column a single observation of all those variables. Also see rowvar below.

  • rowvar (bool) – If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. Defaults to False.

  • bias (bool) – Default normalization (False) is by (N - 1), where N is the number of observations given (unbiased estimate). If bias is True, then normalization is by N. These values can be overridden by using the keyword ddof in numpy versions >= 1.5. Defaults to False

  • ddof (Optional, int) – If not None the default value implied by bias is overridden. Note that ddof=1 will return the unbiased estimate, even if both fweights and aweights are specified, and ddof=0 will return the simple average. See the notes for the details. The default value is None.

  • aweights (Tensor) – 1-D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If ddof=0 the array of weights can be used to assign probabilities to observation vectors. (Default value = None)

Returns

The covariance matrix of the variables.

forward(self, embedding: torch.Tensor) List[torch.Tensor]

Calculate multivariate Gaussian distribution.

Parameters

embedding (Tensor) – CNN features whose dimensionality is reduced via either random sampling or PCA.

Returns

mean and inverse covariance of the multi-variate gaussian distribution that fits the features.

fit(self, embedding: torch.Tensor) List[torch.Tensor]

Fit multi-variate gaussian distribution to the input embedding.

Parameters

embedding (Tensor) – Embedding vector extracted from CNN.

Returns

Mean and the covariance of the embedding.