From 18f0eaa4668edb796f4471cdb50d8671f906243c Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Tue, 1 Dec 2020 04:36:23 +0100 Subject: [PATCH] move resume training configuration to run configs --- README.md | 19 ++++++---------- conda_env.yaml | 2 +- .../example_datamodule/datamodule.py | 2 +- .../mnist_digits_datamodule/datamodule.py | 7 +++--- project/project_config.yaml | 11 ---------- project/run_configs.yaml | 7 ++++++ project/train.py | 16 +++++++++----- project/utils/init_utils.py | 22 ++++++++++++++----- 8 files changed, 47 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 93116cb..2b7f7d1 100644 --- a/README.md +++ b/README.md @@ -67,21 +67,10 @@ The directory structure of new project looks like this: ```yaml num_of_gpus: -1 <- '-1' to use all gpus available, '0' to train on cpu -resume_training: - lightning_ckpt: - resume_from_ckpt: False <- set True if you want to resume - ckpt_path: "epoch=7.ckpt" <- path to your checkpoint - wandb: - resume_wandb_run: False <- set True if you want to resume Weight&Biases run - wandb_run_id: "8uuomodb" <- id of Weight&Biases run you want to resume - loggers: wandb: project: "project_name" <- wandb project name team: "kino" <- wandb entity name - group: None <- wandb group name - job_type: "train" <- wandb job_type name - tags: [] <- wandb tags name log_model: True <- set True if you want to upload ckpts to wandb automatically offline: False <- set True if you want to store all data locally @@ -127,7 +116,13 @@ MNIST_CLASSIFIER_V1: batch_size: 256 train_val_split_ratio: 0.9 num_workers: 1 - pin_memory: False + pin_memory: False + wandb: <- this section is optional and can be removed + group: None + tags: ["v2", "uwu"] + resume_training: <- this section is optional and can be removed + checkpoint_path: "path_to_checkpoint/last.ckpt" + wandb_run_id: "7ab26xrj" ``` diff --git a/conda_env.yaml b/conda_env.yaml index 05f0853..6c94c2f 100644 --- a/conda_env.yaml +++ b/conda_env.yaml @@ -10,4 +10,4 @@ dependencies: - pytorch - torchvision - torchaudio -#prefix: C:\Users\UserX\Anaconda3\envs\hack_env + - jupyter diff --git a/project/data_modules/example_datamodule/datamodule.py b/project/data_modules/example_datamodule/datamodule.py index 6bbcf63..1dc7fe3 100644 --- a/project/data_modules/example_datamodule/datamodule.py +++ b/project/data_modules/example_datamodule/datamodule.py @@ -37,7 +37,7 @@ class DataModule(pl.LightningDataModule): def train_dataloader(self): return DataLoader(self.data_train, batch_size=self.batch_size, num_workers=self.num_workers, - pin_memory=self.pin_memory) + pin_memory=self.pin_memory, shuffle=True) def val_dataloader(self): return DataLoader(self.data_val, batch_size=self.batch_size, num_workers=self.num_workers, diff --git a/project/data_modules/mnist_digits_datamodule/datamodule.py b/project/data_modules/mnist_digits_datamodule/datamodule.py index 3d8ccd9..9c9d7d4 100644 --- a/project/data_modules/mnist_digits_datamodule/datamodule.py +++ b/project/data_modules/mnist_digits_datamodule/datamodule.py @@ -30,15 +30,16 @@ class DataModule(pl.LightningDataModule): """Load data. Set variables: self.data_train, self.data_val, self.data_test.""" trainset = MNIST(self.data_dir, train=True, transform=self.transforms) - train_dataset_length = int(len(trainset) * self.train_val_split_ratio) - train_val_split = [train_dataset_length, len(trainset) - train_dataset_length] + train_length = int(len(trainset) * self.train_val_split_ratio) + val_length = len(trainset) - train_length + train_val_split = [train_length, val_length] self.data_train, self.data_val = random_split(trainset, train_val_split) self.data_test = MNIST(self.data_dir, train=False, transform=self.transforms) def train_dataloader(self): return DataLoader(self.data_train, batch_size=self.batch_size, num_workers=self.num_workers, - pin_memory=self.pin_memory) + pin_memory=self.pin_memory, shuffle=True) def val_dataloader(self): return DataLoader(self.data_val, batch_size=self.batch_size, num_workers=self.num_workers, diff --git a/project/project_config.yaml b/project/project_config.yaml index a4c3c46..5519e18 100644 --- a/project/project_config.yaml +++ b/project/project_config.yaml @@ -1,20 +1,9 @@ num_of_gpus: 0 -resume_training: - lightning_ckpt: - resume_from_ckpt: False - ckpt_path: "logs/checkpoints/epoch=2.ckpt" - wandb: - resume_wandb_run: False - wandb_run_id: "8uuomodb" - loggers: wandb: project: "hackathon_template_test" team: "kino" - group: None - job_type: "train" - tags: [] log_model: True offline: False diff --git a/project/run_configs.yaml b/project/run_configs.yaml index 44679e0..599c703 100644 --- a/project/run_configs.yaml +++ b/project/run_configs.yaml @@ -21,6 +21,7 @@ MNIST_CLASSIFIER_V1: num_workers: 1 pin_memory: False + MNIST_CLASSIFIER_V2: trainer: min_epochs: 1 @@ -43,3 +44,9 @@ MNIST_CLASSIFIER_V2: train_val_split_ratio: 0.9 num_workers: 1 pin_memory: False + wandb: + group: None + tags: ["v2", "uwu"] + resume_training: + checkpoint_path: "logs/wandb/run-20201201_041527-7ab26xrj/files/tests/7ab26xrj/checkpoints/last.ckpt" + wandb_run_id: "7ab26xrj" diff --git a/project/train.py b/project/train.py index b9c773a..625c2d1 100644 --- a/project/train.py +++ b/project/train.py @@ -22,11 +22,14 @@ def train(project_config, run_config): # Add custom callbacks from utils/callbacks.py callbacks.extend([ - # MetricsHeatmapLoggerCallback(), + MetricsHeatmapLoggerCallback(), # UnfreezeModelCallback(wait_epochs=5), SaveCodeToWandbCallback(wandb_save_dir=logger.save_dir, lit_model=lit_model, datamodule=datamodule), ]) + # Get path to checkpoint you want to resume with if it was set in run config + resume_from_checkpoint = run_config.get("resume_training", {}).get("checkpoint_path", None) + # Init PyTorch Lightning trainer ⚡ trainer = pl.Trainer( # whether to use gpu and how many @@ -38,9 +41,12 @@ def train(project_config, run_config): # useful callbacks callbacks=callbacks, - # resuming training from checkpoint - resume_from_checkpoint=project_config["resume_training"]["lightning_ckpt"]["ckpt_path"] - if project_config["resume_training"]["lightning_ckpt"]["resume_from_ckpt"] else None, + # resume training from checkpoint if it was set in the run config + resume_from_checkpoint=resume_from_checkpoint + if resume_from_checkpoint != "None" + and resume_from_checkpoint != "False" + and resume_from_checkpoint is not False + else None, # print related progress_bar_refresh_rate=project_config["printing"]["progress_bar_refresh_rate"], @@ -53,7 +59,7 @@ def train(project_config, run_config): accumulate_grad_batches=run_config["trainer"].get("accumulate_grad_batches", 1), gradient_clip_val=run_config["trainer"].get("gradient_clip_val", 0.5), - # these are mostly for debugging (read TIPS.md for explanation) + # these are mostly for debugging fast_dev_run=run_config["trainer"].get("fast_dev_run", False), limit_train_batches=run_config["trainer"].get("limit_train_batches", 1.0), limit_val_batches=run_config["trainer"].get("limit_val_batches", 1.0), diff --git a/project/utils/init_utils.py b/project/utils/init_utils.py index 29b907e..08f0d9f 100644 --- a/project/utils/init_utils.py +++ b/project/utils/init_utils.py @@ -40,15 +40,25 @@ def init_main_callbacks(project_config): def init_wandb_logger(config, run_config, lit_model, datamodule): """Initialize Weights&Biases logger.""" + resume_from_checkpoint = run_config.get("resume_training", {}).get("resume_from_checkpoint", None) + wandb_run_id = run_config.get("resume_training", {}).get("wandb_run_id", None) 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_training"]["wandb"]["wandb_run_id"] - if config["resume_training"]["wandb"]["resume_wandb_run"] else None, log_model=config["loggers"]["wandb"]["log_model"], offline=config["loggers"]["wandb"]["offline"], + + group=run_config.get("wandb", {}).get("group", None), + job_type=run_config.get("wandb", {}).get("job_type", "train"), + tags=run_config.get("wandb", {}).get("tags", []), + + # resume run only if ckpt was set in the run config + id=wandb_run_id + if resume_from_checkpoint != "None" and wandb_run_id != "None" + and resume_from_checkpoint != "False" and wandb_run_id != "False" + and resume_from_checkpoint is not False and wandb_run_id is not False + else None, + save_dir="logs/", save_code=False ) @@ -62,8 +72,8 @@ def init_wandb_logger(config, run_config, lit_model, datamodule): "model_name": lit_model.model.__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, + "val_size": len(datamodule.data_val) if datamodule.data_val is not None else 0, + "test_size": len(datamodule.data_test) if datamodule.data_test is not None else 0, }) wandb_logger.log_hyperparams(run_config["trainer"]) wandb_logger.log_hyperparams(run_config["model"])