From 2f19e8b863c54d16c1eb855bc89391063def15ce Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Sun, 2 Oct 2022 02:55:53 +0200 Subject: Add support for hydra 1.2, add metadata file --- training/artifacts.py | 16 ++++++++-------- training/main.py | 3 ++- training/metadata.py | 6 ++++++ 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 training/metadata.py diff --git a/training/artifacts.py b/training/artifacts.py index a059833..2cb8bda 100644 --- a/training/artifacts.py +++ b/training/artifacts.py @@ -3,23 +3,20 @@ from datetime import datetime from pathlib import Path import shutil import sys -from typing import Optional, Union +from typing import Optional import click from loguru import logger as log +from training import metadata import wandb from wandb.apis.public import Run -TRAINING_DIR = Path(__file__).parents[0].resolve() -ARTIFACTS_DIR = TRAINING_DIR.parent / "text_recognizer" / "artifacts" -RUNS_DIR = TRAINING_DIR / "logs" / "runs" - def _get_run_dir(run: Run) -> Optional[Path]: created_at = datetime.fromisoformat(run.created_at).astimezone() date = created_at.date() hour = (created_at + created_at.utcoffset()).hour - runs = list((RUNS_DIR / f"{date}").glob(f"{hour}-*")) + runs = list((metadata.RUNS_DIR / f"{date}").glob(f"{hour}-*")) if not runs: return None return runs[0] @@ -45,7 +42,7 @@ def _copy_checkpoint(checkpoint: Path, dst_dir: Path) -> None: def save_model(run: Run, tag: str) -> None: """Save model to artifacts.""" - dst_dir = ARTIFACTS_DIR / f"{tag}_text_recognizer" + dst_dir = metadata.ARTIFACTS_DIR / f"{tag}_text_recognizer" dst_dir.mkdir(parents=True, exist_ok=True) run_dir = _get_run_dir(run) if not run_dir: @@ -77,7 +74,10 @@ def find_best_run(entity: str, project: str, tag: str, metric: str, mode: str) - best_run = runs[0] summary = best_run.summary log.info( - f"Best run is ({best_run.name}, {best_run.id}) picked from {len(runs)} runs with the following metric" + ( + f"Best run is ({best_run.name}, {best_run.id}) picked from {len(runs)} " + "runs with the following metric" + ) ) log.info( f"{metric}: {summary[metric]}" diff --git a/training/main.py b/training/main.py index 26d5aeb..6e54a23 100644 --- a/training/main.py +++ b/training/main.py @@ -1,9 +1,10 @@ """Loads config with hydra and runs experiment.""" import hydra from omegaconf import DictConfig +from training.metadata import TRAINING_DIR -@hydra.main(config_path="conf", config_name="config") +@hydra.main(version_base="1.2", config_path=TRAINING_DIR / "conf", config_name="config") def main(config: DictConfig) -> None: """Loads config with hydra and runs the experiment.""" import utils diff --git a/training/metadata.py b/training/metadata.py new file mode 100644 index 0000000..b5a868d --- /dev/null +++ b/training/metadata.py @@ -0,0 +1,6 @@ +"""Training paths.""" +from pathlib import Path + +TRAINING_DIR = Path(__file__).parents[0].resolve() +ARTIFACTS_DIR = TRAINING_DIR.parent / "text_recognizer" / "artifacts" +RUNS_DIR = TRAINING_DIR / "logs" / "runs" -- cgit v1.2.3-70-g09d2