anomalib.models.padim.anomaly_map¶
Anomaly Map Generator for the PaDiM model implementation.
Module Contents¶
Classes¶
Generate Anomaly Heatmap. |
- class anomalib.models.padim.anomaly_map.AnomalyMapGenerator(image_size: Union[omegaconf.ListConfig, Tuple], sigma: int = 4)[source]¶
Bases:
torch.nn.ModuleGenerate Anomaly Heatmap.
- Parameters
image_size (Union[ListConfig, Tuple]) – Size of the input image. The anomaly map is upsampled to this dimension.
sigma (int, optional) – Standard deviation for Gaussian Kernel. Defaults to 4.
- static compute_distance(embedding: torch.Tensor, stats: List[torch.Tensor]) torch.Tensor[source]¶
Compute anomaly score to the patch in position(i,j) of a test image.
Ref: Equation (2), Section III-C of the paper.
- Parameters
embedding (Tensor) – Embedding Vector
stats (List[Tensor]) – Mean and Covariance Matrix of the multivariate Gaussian distribution
- Returns
Anomaly score of a test image via mahalanobis distance.
- up_sample(distance: torch.Tensor) torch.Tensor[source]¶
Up sample anomaly score to match the input image size.
- Parameters
distance (Tensor) – Anomaly score computed via the mahalanobis distance.
- Returns
Resized distance matrix matching the input image size
- smooth_anomaly_map(anomaly_map: torch.Tensor) torch.Tensor[source]¶
Apply gaussian smoothing to the anomaly map.
- Parameters
anomaly_map (Tensor) – Anomaly score for the test image(s).
- Returns
Filtered anomaly scores
- compute_anomaly_map(embedding: torch.Tensor, mean: torch.Tensor, inv_covariance: torch.Tensor) torch.Tensor[source]¶
Compute anomaly score.
Scores are calculated based on embedding vector, mean and inv_covariance of the multivariate gaussian distribution.
- Parameters
embedding (Tensor) – Embedding vector extracted from the test set.
mean (Tensor) – Mean of the multivariate gaussian distribution
inv_covariance (Tensor) – Inverse Covariance matrix of the multivariate gaussian distribution.
- Returns
Output anomaly score.
- forward(**kwargs)[source]¶
Returns anomaly_map.
Expects embedding, mean and covariance keywords to be passed explicitly.
Example: >>> anomaly_map_generator = AnomalyMapGenerator(image_size=input_size) >>> output = anomaly_map_generator(embedding=embedding, mean=mean, covariance=covariance)
- Raises
ValueError – embedding. mean or covariance keys are not found
- Returns
anomaly map
- Return type
torch.Tensor