Loggers#

Comet Logger

Monitor your experiments with Comet’s comprehensive ML platform.

Comet Logger
Wandb Logger

Track and visualize your ML experiments with Weights & Biases.

Wandb Logger
Tensorboard Logger

Visualize your training metrics with TensorBoard.

Tensorboard Logger
MLFlow Logger

Track and manage your ML lifecycle with MLflow.

MLFlow Logger

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, CometLogger

Logger for Comet ML with image logging capabilities.

This logger extends PyTorch Lightning’s CometLogger with an interface for logging images. It inherits from both ImageLoggerBase and CometLogger.

Parameters:
  • api_key (str | None) – API key found on Comet.ml. If not provided, will be loaded from COMET_API_KEY environment variable or ~/.comet.config. Required for online mode. Defaults to None.

  • save_dir (str | None) – Directory path to save local comet logs. Required for offline mode. Also sets checkpoint directory if provided. Defaults to None.

  • project_name (str | None) – Project name for the experiment. Creates new project if doesn’t exist. Defaults to None.

  • rest_api_key (str | None) – Rest API key from Comet.ml settings. Used for version tracking. Defaults to None.

  • experiment_name (str | None) – Name for this experiment on Comet.ml. Defaults to None.

  • experiment_key (str | None) – Key to restore existing experiment. Defaults to None.

  • offline (bool) – Force offline mode even with API key. Useful when using save_dir for checkpoints with ~/.comet.config. Defaults to False.

  • 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-ml package is not installed.

  • MisconfigurationException – If neither api_key nor save_dir provided.

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 to None.

  • **kwargs – Must contain global_step (int) indicating the step at which to log the image.

Raises:

ValueError – If global_step not 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
... )  

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, WandbLogger

Logger for Weights & Biases with image logging capabilities.

This logger extends PyTorch Lightning’s WandbLogger with an interface for logging images. It inherits from both ImageLoggerBase and WandbLogger.

Parameters:
  • name (str | None) – Display name for the run. Defaults to None.

  • save_dir (Union[str, Path]) – Path where data is saved (wandb dir by default). Defaults to ".".

  • version (str | None) – Sets the version, mainly used to resume a previous run. Defaults to None.

  • offline (bool) – Run offline (data can be streamed later to wandb servers). Defaults to False.

  • dir (Union[str, Path, None]) – Alias for save_dir. Defaults to None.

  • id (str | None) – Sets the version, mainly used to resume a previous run. Defaults to None.

  • anonymous (bool | None) – Enables or explicitly disables anonymous logging. Defaults to None.

  • project (str | None) – The name of the project to which this run will belong. Defaults to None.

  • log_model (Union[Literal['all'], bool]) – Save checkpoints in wandb dir to upload on W&B servers. Defaults to False.

  • experiment (Run | RunDisabled | None) – WandB experiment object. Automatically set when creating a run. Defaults to None.

  • prefix (str) – A string to put at the beginning of metric keys. Defaults to "".

  • checkpoint_name (str | None) – Name of the checkpoint to save. Defaults to None.

  • **kwargs – Additional arguments passed to wandb.init() like entity, group, tags, etc.

Raises:
  • ImportError – If required WandB package is not installed.

  • MisconfigurationException – If both log_model and offline are set to True.

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.log or trainer.logger.experiment.log, make sure to use commit=False so the logging step does not increase.

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 to None.

  • **kwargs – Additional keyword arguments passed to wandb.Image. Currently unused.

Return type:

None

save()#

Upload images to Weights & Biases server. :rtype: None

Note

There is a limit on the number of images that can be logged together to the W&B server.

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, TensorBoardLogger

Logger for TensorBoard with image logging capabilities.

This logger extends PyTorch Lightning’s TensorBoardLogger with an interface for logging images. It inherits from both ImageLoggerBase and TensorBoardLogger.

Parameters:
  • save_dir (str) – Directory path where logs will be saved. The final path will be os.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 to None.

  • log_graph (bool) – If True, adds the computational graph to TensorBoard. This requires that the model has defined the example_input_array attribute. Defaults to False.

  • default_hp_metric (bool) – If True, enables a placeholder metric with key hp_metric when log_hyperparams is called without a metric. Defaults to True.

  • prefix (str) – String to prepend to metric keys. Defaults to "".

  • **kwargs – Additional arguments like comment, filename_suffix, etc. used by SummaryWriter.

Example

>>> from anomalib.loggers import AnomalibTensorBoardLogger
>>> from anomalib.engine import Engine
>>> logger = AnomalibTensorBoardLogger(
...     save_dir="logs",
...     name="my_experiment"
... )  
>>> engine = Engine(logger=logger)  
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 to None.

  • **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_step is not provided in kwargs.

Return type:

None

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, MLFlowLogger

Logger for MLFlow with image logging capabilities.

This logger extends PyTorch Lightning’s MLFlowLogger with an interface for logging images. It inherits from both ImageLoggerBase and MLFlowLogger.

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. The run_name is internally stored as a mlflow.runName tag. If the mlflow.runName tag has already been set in tags, the value is overridden by the run_name.

  • tracking_uri (str | None) – Address of local or remote tracking server. If not provided, defaults to MLFLOW_TRACKING_URI environment variable if set, otherwise falls back to file:<save_dir>.

  • save_dir (str | None) – Path to local directory where MLflow runs are saved. Defaults to "./mlruns" if tracking_uri is not provided. Has no effect if tracking_uri is provided.

  • log_model (Optional[Literal[True, False, 'all']]) –

    Log checkpoints created by ModelCheckpoint as MLFlow artifacts:

    • if "all": checkpoints are logged during training

    • if True: checkpoints are logged at end of training (except when save_top_k == -1 which 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_location etc. used by MLFlowExperiment.

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)  
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 to None.

  • **kwargs – Additional keyword arguments passed to the MLflow logging method when image is a Figure. Has no effect when image is a numpy array.

Return type:

None