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
,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
andCometLogger
.- Parameters:
api_key (
str
|None
) – API key found on Comet.ml. If not provided, will be loaded fromCOMET_API_KEY
environment 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_dir
for 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-ml
package is not installed.MisconfigurationException – If neither
api_key
norsave_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:
- Raises:
ValueError – If
global_step
not provided in kwargs.- Return type:
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
andWandbLogger
.- Parameters:
name (
str
|None
) – Display name for the run. Defaults toNone
.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 toNone
.offline (
bool
) – Run offline (data can be streamed later to wandb servers). Defaults toFalse
.dir (
Union
[str
,Path
,None
]) – Alias forsave_dir
. Defaults toNone
.id (
str
|None
) – Sets the version, mainly used to resume a previous run. Defaults toNone
.anonymous (
bool
|None
) – Enables or explicitly disables anonymous logging. Defaults toNone
.project (
str
|None
) – The name of the project to which this run will belong. Defaults toNone
.log_model (
Union
[Literal
['all'
],bool
]) – Save checkpoints in wandb dir to upload on W&B servers. Defaults toFalse
.experiment (
Run
|RunDisabled
|None
) – WandB experiment object. Automatically set when creating a run. Defaults toNone
.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 toNone
.**kwargs – Additional arguments passed to
wandb.init()
likeentity
,group
,tags
, etc.
- Raises:
ImportError – If required WandB package is not installed.
MisconfigurationException – If both
log_model
andoffline
are 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.log
ortrainer.logger.experiment.log
, make sure to usecommit=False
so the logging step does not increase.See also
- add_image(image, name=None, **kwargs)#
Log an image to Weights & Biases.
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
andTensorBoardLogger
.- 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_array
attribute. Defaults toFalse
.default_hp_metric (
bool
) – IfTrue
, enables a placeholder metric with keyhp_metric
whenlog_hyperparams
is 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_step
is not provided inkwargs
.- Return type:
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
andMLFlowLogger
.- 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_name
is internally stored as amlflow.runName
tag. If themlflow.runName
tag 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_URI
environment 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_uri
is not provided. Has no effect iftracking_uri
is provided.log_model (
Optional
[Literal
[True
,False
,'all'
]]) –Log checkpoints created by
ModelCheckpoint
as MLFlow artifacts:if
"all"
: checkpoints are logged during trainingif
True
: checkpoints are logged at end of training (except whensave_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 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:
- Return type: