anomalib.models.components.dimensionality_reduction

Algorithms for decomposition and dimensionality reduction.

Submodules

Package Contents

Classes

PCA

Principle Component Analysis (PCA).

SparseRandomProjection

Sparse Random Projection using PyTorch operations.

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

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(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.dimensionality_reduction.SparseRandomProjection(eps: float = 0.1, random_state: Optional[int] = None)[source]

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

(SparseRandomProjection)

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)