anomalib.models.fastflow.torch_model

FastFlow Torch Model Implementation.

Module Contents

Classes

FastflowModel

FastFlow.

Functions

subnet_conv_func(→ Callable)

Subnet Convolutional Function.

create_fast_flow_block(...)

Create NF Fast Flow Block.

anomalib.models.fastflow.torch_model.subnet_conv_func(kernel_size: int, hidden_ratio: float) Callable[source]

Subnet Convolutional Function.

Callable class or function f, called as f(channels_in, channels_out) and

should return a torch.nn.Module. Predicts coupling coefficients \(s, t\).

Parameters
  • kernel_size (int) – Kernel Size

  • hidden_ratio (float) – Hidden ratio to compute number of hidden channels.

Returns

Sequential for the subnet constructor.

Return type

Callable

anomalib.models.fastflow.torch_model.create_fast_flow_block(input_dimensions: List[int], conv3x3_only: bool, hidden_ratio: float, flow_steps: int, clamp: float = 2.0) anomalib.models.components.freia.framework.SequenceINN[source]

Create NF Fast Flow Block.

This is to create Normalizing Flow (NF) Fast Flow model block based on Figure 2 and Section 3.3 in the paper.

Parameters
  • input_dimensions (List[int]) – Input dimensions (Channel, Height, Width)

  • conv3x3_only (bool) – Boolean whether to use conv3x3 only or conv3x3 and conv1x1.

  • hidden_ratio (float) – Ratio for the hidden layer channels.

  • flow_steps (int) – Flow steps.

  • clamp (float, optional) – Clamp. Defaults to 2.0.

Returns

FastFlow Block.

Return type

SequenceINN

class anomalib.models.fastflow.torch_model.FastflowModel(input_size: Tuple[int, int], backbone: str, pre_trained: bool = True, flow_steps: int = 8, conv3x3_only: bool = False, hidden_ratio: float = 1.0)[source]

Bases: torch.nn.Module

FastFlow.

Unsupervised Anomaly Detection and Localization via 2D Normalizing Flows.

Parameters
  • input_size (Tuple[int, int]) – Model input size.

  • backbone (str) – Backbone CNN network

  • pre_trained (bool, optional) – Boolean to check whether to use a pre_trained backbone.

  • flow_steps (int, optional) – Flow steps.

  • conv3x3_only (bool, optinoal) – Use only conv3x3 in fast_flow model. Defaults to False.

  • hidden_ratio (float, optional) – Ratio to calculate hidden var channels. Defaults to 1.0.

Raises

ValueError – When the backbone is not supported.

forward(input_tensor: torch.Tensor) Union[Tuple[List[torch.Tensor], List[torch.Tensor]], torch.Tensor][source]

Forward-Pass the input to the FastFlow Model.

Parameters

input_tensor (Tensor) – Input tensor.

Returns

During training, return

(hidden_variables, log-of-the-jacobian-determinants). During the validation/test, return the anomaly map.

Return type

Union[Tuple[Tensor, Tensor], Tensor]

_get_cnn_features(input_tensor: torch.Tensor) List[torch.Tensor][source]

Get CNN-based features.

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]

_get_cait_features(input_tensor: torch.Tensor) List[torch.Tensor][source]

Get Class-Attention-Image-Transformers (CaiT) features.

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]

_get_vit_features(input_tensor: torch.Tensor) List[torch.Tensor][source]

Get Vision Transformers (ViT) features.

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]