anomalib.models.cflow.utils

Helper functions for CFlow implementation.

Module Contents

Functions

get_logp(dim_feature_vector: int, p_u: torch.Tensor, logdet_j: torch.Tensor) → torch.Tensor

Returns the log likelihood estimation.

positional_encoding_2d(condition_vector: int, height: int, width: int) → torch.Tensor

Creates embedding to store relative position of the feature vector using sine and cosine functions.

subnet_fc(dims_in: int, dims_out: int)

Subnetwork which predicts the affine coefficients.

cflow_head(condition_vector: int, coupling_blocks: int, clamp_alpha: float, n_features: int, permute_soft: bool = False) → anomalib.models.components.freia.framework.SequenceINN

Create invertible decoder network.

Attributes

anomalib.models.cflow.utils.logger[source]
anomalib.models.cflow.utils.get_logp(dim_feature_vector: int, p_u: torch.Tensor, logdet_j: torch.Tensor) torch.Tensor[source]

Returns the log likelihood estimation.

Parameters
  • dim_feature_vector (int) – Dimensions of the condition vector

  • p_u (torch.Tensor) – Random variable u

  • logdet_j (torch.Tensor) – log of determinant of jacobian returned from the invertable decoder

Returns

Log probability

Return type

torch.Tensor

anomalib.models.cflow.utils.positional_encoding_2d(condition_vector: int, height: int, width: int) torch.Tensor[source]

Creates embedding to store relative position of the feature vector using sine and cosine functions.

Parameters
  • condition_vector (int) – Length of the condition vector

  • height (int) – H of the positions

  • width (int) – W of the positions

Raises

ValueError – Cannot generate encoding with conditional vector length not as multiple of 4

Returns

condition_vector x HEIGHT x WIDTH position matrix

Return type

torch.Tensor

anomalib.models.cflow.utils.subnet_fc(dims_in: int, dims_out: int)[source]

Subnetwork which predicts the affine coefficients.

Parameters
  • dims_in (int) – input dimensions

  • dims_out (int) – output dimensions

Returns

Feed-forward subnetwork

Return type

nn.Sequential

anomalib.models.cflow.utils.cflow_head(condition_vector: int, coupling_blocks: int, clamp_alpha: float, n_features: int, permute_soft: bool = False) anomalib.models.components.freia.framework.SequenceINN[source]

Create invertible decoder network.

Parameters
  • condition_vector (int) – length of the condition vector

  • coupling_blocks (int) – number of coupling blocks to build the decoder

  • clamp_alpha (float) – clamping value to avoid exploding values

  • n_features (int) – number of decoder features

  • permute_soft (bool) – Whether to sample the permutation matrix \(R\) from \(SO(N)\), or to use hard permutations instead. Note, permute_soft=True is very slow when working with >512 dimensions.

Returns

decoder network block

Return type

SequenceINN