Source code for anomalib.utils.callbacks.cdf_normalization
"""Anomaly Score Normalization Callback."""# Copyright (C) 2020 Intel Corporation## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions# and limitations under the License.importloggingfromtypingimportAny,Dict,Optionalimportpytorch_lightningasplfrompytorch_lightningimportCallback,Trainerfrompytorch_lightning.utilities.cliimportCALLBACK_REGISTRYfrompytorch_lightning.utilities.typesimportSTEP_OUTPUTfromtorch.distributionsimportLogNormalfromanomalib.modelsimportget_modelfromanomalib.models.componentsimportAnomalyModulefromanomalib.post_processing.normalization.cdfimportnormalize,standardize
[docs]classCdfNormalizationCallback(Callback):"""Callback that standardizes the image-level and pixel-level anomaly scores."""def__init__(self):self.image_dist:Optional[LogNormal]=Noneself.pixel_dist:Optional[LogNormal]=None
[docs]defon_test_start(self,_trainer:pl.Trainer,pl_module:AnomalyModule)->None:"""Called when the test begins."""ifpl_module.image_metricsisnotNone:pl_module.image_metrics.set_threshold(0.5)ifpl_module.pixel_metricsisnotNone:pl_module.pixel_metrics.set_threshold(0.5)
[docs]defon_validation_epoch_start(self,trainer:"pl.Trainer",pl_module: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. """logger.info("Collecting the statistics of the normal training data to normalize the scores.")self._collect_stats(trainer,pl_module)
[docs]defon_validation_batch_end(self,_trainer:pl.Trainer,pl_module:AnomalyModule,outputs:Optional[STEP_OUTPUT],_batch:Any,_batch_idx:int,_dataloader_idx:int,)->None:"""Called when the validation batch ends, standardizes the predicted scores and anomaly maps."""self._standardize_batch(outputs,pl_module)
[docs]defon_test_batch_end(self,_trainer:pl.Trainer,pl_module:AnomalyModule,outputs:Optional[STEP_OUTPUT],_batch:Any,_batch_idx:int,_dataloader_idx:int,)->None:"""Called when the test batch ends, normalizes the predicted scores and anomaly maps."""self._standardize_batch(outputs,pl_module)self._normalize_batch(outputs,pl_module)
[docs]defon_predict_batch_end(self,_trainer:pl.Trainer,pl_module: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."""self._standardize_batch(outputs,pl_module)self._normalize_batch(outputs,pl_module)outputs["pred_labels"]=outputs["pred_scores"]>=0.5
[docs]def_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. """predictions=Trainer(gpus=trainer.gpus).predict(model=self._create_inference_model(pl_module),dataloaders=trainer.datamodule.train_dataloader())pl_module.training_distribution.reset()forbatchinpredictions:if"pred_scores"inbatch.keys():pl_module.training_distribution.update(anomaly_scores=batch["pred_scores"])if"anomaly_maps"inbatch.keys():pl_module.training_distribution.update(anomaly_maps=batch["anomaly_maps"])pl_module.training_distribution.compute()
@staticmethod
[docs]def_create_inference_model(pl_module):"""Create a duplicate of the PL module that can be used to perform inference on the training set."""new_model=get_model(pl_module.hparams)new_model.load_state_dict(pl_module.state_dict())returnnew_model