Skip to content

Commit 35b360c

Browse files
Restored metric logging to third-party loggers
Signed-off-by: AdityaSinghDevs <adityasingh.devs@gmail.com>
1 parent 9559c62 commit 35b360c

File tree

1 file changed

+57
-0
lines changed
  • src/anomalib/pipelines/benchmark

1 file changed

+57
-0
lines changed

src/anomalib/pipelines/benchmark/job.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323

2424
logger = logging.getLogger(__name__)
2525

26+
# Import external loggers
27+
try:
28+
from anomalib.loggers import (
29+
AnomalibCometLogger,
30+
AnomalibMLFlowLogger,
31+
AnomalibTensorBoardLogger,
32+
AnomalibWandbLogger,
33+
)
34+
35+
LOGGERS_AVAILABLE = True
36+
logger.info("Successfully imported logger modules.")
37+
except ImportError:
38+
LOGGERS_AVAILABLE = False
39+
logger.warning("To use external loggers, install required packages using `anomalib install -v`")
40+
2641

2742
class BenchmarkJob(Job):
2843
"""Benchmarking job.
@@ -69,6 +84,7 @@ def run(
6984
accelerator=self.accelerator,
7085
devices=devices,
7186
default_root_dir=temp_dir,
87+
logger=self._initialize_loggers(self.flat_cfg or {}) if LOGGERS_AVAILABLE else None,
7288
)
7389
fit_start_time = time.time()
7490
engine.fit(self.model, self.datamodule)
@@ -89,8 +105,49 @@ def run(
89105
**test_results[0],
90106
}
91107
logger.info(f"Completed with result {output}")
108+
109+
# Logging metrics to External Loggers
110+
trainer = engine.trainer()
111+
for logger_instance in trainer.loggers:
112+
if isinstance(logger_instance, AnomalibCometLogger | AnomalibWandbLogger | AnomalibMLFlowLogger):
113+
logger_instance.log_metrics(test_results[0])
114+
logger.debug(f"Successfully logged metrics to {logger_instance.__class__.__name__}")
92115
return output
93116

117+
@staticmethod
118+
def _initialize_loggers(logger_configs: dict[str, dict[str, Any]]) -> list[Any]:
119+
"""Initialize configured external loggers.
120+
121+
Args:
122+
logger_configs: Dictionary mapping logger names to their configurations.
123+
124+
Returns:
125+
Dictionary of initialized loggers.
126+
"""
127+
logger_mapping = {
128+
"tensorboard": AnomalibTensorBoardLogger,
129+
"comet": AnomalibCometLogger,
130+
"wandb": AnomalibWandbLogger,
131+
"mlflow": AnomalibMLFlowLogger,
132+
}
133+
134+
active_loggers = []
135+
default_configs = {
136+
"tensorboard": {"save_dir": "logs/benchmarks"},
137+
"comet": {"project_name": "anomalib"},
138+
"wandb": {"project": "anomalib"},
139+
"mlflow": {"experiment_name": "anomalib"},
140+
}
141+
142+
for logger_name, logger_class in logger_mapping.items():
143+
# Use provided config or fall back to defaults
144+
config = logger_configs.get(logger_name, default_configs.get(logger_name, {}))
145+
logger_instance = logger_class(**config)
146+
active_loggers.append(logger_instance)
147+
logger.info(f"Successfully initialized {logger_name} logger")
148+
149+
return active_loggers
150+
94151
@staticmethod
95152
def collect(results: list[dict[str, Any]]) -> pd.DataFrame:
96153
"""Gather the results returned from run."""

0 commit comments

Comments
 (0)