Callbacks¶
Callbacks for Anomalib models.
- class anomalib.utils.callbacks.CdfNormalizationCallback[source]¶
Bases:
Callback
Callback that standardizes the image-level and pixel-level anomaly scores.
- on_predict_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: dict, batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the predict batch ends, normalizes the predicted scores and anomaly maps.
- on_test_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Optional[Union[Tensor, Dict[str, Any]]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the test batch ends, normalizes the predicted scores and anomaly maps.
- on_test_start(trainer: Trainer, pl_module: AnomalyModule) None [source]¶
Called when the test begins.
- on_validation_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Optional[Union[Tensor, Dict[str, Any]]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the validation batch ends, standardizes the predicted scores and anomaly maps.
- on_validation_epoch_start(trainer: Trainer, pl_module: AnomalyModule) None [source]¶
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.
- setup(trainer: Trainer, pl_module: AnomalyModule, stage: Optional[str] = None) None [source]¶
Adds training_distribution metrics to normalization metrics.
- class anomalib.utils.callbacks.GraphLogger[source]¶
Bases:
Callback
Log model graph to respective logger.
- class anomalib.utils.callbacks.ImageVisualizerCallback(task: TaskType, mode: VisualizationMode, image_save_path: str, inputs_are_normalized: bool = True, show_images: bool = False, log_images: bool = True, save_images: bool = True)[source]¶
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.
Visualizer callback.
- on_predict_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Optional[Union[Tensor, Dict[str, Any]]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
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).
- on_test_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Optional[Union[Tensor, Dict[str, Any]]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
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).
- class anomalib.utils.callbacks.LoadModelCallback(weights_path)[source]¶
Bases:
Callback
Callback that loads the model weights from the state dict.
- setup(trainer: Trainer, pl_module: AnomalyModule, stage: Optional[str] = None) None [source]¶
Call when inference begins.
Loads the model weights from
weights_path
into the PyTorch module.
- class anomalib.utils.callbacks.MetricVisualizerCallback(task: TaskType, mode: VisualizationMode, image_save_path: str, inputs_are_normalized: bool = True, show_images: bool = False, log_images: bool = True, save_images: bool = True)[source]¶
Bases:
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.
Visualizer callback.
- on_test_end(trainer: Trainer, pl_module: AnomalyModule) None [source]¶
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.
- class anomalib.utils.callbacks.MetricsConfigurationCallback(task: TaskType = TaskType.SEGMENTATION, image_metrics: Optional[list[str]] = None, pixel_metrics: Optional[list[str]] = None)[source]¶
Bases:
Callback
Metrics Configuration Callback.
Create image and pixel-level AnomalibMetricsCollection.
- This callback creates AnomalibMetricsCollection based on the
list of strings provided for image and pixel-level metrics.
After these MetricCollections are created, the callback assigns these to the lightning module.
- Parameters:
task (TaskType) – Task type of the current run.
image_metrics (list[str] | None) – List of image-level metrics.
pixel_metrics (list[str] | None) – List of pixel-level metrics.
- setup(trainer: Trainer, pl_module: AnomalyModule, stage: Optional[str] = None) None [source]¶
Setup image and pixel-level AnomalibMetricsCollection within Anomalib Model.
- Parameters:
trainer (pl.Trainer) – PyTorch Lightning Trainer
pl_module (AnomalyModule) – Anomalib Model that inherits pl LightningModule.
stage (str | None, optional) – fit, validate, test or predict. Defaults to None.
- class anomalib.utils.callbacks.MinMaxNormalizationCallback[source]¶
Bases:
Callback
Callback that normalizes the image-level and pixel-level anomaly scores using min-max normalization.
- on_predict_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Any, batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the predict batch ends, normalizes the predicted scores and anomaly maps.
- on_test_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Optional[Union[Tensor, Dict[str, Any]]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the test batch ends, normalizes the predicted scores and anomaly maps.
- on_test_start(trainer: Trainer, pl_module: AnomalyModule) None [source]¶
Called when the test begins.
- on_validation_batch_end(trainer: Trainer, pl_module: AnomalyModule, outputs: Union[Tensor, Dict[str, Any]], batch: Any, batch_idx: int, dataloader_idx: int) None [source]¶
Called when the validation batch ends, update the min and max observed values.
- setup(trainer: Trainer, pl_module: AnomalyModule, stage: Optional[str] = None) None [source]¶
Adds min_max metrics to normalization metrics.
- class anomalib.utils.callbacks.PostProcessingConfigurationCallback(normalization_method: NormalizationMethod = NormalizationMethod.MIN_MAX, threshold_method: ThresholdMethod = ThresholdMethod.ADAPTIVE, manual_image_threshold: Optional[float] = None, manual_pixel_threshold: Optional[float] = None)[source]¶
Bases:
Callback
Post-Processing Configuration Callback.
- Parameters:
normalization_method (NormalizationMethod) – Normalization method. <none, min_max, cdf>
threshold_method (ThresholdMethod) – Flag indicating whether threshold should be manual or adaptive.
manual_image_threshold (float | None) – Default manual image threshold value.
manual_pixel_threshold (float | None) – Default manual pixel threshold value.
- setup(trainer: Trainer, pl_module: LightningModule, stage: Optional[str] = None) None [source]¶
Setup post-processing configuration within Anomalib Model.
- Parameters:
trainer (Trainer) – PyTorch Lightning Trainer
pl_module (LightningModule) – Anomalib Model that inherits pl LightningModule.
stage (str | None, optional) – fit, validate, test or predict. Defaults to None.
- class anomalib.utils.callbacks.TilerConfigurationCallback(enable: bool = False, tile_size: Union[int, Sequence] = 256, stride: Optional[Union[int, Sequence]] = None, remove_border_count: int = 0, mode: ImageUpscaleMode = ImageUpscaleMode.PADDING)[source]¶
Bases:
Callback
Tiler Configuration Callback.
Sets tiling configuration from the command line.
- Parameters:
enable (bool) – Boolean to enable tiling operation. Defaults to False.
tile_size ([int | Sequence]) – Tile size. Defaults to 256.
stride ([int | Sequence]) – Stride to move tiles on the image.
remove_border_count (int, optional) – Number of pixels to remove from the image before tiling. Defaults to 0.
mode (str, optional) – Up-scaling mode when untiling overlapping tiles. Defaults to “padding”.
tile_count (SupportsIndex, optional) – Number of random tiles to sample from the image. Defaults to 4.
- setup(trainer: Trainer, pl_module: LightningModule, stage: Optional[str] = None) None [source]¶
Setup Tiler object within Anomalib Model.
- Parameters:
trainer (pl.Trainer) – PyTorch Lightning Trainer
pl_module (pl.LightningModule) – Anomalib Model that inherits pl LightningModule.
stage (str | None, optional) – fit, validate, test or predict. Defaults to None.
- Raises:
ValueError – When Anomalib Model doesn’t contain
Tiler
object, it means the model doesn not support tiling operation.
- class anomalib.utils.callbacks.TimerCallback[source]¶
Bases:
Callback
Callback that measures the training and testing time of a PyTorch Lightning module.
- on_fit_end(trainer: Trainer, pl_module: LightningModule) None [source]¶
Call when fit ends.
Prints the time taken for training.
- Parameters:
trainer (Trainer) – PyTorch Lightning trainer.
pl_module (LightningModule) – Current training module.
- Returns:
None
- on_fit_start(trainer: Trainer, pl_module: LightningModule) None [source]¶
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.
- Returns:
None
- on_test_end(trainer: Trainer, pl_module: LightningModule) None [source]¶
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.
- Returns:
None
- on_test_start(trainer: Trainer, pl_module: LightningModule) None [source]¶
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.
- Returns:
None