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
This commit is contained in:
1 parent
b60f958717
commit
89b502b090
1 file changed
+41
-8
@@ -1,5 +1,5 @@
|
|||||||
import glob
|
import subprocess
|
||||||
import os
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
@@ -43,10 +43,18 @@ class WatchModel(Callback):
|
|||||||
|
|
||||||
|
|
||||||
class UploadCodeAsArtifact(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.code_dir = code_dir
|
||||||
|
self.use_git = use_git
|
||||||
|
|
||||||
@rank_zero_only
|
@rank_zero_only
|
||||||
def on_train_start(self, trainer, pl_module):
|
def on_train_start(self, trainer, pl_module):
|
||||||
@@ -54,8 +62,29 @@ class UploadCodeAsArtifact(Callback):
|
|||||||
experiment = logger.experiment
|
experiment = logger.experiment
|
||||||
|
|
||||||
code = wandb.Artifact("project-source", type="code")
|
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)
|
experiment.log_artifact(code)
|
||||||
|
|
||||||
@@ -67,6 +96,10 @@ class UploadCheckpointsAsArtifact(Callback):
|
|||||||
self.ckpt_dir = ckpt_dir
|
self.ckpt_dir = ckpt_dir
|
||||||
self.upload_best_only = upload_best_only
|
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
|
@rank_zero_only
|
||||||
def on_train_end(self, trainer, pl_module):
|
def on_train_end(self, trainer, pl_module):
|
||||||
logger = get_wandb_logger(trainer=trainer)
|
logger = get_wandb_logger(trainer=trainer)
|
||||||
@@ -77,8 +110,8 @@ class UploadCheckpointsAsArtifact(Callback):
|
|||||||
if self.upload_best_only:
|
if self.upload_best_only:
|
||||||
ckpts.add_file(trainer.checkpoint_callback.best_model_path)
|
ckpts.add_file(trainer.checkpoint_callback.best_model_path)
|
||||||
else:
|
else:
|
||||||
for path in glob.glob(os.path.join(self.ckpt_dir, "**/*.ckpt"), recursive=True):
|
for path in Path(self.ckpt_dir).rglob("*.ckpt"):
|
||||||
ckpts.add_file(path)
|
ckpts.add_file(str(path))
|
||||||
|
|
||||||
experiment.log_artifact(ckpts)
|
experiment.log_artifact(ckpts)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user