This commit is contained in:
hobogalaxy committed 2021-03-05 16:28:55 +01:00
commit 497515e40d
1 file changed
+81 -74
+81 -74
View File
@@ -6,95 +6,64 @@ Click on <b>`Use this template`</b> button above to initialize new repository.
This template tries to be as generic as possible. You should be able to easily modify behavior in [train.py](train.py) in case you need some unconventional configuration wiring. This template tries to be as generic as possible. You should be able to easily modify behavior in [train.py](train.py) in case you need some unconventional configuration wiring.
*This is work in progress. I'm currently figuring out the best workflow for scalable experimentation process. Suggestions are always welcome!*
</div> </div>
<br> <br>
## Contents ## Contents
- [PyTorch Lightning + Hydra Template](#pytorch-lightning--hydra-template) - [PyTorch Lightning + Hydra Template](#pytorch-lightning--hydra-template)
- [Contents](#contents) - [Why Lightning + Hydra?](#why-lightning--hydra)
- [Main Ideas](#main-ideas) - [Main Ideas](#main-ideas)
- [Some Notes](#some-notes) - [Some Notes](#some-notes)
- [Why Lightning + Hydra?](#why-lightning--hydra)
- [Features](#features)
- [Project Structure](#project-structure) - [Project Structure](#project-structure)
- [Workflow](#workflow) - [Features](#features)
- [Main Project Configuration](#main-project-configuration) - [Main Project Configuration](#main-project-configuration)
- [Experiment Configuration](#experiment-configuration) - [Experiment Configuration](#experiment-configuration)
- [Workflow](#workflow)
- [Logs](#logs) - [Logs](#logs)
- [Experiment Tracking](#experiment-tracking) - [Experiment Tracking](#experiment-tracking)
- [Tests](#tests)
- [Distributed Training](#distributed-training) - [Distributed Training](#distributed-training)
- [Tricks](#tricks) - [Tricks](#tricks)
- [Your Project Name](#your-project-name)
- [Description](#description)
- [How to run](#how-to-run) - [How to run](#how-to-run)
- [Installing project as a package](#installing-project-as-a-package) - [Installing project as a package](#installing-project-as-a-package)
<br> <br>
## Main Ideas
- Predefined Structure: clean and scalable so that work can easily be extended and replicated (see [#Project Structure](#project-structure))
- Modularity: all abstractions are splitted into different submodules
- Rapid Experimentation: thanks to automating pipeline with config files and hydra command line superpowers
- Little Boilerplate: so pipeline can be easily modified (see [train.py](train.py))
- Main Configuration: main config file specifies default training configuration (see [#Main Project Configuration](#main-project-configuration))
- Experiment Configurations: stored in a separate folder, they can be composed out of smaller configs, override chosen parameters or define everything from scratch (see [#Experiment Configuration](#experiment-configuration))
- Experiment Tracking: most logging frameworks can be easily integrated! (see [#Experiment Tracking](#experiment-tracking))
- Tests: simple bash scripts to check if your model doesn't crash under different training conditions (see [tests/](tests/))
- Logs: all logs (checkpoints, data from loggers, chosen hparams, etc.) are stored in a convenient folder structure imposed by Hydra (see [#Logs](#logs))
- Hyperparameter Search: made easier with Hydra built in plugins like [Optuna Sweeper](https://hydra.cc/docs/next/plugins/optuna_sweeper)
- Workflow: comes down to 4 simple steps (see [#Workflow](#workflow))
<br>
## Some Notes
- ***Warning: this template currently uses development version of hydra which might be unstable (we wait until Hydra 1.1 is released).*** <br>
- *Based on:
[deep-learninig-project-template](https://github.com/PyTorchLightning/deep-learning-project-template),
[cookiecutter-data-science](https://github.com/drivendata/cookiecutter-data-science),
[hydra-torch](https://github.com/pytorch/hydra-torch),
[hydra-lightning](https://github.com/romesco/hydra-lightning),
[lightning-hydra-seed](https://github.com/tchaton/lightning-hydra-seed),
[pytorch_tempest](https://github.com/Erlemar/pytorch_tempest),
[pytorch-project-template](https://github.com/ryul99/pytorch-project-template).*<br>
- *To learn how to configure PyTorch with Hydra take a look at [this detailed MNIST tutorial](https://github.com/pytorch/hydra-torch/blob/master/examples/mnist_00.md).*
- *Suggestions are always welcome!*
<br>
## Why Lightning + Hydra? ## Why Lightning + Hydra?
- <b>[PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning)</b> provides great abstractions for well structured ML code and advanced features like checkpointing, gradient accumulation, distributed training, etc. - <b>[PyTorch Lightning](https://github.com/PyTorchLightning/pytorch-lightning)</b> provides great abstractions for well structured ML code and advanced features like checkpointing, gradient accumulation, distributed training, etc.
- <b>[Hydra](https://github.com/facebookresearch/hydra)</b> provides convenient way to manage experiment configurations and advanced features like overriding any config parameter from command line, scheduling execution of many runs, etc. - <b>[Hydra](https://github.com/facebookresearch/hydra)</b> provides convenient way to manage experiment configurations and advanced features like overriding any config parameter from command line, scheduling execution of many runs, etc.
<br> <br>
## Features ## Main Ideas
- Hydra superpowers - Predefined Structure: clean and scalable so that work can easily be extended and replicated (see [#Project Structure](#project-structure))
- Override any config parameter from command line - Rapid Experimentation: thanks to automating pipeline with config files and hydra command line superpowers
- Easily switch between different loggers, callbacks sets, optimizers, etc. from command line - Little Boilerplate: so pipeline can be easily modified (see [train.py](train.py))
- Sweep over hyperparameters from command line - Main Configuration: main config file specifies default training configuration (see [#Main Project Configuration](#main-project-configuration))
- Automatic logging of run history - Experiment Configurations: stored in a separate folder, they can be composed out of smaller configs, override chosen parameters or define everything from scratch (see [#Experiment Configuration](#experiment-configuration))
- Sweeper integrations for Optuna, Ray and others - Experiment Tracking: many logging frameworks can be easily integrated! (see [#Experiment Tracking](#experiment-tracking))
- Optional callbacks for Weigths&Biases ([wandb_callbacks.py](src/callbacks/wandb_callbacks.py)) - Logs: all logs (checkpoints, data from loggers, chosen hparams, etc.) are stored in a convenient folder structure imposed by Hydra (see [#Logs](#logs))
- To support reproducibility: - Smoke Tests: simple bash scripts running 1-2 epoch experiments to check if your model doesn't crash under different conditions (see [tests](tests/))
- UploadCodeToWandbAsArtifact - Hyperparameter Search: made easier with Hydra built in plugins like [Optuna Sweeper](https://hydra.cc/docs/next/plugins/optuna_sweeper)
- UploadCheckpointsToWandbAsArtifact - Workflow: comes down to 4 simple steps (see [#Workflow](#workflow))
- WatchModelWithWandb <br>
- To provide examples of logging custom visualisations and metrics with callbacks:
- LogBestMetricScoresToWandb
- LogF1PrecisionRecallHeatmapToWandb ## Some Notes
- LogConfusionMatrixToWandb - ***Warning: this template currently uses development version of hydra which might be unstable (we wait until Hydra 1.1 is released).*** <br>
- ~~Validating correctness of config with Hydra schemas~~ (TODO) - *Inspired by:
- Method to pretty print configuration composed by Hydra at the start of the run, using [Rich](https://github.com/willmcgugan/rich/) library ([template_utils.py](src/utils/template_utils.py)) [PyTorchLightning/deep-learninig-project-template](https://github.com/PyTorchLightning/deep-learning-project-template),
- Method to log chosen parts of Hydra config to all loggers ([template_utils.py](src/utils/template_utils.py)) [drivendata/cookiecutter-data-science](https://github.com/drivendata/cookiecutter-data-science),
- Example of hyperparameter search with Optuna sweeps ([config_optuna.yaml](configs/config_optuna.yaml)) [tchaton/lightning-hydra-seed](https://github.com/tchaton/lightning-hydra-seed),
- ~~Example of hyperparameter search with Weights&Biases sweeps~~ (TODO) [Erlemar/pytorch_tempest](https://github.com/Erlemar/pytorch_tempest),
- Examples of simple bash scripts to check if your model doesn't crash under different training conditions ([tests/](tests/)) [ryul99/pytorch-project-template](https://github.com/ryul99/pytorch-project-template).*
- Example of inference with trained model ([inference_example.py](src/utils/inference_example.py)) - *To learn how to configure PyTorch with Hydra take a look at [this detailed MNIST tutorial](https://github.com/pytorch/hydra-torch/blob/master/examples/mnist_00.md).*
- Built in requirements ([requirements.txt](requirements.txt)) - *Repositories useful for configuring PyTorch and PyTorch Lightning classes with Hydra:
- Built in conda environment initialization ([conda_env_gpu.yaml](conda_env_gpu.yaml), [conda_env_cpu.yaml](conda_env_cpu.yaml)) [romesco/hydra-lightning](https://github.com/romesco/hydra-lightning),
- Built in python package setup ([setup.py](setup.py)) [pytorch/hydra-torch](https://github.com/pytorch/hydra-torch).*
- Example with MNIST classification ([mnist_model.py](src/models/mnist_model.py), [mnist_datamodule.py](src/datamodules/mnist_datamodule.py)) - *Suggestions are always welcome!*
<br> <br>
@@ -147,14 +116,33 @@ The directory structure of new project looks like this:
<br> <br>
## Workflow ## Features
1. Write your PyTorch Lightning model (see [mnist_model.py](project/src/models/mnist_model.py) for example) - Hydra superpowers
2. Write your PyTorch Lightning datamodule (see [mnist_datamodule.py](project/src/datamodules/mnist_datamodule.py) for example) - Override any config parameter from command line
3. Write your experiment config, containing paths to your model and datamodule (see [project/configs/experiment](project/configs/experiment) for examples) - Easily switch between different loggers, callbacks sets, optimizers, etc. from command line
4. Run training with chosen experiment config:<br> - Sweep over hyperparameters from command line
```bash - Automatic logging of run history
python train.py +experiment=experiment_name.yaml - Sweeper integrations for Optuna, Ray and others
``` - Optional callbacks for Weigths&Biases ([wandb_callbacks.py](src/callbacks/wandb_callbacks.py))
- To support reproducibility:
- UploadCodeToWandbAsArtifact
- UploadCheckpointsToWandbAsArtifact
- WatchModelWithWandb
- To provide examples of logging custom visualisations and metrics with callbacks:
- LogBestMetricScoresToWandb
- LogF1PrecisionRecallHeatmapToWandb
- LogConfusionMatrixToWandb
- ~~Validating correctness of config with Hydra schemas~~ (TODO)
- Method to pretty print configuration composed by Hydra at the start of the run, using [Rich](https://github.com/willmcgugan/rich/) library ([template_utils.py](src/utils/template_utils.py))
- Method to log chosen parts of Hydra config to all loggers ([template_utils.py](src/utils/template_utils.py))
- Example of hyperparameter search with Optuna sweeps ([config_optuna.yaml](configs/config_optuna.yaml))
- ~~Example of hyperparameter search with Weights&Biases sweeps~~ (TODO)
- Examples of simple bash scripts to check if your model doesn't crash under different training conditions ([tests/](tests/))
- Example of inference with trained model ([inference_example.py](src/utils/inference_example.py))
- Built in requirements ([requirements.txt](requirements.txt))
- Built in conda environment initialization ([conda_env_gpu.yaml](conda_env_gpu.yaml), [conda_env_cpu.yaml](conda_env_cpu.yaml))
- Built in python package setup ([setup.py](setup.py))
- Example with MNIST classification ([mnist_model.py](src/models/mnist_model.py), [mnist_datamodule.py](src/datamodules/mnist_datamodule.py))
<br> <br>
@@ -294,6 +282,17 @@ logger:
<br> <br>
## Workflow
1. Write your PyTorch Lightning model (see [mnist_model.py](project/src/models/mnist_model.py) for example)
2. Write your PyTorch Lightning datamodule (see [mnist_datamodule.py](project/src/datamodules/mnist_datamodule.py) for example)
3. Write your experiment config, containing paths to your model and datamodule (see [project/configs/experiment](project/configs/experiment) for examples)
4. Run training with chosen experiment config:<br>
```bash
python train.py +experiment=experiment_name.yaml
```
<br>
## Logs ## Logs
Hydra creates new working directory for every executed run. <br> Hydra creates new working directory for every executed run. <br>
By default, logs have the following structure: By default, logs have the following structure:
@@ -331,12 +330,19 @@ You can change this structure by modifying paths in [config.yaml](configs/config
## Experiment Tracking ## Experiment Tracking
PyTorch Lightning provides built in loggers for Weights&Biases, Neptune, Comet, MLFlow, Tensorboard, TestTube and CSV. To use one of them, simply add its configuration to [configs/logger/](configs/logger/) and run:<br> PyTorch Lightning provides built in loggers for Weights&Biases, Neptune, Comet, MLFlow, Tensorboard and CSV. To use one of them, simply add its config to [configs/logger](configs/logger) and run:
`python train.py logger=logger_config.yaml` <br> ```
python train.py logger=logger_config.yaml
```
You can use many of them at once (see [configs/logger/many_loggers.yaml](configs/logger/many_loggers.yaml) for example). You can use many of them at once (see [configs/logger/many_loggers.yaml](configs/logger/many_loggers.yaml) for example).
<br><br> <br><br>
## Tests
(TODO)
<br><br>
## Distributed Training ## Distributed Training
(TODO) (TODO)
<br><br> <br><br>
@@ -455,6 +461,7 @@ Resume from checkpoint:
# use quotes '' around argument or otherwise $ symbol breaks it # use quotes '' around argument or otherwise $ symbol breaks it
python train.py '+trainer.resume_from_checkpoint=${work_dir}/logs/runs/2021-02-28/16-50-49/checkpoints/last.ckpt' python train.py '+trainer.resume_from_checkpoint=${work_dir}/logs/runs/2021-02-28/16-50-49/checkpoints/last.ckpt'
``` ```
<br>
## Installing project as a package ## Installing project as a package