From 166bd57198a2bd2bee6ae2d2b3942065d68dab53 Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Fri, 27 Nov 2020 02:11:51 +0100 Subject: [PATCH] adjust sweep file to new config structure --- README.md | 35 +++++++++++++++++++------ project/execute_sweep.py | 22 ++++++++++------ project/lightning_modules/init_utils.py | 2 +- project/train.py | 6 ++--- project/utils/predict.py | 8 +++--- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 84be18f..8284f08 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The directory structure of new project looks like this: │ ├── data_modules <- Data related modules │ │ ├── datamodules.py <- "LightningDataModule" modules │ │ ├── datasets.py <- "Dataset" modules -│ │ └── transforms.py <- Data transformations (data preprocessing) +│ │ └── transforms.py <- Data transformations │ │ │ ├── logs <- Logs generated by Weights&Biases and PyTorch Lightning │ │ @@ -29,19 +29,19 @@ The directory structure of new project looks like this: │ │ ├── callbacks.py <- Training callbacks │ │ └── init_utils.py <- Some useful initializers │ │ -│ ├── models +│ ├── models <- All your models should be located here │ │ ├── simple_mnist_classifier <- Example model │ │ │ ├── lightning_module.py <- Contains train/val/test step methods -│ │ │ └── models.py <- Models declarations used in lightning_module.py +│ │ │ └── models.py <- Model declarations used in lightning_module.py │ │ │ │ │ ├── simple_cifar10_classifier <- Example model │ │ │ ├── lightning_module.py <- Contains train/val/test step methods -│ │ │ └── models.py <- Models declarations used in lightning_module.py +│ │ │ └── models.py <- Model declarations used in lightning_module.py │ │ │ │ │ └── ... │ │ │ ├── project_config.yaml <- Project configuration -│ ├── run_configs.yaml <- Different runs configurations +│ ├── run_configs.yaml <- Run configurations │ ├── execute_sweep.py <- Special file for executing wandb sweeps (hyperparameter search) │ └── train.py <- Train model │ @@ -54,7 +54,7 @@ The directory structure of new project looks like this: ``` ## Project config parameters -#### [project_config.yaml](project/project_config.yaml): +#### [project_config.yaml](project/project_config.yaml) ```yaml num_of_gpus: -1 @@ -94,7 +94,9 @@ printing: ``` ## Run config parameters -#### [run_configs.yaml](project/run_configs.yaml): +#### [run_configs.yaml](project/run_configs.yaml) +You can store many run configurations in this file.
+Example run configuration: ```yaml CIFAR10_CLASSIFIER_V2: trainer: @@ -117,5 +119,22 @@ CIFAR10_CLASSIFIER_V2: pin_memory: False ``` +The run configuration that you want to train with needs to be chose in [train.py](project/train.py): +```python +if __name__ == "__main__": + + # --------------------------------------- CHOOSE YOUR RUN CONFIG HEREE --------------------------------------- # + # --------------------------------------------------- ↓↓↓ ---------------------------------------------------- # + RUN_CONFIG = "MNIST_CLASSIFIER_V1" + # --------------------------------------------------- ↑↑↑ ---------------------------------------------------- # + # ------------------------------------------------------------------------------------------------------------ # + + project_conf = load_config("project_config.yaml") + run_conf = load_config("run_configs.yaml")[RUN_CONFIG] + + train(project_config=project_conf, run_config=run_conf) +``` + + ## Tips -Read [TIPS.md](TIPS.md) +For training tips read [TIPS.md](TIPS.md) diff --git a/project/execute_sweep.py b/project/execute_sweep.py index 2e93d17..ec42e10 100644 --- a/project/execute_sweep.py +++ b/project/execute_sweep.py @@ -1,14 +1,14 @@ +# THIS FILE SHOULDN't BE EXECUTED DIRECTLY, IT SHOULD BE RUN BY WANDB SWEEP AGENT! import wandb import train -# THIS FILE SHOULDN't BE EXECUTED DIRECTLY, IT SHOULD BE RUN BY WANDB SWEEP AGENT! +# Choose which run config to optimize +RUN_CONFIG = "MNIST_CLASSIFIER_V1" # Load project config -conf = train.load_config() - -# Choose model for hyperparameter search -model_conf = conf["model_configs"]["simple_mnist_classifier_v1"] +project_conf = train.load_config("project_config.yaml") +run_conf = train.load_config("run_configs.yaml")[RUN_CONFIG] # Initialize wandb wandb.init() @@ -16,8 +16,14 @@ wandb.init() # Replace project config hyperparameters with the ones loaded from wandb sweep server sweep_hparams = wandb.Config._as_dict(wandb.config) for key, value in sweep_hparams.items(): - if key != "_wandb" and not key.startswith("dataset/"): - model_conf[key] = value + if key == "_wandb": + continue + elif key in run_conf["trainer"].keys(): + run_conf["trainer"][key] = value + elif key in run_conf["model"].keys(): + run_conf["model"][key] = value + elif key in run_conf["dataset"].keys(): + run_conf["dataset"][key] = value # Execute run -train.train(project_config=conf, run_config=model_conf) +train.train(project_config=project_conf, run_config=run_conf) diff --git a/project/lightning_modules/init_utils.py b/project/lightning_modules/init_utils.py index 962168b..aced490 100644 --- a/project/lightning_modules/init_utils.py +++ b/project/lightning_modules/init_utils.py @@ -68,9 +68,9 @@ def init_wandb_logger(config, run_config, lit_model, datamodule): "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, }) + wandb_logger.log_hyperparams(run_config["trainer"]) wandb_logger.log_hyperparams(run_config["model"]) wandb_logger.log_hyperparams(run_config["dataset"]) - wandb_logger.log_hyperparams(run_config["trainer"]) return wandb_logger diff --git a/project/train.py b/project/train.py index 6f675b5..d1e4a03 100644 --- a/project/train.py +++ b/project/train.py @@ -85,11 +85,9 @@ if __name__ == "__main__": # --------------------------------------- CHOOSE YOUR RUN CONFIG HEREE --------------------------------------- # # --------------------------------------------------- ↓↓↓ ---------------------------------------------------- # - # ------------------------------------------------------------------------------------------------------------ # - # RUN_CONFIG = "MNIST_CLASSIFIER_V1" + RUN_CONFIG = "MNIST_CLASSIFIER_V1" # RUN_CONFIG = "CIFAR10_CLASSIFIER_V1" - RUN_CONFIG = "CIFAR10_CLASSIFIER_V2" - # ------------------------------------------------------------------------------------------------------------ # + # RUN_CONFIG = "CIFAR10_CLASSIFIER_V2" # --------------------------------------------------- ↑↑↑ ---------------------------------------------------- # # --------------------------------------- CHOOSE YOUR RUN CONFIG HEREE --------------------------------------- # diff --git a/project/utils/predict.py b/project/utils/predict.py index bbb567a..6fa11a0 100644 --- a/project/utils/predict.py +++ b/project/utils/predict.py @@ -13,11 +13,11 @@ def predict(): CKPT_PATH = "epoch=0.ckpt" # load model from checkpoint - pretrained_model = LitModel.load_from_checkpoint(checkpoint_path=CKPT_PATH) + trained_model = LitModel.load_from_checkpoint(checkpoint_path=CKPT_PATH) # switch to evaluation mode - pretrained_model.eval() - pretrained_model.freeze() + trained_model.eval() + trained_model.freeze() # load data img = Image.open("../../example_img.png").convert("RGB") @@ -27,7 +27,7 @@ def predict(): img = img.reshape((1, *img.size())) # reshape to form batch of size 1 # inference - output = pretrained_model(img) + output = trained_model(img) print(output)