:py:mod:`anomalib.models.fastflow.torch_model` ============================================== .. py:module:: anomalib.models.fastflow.torch_model .. autoapi-nested-parse:: FastFlow Torch Model Implementation. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.fastflow.torch_model.FastflowModel Functions ~~~~~~~~~ .. autoapisummary:: anomalib.models.fastflow.torch_model.subnet_conv_func anomalib.models.fastflow.torch_model.create_fast_flow_block .. py:function:: subnet_conv_func(kernel_size: int, hidden_ratio: float) -> Callable 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 :math:`s, t`. :param kernel_size: Kernel Size :type kernel_size: int :param hidden_ratio: Hidden ratio to compute number of hidden channels. :type hidden_ratio: float :returns: Sequential for the subnet constructor. :rtype: Callable .. py:function:: 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 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. :param input_dimensions: Input dimensions (Channel, Height, Width) :type input_dimensions: List[int] :param conv3x3_only: Boolean whether to use conv3x3 only or conv3x3 and conv1x1. :type conv3x3_only: bool :param hidden_ratio: Ratio for the hidden layer channels. :type hidden_ratio: float :param flow_steps: Flow steps. :type flow_steps: int :param clamp: Clamp. Defaults to 2.0. :type clamp: float, optional :returns: FastFlow Block. :rtype: SequenceINN .. py:class:: FastflowModel(input_size: Tuple[int, int], backbone: str, flow_steps: int, conv3x3_only: bool = False, hidden_ratio: float = 1.0) Bases: :py:obj:`torch.nn.Module` FastFlow. Unsupervised Anomaly Detection and Localization via 2D Normalizing Flows. :param input_size: Model input size. :type input_size: Tuple[int, int] :param backbone: Backbone CNN network :type backbone: str :param flow_steps: Flow steps. :type flow_steps: int :param conv3x3_only: Use only conv3x3 in fast_flow model. Defaults to False. :type conv3x3_only: bool, optinoal :param hidden_ratio: Ratio to calculate hidden var channels. Defaults to 1.0. :type hidden_ratio: float, optional :raises ValueError: When the backbone is not supported. .. py:method:: forward(self, input_tensor: torch.Tensor) -> Union[Tuple[List[torch.Tensor], List[torch.Tensor]], torch.Tensor] Forward-Pass the input to the FastFlow Model. :param input_tensor: Input tensor. :type input_tensor: Tensor :returns: During training, return (hidden_variables, log-of-the-jacobian-determinants). During the validation/test, return the anomaly map. :rtype: Union[Tuple[Tensor, Tensor], Tensor] .. py:method:: _get_cnn_features(self, input_tensor: torch.Tensor) -> List[torch.Tensor] Get CNN-based features. :param input_tensor: Input Tensor. :type input_tensor: Tensor :returns: List of features. :rtype: List[Tensor] .. py:method:: _get_cait_features(self, input_tensor: torch.Tensor) -> List[torch.Tensor] Get Class-Attention-Image-Transformers (CaiT) features. :param input_tensor: Input Tensor. :type input_tensor: Tensor :returns: List of features. :rtype: List[Tensor] .. py:method:: _get_vit_features(self, input_tensor: torch.Tensor) -> List[torch.Tensor] Get Vision Transformers (ViT) features. :param input_tensor: Input Tensor. :type input_tensor: Tensor :returns: List of features. :rtype: List[Tensor]