anomalib.models.components.freia.modules.all_in_one_block

All in One Block Module.

Module Contents

Classes

AllInOneBlock

Module combining the most common operations in a normalizing flow or similar model.

class anomalib.models.components.freia.modules.all_in_one_block.AllInOneBlock(dims_in, dims_c=[], subnet_constructor: Callable = None, affine_clamping: float = 2.0, gin_block: bool = False, global_affine_init: float = 1.0, global_affine_type: str = 'SOFTPLUS', permute_soft: bool = False, learned_householder_permutation: int = 0, reverse_permutation: bool = False)[source]

Bases: anomalib.models.components.freia.modules.base.InvertibleModule

Module combining the most common operations in a normalizing flow or similar model.

It combines affine coupling, permutation, and global affine transformation (‘ActNorm’). It can also be used as GIN coupling block, perform learned householder permutations, and use an inverted pre-permutation. The affine transformation includes a soft clamping mechanism, first used in Real-NVP. The block as a whole performs the following computation: .. math:

y = V\\,R \\; \\Psi(s_\\mathrm{global}) \\odot \\mathrm{Coupling}\\Big(R^{-1} V^{-1} x\\Big)+ t_\\mathrm{global}
  • The inverse pre-permutation of x (i.e. \(R^{-1} V^{-1}\)) is optional (see reverse_permutation below).

  • The learned householder reflection matrix \(V\) is also optional all together (see learned_householder_permutation below).

  • For the coupling, the input is split into \(x_1, x_2\) along the channel dimension. Then the output of the coupling operation is the two halves \(u = \\mathrm{concat}(u_1, u_2)\). .. math:

    u_1 &= x_1 \\odot \\exp \\Big( \\alpha \\; \\mathrm{tanh}\\big( s(x_2) \\big)\\Big) + t(x_2) \\\\
    u_2 &= x_2
    

    Because \(\\mathrm{tanh}(s) \\in [-1, 1]\), this clamping mechanism prevents exploding values in the exponential. The hyperparameter \(\\alpha\) can be adjusted.

_construct_householder_permutation()[source]

Compute a permutation matrix.

Compute a permutation matrix from the reflection vectors that are learned internally as nn.Parameters.

_permute(x, rev=False)[source]

Perform permutation.

Performs the permutation and scaling after the coupling operation. Returns transformed outputs and the LogJacDet of the scaling operation.

_pre_permute(x, rev=False)[source]

Permute before the coupling block, only used if reverse_permutation is set.

_affine(x, a, rev=False)[source]

Perform affine coupling operation.

Given the passive half, and the pre-activation outputs of the coupling subnetwork, perform the affine coupling operation. Returns both the transformed inputs and the LogJacDet.

forward(x, c=[], rev=False, jac=True)[source]

See base class docstring.

output_dims(input_dims)[source]

Output Dims.