anomalib.models.components¶
Components used within the models.
Subpackages¶
Package Contents¶
Classes¶
AnomalyModule to train, validate, predict and test images. |
|
Torch module that allows loading variables from the state dict even in the case of shape mismatch. |
|
Principle Component Analysis (PCA). |
|
Sparse Random Projection using PyTorch operations. |
|
Extract features from a CNN. |
|
Implements k-center-greedy method. |
|
Gaussian Kernel Density Estimation. |
|
Multi Variate Gaussian Distribution. |
- class anomalib.models.components.AnomalyModule¶
Bases:
pytorch_lightning.LightningModule,abc.ABCAnomalyModule to train, validate, predict and test images.
Acts as a base class for all the Anomaly Modules in the library.
- forward(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(batch, batch_idx) dict¶
To be implemented in the subclasses.
- predict_step(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(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(val_step_outputs)¶
Called at the end of each validation step.
- test_step_end(test_step_outputs)¶
Called at the end of each test step.
- validation_epoch_end(outputs)¶
Compute threshold and performance metrics.
- Parameters
outputs – Batch of outputs from the validation step
- test_epoch_end(outputs)¶
Compute and save anomaly scores of the test set.
- Parameters
outputs – Batch of outputs from the validation step
- _compute_adaptive_threshold(outputs)¶
- _collect_outputs(image_metric, pixel_metric, outputs)¶
- _post_process(outputs)¶
Compute labels based on model predictions.
- _outputs_to_cpu(output)¶
- _log_metrics()¶
Log computed performance metrics.
- class anomalib.models.components.DynamicBufferModule¶
Bases:
abc.ABC,torch.nn.ModuleTorch module that allows loading variables from the state dict even in the case of shape mismatch.
- get_tensor_attribute(attribute_name: str) torch.Tensor¶
Get attribute of the tensor given the name.
- Parameters
attribute_name (str) – Name of the tensor
- Raises
ValueError – attribute_name is not a torch Tensor
- Returns
Tensor attribute
- Return type
Tensor
- _load_from_state_dict(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.DynamicBufferModulePrinciple Component Analysis (PCA).
- Parameters
n_components (float) – Number of components. Can be either integer number of components or a ratio between 0-1.
- fit(dataset: torch.Tensor) None¶
Fits the PCA model to the dataset.
- Parameters
dataset (Tensor) – Input dataset to fit the model.
- fit_transform(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(features: torch.Tensor) torch.Tensor¶
Transforms the features based on singular vectors calculated earlier.
- Parameters
features (Tensor) – Input features
- Returns
Transformed features
- inverse_transform(features: torch.Tensor) torch.Tensor¶
Inverses the transformed features.
- Parameters
features (Tensor) – Transformed features
Returns: Inverse features
- forward(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(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(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(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
- transform(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.ModuleExtract 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(layer_id: str) Callable¶
Get layer features.
- Parameters
layer_id (str) – Layer ID
- Returns
Layer features
- forward(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() None¶
Reset minimum distances.
- update_distances(cluster_centers: List[int]) None¶
Update min distances given cluster centers.
- Parameters
cluster_centers (List[int]) – indices of cluster centers
- get_new_idx() int¶
Get index value of a sample.
Based on minimum distance of the cluster
- Returns
Sample index
- Return type
int
- select_coreset_idxs(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(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.DynamicBufferModuleGaussian Kernel Density Estimation.
- Parameters
dataset (Optional[Tensor], optional) – Dataset on which to fit the KDE model. Defaults to None.
- forward(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(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.ModuleMulti 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), whereNis the number of observations given (unbiased estimate). If bias is True, then normalization is byN. These values can be overridden by using the keywordddofin numpy versions >= 1.5. Defaults to Falseddof (Optional, int) – If not
Nonethe default value implied by bias is overridden. Note thatddof=1will return the unbiased estimate, even if both fweights and aweights are specified, andddof=0will return the simple average. See the notes for the details. The default value isNone.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=0the array of weights can be used to assign probabilities to observation vectors. (Default value = None)
- Returns
The covariance matrix of the variables.
- forward(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(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.