:py:mod:`anomalib.utils.callbacks` ================================== .. py:module:: anomalib.utils.callbacks .. autoapi-nested-parse:: Callbacks for Anomalib models. Subpackages ----------- .. toctree:: :titlesonly: :maxdepth: 3 nncf/index.rst Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 cdf_normalization/index.rst graph/index.rst metrics_configuration/index.rst min_max_normalization/index.rst model_loader/index.rst openvino/index.rst tiler_configuration/index.rst timer/index.rst visualizer_callback/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.utils.callbacks.CdfNormalizationCallback anomalib.utils.callbacks.MetricsConfigurationCallback anomalib.utils.callbacks.MinMaxNormalizationCallback anomalib.utils.callbacks.LoadModelCallback anomalib.utils.callbacks.TilerConfigurationCallback anomalib.utils.callbacks.TimerCallback anomalib.utils.callbacks.VisualizerCallback .. py:class:: CdfNormalizationCallback Bases: :py:obj:`pytorch_lightning.Callback` Callback that standardizes the image-level and pixel-level anomaly scores. .. py:method:: on_test_start(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Called when the test begins. .. py:method:: on_validation_epoch_start(self, trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Called when the validation starts after training. Use the current model to compute the anomaly score distributions of the normal training data. This is needed after every epoch, because the statistics must be stored in the state dict of the checkpoint file. .. py:method:: on_validation_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: Optional[pytorch_lightning.utilities.types.STEP_OUTPUT], _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the validation batch ends, standardizes the predicted scores and anomaly maps. .. py:method:: on_test_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: Optional[pytorch_lightning.utilities.types.STEP_OUTPUT], _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the test batch ends, normalizes the predicted scores and anomaly maps. .. py:method:: on_predict_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: Dict, _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the predict batch ends, normalizes the predicted scores and anomaly maps. .. py:method:: _collect_stats(self, trainer, pl_module) Collect the statistics of the normal training data. Create a trainer and use it to predict the anomaly maps and scores of the normal training data. Then estimate the distribution of anomaly scores for normal data at the image and pixel level by computing the mean and standard deviations. A dictionary containing the computed statistics is stored in self.stats. .. py:method:: _create_inference_model(pl_module) :staticmethod: Create a duplicate of the PL module that can be used to perform inference on the training set. .. py:method:: _standardize_batch(outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, pl_module) -> None :staticmethod: .. py:method:: _normalize_batch(outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, pl_module: anomalib.models.components.AnomalyModule) -> None :staticmethod: .. py:class:: MetricsConfigurationCallback(adaptive_threshold: bool, default_image_threshold: Optional[float] = None, default_pixel_threshold: Optional[float] = None, image_metric_names: Optional[List[str]] = None, pixel_metric_names: Optional[List[str]] = None, normalization_method: str = 'min_max') Bases: :py:obj:`pytorch_lightning.callbacks.Callback` Metrics Configuration Callback. .. py:method:: setup(self, _trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule, stage: Optional[str] = None) -> None Setup image and pixel-level AnomalibMetricsCollection within Anomalib Model. :param _trainer: PyTorch Lightning Trainer :type _trainer: pl.Trainer :param pl_module: Anomalib Model that inherits pl LightningModule. :type pl_module: pl.LightningModule :param stage: fit, validate, test or predict. Defaults to None. :type stage: Optional[str], optional .. py:class:: MinMaxNormalizationCallback Bases: :py:obj:`pytorch_lightning.Callback` Callback that normalizes the image-level and pixel-level anomaly scores using min-max normalization. .. py:method:: on_test_start(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Called when the test begins. .. py:method:: on_validation_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the validation batch ends, update the min and max observed values. .. py:method:: on_test_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the test batch ends, normalizes the predicted scores and anomaly maps. .. py:method:: on_predict_batch_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: Dict, _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Called when the predict batch ends, normalizes the predicted scores and anomaly maps. .. py:method:: _normalize_batch(outputs, pl_module) :staticmethod: Normalize a batch of predictions. .. py:class:: LoadModelCallback(weights_path) Bases: :py:obj:`pytorch_lightning.Callback` Callback that loads the model weights from the state dict. .. py:method:: on_test_start(self, _trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Call when the test begins. Loads the model weights from ``weights_path`` into the PyTorch module. .. py:method:: on_predict_start(self, _trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Call when inference begins. Loads the model weights from ``weights_path`` into the PyTorch module. .. py:class:: TilerConfigurationCallback(enable: bool = False, tile_size: Union[int, Sequence] = 256, stride: Optional[Union[int, Sequence]] = None, remove_border_count: int = 0, mode: str = 'padding', tile_count: int = 4) Bases: :py:obj:`pytorch_lightning.callbacks.Callback` Tiler Configuration Callback. .. py:method:: setup(self, _trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule, stage: Optional[str] = None) -> None Setup Tiler object within Anomalib Model. :param _trainer: PyTorch Lightning Trainer :type _trainer: pl.Trainer :param pl_module: Anomalib Model that inherits pl LightningModule. :type pl_module: pl.LightningModule :param stage: fit, validate, test or predict. Defaults to None. :type stage: Optional[str], optional :raises ValueError: When Anomalib Model doesn't contain ``Tiler`` object, it means the model doesn not support tiling operation. .. py:class:: TimerCallback Bases: :py:obj:`pytorch_lightning.Callback` Callback that measures the training and testing time of a PyTorch Lightning module. .. py:method:: on_fit_start(self, trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) -> None Call when fit begins. Sets the start time to the time training started. :param trainer: PyTorch Lightning trainer. :type trainer: Trainer :param pl_module: Current training module. :type pl_module: LightningModule :returns: None .. py:method:: on_fit_end(self, trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) -> None Call when fit ends. Prints the time taken for training. :param trainer: PyTorch Lightning trainer. :type trainer: Trainer :param pl_module: Current training module. :type pl_module: LightningModule :returns: None .. py:method:: on_test_start(self, trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) -> None Call when the test begins. Sets the start time to the time testing started. Goes over all the test dataloaders and adds the number of images in each. :param trainer: PyTorch Lightning trainer. :type trainer: Trainer :param pl_module: Current training module. :type pl_module: LightningModule :returns: None .. py:method:: on_test_end(self, trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) -> None Call when the test ends. Prints the time taken for testing and the throughput in frames per second. :param trainer: PyTorch Lightning trainer. :type trainer: Trainer :param pl_module: Current training module. :type pl_module: LightningModule :returns: None .. py:class:: VisualizerCallback(task: str, mode: str, image_save_path: str, inputs_are_normalized: bool = True, show_images: bool = False, log_images: bool = True, save_images: bool = True) Bases: :py:obj:`pytorch_lightning.Callback` Callback that visualizes the inference results of a model. The callback generates a figure showing the original image, the ground truth segmentation mask, the predicted error heat map, and the predicted segmentation mask. To save the images to the filesystem, add the 'local' keyword to the `project.log_images_to` parameter in the config.yaml file. .. py:method:: _add_to_logger(self, image: numpy.ndarray, module: anomalib.models.components.AnomalyModule, trainer: pytorch_lightning.Trainer, filename: pathlib.Path) Log image from a visualizer to each of the available loggers in the project. :param image: Image that should be added to the loggers. :type image: np.ndarray :param module: Anomaly module. :type module: AnomalyModule :param trainer: Pytorch Lightning trainer which holds reference to `logger` :type trainer: Trainer :param filename: Path of the input image. This name is used as name for the generated image. :type filename: Path .. py:method:: on_predict_batch_end(self, _trainer: pytorch_lightning.Trainer, _pl_module: anomalib.models.components.AnomalyModule, outputs: Optional[pytorch_lightning.utilities.types.STEP_OUTPUT], _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Show images at the end of every batch. :param _trainer: Pytorch lightning trainer object (unused). :type _trainer: Trainer :param _pl_module: Lightning modules derived from BaseAnomalyLightning object as :type _pl_module: LightningModule :param currently only they support logging images.: :param outputs: Outputs of the current test step. :type outputs: Dict[str, Any] :param _batch: Input batch of the current test step (unused). :type _batch: Any :param _batch_idx: Index of the current test batch (unused). :type _batch_idx: int :param _dataloader_idx: Index of the dataloader that yielded the current batch (unused). :type _dataloader_idx: int .. py:method:: on_test_batch_end(self, trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule, outputs: Optional[pytorch_lightning.utilities.types.STEP_OUTPUT], _batch: Any, _batch_idx: int, _dataloader_idx: int) -> None Log images at the end of every batch. :param trainer: Pytorch lightning trainer object (unused). :type trainer: Trainer :param pl_module: Lightning modules derived from BaseAnomalyLightning object as :type pl_module: LightningModule :param currently only they support logging images.: :param outputs: Outputs of the current test step. :type outputs: Dict[str, Any] :param _batch: Input batch of the current test step (unused). :type _batch: Any :param _batch_idx: Index of the current test batch (unused). :type _batch_idx: int :param _dataloader_idx: Index of the dataloader that yielded the current batch (unused). :type _dataloader_idx: int .. py:method:: on_test_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) -> None Sync logs. Currently only ``AnomalibWandbLogger`` is called from this method. This is because logging as a single batch ensures that all images appear as part of the same step. :param _trainer: Pytorch Lightning trainer (unused) :type _trainer: pl.Trainer :param pl_module: Anomaly module :type pl_module: AnomalyModule