Merge pull request #56 from hobogalaxy/version-4.0

Version 4.0
This commit is contained in:
Lucas authored and GitHub committed 2021-01-14 20:05:52 +01:00
commit b0dc25efc4
13 files changed
+76 -54

No files matched your search

+28 -18
View File
@@ -1,26 +1,36 @@
## Deep learning project template ## Deep learning project template
A convenient starting template and pipeline for deep learning projects. Built with <b>PyTorch Lightning</b>, <b>Hydra</b> and <b>Weights&Biases</b>.<br> This is my starting template and pipeline for most deep learning projects.<br>
Click on <b>"Use this template"</b> button above to initialize new repository.<br> Built with <b>PyTorch Lightning</b>, <b>Hydra</b> and <b>Weights&Biases</b>.<br>
It's supposed to be enchancement/expansion on original [deep-learninig-project-template](https://github.com/PyTorchLightning/deep-learning-project-template) repository.<br>
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 <b>`Use this template`</b> button above to initialize new repository.<br>
## Features ## Features
- Predefined folder structure - Predefined folder structure
- Automates training with PyTorch Lightning - you only need to create model and datamodule and specify them in configuration files - Automates training with PyTorch Lightning
- All advantages of Hydra: - 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)) - 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 - Composing configuration files out of other configuration files
- Scheduling execution of many experiments - Scheduling execution of many experiments
- Overriding any config parameter from command line - Overriding any config parameter from command line
- Command line tab completion - Command line tab completion
- Logging history of all executed runs - Logging history of all executed runs/experiments
- Weights&Biases integration: - Weights&Biases integration
- Automatically stores all code files, configs and model checkpoints in Weights&Biases cloud - 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))
- Useful callbacks ([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) - ~~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 requirements ([requirements.txt](requirements.txt))
- Built in conda environment initialization ([conda_env.yaml](conda_env.yaml)) - Built in conda environment initialization ([conda_env.yaml](conda_env.yaml))
- Built in package setup ([setup.py](setup.py)) - Built in python package setup ([setup.py](setup.py))
- Example with MNIST digits classification - Example with MNIST digits classification ([mnist_model](project/src/models/mnist_model.py), [mnist_datamodule](project/src/datamodules/mnist_datamodule.py))
<br> <br>
@@ -57,13 +67,13 @@ The directory structure of new project looks like this:
│ │ ├── inference_example.py <- Example of inference with trained model │ │ ├── inference_example.py <- Example of inference with trained model
│ │ └── initializers.py <- Initializers for different modules │ │ └── 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 ├── .gitignore <- Ignored files
├── LICENSE <- Project license ├── LICENSE <- Project license
├── README.md <- Readme ├── README.md <- Readme
├── conda_env.yaml <- File for installing conda environment ├── 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 └── setup.py <- File for installing project as a package
``` ```
<br> <br>
@@ -74,7 +84,7 @@ The directory structure of new project looks like this:
# specify here default training configuration # specify here default training configuration
defaults: defaults:
- trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - 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 - datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder
- optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' 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 - seeds: default_seeds.yaml # set this to null if you don't want to use seeds
@@ -118,7 +128,7 @@ Example experiment configuration:
```yaml ```yaml
defaults: defaults:
- override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' - 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 /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/'
- override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/'
- override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/'
@@ -177,7 +187,7 @@ Logs are created automatically with the following structure:
│ │ └── ... │ │ └── ...
│ │ │ │
│ ├── multiruns # Folder for logs generated from sweeps │ ├── 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 │ │ │ ├── 0 # Job number
│ │ │ │ ├── .hydra # Hydra logs │ │ │ │ ├── .hydra # Hydra logs
│ │ │ │ ├── wandb # Weights&Biases logs │ │ │ │ ├── wandb # Weights&Biases logs
@@ -195,8 +205,8 @@ Logs are created automatically with the following structure:
## Workflow ## Workflow
1. Create PyTorch Lightning model 1. Create PyTorch Lightning model
2. Create PyTorch Lightning datamodule 2. Create PyTorch Lightning datamodule
3. Create new experiment config in [configs/experiment](project/configs/experiment) 3. Create new experiment config in [configs/experiment](project/configs/experiment) folder
4. Run training with chosen run config<br> 4. Run training with chosen experiment config<br>
```bash ```bash
python train.py +experiment=experiment_name.yaml python train.py +experiment=experiment_name.yaml
``` ```
@@ -3,3 +3,15 @@
defaults: defaults:
- default_callbacks.yaml - default_callbacks.yaml
- wandb_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']
+19 -16
View File
@@ -1,31 +1,34 @@
# @package _group_ # @package _group_
defaults:
- default_callbacks.yaml
save_best_metric_scores_to_wandb: save_best_metric_scores_to_wandb:
class: src.callbacks.wandb_callbacks.SaveBestMetricScoresToWandb class: src.callbacks.wandb_callbacks.SaveBestMetricScoresToWandb
args: {} 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: #save_confusion_matrix_to_wandb:
# class: src.callbacks.wandb_callbacks.SaveConfusionMatrixToWandb # class: src.callbacks.wandb_callbacks.SaveConfusionMatrixToWandb
# args: # args:
# class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
#
#
#save_f1_precision_recall_heatmap_to_wandb: #save_f1_precision_recall_heatmap_to_wandb:
# class: src.callbacks.wandb_callbacks.SaveMetricsHeatmapToWandb # class: src.callbacks.wandb_callbacks.SaveMetricsHeatmapToWandb
# args: # args:
# class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # 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
+3 -6
View File
@@ -2,15 +2,15 @@
defaults: defaults:
- trainer: default_trainer.yaml - trainer: default_trainer.yaml
- model: simple_mnist_classifier.yaml - model: mnist_model.yaml
- datamodule: mnist_datamodule.yaml - datamodule: mnist_datamodule.yaml
- optimizer: adam.yaml - optimizer: adam.yaml
- seeds: default_seeds.yaml # set this to null if you don't want to use seeds - seeds: default_seeds.yaml # set this to null if you don't want to use seeds
- callbacks: default_callbacks.yaml # set this to null if you don't want to use callbacks - 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`) - logger: null # set logger here or use command line (e.g. `python train.py logger=wandb`)
- override hydra/job_logging: colorlog # - override hydra/job_logging: colorlog
- override hydra/hydra_logging: colorlog # - override hydra/hydra_logging: colorlog
# path to working directory (the directory that `train.py` was executed from in command line) # path to working directory (the directory that `train.py` was executed from in command line)
work_dir: ${hydra:runtime.cwd} work_dir: ${hydra:runtime.cwd}
@@ -29,13 +29,10 @@ hydra:
# extra things that are logged by all loggers as hyperparameters # extra things that are logged by all loggers as hyperparameters
extra_logs: extra_logs:
log_seeds: True log_seeds: True
log_trainer_args: False log_trainer_args: False
log_datamodule_args: True log_datamodule_args: True
log_model_class: True log_model_class: True
log_optimizer_class: True log_optimizer_class: True
log_datamodule_class: True log_datamodule_class: True
log_model_architecture_class: True log_model_architecture_class: True
log_train_val_test_sizes: False log_train_val_test_sizes: False
@@ -5,7 +5,7 @@
defaults: defaults:
- override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - 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 /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder
- override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder
- override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder
@@ -5,7 +5,7 @@
defaults: defaults:
- override /trainer: default_trainer.yaml # choose trainer from 'configs/trainer/' folder - 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 /datamodule: mnist_datamodule.yaml # choose datamodule from 'configs/datamodule/' folder
- override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder - override /optimizer: adam.yaml # choose optimizer from 'configs/optimizer/' folder
- override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder - override /seeds: default_seeds.yaml # choose seeds from 'configs/seeds/' folder
+2 -2
View File
@@ -1,8 +1,8 @@
# @package _group_ # @package _group_
# Train with all logger at once # Train with many loggers at once
defaults: defaults:
- csv_logger.yaml - csv_logger.yaml
- wandb.yaml - wandb.yaml
# - comet.yaml # - comet.yaml
# - tensorboard.yaml # - tensorboard.yaml
@@ -2,11 +2,8 @@
class: src.models.simple_mnist_classifier.LitModel class: src.models.simple_mnist_classifier.LitModel
args: args:
lr: 0.001
weight_decay: 0.00001
input_size: 784 input_size: 784
output_size: 10 output_size: 10
lin1_size: 256 lin1_size: 256
lin2_size: 256 lin2_size: 256
lin3_size: 128 lin3_size: 128
+4 -2
View File
@@ -2,8 +2,10 @@
# trainer args for debugging model # trainer args for debugging model
args: args:
gpus: -1 # set -1 to train on all GPUs abailable, set 0 to train on CPU only gpus: 0 # set -1 to train on all GPUs abailable, set 0 to train on CPU only
auto_select_gpus: True # auto_select_gpus: True
gradient_clip_val: 0.5
# fast_dev_run: True # bugged :( # fast_dev_run: True # bugged :(
limit_train_batches: 1.0 limit_train_batches: 1.0
+4 -2
View File
@@ -3,9 +3,11 @@
# default args passed to Lightning Trainer on every experiment # default args passed to Lightning Trainer on every experiment
# they can be combined or overwritten with args set in experiment config # they can be combined or overwritten with args set in experiment config
args: args:
gpus: -1 # set -1 to train on all GPUs available, set 0 to train on CPU only gpus: 0 # set -1 to train on all GPUs available, set 0 to train on CPU only
auto_select_gpus: True # auto_select_gpus: True
max_epochs: 10 max_epochs: 10
gradient_clip_val: 0.5
num_sanity_val_steps: 3 num_sanity_val_steps: 3
progress_bar_refresh_rate: 10 progress_bar_refresh_rate: 10
weights_summary: null # null in yaml represents python None value weights_summary: null # null in yaml represents python None value
+2 -2
View File
@@ -2,7 +2,7 @@ from src.transforms import mnist_transforms
from PIL import Image from PIL import Image
# the LitModel you import should be the same as the one you used for training! # 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! # ckpt can be a url!
@@ -12,7 +12,7 @@ def predict():
This method is example of inference with a trained model. This method is example of inference with a trained model.
It Loads trained image classification model from checkpoint. It Loads trained image classification model from checkpoint.
Then it loads example image and predicts its label. 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" CKPT_PATH = "epoch=0.ckpt"
-1
View File
@@ -16,7 +16,6 @@ import src.utils.initializers as utils
def train(config): def train(config):
# Set global PyTorch seed # Set global PyTorch seed
if "seeds" in config and "pytorch_seed" in config["seeds"]: if "seeds" in config and "pytorch_seed" in config["seeds"]:
torch.manual_seed(seed=config["seeds"]["pytorch_seed"]) torch.manual_seed(seed=config["seeds"]["pytorch_seed"])