anomalib.models.fastflow

FastFlow Algorithm Implementation.

Submodules

Package Contents

Classes

Fastflow

PL Lightning Module for the FastFlow algorithm.

FastflowLightning

PL Lightning Module for the FastFlow algorithm.

FastflowLoss

FastFlow Loss.

FastflowModel

FastFlow.

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

Bases: anomalib.models.components.AnomalyModule

PL Lightning Module for the FastFlow algorithm.

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

  • backbone (str) – Backbone CNN network

  • flow_steps (int) – 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.

training_step(self, batch, _)

Forward-pass input and return the loss.

Parameters
  • batch (Tensor) – Input batch

  • _batch_idx – Index of the batch.

Returns

Dictionary containing the loss value.

Return type

STEP_OUTPUT

validation_step(self, batch, _)

Forward-pass the input and return the anomaly map.

Parameters
  • batch (Tensor) – Input batch

  • _batch_idx – Index of the batch.

Returns

batch dictionary containing anomaly-maps.

Return type

dict

class anomalib.models.fastflow.FastflowLightning(hparams: Union[omegaconf.DictConfig, omegaconf.ListConfig])[source]

Bases: Fastflow

PL Lightning Module for the FastFlow algorithm.

Parameters

hparams (Union[DictConfig, ListConfig]) – Model params

configure_callbacks(self)

Configure model-specific callbacks.

Note

This method is used for the existing CLI. When PL CLI is introduced, configure callback method will be

deprecated, and callbacks will be configured from either config.yaml file or from CLI.

configure_optimizers(self) torch.optim.Optimizer

Configures optimizers for each decoder.

Note

This method is used for the existing CLI. When PL CLI is introduced, configure optimizers method will be

deprecated, and optimizers will be configured from either config.yaml file or from CLI.

Returns

Adam optimizer for each decoder

Return type

Optimizer

class anomalib.models.fastflow.FastflowLoss[source]

Bases: torch.nn.Module

FastFlow Loss.

forward(self, hidden_variables: List[torch.Tensor], jacobians: List[torch.Tensor]) torch.Tensor

Calculate the Fastflow loss.

Parameters
  • hidden_variables (List[Tensor]) – Hidden variables from the fastflow model. f: X -> Z

  • jacobians (List[Tensor]) – Log of the jacobian determinants from the fastflow model.

Returns

Fastflow loss computed based on the hidden variables and the log of the Jacobians.

Return type

Tensor

class anomalib.models.fastflow.FastflowModel(input_size: Tuple[int, int], backbone: str, flow_steps: int, 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

  • flow_steps (int) – 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(self, input_tensor: torch.Tensor) Union[Tuple[List[torch.Tensor], List[torch.Tensor]], torch.Tensor]

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(self, input_tensor: torch.Tensor) List[torch.Tensor]

Get CNN-based features.

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]

_get_cait_features(self, input_tensor: torch.Tensor) List[torch.Tensor]

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

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]

_get_vit_features(self, input_tensor: torch.Tensor) List[torch.Tensor]

Get Vision Transformers (ViT) features.

Parameters

input_tensor (Tensor) – Input Tensor.

Returns

List of features.

Return type

List[Tensor]