add utils folder
This commit is contained in:
1 parent
94eaaa5072
commit
b9af36fe28
6 files changed
+45
-337
No files matched your search
@@ -1,289 +0,0 @@
|
|||||||
## 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>
|
|
||||||
Click on <b>"Use this template"</b> button above to initialize new repository.<br>
|
|
||||||
|
|
||||||
## 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:
|
|
||||||
- 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))
|
|
||||||
- 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))
|
|
||||||
- ~~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
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
## Project structure
|
|
||||||
The directory structure of new project looks like this:
|
|
||||||
```
|
|
||||||
├── project
|
|
||||||
│ ├── configs <- Hydra configuration files
|
|
||||||
│ │ ├── trainer <- Configurations of lightning trainers
|
|
||||||
│ │ ├── model <- Configurations of lightning models
|
|
||||||
│ │ ├── datamodule <- Configurations of lightning datamodules
|
|
||||||
│ │ ├── callbacks <- Configurations of lightning callbacks
|
|
||||||
│ │ ├── logger <- Configurations of lightning loggers
|
|
||||||
│ │ ├── optimizer <- Configurations of optimizers
|
|
||||||
│ │ ├── seeds <- Configurations of seeds
|
|
||||||
│ │ ├── experiment <- Configurations of experiments
|
|
||||||
│ │ │
|
|
||||||
│ │ └── config.yaml <- Main project configuration file
|
|
||||||
│ │
|
|
||||||
│ ├── data <- Project data
|
|
||||||
│ │
|
|
||||||
│ ├── logs <- Logs generated by hydra and pytorch lightning loggers
|
|
||||||
│ │
|
|
||||||
│ ├── notebooks <- Jupyter notebooks
|
|
||||||
│ │
|
|
||||||
│ ├── template_utils <- Different project utilities
|
|
||||||
│ │ ├── execute_sweep.py <- Special file for executing Weights&Biases sweeps
|
|
||||||
│ │ ├── inference_example.py <- Example of inference with trained model
|
|
||||||
│ │ └── initializers.py <- Initializers for different modules
|
|
||||||
│ │
|
|
||||||
│ ├── src
|
|
||||||
│ │ ├── architectures <- PyTorch model architectures
|
|
||||||
│ │ ├── callbacks <- PyTorch Lightning callbacks
|
|
||||||
│ │ ├── datamodules <- PyTorch Lightning datamodules
|
|
||||||
│ │ ├── datasets <- PyTorch datasets
|
|
||||||
│ │ ├── models <- PyTorch Lightning models
|
|
||||||
│ │ ├── transforms <- Data transformations
|
|
||||||
│ │ └── utils <- Utility scripts
|
|
||||||
│ │
|
|
||||||
│ └── train.py <- Train model with chosen run configuration
|
|
||||||
│
|
|
||||||
├── .gitignore <- Ignored files
|
|
||||||
├── LICENSE <- Project license
|
|
||||||
├── README.md <- Readme
|
|
||||||
├── conda_env.yaml <- File for installing conda environment
|
|
||||||
├── requirements.txt <- List of python dependencies
|
|
||||||
└── setup.py <- File for installing project as a package
|
|
||||||
```
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
## Main project configuration file ([config.yaml](project/configs/config.yaml))
|
|
||||||
```yaml
|
|
||||||
# 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
|
|
||||||
- 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
|
|
||||||
- callbacks: default_callbacks.yaml # set this to null if you don't want to use callbacks
|
|
||||||
- logger: null # set this to null if you don't want to use loggers
|
|
||||||
|
|
||||||
# path to working directory (the directory that `train.py` was executed from in command line)
|
|
||||||
work_dir: ${hydra:runtime.cwd}
|
|
||||||
|
|
||||||
# path to folder with data
|
|
||||||
data_dir: ${work_dir}/data/
|
|
||||||
|
|
||||||
# hydra output paths
|
|
||||||
hydra:
|
|
||||||
run:
|
|
||||||
dir: logs/runs/${now:%Y-%m-%d}/${now:%H-%M-%S}
|
|
||||||
sweep:
|
|
||||||
dir: logs/multiruns/${now:%Y-%m-%d_%H-%M-%S}
|
|
||||||
subdir: ${hydra.job.num}
|
|
||||||
|
|
||||||
# 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
|
|
||||||
```
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
## Experiment configuration ([configs/experiment](project/configs/experiment))
|
|
||||||
You can store many experiment configurations in this folder.<br>
|
|
||||||
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 /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/'
|
|
||||||
- override /callbacks: default_callbacks.yaml # choose callback set from 'configs/callbacks/'
|
|
||||||
- override /logger: null # choose logger from 'configs/logger/'
|
|
||||||
|
|
||||||
# all parameters below will be merged with parameters from default configurations set above
|
|
||||||
# this allows you to overwrite only specified parameters
|
|
||||||
|
|
||||||
seeds:
|
|
||||||
pytorch_seed: 12345
|
|
||||||
|
|
||||||
trainer:
|
|
||||||
args:
|
|
||||||
max_epochs: 10
|
|
||||||
|
|
||||||
model:
|
|
||||||
args:
|
|
||||||
input_size: 784
|
|
||||||
output_size: 10
|
|
||||||
lin1_size: 256
|
|
||||||
lin2_size: 256
|
|
||||||
lin3_size: 128
|
|
||||||
|
|
||||||
datamodule:
|
|
||||||
args:
|
|
||||||
batch_size: 32
|
|
||||||
train_val_test_split: [55_000, 5_000, 10_000]
|
|
||||||
|
|
||||||
optimizer:
|
|
||||||
args:
|
|
||||||
lr: 0.001
|
|
||||||
weight_decay: 0.00001
|
|
||||||
```
|
|
||||||
To execute this experiment run:
|
|
||||||
```bash
|
|
||||||
python train.py +experiment=exp_example_simple
|
|
||||||
```
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
## Logs
|
|
||||||
Logs are created automatically with the following structure:
|
|
||||||
```
|
|
||||||
├── logs
|
|
||||||
│ ├── runs # Folder for logs generated from single runs
|
|
||||||
│ │ ├── 2021-02-15 # Date of executing run
|
|
||||||
│ │ │ ├── 16-50-49 # Hour of executing run
|
|
||||||
│ │ │ │ ├── .hydra # Hydra logs
|
|
||||||
│ │ │ │ ├── wandb # Weights&Biases logs
|
|
||||||
│ │ │ │ └── checkpoints # Training checkpoints
|
|
||||||
│ │ │ │
|
|
||||||
│ │ │ ├── ...
|
|
||||||
│ │ │ └── ...
|
|
||||||
│ │ ├── ...
|
|
||||||
│ │ └── ...
|
|
||||||
│ │
|
|
||||||
│ ├── multiruns # Folder for logs generated from sweeps
|
|
||||||
│ │ ├── 2021-02-15_16-50-49 # Date and hour of executing run
|
|
||||||
│ │ │ ├── 0 # Job number
|
|
||||||
│ │ │ │ ├── .hydra # Hydra logs
|
|
||||||
│ │ │ │ ├── wandb # Weights&Biases logs
|
|
||||||
│ │ │ │ └── checkpoints # Training checkpoints
|
|
||||||
│ │ │ ├── 1
|
|
||||||
│ │ │ ├── 2
|
|
||||||
│ │ │ └── ...
|
|
||||||
│ │ ├── ...
|
|
||||||
│ │ └── ...
|
|
||||||
│ │
|
|
||||||
```
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
## 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<br>
|
|
||||||
```bash
|
|
||||||
python train.py +experiment=experiment_name.yaml
|
|
||||||
```
|
|
||||||
<br><br>
|
|
||||||
|
|
||||||
|
|
||||||
### DELETE EVERYTHING ABOVE FOR YOUR PROJECT
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
<div align="center">
|
|
||||||
|
|
||||||
# Your Project Name
|
|
||||||
|
|
||||||
[](https://www.nature.com/articles/nature14539)
|
|
||||||
[](https://papers.nips.cc/book/advances-in-neural-information-processing-systems-31-2018)
|
|
||||||
[](https://papers.nips.cc/book/advances-in-neural-information-processing-systems-31-2018)
|
|
||||||
[](https://papers.nips.cc/book/advances-in-neural-information-processing-systems-31-2018)
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
## Description
|
|
||||||
What it does
|
|
||||||
|
|
||||||
## How to run
|
|
||||||
First, install dependencies
|
|
||||||
```bash
|
|
||||||
# clone project
|
|
||||||
git clone https://github.com/YourGithubName/your-repo-name
|
|
||||||
cd your-repo-name
|
|
||||||
|
|
||||||
# optionally create conda environment
|
|
||||||
conda update conda
|
|
||||||
conda env create -f conda_env.yaml -n your_env_name
|
|
||||||
conda activate your_env_name
|
|
||||||
|
|
||||||
# install requirements
|
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
pip install hydra-core --upgrade --pre
|
|
||||||
```
|
|
||||||
|
|
||||||
Next, you can train model with default configuration without logging
|
|
||||||
```bash
|
|
||||||
cd project
|
|
||||||
python train.py
|
|
||||||
```
|
|
||||||
|
|
||||||
Or you can train model with chosen logger like Weights&Biases
|
|
||||||
```yaml
|
|
||||||
# set project and entity names in project/configs/logger/wandb.yaml
|
|
||||||
wandb:
|
|
||||||
args:
|
|
||||||
project: "your_project_name"
|
|
||||||
entity: "your_wandb_team_name"
|
|
||||||
```
|
|
||||||
```bash
|
|
||||||
# train model with Weights&Biases
|
|
||||||
python train.py logger=wandb.yaml
|
|
||||||
```
|
|
||||||
|
|
||||||
Or you can train model with chosen experiment config
|
|
||||||
```bash
|
|
||||||
python train.py +experiment=exp_example_simple.yaml
|
|
||||||
```
|
|
||||||
|
|
||||||
Optionally you can install project as a package with [setup.py](setup.py)
|
|
||||||
```bash
|
|
||||||
pip install -e .
|
|
||||||
```
|
|
||||||
<br>
|
|
||||||
|
|
||||||
|
|
||||||
#### PyCharm setup
|
|
||||||
- open this repository as PyCharm project
|
|
||||||
- set project interpreter:<br>
|
|
||||||
`Ctrl + Shift + A -> type "Project Interpreter"`
|
|
||||||
- mark folder "project" as sources root:<br>
|
|
||||||
`right click on directory -> "Mark Directory as" -> "Sources Root"`
|
|
||||||
- set terminal emulation:<br>
|
|
||||||
`Ctrl + Shift + A -> type "Edit Configurations..." -> select "Emulate terminal in output console"`
|
|
||||||
- run training:<br>
|
|
||||||
`right click on train.py file -> "Run 'train'"`
|
|
||||||
|
|
||||||
|
|
||||||
#### VS Code setup
|
|
||||||
- TODO
|
|
||||||
Whitespace-only changes.
@@ -1,42 +1,42 @@
|
|||||||
# THIS FILE IS FOR HYPERPARAMETER SEARCH WITH Weights&Biases
|
# # THIS FILE IS FOR HYPERPARAMETER SEARCH WITH Weights&Biases
|
||||||
# IT SHOULDN't BE EXECUTED DIRECTLY, IT SHOULD BE RUN BY WANDB SWEEP AGENT!
|
# # IT SHOULDN't BE EXECUTED DIRECTLY, IT SHOULD BE RUN BY WANDB SWEEP AGENT!
|
||||||
|
#
|
||||||
# hydra imports
|
# # hydra imports
|
||||||
from omegaconf import DictConfig
|
# from omegaconf import DictConfig
|
||||||
import hydra
|
# import hydra
|
||||||
|
#
|
||||||
# template utils imports
|
# # template utils imports
|
||||||
import template_utils.initializers as utils
|
# import src.utils.initializers as utils
|
||||||
|
#
|
||||||
import wandb
|
# import wandb
|
||||||
from train import train
|
# from train import train
|
||||||
|
#
|
||||||
|
#
|
||||||
# Replace run config hyperparameters with the ones loaded from wandb sweep server
|
# # Replace run config hyperparameters with the ones loaded from wandb sweep server
|
||||||
def replace_hparams(config, sweep_hparams):
|
# def replace_hparams(config, sweep_hparams):
|
||||||
for key, value in sweep_hparams.items():
|
# for key, value in sweep_hparams.items():
|
||||||
if key == "_wandb":
|
# if key == "_wandb":
|
||||||
continue
|
# continue
|
||||||
elif key in config["trainer"]["args"]:
|
# elif key in config["trainer"]["args"]:
|
||||||
config["trainer"]["args"][key] = value
|
# config["trainer"]["args"][key] = value
|
||||||
elif key in config["model"]["args"]:
|
# elif key in config["model"]["args"]:
|
||||||
config["model"]["args"][key] = value
|
# config["model"]["args"][key] = value
|
||||||
elif key in config["optimizer"]["args"]:
|
# elif key in config["optimizer"]["args"]:
|
||||||
config["optimizer"]["args"][key] = value
|
# config["optimizer"]["args"][key] = value
|
||||||
elif key in config["datamodule"]["args"]:
|
# elif key in config["datamodule"]["args"]:
|
||||||
config["datamodule"]["args"][key] = value
|
# config["datamodule"]["args"][key] = value
|
||||||
|
#
|
||||||
|
#
|
||||||
@hydra.main(config_path="configs/", config_name="config.yaml")
|
# @hydra.main(config_path="configs/", config_name="config.yaml")
|
||||||
def main(config: DictConfig) -> None:
|
# def main(config: DictConfig) -> None:
|
||||||
wandb.init()
|
# wandb.init()
|
||||||
|
#
|
||||||
sweep_hparams = wandb.Config._as_dict(wandb.config)
|
# sweep_hparams = wandb.Config._as_dict(wandb.config)
|
||||||
utils.show_config(config) # print content of config
|
# utils.show_config(config) # print content of config
|
||||||
|
#
|
||||||
train(config)
|
# train(config)
|
||||||
wandb.finish()
|
# wandb.finish()
|
||||||
|
#
|
||||||
|
#
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
main()
|
# main()
|
||||||
@@ -1 +0,0 @@
|
|||||||
import hydra
|
|
||||||
@@ -16,8 +16,6 @@ import template_utils.initializers as utils
|
|||||||
|
|
||||||
|
|
||||||
def train(config):
|
def train(config):
|
||||||
# Validate the correctness of loaded config
|
|
||||||
utils.validate_config(config=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"]:
|
||||||
|
|||||||
+3
-3
@@ -2,8 +2,8 @@ torch>=1.7.0
|
|||||||
torchvision>=0.8.1
|
torchvision>=0.8.1
|
||||||
torchaudio>=0.7.0
|
torchaudio>=0.7.0
|
||||||
pytorch-lightning>=1.1.3
|
pytorch-lightning>=1.1.3
|
||||||
hydra-core
|
hydra-core>=1.0.5
|
||||||
wandb>=0.10.12
|
wandb>=0.10.13
|
||||||
scikit-learn>=0.23.2
|
scikit-learn>=0.23.2
|
||||||
pandas>=1.1.5
|
pandas>=1.1.5
|
||||||
hydra_colorlog
|
hydra_colorlog>=1.0.0
|
||||||
Reference in new issue
Block a user