:py:mod:`anomalib.models.fastflow` ================================== .. py:module:: anomalib.models.fastflow .. autoapi-nested-parse:: FastFlow Algorithm Implementation. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 anomaly_map/index.rst lightning_model/index.rst loss/index.rst torch_model/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.fastflow.Fastflow anomalib.models.fastflow.FastflowLightning anomalib.models.fastflow.FastflowLoss anomalib.models.fastflow.FastflowModel .. py:class:: Fastflow(input_size: Tuple[int, int], backbone: str, pre_trained: bool = True, flow_steps: int = 8, conv3x3_only: bool = False, hidden_ratio: float = 1.0) Bases: :py:obj:`anomalib.models.components.AnomalyModule` PL Lightning Module for the FastFlow algorithm. :param input_size: Model input size. :type input_size: Tuple[int, int] :param backbone: Backbone CNN network :type backbone: str :param pre_trained: Boolean to check whether to use a pre_trained backbone. :type pre_trained: bool, optional :param flow_steps: Flow steps. :type flow_steps: int, optional :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 .. py:method:: training_step(batch, _) Forward-pass input and return the loss. :param batch: Input batch :type batch: Tensor :param _batch_idx: Index of the batch. :returns: Dictionary containing the loss value. :rtype: STEP_OUTPUT .. py:method:: validation_step(batch, _) Forward-pass the input and return the anomaly map. :param batch: Input batch :type batch: Tensor :param _batch_idx: Index of the batch. :returns: batch dictionary containing anomaly-maps. :rtype: dict .. py:class:: FastflowLightning(hparams: Union[omegaconf.DictConfig, omegaconf.ListConfig]) Bases: :py:obj:`Fastflow` PL Lightning Module for the FastFlow algorithm. :param hparams: Model params :type hparams: Union[DictConfig, ListConfig] .. py:method:: configure_callbacks() 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. .. py:method:: configure_optimizers() -> 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 :rtype: Optimizer .. py:class:: FastflowLoss Bases: :py:obj:`torch.nn.Module` FastFlow Loss. .. py:method:: forward(hidden_variables: List[torch.Tensor], jacobians: List[torch.Tensor]) -> torch.Tensor Calculate the Fastflow loss. :param hidden_variables: Hidden variables from the fastflow model. f: X -> Z :type hidden_variables: List[Tensor] :param jacobians: Log of the jacobian determinants from the fastflow model. :type jacobians: List[Tensor] :returns: Fastflow loss computed based on the hidden variables and the log of the Jacobians. :rtype: Tensor .. py:class:: 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) 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 pre_trained: Boolean to check whether to use a pre_trained backbone. :type pre_trained: bool, optional :param flow_steps: Flow steps. :type flow_steps: int, optional :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(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(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(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(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]