anomalib.utils.callbacks.visualizer

Callbacks to visualize anomaly detection performance.

Submodules

Package Contents

Classes

BaseVisualizerCallback

Callback that visualizes the results of a model.

ImageVisualizerCallback

Callback that visualizes the inference results of a model.

MetricVisualizerCallback

Callback that visualizes the metric results of a model by plotting the corresponding curves.

class anomalib.utils.callbacks.visualizer.BaseVisualizerCallback(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)[source]

Bases: pytorch_lightning.Callback

Callback that visualizes the results of a model.

To save the images to the filesystem, add the ‘local’ keyword to the project.log_images_to parameter in the config.yaml file.

_add_to_logger(image: numpy.ndarray, module: anomalib.models.components.AnomalyModule, trainer: pytorch_lightning.Trainer, filename: Union[pathlib.Path, str])

Log image from a visualizer to each of the available loggers in the project.

Parameters
  • image (np.ndarray) – Image that should be added to the loggers.

  • module (AnomalyModule) – Anomaly module.

  • trainer (Trainer) – Pytorch Lightning trainer which holds reference to logger

  • filename (Path) – Path of the input image. This name is used as name for the generated image.

on_test_end(trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) None

Sync logs.

Currently only AnomalibWandbLogger.save is called from this method. This is because logging as a single batch ensures that all images appear as part of the same step.

Parameters
  • trainer (pl.Trainer) – Pytorch Lightning trainer

  • pl_module (AnomalyModule) – Anomaly module (unused)

class anomalib.utils.callbacks.visualizer.ImageVisualizerCallback(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)[source]

Bases: anomalib.utils.callbacks.visualizer.visualizer_base.BaseVisualizerCallback

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.

on_predict_batch_end(_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.

Parameters
  • _trainer (Trainer) – Pytorch lightning trainer object (unused).

  • _pl_module (LightningModule) – Lightning modules derived from BaseAnomalyLightning object as

  • images. (currently only they support logging) –

  • outputs (Dict[str, Any]) – Outputs of the current test step.

  • _batch (Any) – Input batch of the current test step (unused).

  • _batch_idx (int) – Index of the current test batch (unused).

  • _dataloader_idx (int) – Index of the dataloader that yielded the current batch (unused).

on_test_batch_end(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.

Parameters
  • trainer (Trainer) – Pytorch lightning trainer object (unused).

  • pl_module (LightningModule) – Lightning modules derived from BaseAnomalyLightning object as

  • images. (currently only they support logging) –

  • outputs (Dict[str, Any]) – Outputs of the current test step.

  • _batch (Any) – Input batch of the current test step (unused).

  • _batch_idx (int) – Index of the current test batch (unused).

  • _dataloader_idx (int) – Index of the dataloader that yielded the current batch (unused).

class anomalib.utils.callbacks.visualizer.MetricVisualizerCallback(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)[source]

Bases: anomalib.utils.callbacks.visualizer.visualizer_base.BaseVisualizerCallback

Callback that visualizes the metric results of a model by plotting the corresponding curves.

To save the images to the filesystem, add the ‘local’ keyword to the project.log_images_to parameter in the config.yaml file.

on_test_end(trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) None

Log images of the metrics contained in pl_module.

In order to also plot custom metrics, they need to have implemented a generate_figure function that returns Tuple[matplotlib.figure.Figure, str].

Parameters
  • trainer (pl.Trainer) – pytorch lightning trainer.

  • pl_module (AnomalyModule) – pytorch lightning module.