Dimensionality Reduction

Algorithms for decomposition and dimensionality reduction.

class anomalib.models.components.dimensionality_reduction.PCA(n_components: int | float)[source]

Bases: 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.

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

fit(dataset: Tensor) None[source]

Fits the PCA model to the dataset.

Parameters:

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

fit_transform(dataset: Tensor) Tensor[source]

Fit and transform PCA to dataset.

Parameters:

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

Returns:

Transformed dataset

forward(features: Tensor) Tensor[source]

Transforms the features.

Parameters:

features (Tensor) – Input features

Returns:

Transformed features

inverse_transform(features: Tensor) Tensor[source]

Inverses the transformed features.

Parameters:

features (Tensor) – Transformed features

Returns: Inverse features

transform(features: Tensor) Tensor[source]

Transforms the features based on singular vectors calculated earlier.

Parameters:

features (Tensor) – Input features

Returns:

Transformed features

training: bool
class anomalib.models.components.dimensionality_reduction.SparseRandomProjection(eps: float = 0.1, random_state: int | None = None)[source]

Bases: object

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 (int | None, optional) – Uses the seed to set the random state for sample_without_replacement function. Defaults to None.

fit(embedding: Tensor) SparseRandomProjection[source]

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)

johnson_lindenstrauss_min_dim(n_samples: int, eps: float = 0.1)[source]

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.

transform(embedding: Tensor) Tensor[source]

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)