From 6ed07c94cef93e9fe718026d10ece1d649ada66c Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Sat, 5 Dec 2020 19:56:54 +0100 Subject: [PATCH] fix some bugs --- README.md | 10 +++++----- project/run_configs.yaml | 5 +++-- project/train.py | 6 ++---- project/utils/init_utils.py | 23 ++++++++++++++--------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ab7c826..8d29a72 100644 --- a/README.md +++ b/README.md @@ -99,12 +99,12 @@ You can store many run configurations in this file.
Example run configuration: ```yaml MNIST_CLASSIFIER_V1: - trainer: <- lightning 'Trainer' parameters (all except 'max_epochs' are optional) + trainer: <- parameters passed to lightning 'Trainer' max_epochs: 5 gradient_clip_val: 0.5 accumulate_grad_batches: 3 limit_train_batches: 1.0 - model: <- all of the parameters here will be passed to 'LitModel' in 'hparams' dictionary + model: <- parameters passed to 'LitModel' in 'hparams' dictionary model_folder: "simple_mnist_classifier" <- name of folder from which 'lightning_module.py' (with 'LitMdodel' class) will be loaded lr: 0.001 weight_decay: 0.000001 @@ -113,7 +113,7 @@ MNIST_CLASSIFIER_V1: lin1_size: 256 lin2_size: 256 lin3_size: 128 - dataset: <- all of the parameters here will be passed to 'DataModule' in 'hparams' dictionary + dataset: <- parameters passed to 'DataModule' in 'hparams' dictionary datamodule_folder: "mnist_digits_datamodule" <- name of folder from which 'datamodule.py' (with 'DataModule' class) will be loaded batch_size: 256 train_val_split_ratio: 0.9 @@ -122,8 +122,8 @@ MNIST_CLASSIFIER_V1: wandb: <- this section is optional and can be removed group: "" tags: ["v1", "uwu"] - resume_training: <- this section is optional and can be removed if you don't want to resume training - checkpoint_path: "path_to_checkpoint/last.ckpt" <- path to checkpoint + resume_training: <- this section is optional and can be removed + checkpoint_path: "path_to_checkpoint/last.ckpt" wandb_run_id: None <- you can set id of Weights&Biases run that you want to resume but it's optional ```
diff --git a/project/run_configs.yaml b/project/run_configs.yaml index 0873359..3627758 100644 --- a/project/run_configs.yaml +++ b/project/run_configs.yaml @@ -43,11 +43,12 @@ MNIST_CLASSIFIER_V2: datamodule_folder: "mnist_digits_datamodule" batch_size: 256 train_val_split_ratio: 0.9 - num_workers: 1 + num_workers: 4 pin_memory: False wandb: group: None tags: ["v2", "uwu"] + notes: "This is example run." resume_training: - checkpoint_path: "logs/wandb/run-20201201_041527-7ab26xrj/files/tests/7ab26xrj/checkpoints/last.ckpt" + checkpoint_path: "logs/checkpoints/7ab26xr/last.ckpt" wandb_run_id: "7ab26xrj" diff --git a/project/train.py b/project/train.py index f7acc58..7aa6343 100644 --- a/project/train.py +++ b/project/train.py @@ -60,9 +60,7 @@ def train(project_config: dict, run_config: dict, use_wandb: bool): # 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 + if resume_from_checkpoint != "None" and resume_from_checkpoint is not False else None, # print related @@ -74,7 +72,7 @@ def train(project_config: dict, run_config: dict, use_wandb: bool): num_sanity_val_steps=3, # default log dir if no logger is found - default_root_dir="logs/lightning_logs", + default_root_dir=os.path.dirname(__file__) + "logs/lightning_logs", # insert all other trainer parameters specified in run config **run_config["trainer"] diff --git a/project/utils/init_utils.py b/project/utils/init_utils.py index 4429305..99937a0 100644 --- a/project/utils/init_utils.py +++ b/project/utils/init_utils.py @@ -40,16 +40,18 @@ def init_main_callbacks(project_config: dict) -> List[pl.Callback]: return callbacks -def init_wandb_logger( - project_config: dict, - run_config: dict, - lit_model: pl.LightningModule, - datamodule: pl.LightningDataModule, - log_path: str = "logs/" -) -> pl.loggers.WandbLogger: +def init_wandb_logger(project_config: dict, run_config: dict, + lit_model: pl.LightningModule, datamodule: pl.LightningDataModule, + log_path: str = "logs/") -> pl.loggers.WandbLogger: """Initialize Weights&Biases logger.""" + + # with this line wandb will throw an error if the run to be resumed does not exist yet + # instead of auto-creating a new run + os.environ["WANDB_RESUME"] = "must" + 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=project_config["loggers"]["wandb"]["project"], entity=project_config["loggers"]["wandb"]["entity"], @@ -59,23 +61,26 @@ def init_wandb_logger( 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", []), + notes=run_config.get("wandb", {}).get("notes", ""), # 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" + if resume_from_checkpoint != "None" and wandb_run_id != "None" and resume_from_checkpoint is not None and resume_from_checkpoint is not False and wandb_run_id is not False else None, save_dir=log_path, save_code=False ) + if not os.path.exists(log_path): os.makedirs(log_path) + if hasattr(lit_model, 'model'): wandb_logger.watch(lit_model.model, log=None) else: wandb_logger.watch(lit_model, log=None) + wandb_logger.log_hyperparams({ "model_name": lit_model.model.__class__.__name__, "optimizer": lit_model.configure_optimizers().__class__.__name__,