From 2c7c2158c4d0489f90afa4e7cd3699b62682c2f9 Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Tue, 17 Nov 2020 22:30:39 +0100 Subject: [PATCH] move wandb initializer to logger_initializers.py --- project/config.yaml | 4 +- project/pipeline_modules/lightning_wrapper.py | 4 +- .../pipeline_modules/logger_initializers.py | 28 ++++++++++++++ project/train.py | 38 +++---------------- 4 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 project/pipeline_modules/logger_initializers.py diff --git a/project/config.yaml b/project/config.yaml index 04423da..2ccd0e6 100644 --- a/project/config.yaml +++ b/project/config.yaml @@ -16,7 +16,7 @@ hparams: resume: resume_from_ckpt: False wandb_run_id: "8uuomodb" - ckpt_path: "wandb/run-20201111_022721-8uuomodb/files/hackathon_template_test/8uuomodb/checkpoints/epoch=2.ckpt" + ckpt_path: "checkpoints/epoch=2.ckpt" loggers: wandb: @@ -41,5 +41,5 @@ callbacks: printing: progress_bar_refresh_rate: 5 - weights_summary: "top" # or "full" + weights_summary: "top" # "full" profiler: False diff --git a/project/pipeline_modules/lightning_wrapper.py b/project/pipeline_modules/lightning_wrapper.py index 9e26ead..c073863 100644 --- a/project/pipeline_modules/lightning_wrapper.py +++ b/project/pipeline_modules/lightning_wrapper.py @@ -12,10 +12,10 @@ from pipeline_modules.models import * class LitModel(pl.LightningModule): - def __init__(self, config): + def __init__(self, hparams): super().__init__() - self.save_hyperparameters(config["hparams"]) + self.save_hyperparameters(hparams) self.model = SimpleLinearMNIST(config=self.hparams) def forward(self, x): diff --git a/project/pipeline_modules/logger_initializers.py b/project/pipeline_modules/logger_initializers.py new file mode 100644 index 0000000..1fd90d9 --- /dev/null +++ b/project/pipeline_modules/logger_initializers.py @@ -0,0 +1,28 @@ +from pytorch_lightning.loggers import WandbLogger, TensorBoardLogger, CometLogger, MLFlowLogger, NeptuneLogger + + +def init_wandb(config, lit_model, datamodule): + wandb_logger = WandbLogger( + project=config["loggers"]["wandb"]["project"], + job_type=config["loggers"]["wandb"]["job_type"], + tags=config["loggers"]["wandb"]["tags"], + entity=config["loggers"]["wandb"]["team"], + id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None, + log_model=config["loggers"]["wandb"]["log_model"], + offline=config["loggers"]["wandb"]["offline"], + save_dir="logs/" + ) + wandb_logger.watch(lit_model.model, log=None) + wandb_logger.log_hyperparams({ + "model_name": lit_model.model.__class__.__name__, + "datamodule_name": datamodule.__class__.__name__, + "optimizer": lit_model.configure_optimizers().__class__.__name__, + "train_size": len(datamodule.data_train) if datamodule.data_train is not None else 0, + "val_size": len(datamodule.data_val) if datamodule.data_train is not None else 0, + "test_size": len(datamodule.data_test) if datamodule.data_train is not None else 0, + "input_dims": datamodule.input_dims, + "input_size": datamodule.input_size, + }) + # download model from a specific wandb run + # wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d") + return wandb_logger diff --git a/project/train.py b/project/train.py index 65aef07..0034ac6 100644 --- a/project/train.py +++ b/project/train.py @@ -1,16 +1,15 @@ from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping -from pytorch_lightning.profiler import SimpleProfiler, AdvancedProfiler -from pytorch_lightning.loggers import WandbLogger +from pytorch_lightning.profiler import SimpleProfiler import yaml # custom from pipeline_modules.lightning_wrapper import LitModel -from project.pipeline_modules.data_modules import * -from project.pipeline_modules.callbacks import * +from pipeline_modules.logger_initializers import * +from pipeline_modules.data_modules import * +from pipeline_modules.callbacks import * def train(config): - # Init data module datamodule = MNISTDataModule( batch_size=config["hparams"]["batch_size"], @@ -20,7 +19,7 @@ def train(config): datamodule.setup() # Init our model - model = LitModel(config) + model = LitModel(hparams=config["hparams"]) # Init wandb logger wandb_logger = init_wandb(config, model, datamodule) @@ -81,33 +80,6 @@ def train(config): trainer.test() -def init_wandb(config, model, datamodule): - wandb_logger = WandbLogger( - project=config["loggers"]["wandb"]["project"], - job_type=config["loggers"]["wandb"]["job_type"], - tags=config["loggers"]["wandb"]["tags"], - entity=config["loggers"]["wandb"]["team"], - id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None, - log_model=config["loggers"]["wandb"]["log_model"], - offline=config["loggers"]["wandb"]["offline"], - save_dir="logs/" - ) - wandb_logger.watch(model.model, log=None) - wandb_logger.log_hyperparams({ - "model_name": model.model.__class__.__name__, - "datamodule_name": datamodule.__class__.__name__, - "optimizer": model.configure_optimizers().__class__.__name__, - "train_size": len(datamodule.data_train) if datamodule.data_train is not None else 0, - "val_size": len(datamodule.data_val) if datamodule.data_train is not None else 0, - "test_size": len(datamodule.data_test) if datamodule.data_train is not None else 0, - "input_dims": datamodule.input_dims, - "input_size": datamodule.input_size, - }) - # download model from a specific wandb run - # wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d") - return wandb_logger - - def load_config(): with open("config.yaml", "r") as ymlfile: config = yaml.load(ymlfile, Loader=yaml.FullLoader)