Loggers#
Monitor your experiments with Comet’s comprehensive ML platform.
Track and visualize your ML experiments with Weights & Biases.
Visualize your training metrics with TensorBoard.
Track and manage your ML lifecycle with MLflow.
Comet Logger#
Comet logger with image logging capabilities.
This module provides a Comet logger implementation that adds an interface for logging images. It extends both the base image logger and PyTorch Lightning’s Comet logger.
Example
>>> from anomalib.loggers import AnomalibCometLogger
>>> from anomalib.engine import Engine
>>> comet_logger = AnomalibCometLogger()
>>> engine = Engine(logger=comet_logger)
Log an image: >>> import numpy as np >>> image = np.random.rand(32, 32, 3) # doctest: +SKIP >>> comet_logger.add_image( … image=image, … name=”test_image”, … global_step=0 … ) # doctest: +SKIP
- class anomalib.loggers.comet.AnomalibCometLogger(api_key=None, save_dir=None, project_name=None, rest_api_key=None, experiment_name=None, experiment_key=None, offline=False, prefix='', **kwargs)#
Bases:
ImageLoggerBase,CometLoggerLogger for Comet ML with image logging capabilities.
This logger extends PyTorch Lightning’s CometLogger with an interface for logging images. It inherits from both
ImageLoggerBaseandCometLogger.- Parameters:
api_key (
str|None) – API key found on Comet.ml. If not provided, will be loaded fromCOMET_API_KEYenvironment variable or~/.comet.config. Required for online mode. Defaults toNone.save_dir (
str|None) – Directory path to save local comet logs. Required for offline mode. Also sets checkpoint directory if provided. Defaults toNone.project_name (
str|None) – Project name for the experiment. Creates new project if doesn’t exist. Defaults toNone.rest_api_key (
str|None) – Rest API key from Comet.ml settings. Used for version tracking. Defaults toNone.experiment_name (
str|None) – Name for this experiment on Comet.ml. Defaults toNone.experiment_key (
str|None) – Key to restore existing experiment. Defaults toNone.offline (
bool) – Force offline mode even with API key. Useful when usingsave_dirfor checkpoints with~/.comet.config. Defaults toFalse.prefix (
str) – String to prepend to metric keys. Defaults to"".**kwargs – Additional arguments passed to
CometExperiment(e.g.workspace,log_code).
- Raises:
ModuleNotFoundError – If
comet-mlpackage is not installed.MisconfigurationException – If neither
api_keynorsave_dirprovided.
Example
>>> from anomalib.loggers import AnomalibCometLogger >>> comet_logger = AnomalibCometLogger( ... project_name="anomaly_detection" ... )
Note
For more details, see the Comet Documentation
- add_image(image, name=None, **kwargs)#
Log an image to Comet.
- Parameters:
image (
ndarray|Figure) – Image to log, either numpy array or matplotlib figure.name (
str|None) – Name/tag for the image. Defaults toNone.**kwargs – Must contain
global_step(int) indicating the step at which to log the image.
- Raises:
ValueError – If
global_stepnot provided in kwargs.- Return type:
None
Example
>>> import numpy as np >>> from matplotlib.figure import Figure >>> logger = AnomalibCometLogger() >>> # Log numpy array >>> image_array = np.random.rand(32, 32, 3) >>> logger.add_image( ... image=image_array, ... name="test_image", ... global_step=0 ... ) >>> # Log matplotlib figure >>> fig = Figure() >>> logger.add_image( ... image=fig, ... name="test_figure", ... global_step=1 ... )
- class anomalib.loggers.comet.CometLogger(*args, **kwargs)#
Bases:
objectDummy CometLogger class for when comet-ml is not installed.
Wandb Logger#
Weights & Biases logger with image logging capabilities.
This module provides a Weights & Biases logger implementation that adds an interface for logging images. It extends both the base image logger and PyTorch Lightning’s WandbLogger.
Example
>>> from anomalib.loggers import AnomalibWandbLogger
>>> from anomalib.engine import Engine
>>> wandb_logger = AnomalibWandbLogger()
>>> engine = Engine(logger=wandb_logger)
Log an image: >>> import numpy as np >>> image = np.random.rand(32, 32, 3) # doctest: +SKIP >>> wandb_logger.add_image( … image=image, … name=”test_image” … ) # doctest: +SKIP
- class anomalib.loggers.wandb.AnomalibWandbLogger(name=None, save_dir='.', version=None, offline=False, dir=None, id=None, anonymous=None, project=None, log_model=False, experiment=None, prefix='', checkpoint_name=None, **kwargs)#
Bases:
ImageLoggerBase,WandbLoggerLogger for Weights & Biases with image logging capabilities.
This logger extends PyTorch Lightning’s WandbLogger with an interface for logging images. It inherits from both
ImageLoggerBaseandWandbLogger.- Parameters:
name – Display name for the run. Defaults to
None.save_dir – Path where data is saved (wandb dir by default). Defaults to
".".version – Sets the version, mainly used to resume a previous run. Defaults to
None.offline – Run offline (data can be streamed later to wandb servers). Defaults to
False.dir – Alias for
save_dir. Defaults toNone.id – Sets the version, mainly used to resume a previous run. Defaults to
None.anonymous – Enables or explicitly disables anonymous logging. Defaults to
None.project – The name of the project to which this run will belong. Defaults to
None.log_model – Save checkpoints in wandb dir to upload on W&B servers. Defaults to
False.experiment – WandB experiment object. Automatically set when creating a run. Defaults to
None.prefix – A string to put at the beginning of metric keys. Defaults to
"".checkpoint_name – Name of the checkpoint to save. Defaults to
None.**kwargs – Additional arguments passed to
wandb.init()likeentity,group,tags, etc.
- Raises:
ImportError – If required WandB package is not installed.
MisconfigurationException – If both
log_modelandofflineare set toTrue.
Example
>>> from anomalib.loggers import AnomalibWandbLogger >>> from anomalib.engine import Engine >>> wandb_logger = AnomalibWandbLogger( ... project="my_project", ... name="my_run" ... ) >>> engine = Engine(logger=wandb_logger)
Note
When logging manually through
wandb.logortrainer.logger.experiment.log, make sure to usecommit=Falseso the logging step does not increase.See also
- add_image(image, name=None, **kwargs)#
Log an image to Weights & Biases.
- Parameters:
image (
ndarray|Figure) – Image to log, can be either a numpy array or matplotlib Figure.name (
str|None) – Name/title of the image. Defaults toNone.**kwargs – Additional keyword arguments passed to
wandb.Image. Currently unused.
- Return type:
None
- save()#
Upload images to Weights & Biases server.
Note
There is a limit on the number of images that can be logged together to the W&B server.
- Return type:
None
- class anomalib.loggers.wandb.WandbLogger(*args, **kwargs)#
Bases:
objectDummy WandbLogger class for when wandb is not installed.
Tensorboard Logger#
TensorBoard logger with image logging capabilities.
This module provides a TensorBoard logger implementation that adds an interface for logging images. It extends both the base image logger and PyTorch Lightning’s TensorBoard logger.
Example
>>> from anomalib.loggers import AnomalibTensorBoardLogger
>>> from anomalib.engine import Engine
>>> tensorboard_logger = AnomalibTensorBoardLogger("logs")
>>> engine = Engine(logger=tensorboard_logger)
Log an image: >>> import numpy as np >>> image = np.random.rand(32, 32, 3) # doctest: +SKIP >>> tensorboard_logger.add_image( … image=image, … name=”test_image”, … global_step=0 … ) # doctest: +SKIP
- class anomalib.loggers.tensorboard.AnomalibTensorBoardLogger(save_dir, name='default', version=None, log_graph=False, default_hp_metric=True, prefix='', **kwargs)#
Bases:
ImageLoggerBase,TensorBoardLoggerLogger for TensorBoard with image logging capabilities.
This logger extends PyTorch Lightning’s TensorBoardLogger with an interface for logging images. It inherits from both
ImageLoggerBaseandTensorBoardLogger.- Parameters:
save_dir (
str) – Directory path where logs will be saved. The final path will beos.path.join(save_dir, name, version).name (
str|None) – Name of the experiment. If it is an empty string, no per-experiment subdirectory is used. Defaults to"default".version (
int|str|None) – Version of the experiment. If not specified, the logger checks the save directory for existing versions and assigns the next available one. If a string is provided, it is used as the run-specific subdirectory name. Otherwise"version_${version}"is used. Defaults toNone.log_graph (
bool) – IfTrue, adds the computational graph to TensorBoard. This requires that the model has defined theexample_input_arrayattribute. Defaults toFalse.default_hp_metric (
bool) – IfTrue, enables a placeholder metric with keyhp_metricwhenlog_hyperparamsis called without a metric. Defaults toTrue.prefix (
str) – String to prepend to metric keys. Defaults to"".**kwargs – Additional arguments like
comment,filename_suffix, etc. used bySummaryWriter.
Example
>>> from anomalib.loggers import AnomalibTensorBoardLogger >>> from anomalib.engine import Engine >>> logger = AnomalibTensorBoardLogger( ... save_dir="logs", ... name="my_experiment" ... ) >>> engine = Engine(logger=logger)
See also
- add_image(image, name=None, **kwargs)#
Log images to TensorBoard.
- Parameters:
image (
ndarray|Figure) – Image to log, can be either a numpy array or matplotlib Figure.name (
str|None) – Name/title of the image. Defaults toNone.**kwargs – Must contain
global_step(int) indicating the step at which to log the image. Additional keyword arguments are passed to the TensorBoard logging method.
- Raises:
ValueError – If
global_stepis not provided inkwargs.- Return type:
None
- class anomalib.loggers.tensorboard.TensorBoardLogger(*args, **kwargs)#
Bases:
objectDummy TensorBoardLogger class for when tensorboard is not installed.
MLFlow Logger#
MLFlow logger with image logging capabilities.
This module provides an MLFlow logger implementation that adds an interface for logging images. It extends both the base image logger and PyTorch Lightning’s MLFlow logger.
Example
>>> from anomalib.loggers import AnomalibMLFlowLogger
>>> from anomalib.engine import Engine
>>> mlflow_logger = AnomalibMLFlowLogger()
>>> engine = Engine(logger=mlflow_logger)
Log an image: >>> import numpy as np >>> image = np.random.rand(32, 32, 3) # doctest: +SKIP >>> mlflow_logger.add_image( … image=image, … name=”test_image” … ) # doctest: +SKIP
- class anomalib.loggers.mlflow.AnomalibMLFlowLogger(experiment_name='anomalib_logs', run_name=None, tracking_uri=None, save_dir='./mlruns', log_model=False, prefix='', **kwargs)#
Bases:
ImageLoggerBase,MLFlowLoggerLogger for MLFlow with image logging capabilities.
This logger extends PyTorch Lightning’s MLFlowLogger with an interface for logging images. It inherits from both
ImageLoggerBaseandMLFlowLogger.- Parameters:
experiment_name (
str|None) – Name of the experiment. If not provided, defaults to"anomalib_logs".run_name (
str|None) – Name of the new run. Therun_nameis internally stored as amlflow.runNametag. If themlflow.runNametag has already been set intags, the value is overridden by therun_name.tracking_uri (
str|None) – Address of local or remote tracking server. If not provided, defaults toMLFLOW_TRACKING_URIenvironment variable if set, otherwise falls back tofile:<save_dir>.save_dir (
str|None) – Path to local directory where MLflow runs are saved. Defaults to"./mlruns"iftracking_uriis not provided. Has no effect iftracking_uriis provided.log_model (
Union[Literal['all'],bool,None]) –Log checkpoints created by
ModelCheckpointas MLFlow artifacts:if
"all": checkpoints are logged during trainingif
True: checkpoints are logged at end of training (except whensave_top_k == -1which logs every checkpoint during training)if
False(default): no checkpoints are logged
prefix (
str|None) – String to prepend to metric keys. Defaults to"".**kwargs – Additional arguments like
tags,artifact_locationetc. used byMLFlowExperiment.
Example
>>> from anomalib.loggers import AnomalibMLFlowLogger >>> from anomalib.engine import Engine >>> mlflow_logger = AnomalibMLFlowLogger( ... experiment_name="my_experiment", ... run_name="my_run" ... ) >>> engine = Engine(logger=mlflow_logger)
See also
- add_image(image, name=None, **kwargs)#
Log images to MLflow.
- Parameters:
image (
ndarray|Figure) – Image to log, can be either a numpy array or matplotlib Figure.name (
str|None) – Name/title of the image. Defaults toNone.**kwargs – Additional keyword arguments passed to the MLflow logging method when
imageis a Figure. Has no effect whenimageis a numpy array.
- Return type:
None
- class anomalib.loggers.mlflow.MLFlowLogger(*args, **kwargs)#
Bases:
objectDummy MLFlowLogger class for when mlflow is not installed.