Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed

- Export flat configurations in benchmark CSV results by [mzweilin](https://github.com/mzweilin) in https://github.com/openvinotoolkit/anomalib/pull/2391

### Deprecated

### Fixed
Expand Down
16 changes: 16 additions & 0 deletions src/anomalib/pipelines/benchmark/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
from .job import BenchmarkJob


def flatten_dict(d: dict | object, key: str = "", output: dict | None = None) -> dict:
"""Flatten a dictionary by dot-connecting the hierarchical keys."""
# B006 Do not use mutable data structures for argument defaults.
if output is None:
output = {}
if isinstance(d, dict):
for k, v in d.items():
flatten_dict(v, f"{key}.{k}" if key != "" else k, output)
else:
output[key] = d
return output


class BenchmarkJobGenerator(JobGenerator):
"""Generate BenchmarkJob.

Expand All @@ -39,9 +52,12 @@ def generate_jobs(
"""Return iterator based on the arguments."""
del previous_stage_result # Not needed for this job
for _container in get_iterator_from_grid_dict(args):
# Pass experimental configs as a flatten dictionary to the job runner.
flat_cfg = flatten_dict(_container)
yield BenchmarkJob(
accelerator=self.accelerator,
seed=_container["seed"],
model=get_model(_container["model"]),
datamodule=get_datamodule(_container["data"]),
flat_cfg=flat_cfg,
)
16 changes: 11 additions & 5 deletions src/anomalib/pipelines/benchmark/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ class BenchmarkJob(Job):
model (AnomalyModule): The model to use.
datamodule (AnomalibDataModule): The data module to use.
seed (int): The seed to use.
flat_cfg (dict): The flat dictionary of configs with dotted keys.
"""

name = "benchmark"

def __init__(self, accelerator: str, model: AnomalyModule, datamodule: AnomalibDataModule, seed: int) -> None:
def __init__(
self,
accelerator: str,
model: AnomalyModule,
datamodule: AnomalibDataModule,
seed: int,
flat_cfg: dict,
) -> None:
super().__init__()
self.accelerator = accelerator
self.model = model
self.datamodule = datamodule
self.seed = seed
self.flat_cfg = flat_cfg

@hide_output
def run(
Expand All @@ -64,11 +73,8 @@ def run(
# TODO(ashwinvaidya17): Restore throughput
# https://github.com/openvinotoolkit/anomalib/issues/2054
output = {
"seed": self.seed,
"accelerator": self.accelerator,
"model": self.model.__class__.__name__,
"data": self.datamodule.__class__.__name__,
"category": self.datamodule.category,
**self.flat_cfg,
**test_results[0],
}
logger.info(f"Completed with result {output}")
Expand Down