anomalib.utils.callbacks¶
Callbacks for Anomalib models.
Subpackages¶
Submodules¶
anomalib.utils.callbacks.cdf_normalizationanomalib.utils.callbacks.graphanomalib.utils.callbacks.metrics_configurationanomalib.utils.callbacks.min_max_normalizationanomalib.utils.callbacks.model_loaderanomalib.utils.callbacks.openvinoanomalib.utils.callbacks.tiler_configurationanomalib.utils.callbacks.timeranomalib.utils.callbacks.visualizer_callback
Package Contents¶
Classes¶
Callback that standardizes the image-level and pixel-level anomaly scores. |
|
Metrics Configuration Callback. |
|
Callback that normalizes the image-level and pixel-level anomaly scores using min-max normalization. |
|
Callback that loads the model weights from the state dict. |
|
Tiler Configuration Callback. |
|
Callback that measures the training and testing time of a PyTorch Lightning module. |
|
Callback that visualizes the inference results of a model. |
- class anomalib.utils.callbacks.CdfNormalizationCallback[source]¶
Bases:
pytorch_lightning.CallbackCallback that standardizes the image-level and pixel-level anomaly scores.
- on_test_start(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) None¶
Called when the test begins.
- 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.
- 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.
- 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.
- 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.
- _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.
- static _create_inference_model(pl_module)¶
Create a duplicate of the PL module that can be used to perform inference on the training set.
- static _standardize_batch(outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, pl_module) None¶
- static _normalize_batch(outputs: pytorch_lightning.utilities.types.STEP_OUTPUT, pl_module: anomalib.models.components.AnomalyModule) None¶
- class anomalib.utils.callbacks.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')[source]¶
Bases:
pytorch_lightning.callbacks.CallbackMetrics Configuration Callback.
- 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.
- Parameters
_trainer (pl.Trainer) – PyTorch Lightning Trainer
pl_module (pl.LightningModule) – Anomalib Model that inherits pl LightningModule.
stage (Optional[str], optional) – fit, validate, test or predict. Defaults to None.
- class anomalib.utils.callbacks.MinMaxNormalizationCallback[source]¶
Bases:
pytorch_lightning.CallbackCallback that normalizes the image-level and pixel-level anomaly scores using min-max normalization.
- on_test_start(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) None¶
Called when the test begins.
- 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.
- 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.
- 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.
- static _normalize_batch(outputs, pl_module)¶
Normalize a batch of predictions.
- class anomalib.utils.callbacks.LoadModelCallback(weights_path)[source]¶
Bases:
pytorch_lightning.CallbackCallback that loads the model weights from the state dict.
- on_test_start(self, _trainer, pl_module: anomalib.models.components.AnomalyModule) None¶
Call when the test begins.
Loads the model weights from
weights_pathinto the PyTorch module.
- on_predict_start(self, _trainer, pl_module: anomalib.models.components.AnomalyModule) None¶
Call when inference begins.
Loads the model weights from
weights_pathinto the PyTorch module.
- 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: str = 'padding', tile_count: int = 4)[source]¶
Bases:
pytorch_lightning.callbacks.CallbackTiler Configuration Callback.
- setup(self, _trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule, stage: Optional[str] = None) None¶
Setup Tiler object within Anomalib Model.
- Parameters
_trainer (pl.Trainer) – PyTorch Lightning Trainer
pl_module (pl.LightningModule) – Anomalib Model that inherits pl LightningModule.
stage (Optional[str], optional) – fit, validate, test or predict. Defaults to None.
- Raises
ValueError – When Anomalib Model doesn’t contain
Tilerobject, it means the model doesn not support tiling operation.
- class anomalib.utils.callbacks.TimerCallback[source]¶
Bases:
pytorch_lightning.CallbackCallback that measures the training and testing time of a PyTorch Lightning module.
- 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.
- Parameters
trainer (Trainer) – PyTorch Lightning trainer.
pl_module (LightningModule) – Current training module.
- Returns
None
- on_fit_end(self, trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) None¶
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_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.
- Parameters
trainer (Trainer) – PyTorch Lightning trainer.
pl_module (LightningModule) – Current training module.
- Returns
None
- 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.
- Parameters
trainer (Trainer) – PyTorch Lightning trainer.
pl_module (LightningModule) – Current training module.
- Returns
None
- class anomalib.utils.callbacks.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)[source]¶
Bases:
pytorch_lightning.CallbackCallback 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.
- _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.
- 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_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.
- 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(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.
- 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_end(self, _trainer: pytorch_lightning.Trainer, pl_module: anomalib.models.components.AnomalyModule) None¶
Sync logs.
Currently only
AnomalibWandbLoggeris 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 (unused)
pl_module (AnomalyModule) – Anomaly module