:py:mod:`anomalib.models.components.base` ========================================= .. py:module:: anomalib.models.components.base .. autoapi-nested-parse:: Base classes for all anomaly components. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 anomaly_module/index.rst dynamic_module/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.components.base.AnomalyModule anomalib.models.components.base.DynamicBufferModule .. py:class:: AnomalyModule Bases: :py:obj:`pytorch_lightning.LightningModule`, :py:obj:`abc.ABC` AnomalyModule to train, validate, predict and test images. Acts as a base class for all the Anomaly Modules in the library. .. py:method:: forward(batch) Forward-pass input tensor to the module. :param batch: Input Tensor :type batch: Tensor :returns: Output tensor from the model. :rtype: Tensor .. py:method:: validation_step(batch, batch_idx) -> dict :abstractmethod: To be implemented in the subclasses. .. py:method:: predict_step(batch: Any, batch_idx: int, _dataloader_idx: Optional[int] = None) -> Any Step function called during :meth:`~pytorch_lightning.trainer.trainer.Trainer.predict`. By default, it calls :meth:`~pytorch_lightning.core.lightning.LightningModule.forward`. Override to add any processing logic. :param batch: Current batch :type batch: Tensor :param batch_idx: Index of current batch :type batch_idx: int :param _dataloader_idx: Index of the current dataloader :type _dataloader_idx: int :returns: Predicted output .. py:method:: test_step(batch, _) Calls validation_step for anomaly map/score calculation. :param batch: Input batch :type batch: Tensor :param _: Index of the batch. :returns: Dictionary containing images, features, true labels and masks. These are required in `validation_epoch_end` for feature concatenation. .. py:method:: validation_step_end(val_step_outputs) Called at the end of each validation step. .. py:method:: test_step_end(test_step_outputs) Called at the end of each test step. .. py:method:: validation_epoch_end(outputs) Compute threshold and performance metrics. :param outputs: Batch of outputs from the validation step .. py:method:: test_epoch_end(outputs) Compute and save anomaly scores of the test set. :param outputs: Batch of outputs from the validation step .. py:method:: _compute_adaptive_threshold(outputs) .. py:method:: _collect_outputs(image_metric, pixel_metric, outputs) :staticmethod: .. py:method:: _post_process(outputs) :staticmethod: Compute labels based on model predictions. .. py:method:: _outputs_to_cpu(output) :staticmethod: .. py:method:: _log_metrics() Log computed performance metrics. .. py:method:: _load_normalization_class(state_dict: OrderedDict[str, torch.Tensor]) Assigns the normalization method to use. .. py:method:: load_state_dict(state_dict: OrderedDict[str, torch.Tensor], strict: bool = True) Load state dict from checkpoint. Ensures that normalization and thresholding attributes is properly setup before model is loaded. .. py:class:: DynamicBufferModule Bases: :py:obj:`abc.ABC`, :py:obj:`torch.nn.Module` Torch module that allows loading variables from the state dict even in the case of shape mismatch. .. py:method:: get_tensor_attribute(attribute_name: str) -> torch.Tensor Get attribute of the tensor given the name. :param attribute_name: Name of the tensor :type attribute_name: str :raises ValueError: `attribute_name` is not a torch Tensor :returns: Tensor attribute :rtype: Tensor .. py:method:: _load_from_state_dict(state_dict: dict, prefix: str, *args) Resizes the local buffers to match those stored in the state dict. Overrides method from parent class. :param state_dict: State dictionary containing weights :type state_dict: dict :param prefix: Prefix of the weight file. :type prefix: str :param \*args: