Normalizing Flows#

Flow components used in anomaly detection models.

This module provides flow-based components that can be used in anomaly detection models. These components help model complex data distributions and transformations.

Classes:
AllInOneBlock: A block that combines multiple flow operations into a single

transformation.

Example

>>> import torch
>>> from anomalib.models.components.flow import AllInOneBlock
>>> # Create flow block
>>> flow = AllInOneBlock(channels=64)
>>> # Apply flow transformation
>>> x = torch.randn(1, 64, 32, 32)
>>> y, logdet = flow(x)
class anomalib.models.components.flow.AllInOneBlock(dims_in, dims_c=None, subnet_constructor=None, affine_clamping=2.0, gin_block=False, global_affine_init=1.0, global_affine_type='SOFTPLUS', permute_soft=False, learned_householder_permutation=0, reverse_permutation=False)#

Bases: InvertibleModule

Module combining common operations in normalizing flows.

This block combines affine coupling, permutation, and global affine transformation (‘ActNorm’). It supports:

  • GIN coupling blocks

  • Learned householder permutations

  • Inverted pre-permutation

  • Soft clamping mechanism from Real-NVP

Parameters:
  • dims_in (list[tuple[int]]) – Dimensions of input tensor(s)

  • dims_c (list[tuple[int]], optional) – Dimensions of conditioning tensor(s). Defaults to None.

  • subnet_constructor (Callable, optional) – Function that constructs the subnet, called as f(channels_in, channels_out). Defaults to None.

  • affine_clamping (float, optional) – Clamping value for affine coupling. Defaults to 2.0.

  • gin_block (bool, optional) – Use GIN coupling from Sorrenson et al, 2019. Defaults to False.

  • global_affine_init (float, optional) – Initial value for global affine scaling. Defaults to 1.0.

  • global_affine_type (str, optional) – Type of activation for global affine scaling. One of 'SIGMOID', 'SOFTPLUS', 'EXP'. Defaults to 'SOFTPLUS'.

  • permute_soft (bool, optional) – Use soft permutation matrix from SO(N). Defaults to False.

  • learned_householder_permutation (int, optional) – Number of learned householder reflections. Defaults to 0.

  • reverse_permutation (bool, optional) – Apply inverse permutation before block. Defaults to False.

Raises:

ValueError – If subnet_constructor is None or dimensions are invalid.

forward(x, c=None, rev=False, jac=True)#

Forward pass through the invertible block.

Parameters:
  • x (torch.Tensor) – Input tensor

  • c (list, optional) – Conditioning tensors. Defaults to None.

  • rev (bool, optional) – Reverse the flow. Defaults to False.

  • jac (bool, optional) – Compute Jacobian determinant. Defaults to True.

Returns:

Tuple of (output tensors,

LogJacDet)

Return type:

tuple[tuple[torch.Tensor], torch.Tensor]

static output_dims(input_dims)#

Get output dimensions of the layer.

Parameters:

input_dims (list[tuple[int]]) – Input dimensions

Returns:

Output dimensions

Return type:

list[tuple[int]]