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:
InvertibleModuleModule 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_c (
list[tuple[int]] |None) – Dimensions of conditioning tensor(s). Defaults to None.subnet_constructor (
Callable|None) – Function that constructs the subnet, called asf(channels_in, channels_out). Defaults to None.affine_clamping (
float) – Clamping value for affine coupling. Defaults to 2.0.gin_block (
bool) – Use GIN coupling from Sorrenson et al, 2019. Defaults to False.global_affine_init (
float) – Initial value for global affine scaling. Defaults to 1.0.global_affine_type (
str) – Type of activation for global affine scaling. One of'SIGMOID','SOFTPLUS','EXP'. Defaults to'SOFTPLUS'.permute_soft (
bool) – Use soft permutation matrix from SO(N). Defaults to False.learned_householder_permutation (
int) – Number of learned householder reflections. Defaults to 0.reverse_permutation (
bool) – Apply inverse permutation before block. Defaults to False.
- Raises:
ValueError – If
subnet_constructoris None or dimensions are invalid.
- forward(x, c=None, rev=False, jac=True)#
Forward pass through the invertible block.