From 55e5f5e2c42d176a928cde9ec88c276d554b40c6 Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Wed, 13 Jan 2021 01:15:34 +0100 Subject: [PATCH 1/3] Improve readme --- README.md | 31 ++++++++++++++----------- project/configs/logger/all_loggers.yaml | 6 ++--- project/train.py | 1 - 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 638409d..5a5e4de 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,29 @@ ## Deep learning project template -A convenient starting template and pipeline for deep learning projects. Built with PyTorch Lightning, Hydra and Weights&Biases.
-Click on "Use this template" button above to initialize new repository.
+This is my starting template and pipeline for most deep learning projects.
+Built with PyTorch Lightning, Hydra and Weights&Biases.
+Click on `Use this template` button above to initialize new repository.
## Features - Predefined folder structure -- Automates training with PyTorch Lightning - you only need to create model and datamodule and specify them in configuration files -- All advantages of Hydra: +- Automates training with PyTorch Lightning + - You only need to create model and datamodule and specify them in configuration files +- All advantages of Hydra - Main config file contains default training configuration ([config.yaml](project/configs/config.yaml)) - - Storing many experiment configurations in a convenient way - experiments override chosen parameters from default training configuration ([configs/experiment](project/configs/experiment)) + - Storing many experiment configurations in a convenient way ([configs/experiment](project/configs/experiment)) - Composing configuration files out of other configuration files - Scheduling execution of many experiments - Overriding any config parameter from command line - Command line tab completion - - Logging history of all executed runs -- Weights&Biases integration: - - Automatically stores all code files, configs and model checkpoints in Weights&Biases cloud - - Useful callbacks ([wandb_callbacks.py](project/src/callbacks/wandb_callbacks.py)) + - Logging history of all executed runs/experiments +- Weights&Biases integration + - Available callbacks that store all code files, and model checkpoints as artifacts in Weights&Biases cloud ([wandb_callbacks.py](project/src/callbacks/wandb_callbacks.py)) + - Other examples of useful wandb callbacks ([wandb_callbacks.py](project/src/callbacks/wandb_callbacks.py)) + - Configuring which hyperparameters are saved by loggers through main config file ([config.yaml](project/configs/config.yaml)) - ~~Hyperparameter search with Weights&Biases sweeps ([execute_sweep.py](project/template_utils/execute_sweep.py))~~ (TODO) - Built in requirements ([requirements.txt](requirements.txt)) - Built in conda environment initialization ([conda_env.yaml](conda_env.yaml)) -- Built in package setup ([setup.py](setup.py)) -- Example with MNIST digits classification +- Built in python package setup ([setup.py](setup.py)) +- Example with MNIST digits classification ([mnist_model](project/src/models/simple_mnist_classifier.py), [mnist_datamodule](project/src/datamodules/mnist_datamodule.py))
@@ -57,7 +60,7 @@ The directory structure of new project looks like this: │ │ ├── inference_example.py <- Example of inference with trained model │ │ └── initializers.py <- Initializers for different modules │ │ -│ └── train.py <- Train model with chosen run configuration +│ └── train.py <- Train model with chosen experiment configuration │ ├── .gitignore <- Ignored files ├── LICENSE <- Project license @@ -195,8 +198,8 @@ Logs are created automatically with the following structure: ## Workflow 1. Create PyTorch Lightning model 2. Create PyTorch Lightning datamodule -3. Create new experiment config in [configs/experiment](project/configs/experiment) -4. Run training with chosen run config
+3. Create new experiment config in [configs/experiment](project/configs/experiment) folder +4. Run training with chosen experiment config
```bash python train.py +experiment=experiment_name.yaml ``` diff --git a/project/configs/logger/all_loggers.yaml b/project/configs/logger/all_loggers.yaml index 76af7ec..9df1021 100644 --- a/project/configs/logger/all_loggers.yaml +++ b/project/configs/logger/all_loggers.yaml @@ -1,8 +1,8 @@ # @package _group_ -# Train with all logger at once +# Train with many loggers at once defaults: - csv_logger.yaml - wandb.yaml - # - comet.yaml -# - tensorboard.yaml + # - comet.yaml + # - tensorboard.yaml diff --git a/project/train.py b/project/train.py index 723bff6..e80c344 100644 --- a/project/train.py +++ b/project/train.py @@ -16,7 +16,6 @@ import src.utils.initializers as utils def train(config): - # Set global PyTorch seed if "seeds" in config and "pytorch_seed" in config["seeds"]: torch.manual_seed(seed=config["seeds"]["pytorch_seed"]) From f2e242534ebee9708a38d9880a7c86ba92f3fc50 Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Wed, 13 Jan 2021 01:33:03 +0100 Subject: [PATCH 2/3] another refactor --- README.md | 16 +++++++++++----- project/configs/config.yaml | 2 +- .../configs/experiment/exp_example_advanced.yaml | 2 +- .../configs/experiment/exp_example_simple.yaml | 2 +- ...le_mnist_classifier.yaml => mnist_model.yaml} | 0 ...simple_mnist_classifier.py => mnist_model.py} | 0 project/src/utils/inference_example.py | 4 ++-- 7 files changed, 16 insertions(+), 10 deletions(-) rename project/configs/model/{simple_mnist_classifier.yaml => mnist_model.yaml} (100%) rename project/src/models/{simple_mnist_classifier.py => mnist_model.py} (100%) diff --git a/README.md b/README.md index 5a5e4de..10950f6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ ## Deep learning project template -This is my starting template and pipeline for most deep learning projects.
+This is my starting template and pipeline for most deep learning projects 🔥
Built with PyTorch Lightning, Hydra and Weights&Biases.
+It's supposed to be enchancement/expansion on original [deep-learninig-project-template](https://github.com/PyTorchLightning/deep-learning-project-template) repository.
+The goal is to: +- structure ML code the same so that work can easily be extended and replicated +- allow for quick and efficient experimentation process thanks to automating pipeline with config files +- extend functionality of popular experiment loggers like Weights&Biases + Click on `Use this template` button above to initialize new repository.
## Features @@ -16,14 +22,14 @@ Click on `Use this template` button above to initialize new repository. @@ -77,7 +83,7 @@ The directory structure of new project looks like this: # specify here default training configuration defaults: - trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - - model: simple_mnist_classifier.yaml # choose model from 'configs/model/' folder + - model: mnist_model.yaml # choose model from 'configs/model/' folder - datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder - optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - seeds: default_seeds.yaml # set this to null if you don't want to use seeds @@ -121,7 +127,7 @@ Example experiment configuration: ```yaml defaults: - override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' - - override /model: simple_mnist_classifier.yaml # choose model from 'configs/model/' + - override /model: mnist_model.yaml # choose model from 'configs/model/' - override /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' diff --git a/project/configs/config.yaml b/project/configs/config.yaml index ef1deac..86b95d4 100644 --- a/project/configs/config.yaml +++ b/project/configs/config.yaml @@ -2,7 +2,7 @@ defaults: - trainer: default_trainer.yaml - - model: simple_mnist_classifier.yaml + - model: mnist_model.yaml - datamodule: mnist_datamodule.yaml - optimizer: adam.yaml - seeds: default_seeds.yaml # set this to null if you don't want to use seeds diff --git a/project/configs/experiment/exp_example_advanced.yaml b/project/configs/experiment/exp_example_advanced.yaml index adab2d1..2cc6a6d 100644 --- a/project/configs/experiment/exp_example_advanced.yaml +++ b/project/configs/experiment/exp_example_advanced.yaml @@ -5,7 +5,7 @@ defaults: - override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - - override /model: simple_mnist_classifier.yaml # choose model from 'configs/model/' folder + - override /model: mnist_model.yaml # choose model from 'configs/model/' folder - override /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder diff --git a/project/configs/experiment/exp_example_simple.yaml b/project/configs/experiment/exp_example_simple.yaml index 9f3d6ea..2afce96 100644 --- a/project/configs/experiment/exp_example_simple.yaml +++ b/project/configs/experiment/exp_example_simple.yaml @@ -5,7 +5,7 @@ defaults: - override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - - override /model: simple_mnist_classifier.yaml # choose model from 'configs/model/' folder + - override /model: mnist_model.yaml # choose model from 'configs/model/' folder - override /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder diff --git a/project/configs/model/simple_mnist_classifier.yaml b/project/configs/model/mnist_model.yaml similarity index 100% rename from project/configs/model/simple_mnist_classifier.yaml rename to project/configs/model/mnist_model.yaml diff --git a/project/src/models/simple_mnist_classifier.py b/project/src/models/mnist_model.py similarity index 100% rename from project/src/models/simple_mnist_classifier.py rename to project/src/models/mnist_model.py diff --git a/project/src/utils/inference_example.py b/project/src/utils/inference_example.py index fbee106..206a82e 100644 --- a/project/src/utils/inference_example.py +++ b/project/src/utils/inference_example.py @@ -2,7 +2,7 @@ from src.transforms import mnist_transforms from PIL import Image # the LitModel you import should be the same as the one you used for training! -from src.models.simple_mnist_classifier import LitModel +from src.models.mnist_model import LitModel # ckpt can be a url! @@ -12,7 +12,7 @@ def predict(): This method is example of inference with a trained model. It Loads trained image classification model from checkpoint. Then it loads example image and predicts its label. - Model used in simple_mnist_classifier.py should be the same as during training!!! + Model used in mnist_model.py should be the same as during training!!! """ CKPT_PATH = "epoch=0.ckpt" From 5ff284ec42baccf2913bd961ecfe2d702350679f Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Thu, 14 Jan 2021 20:04:54 +0100 Subject: [PATCH 3/3] small improvements --- README.md | 11 +++--- .../configs/callbacks/mnist_callbacks.yaml | 12 ++++++ .../configs/callbacks/wandb_callbacks.yaml | 37 ++++++++++--------- project/configs/config.yaml | 7 +--- project/configs/model/mnist_model.yaml | 3 -- project/configs/trainer/debug_trainer.yaml | 6 ++- project/configs/trainer/default_trainer.yaml | 6 ++- 7 files changed, 48 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 10950f6..fd2de5d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ ## Deep learning project template -This is my starting template and pipeline for most deep learning projects 🔥
+This is my starting template and pipeline for most deep learning projects.
Built with PyTorch Lightning, Hydra and Weights&Biases.
It's supposed to be enchancement/expansion on original [deep-learninig-project-template](https://github.com/PyTorchLightning/deep-learning-project-template) repository.
+ The goal is to: - structure ML code the same so that work can easily be extended and replicated - allow for quick and efficient experimentation process thanks to automating pipeline with config files @@ -72,7 +73,7 @@ The directory structure of new project looks like this: ├── LICENSE <- Project license ├── README.md <- Readme ├── conda_env.yaml <- File for installing conda environment -├── requirements.txt <- List of python dependencies +├── requirements.txt <- File for installing python dependencies └── setup.py <- File for installing project as a package ```
@@ -83,7 +84,7 @@ The directory structure of new project looks like this: # specify here default training configuration defaults: - trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - - model: mnist_model.yaml # choose model from 'configs/model/' folder + - model: mnist_model.yaml # choose model from 'configs/model/' folder - datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder - optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - seeds: default_seeds.yaml # set this to null if you don't want to use seeds @@ -127,7 +128,7 @@ Example experiment configuration: ```yaml defaults: - override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' - - override /model: mnist_model.yaml # choose model from 'configs/model/' + - override /model: mnist_model.yaml # choose model from 'configs/model/' - override /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' @@ -186,7 +187,7 @@ Logs are created automatically with the following structure: │ │ └── ... │ │ │ ├── multiruns # Folder for logs generated from sweeps -│ │ ├── 2021-02-15_16-50-49 # Date and hour of executing run +│ │ ├── 2021-02-15_16-50-49 # Date and hour of executing sweep │ │ │ ├── 0 # Job number │ │ │ │ ├── .hydra # Hydra logs │ │ │ │ ├── wandb # Weights&Biases logs diff --git a/project/configs/callbacks/mnist_callbacks.yaml b/project/configs/callbacks/mnist_callbacks.yaml index 34e7b77..9060e0f 100644 --- a/project/configs/callbacks/mnist_callbacks.yaml +++ b/project/configs/callbacks/mnist_callbacks.yaml @@ -3,3 +3,15 @@ defaults: - default_callbacks.yaml - wandb_callbacks.yaml + + +save_confusion_matrix_to_wandb: + class: src.callbacks.wandb_callbacks.SaveConfusionMatrixToWandb + args: + class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + + +save_f1_precision_recall_heatmap_to_wandb: + class: src.callbacks.wandb_callbacks.SaveMetricsHeatmapToWandb + args: + class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] diff --git a/project/configs/callbacks/wandb_callbacks.yaml b/project/configs/callbacks/wandb_callbacks.yaml index d9fbf88..6675c4b 100644 --- a/project/configs/callbacks/wandb_callbacks.yaml +++ b/project/configs/callbacks/wandb_callbacks.yaml @@ -1,31 +1,34 @@ # @package _group_ +defaults: + - default_callbacks.yaml + + save_best_metric_scores_to_wandb: class: src.callbacks.wandb_callbacks.SaveBestMetricScoresToWandb args: {} +upload_code_to_wandb_as_artifact: + class: src.callbacks.wandb_callbacks.SaveCodeToWandb + args: + code_dir: ${work_dir}/ + + +upload_ckpts_to_wandb_as_artifact: + class: src.callbacks.wandb_callbacks.UploadAllCheckpointsToWandb + args: + ckpt_dir: "checkpoints/" + upload_best_only: False + + #save_confusion_matrix_to_wandb: # class: src.callbacks.wandb_callbacks.SaveConfusionMatrixToWandb # args: # class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] -# -# + + #save_f1_precision_recall_heatmap_to_wandb: # class: src.callbacks.wandb_callbacks.SaveMetricsHeatmapToWandb # args: -# class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] - - -#upload_code_to_wandb_as_artifact: -# class: src.callbacks.wandb_callbacks.SaveCodeToWandb -# args: -# code_dir: ${work_dir}/ - - -#upload_ckpts_to_wandb_as_artifact: -# class: src.callbacks.wandb_callbacks.UploadAllCheckpointsToWandb -# args: -# ckpt_dir: "checkpoints/" -# upload_best_only: True - +# class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] \ No newline at end of file diff --git a/project/configs/config.yaml b/project/configs/config.yaml index 86b95d4..f9a3122 100644 --- a/project/configs/config.yaml +++ b/project/configs/config.yaml @@ -9,8 +9,8 @@ defaults: - callbacks: default_callbacks.yaml # set this to null if you don't want to use callbacks - logger: null # set logger here or use command line (e.g. `python train.py logger=wandb`) - - override hydra/job_logging: colorlog - - override hydra/hydra_logging: colorlog +# - override hydra/job_logging: colorlog +# - override hydra/hydra_logging: colorlog # path to working directory (the directory that `train.py` was executed from in command line) work_dir: ${hydra:runtime.cwd} @@ -29,13 +29,10 @@ hydra: # extra things that are logged by all loggers as hyperparameters extra_logs: log_seeds: True - log_trainer_args: False log_datamodule_args: True - log_model_class: True log_optimizer_class: True log_datamodule_class: True log_model_architecture_class: True - log_train_val_test_sizes: False diff --git a/project/configs/model/mnist_model.yaml b/project/configs/model/mnist_model.yaml index 4d8f282..c276460 100644 --- a/project/configs/model/mnist_model.yaml +++ b/project/configs/model/mnist_model.yaml @@ -2,11 +2,8 @@ class: src.models.simple_mnist_classifier.LitModel args: - lr: 0.001 - weight_decay: 0.00001 input_size: 784 output_size: 10 lin1_size: 256 lin2_size: 256 lin3_size: 128 - diff --git a/project/configs/trainer/debug_trainer.yaml b/project/configs/trainer/debug_trainer.yaml index 8cfe1ed..c200895 100644 --- a/project/configs/trainer/debug_trainer.yaml +++ b/project/configs/trainer/debug_trainer.yaml @@ -2,8 +2,10 @@ # trainer args for debugging model args: - gpus: -1 # set -1 to train on all GPUs abailable, set 0 to train on CPU only - auto_select_gpus: True + gpus: 0 # set -1 to train on all GPUs abailable, set 0 to train on CPU only +# auto_select_gpus: True + + gradient_clip_val: 0.5 # fast_dev_run: True # bugged :( limit_train_batches: 1.0 diff --git a/project/configs/trainer/default_trainer.yaml b/project/configs/trainer/default_trainer.yaml index 80a2f39..257a95b 100644 --- a/project/configs/trainer/default_trainer.yaml +++ b/project/configs/trainer/default_trainer.yaml @@ -3,9 +3,11 @@ # default args passed to Lightning Trainer on every experiment # they can be combined or overwritten with args set in experiment config args: - gpus: -1 # set -1 to train on all GPUs available, set 0 to train on CPU only - auto_select_gpus: True + gpus: 0 # set -1 to train on all GPUs available, set 0 to train on CPU only +# auto_select_gpus: True + max_epochs: 10 + gradient_clip_val: 0.5 num_sanity_val_steps: 3 progress_bar_refresh_rate: 10 weights_summary: null # null in yaml represents python None value