add callback for saving code to wandb

This commit is contained in:
hobogalaxy committed 2020-11-21 03:33:48 +01:00
1 parent 73369bdbe5
commit 30329abc28
3 files changed
+41 -6

No files matched your search

+2 -1
View File
@@ -40,7 +40,8 @@ def train(config):
# MetricsHeatmapLoggerCallback(), # MetricsHeatmapLoggerCallback(),
# UnfreezeModelCallback(wait_epochs=5), # UnfreezeModelCallback(wait_epochs=5),
# ImagePredictionLoggerCallback(datamodule=datamodule), # ImagePredictionLoggerCallback(datamodule=datamodule),
# SaveModelOnnxCallback(datamodule=datamodule, save_dir=wandb_logger.save_dir) SaveCodeToWandbCallback(wandb_save_dir=logger.save_dir),
# SaveOnnxModelToWandbCallback(datamodule=datamodule, save_dir=logger.save_dir)
] ]
# Init trainer # Init trainer
+35 -3
View File
@@ -1,5 +1,6 @@
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import pytorch_lightning as pl import pytorch_lightning as pl
from shutil import copy, copyfile
import seaborn import seaborn
import torch import torch
import wandb import wandb
@@ -20,9 +21,9 @@ class ExampleCallback(pl.Callback):
print('Do something when training ends.') print('Do something when training ends.')
class SaveModelOnnxCallback(pl.Callback): class SaveOnnxModelToWandbCallback(pl.Callback):
""" """
Save model in .onnx format. Save model in .onnx format and upload to wandb.
Might crash since not all models are compatible with onnx. Might crash since not all models are compatible with onnx.
""" """
def __init__(self, datamodule, save_dir): def __init__(self, datamodule, save_dir):
@@ -40,7 +41,7 @@ class SaveModelOnnxCallback(pl.Callback):
def save_onnx_model(self, pl_module): def save_onnx_model(self, pl_module):
file_path = os.path.join(self.save_dir, "model.onnx") file_path = os.path.join(self.save_dir, "model.onnx")
pl_module.to_onnx(file_path=file_path, input_sample=self.dummy_input.to(pl_module.device)) pl_module.to_onnx(file_path=file_path, input_sample=self.dummy_input.to(pl_module.device))
wandb.save(file_path) wandb.save(file_path, base_path=self.save_dir)
class ImagePredictionLoggerCallback(pl.Callback): class ImagePredictionLoggerCallback(pl.Callback):
@@ -123,3 +124,34 @@ class MetricsHeatmapLoggerCallback(pl.Callback):
self.preds = [] self.preds = []
self.targets = [] self.targets = []
class SaveCodeToWandbCallback(pl.Callback):
"""
Upload specified files to wandb at the beginning of the run.
"""
def __init__(self, wandb_save_dir):
self.wandb_save_dir = wandb_save_dir
self.files_to_be_saved = [
"training_modules/callbacks.py",
"training_modules/data_modules.py",
"training_modules/datasets.py",
"training_modules/lightning_wrapper.py",
"training_modules/loggers.py",
"training_modules/models.py",
"training_modules/transforms.py",
"train.py",
"config.yaml"
]
def on_sanity_check_end(self, trainer, pl_module):
"""Upload files when all validation sanity checks end."""
for file in self.files_to_be_saved:
dst_path = os.path.join(self.wandb_save_dir, "code", file)
path, filename = os.path.split(dst_path)
if not os.path.exists(path):
os.makedirs(path)
copy(file, dst_path)
wandb.save(dst_path, base_path=self.wandb_save_dir) # this line is only to make upload immediate
+4 -2
View File
@@ -11,9 +11,11 @@ def get_wandb_logger(config, lit_model, datamodule):
id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None, id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None,
log_model=config["loggers"]["wandb"]["log_model"], log_model=config["loggers"]["wandb"]["log_model"],
offline=config["loggers"]["wandb"]["offline"], offline=config["loggers"]["wandb"]["offline"],
save_dir="logs/" save_dir="logs/",
save_code=False
) )
os.mkdir("logs/") if not os.path.exists("logs/") else None if not os.path.exists("logs/"):
os.mkdir("logs/")
wandb_logger.watch(lit_model.model, log=None) wandb_logger.watch(lit_model.model, log=None)
wandb_logger.log_hyperparams({ wandb_logger.log_hyperparams({
"model_name": lit_model.model.__class__.__name__, "model_name": lit_model.model.__class__.__name__,