Callbacks#

Callbacks for Anomalib models.

class anomalib.callbacks.GraphLogger#

Bases: Callback

Log model graph to respective logger.

Examples

Log model graph to Tensorboard

>>> from anomalib.callbacks import GraphLogger
>>> from anomalib.loggers import AnomalibTensorBoardLogger
>>> from anomalib.engine import Engine
...
>>> logger = AnomalibTensorBoardLogger()
>>> callbacks = [GraphLogger()]
>>> engine = Engine(logger=logger, callbacks=callbacks)

Log model graph to Comet

>>> from anomalib.loggers import AnomalibCometLogger
>>> from anomalib.engine import Engine
...
>>> logger = AnomalibCometLogger()
>>> callbacks = [GraphLogger()]
>>> engine = Engine(logger=logger, callbacks=callbacks)
on_train_end(trainer, pl_module)#

Unwatch model if configured for wandb and log it model graph in Tensorboard if specified.

Parameters:
  • trainer (Trainer) – Trainer object which contans reference to loggers.

  • pl_module (LightningModule) – LightningModule object which is logged.

Return type:

None

on_train_start(trainer, pl_module)#

Log model graph to respective logger.

Parameters:
  • trainer (Trainer) – Trainer object which contans reference to loggers.

  • pl_module (LightningModule) – LightningModule object which is logged.

Return type:

None

class anomalib.callbacks.ImageVisualizerCallback(task, mode, image_save_path, inputs_are_normalized=True, show_images=False, log_images=True, save_images=True)#

Bases: 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.

Examples

>>> from anomalib.callbacks import ImageVisualizerCallback
>>> from anomalib.engine import Engine
...
>>> callbacks = [ImageVisualizerCallback()]
>>> engine = Engine(callbacks=callbacks)
on_predict_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)#

Show images at the end of every batch.

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

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

  • images. (currently only they support logging) –

  • outputs (STEP_OUTPUT | None) – 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).

Return type:

None

on_test_batch_end(trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0)#

Log images at the end of every batch.

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

  • pl_module (AnomalyModule) – Lightning modules derived from BaseAnomalyLightning object as currently only they support logging images.

  • outputs (STEP_OUTPUT | None) – 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).

Return type:

None

class anomalib.callbacks.LoadModelCallback(weights_path)#

Bases: Callback

Callback that loads the model weights from the state dict.

Examples

>>> from anomalib.callbacks import LoadModelCallback
>>> from anomalib.engine import Engine
...
>>> callbacks = [LoadModelCallback(weights_path="path/to/weights.pt")]
>>> engine = Engine(callbacks=callbacks)
setup(trainer, pl_module, stage=None)#

Call when inference begins.

Loads the model weights from weights_path into the PyTorch module.

Return type:

None

class anomalib.callbacks.MetricVisualizerCallback(task, mode, image_save_path, inputs_are_normalized=True, show_images=False, log_images=True, save_images=True)#

Bases: BaseVisualizerCallback

Metric Visualization.

This callback 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.

Examples

>>> from anomalib.callbacks import MetricVisualizerCallback
>>> from anomalib.engine import Engine
...
>>> callbacks = [MetricVisualizerCallback()]
>>> engine = Engine(callbacks=callbacks)
on_test_end(trainer, pl_module)#

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.

Return type:

None

class anomalib.callbacks.TimerCallback#

Bases: Callback

Callback that measures the training and testing time of a PyTorch Lightning module.

Examples

>>> from anomalib.callbacks import TimerCallback
>>> from anomalib.engine import Engine
...
>>> callbacks = [TimerCallback()]
>>> engine = Engine(callbacks=callbacks)
on_fit_end(trainer, pl_module)#

Call when fit ends.

Prints the time taken for training.

Parameters:
  • trainer (Trainer) – PyTorch Lightning trainer.

  • pl_module (LightningModule) – Current training module.

Return type:

None

Returns:

None

on_fit_start(trainer, pl_module)#

Call when fit begins.

Sets the start time to the time training started.

Parameters:
  • trainer (Trainer) – PyTorch Lightning trainer.

  • pl_module (LightningModule) – Current training module.

Return type:

None

Returns:

None

on_test_end(trainer, pl_module)#

Call when the test ends.

Prints the time taken for testing and the throughput in frames per second.

Parameters:
  • trainer (Trainer) – PyTorch Lightning trainer.

  • pl_module (LightningModule) – Current training module.

Return type:

None

Returns:

None

on_test_start(trainer, pl_module)#

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.

Parameters:
  • trainer (Trainer) – PyTorch Lightning trainer.

  • pl_module (LightningModule) – Current training module.

Return type:

None

Returns:

None