From 89b502b0907595b1ff9ba02c8612d2153eeae951 Mon Sep 17 00:00:00 2001 From: Zhengyu Yang <25852061+zhengyu-yang@users.noreply.github.com> Date: Fri, 13 Aug 2021 01:02:22 -0700 Subject: [PATCH] Update wandb_callbacks.py (#152) * Update wandb_callbacks.py * UploadCodeAsArtifact: Add the functionality to use git to decide which files are source files. Can upload all files that are not ignored by git instead of all '*.py' file. * UploadCheckpointsAsArtifact: ckpts are output of the run, so use `experiment.log_artifact(ckpts)` instead --- src/callbacks/wandb_callbacks.py | 49 ++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/callbacks/wandb_callbacks.py b/src/callbacks/wandb_callbacks.py index 7cc37e8..f8804a4 100644 --- a/src/callbacks/wandb_callbacks.py +++ b/src/callbacks/wandb_callbacks.py @@ -1,5 +1,5 @@ -import glob -import os +import subprocess +from pathlib import Path from typing import List import matplotlib.pyplot as plt @@ -43,10 +43,18 @@ class WatchModel(Callback): class UploadCodeAsArtifact(Callback): - """Upload all *.py files to wandb as an artifact, at the beginning of the run.""" + """Upload all code files to wandb as an artifact, at the beginning of the run.""" - def __init__(self, code_dir: str): + def __init__(self, code_dir: str, use_git: bool = True): + """ + + Args: + code_dir: the code directory + use_git: if using git, then upload all files that are not ignored by git. + if not using git, then upload all '*.py' file + """ self.code_dir = code_dir + self.use_git = use_git @rank_zero_only def on_train_start(self, trainer, pl_module): @@ -54,8 +62,29 @@ class UploadCodeAsArtifact(Callback): experiment = logger.experiment code = wandb.Artifact("project-source", type="code") - for path in glob.glob(os.path.join(self.code_dir, "**/*.py"), recursive=True): - code.add_file(path) + + if self.use_git: + # get .git folder + # https://alexwlchan.net/2020/11/a-python-function-to-ignore-a-path-with-git-info-exclude/ + git_dir_path = Path( + subprocess.check_output(["git", "rev-parse", "--git-dir"]).strip().decode("utf8") + ).resolve() + + for path in Path(self.code_dir).resolve().rglob("*"): + if ( + path.is_file() + # ignore files in .git + and not str(path).startswith(str(git_dir_path)) # noqa: W503 + # ignore files ignored by git + and ( # noqa: W503 + subprocess.run(["git", "check-ignore", "-q", str(path)]).returncode == 1 + ) + ): + code.add_file(str(path), name=str(path.relative_to(self.code_dir))) + + else: + for path in Path(self.code_dir).resolve().rglob("*.py"): + code.add_file(str(path), name=str(path.relative_to(self.code_dir))) experiment.log_artifact(code) @@ -67,6 +96,10 @@ class UploadCheckpointsAsArtifact(Callback): self.ckpt_dir = ckpt_dir self.upload_best_only = upload_best_only + @rank_zero_only + def on_keyboard_interrupt(self, trainer, pl_module): + self.on_train_end(trainer, pl_module) + @rank_zero_only def on_train_end(self, trainer, pl_module): logger = get_wandb_logger(trainer=trainer) @@ -77,8 +110,8 @@ class UploadCheckpointsAsArtifact(Callback): if self.upload_best_only: ckpts.add_file(trainer.checkpoint_callback.best_model_path) else: - for path in glob.glob(os.path.join(self.ckpt_dir, "**/*.ckpt"), recursive=True): - ckpts.add_file(path) + for path in Path(self.ckpt_dir).rglob("*.ckpt"): + ckpts.add_file(str(path)) experiment.log_artifact(ckpts)