Source code for anomalib.utils.callbacks.visualizer.visualizer_base
"""Base Visualizer 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.frompathlibimportPathfromtypingimportUnion,castimportnumpyasnpimportpytorch_lightningasplfrompytorch_lightningimportCallbackfromanomalib.models.componentsimportAnomalyModulefromanomalib.post_processingimportVisualizerfromanomalib.utils.loggersimportAnomalibWandbLoggerfromanomalib.utils.loggers.baseimportImageLoggerBase
[docs]classBaseVisualizerCallback(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. """def__init__(self,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,):"""Visualizer callback."""ifmodenotin["full","simple"]:raiseValueError(f"Unknown visualization mode: {mode}. Please choose one of ['full', 'simple']")self.mode=modeiftasknotin["classification","segmentation"]:raiseValueError(f"Unknown task type: {mode}. Please choose one of ['classification', 'segmentation']")self.task=taskself.inputs_are_normalized=inputs_are_normalizedself.show_images=show_imagesself.log_images=log_imagesself.save_images=save_imagesself.image_save_path=Path(image_save_path)self.visualizer=Visualizer(mode,task)
[docs]def_add_to_logger(self,image:np.ndarray,module:AnomalyModule,trainer:pl.Trainer,filename:Union[Path,str],):"""Log image from a visualizer to each of the available loggers in the project. Args: 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. """# Store names of logger and the logger in a dictavailable_loggers={type(logger).__name__.lower().rstrip("logger").lstrip("anomalib"):loggerforloggerintrainer.loggers}# save image to respective loggerifself.log_images:forlog_toinavailable_loggers:# check if logger object is same as the requested objectifisinstance(available_loggers[log_to],ImageLoggerBase):logger:ImageLoggerBase=cast(ImageLoggerBase,available_loggers[log_to])# placate mypyifisinstance(filename,Path):_name=filename.parent.name+"_"+filename.nameelifisinstance(filename,str):_name=filenamelogger.add_image(image=image,name=_name,global_step=module.global_step,
)
[docs]defon_test_end(self,trainer:pl.Trainer,pl_module: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. Args: trainer (pl.Trainer): Pytorch Lightning trainer pl_module (AnomalyModule): Anomaly module (unused) """forloggerintrainer.loggers:ifisinstance(logger,AnomalibWandbLogger):logger.save()