Stats

Statistical functions.

class anomalib.models.components.stats.GaussianKDE(dataset: Optional[Tensor] = None)[source]

Bases: DynamicBufferModule

Gaussian Kernel Density Estimation.

Parameters:

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

Initializes internal Module state, shared by both nn.Module and ScriptModule.

static cov(tensor: Tensor) Tensor[source]

Calculate the unbiased covariance matrix.

Parameters:

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

Returns:

Output covariance matrix.

fit(dataset: Tensor) None[source]

Fit a KDE model to the input dataset.

Parameters:

dataset (Tensor) – Input dataset.

Returns:

None

forward(features: Tensor) Tensor[source]

Get the KDE estimates from the feature map.

Parameters:

features (Tensor) – Feature map extracted from the CNN

Returns: KDE Estimates

class anomalib.models.components.stats.MultiVariateGaussian(n_features, n_patches)[source]

Bases: Module

Multi Variate Gaussian Distribution.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

fit(embedding: Tensor) list[torch.Tensor][source]

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.

forward(embedding: Tensor) list[torch.Tensor][source]

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.