everything except callbacks works

This commit is contained in:
hobogalaxy committed 2020-12-12 22:05:48 +01:00
1 parent f46f380261
commit 757f0fc25a
7 files changed
+189 -148

No files matched your search

+52 -54
View File
@@ -38,16 +38,16 @@ The directory structure of new project looks like this:
│ │
│ ├── data_modules <- All your data modules should be located here!
│ │ ├── example_datamodule <- Each datamodule should be located in separate folder!
│ │ │ ├── datamodule.py <- Contains 'DataModule' class
│ │ │ ├── datasets.py <- Contains pytorch 'Dataset' classes (optional file)
│ │ │ └── transforms.py <- Contains data transformations (optional file)
│ │ │ ├── datamodule.py <- Contains class of type 'LightningDataModule'
│ │ │ ├── datasets.py <- Optional file
│ │ │ └── transforms.py <- Optional file
│ │ ├── ...
│ │ └── ...
│ │
│ ├── models <- All your models should be located here!
│ │ ├── example_model <- Each model should be located in separate folder!
│ │ │ ├── lightning_module.py <- Contains 'LitModel' class with train/val/test step methods
│ │ │ └── models.py <- Model architectures used by lightning_module.py (optional file)
│ │ │ ├── lightning_module.py <- Contains class of type 'LightningModule'
│ │ │ └── models.py <- Model architectures used by 'LightningModule'
│ │ ├── ...
│ │ └── ...
│ │
@@ -68,23 +68,24 @@ The directory structure of new project looks like this:
## Project config parameters ([project_config.yaml](project/project_config.yaml))
Example project configuration:
```yaml
num_of_gpus: -1 <- '-1' to use all gpus available, '0' to train on cpu
num_of_gpus: -1 <- '-1' to train on all GPUs available, '0' to train on CPU
loggers:
wandb:
project: "project_name" <- wandb project name
entity: "some_name" <- wandb entity name
log_model: True <- set True if you want to upload ckpts to wandb automatically
log_gradients: False <- set True to log gradient histograms
offline: False <- set True if you want to store all data locally
callbacks:
checkpoint:
monitor: "val_acc" <- name of the logged metric that determines when model is improving
default_callbacks:
ModelCheckpoint:
monitor: "val_acc" <- name of the logged metric which determines when model is improving
save_top_k: 1 <- save k best models (determined by above metric)
save_last: True <- additionaly always save model from last epoch
mode: "max" <- can be "max" or "min"
early_stop:
monitor: "val_acc" <- name of the logged metric that determines when model is improving
EarlyStopping:
monitor: "val_acc" <- name of the logged metric which determines when model is improving
patience: 5 <- how many epochs of not improving until training stops
mode: "max" <- can be "max" or "min"
@@ -100,55 +101,52 @@ printing:
You can store many run configurations in this file.<br>
Example run configuration:
```yaml
MNIST_CLASSIFIER_V1:
trainer: # these parameters will be passed directly to PyTorch Lightning 'Trainer' object
max_epochs: 5
gradient_clip_val: 0.5
accumulate_grad_batches: 1
limit_train_batches: 1.0
model: # these parameters will be passed to 'LightningModule' object as 'hparams' dictionary
model_folder: "simple_mnist_classifier"
lr: 0.001
weight_decay: 0.000001
input_size: 784
output_size: 10
lin1_size: 256
lin2_size: 256
lin3_size: 128
dataset: # these parameters will be passed to 'LightningDataModule' object as 'hparams' dictionary
datamodule_folder: "mnist_datamodule"
batch_size: 256
train_val_split_ratio: 0.9
num_workers: 1
pin_memory: False
callbacks:
ConfusionMatrixLoggerCallback:
class_names: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
wandb:
group: ""
tags: ["v1", "uwu"]
resume_training:
checkpoint_path: "path_to_checkpoint/last.ckpt"
wandb_run_id: None
SIMPLE_CONFIG_EXAMPLE_MNIST:
seed: 1234
trainer:
args:
max_epochs: 10
model:
load_from:
model_path: "models/simple_mnist_classifier/lightning_module.py"
model_class: "LitModel"
hparams:
lr: 0.001
weight_decay: 0.00001
input_size: 784
output_size: 10
lin1_size: 256
lin2_size: 256
lin3_size: 128
datamodule:
load_from:
datamodule_path: "datamodules/mnist_datamodule/datamodule.py"
datamodule_class: "MNISTDataModule"
hparams:
batch_size: 64
train_val_test_split: [55_000, 5_000, 10_000]
```
Each run configuration needs to contain sections `trainer`, `model` and `dataset`. Sections `callbacks`, `wandb` and `resume_training` are optional and can be removed.<br>
To start training with this configuration run: `python train.py --run_config_name SIMPLE_CONFIG_EXAMPLE_MNIST`
Section `model` always needs to contain `model_folder` parameter (name of the folder from which `lightning_module.py` will be loaded, which should contain `LitModel` class).<br>
Section `dataset` always needs to contain `datamodule_folder` parameter (name of the folder from which `datamodule.py` will be loaded, which should contain `DataModule` class).<br>
Each run configuration needs to contain sections `trainer`, `model` and `datamodule`. <br>
Sections `callbacks`, `wandb` and `resume_training` are optional and can be removed.<br>
Every parameter in `model` section will be passed to your model class and can be retrieved through `hparams` dictionary (see example with [simple_mnist_classifier](project/models/simple_mnist_classifier/lightning_module.py)).<br>
Every parameter in `dataset` section will be passed to your datamodule class and can be retrieved through `hparams` dictionary (see example with [mnist_digits_datamodule](project/datamodules/mnist_datamodule/datamodule.py)).<br>
Every parameter specified in model hparams section will be passed to your model class and can be retrieved through `hparams` dictionary (see example with [simple_mnist_classifier](project/models/simple_mnist_classifier/lightning_module.py)).<br>
Every parameter specified in datamodule hparams section will be passed to your datamodule class and can be retrieved through `hparams` dictionary (see example with [mnist_digits_datamodule](project/datamodules/mnist_datamodule/datamodule.py)).<br>
<br>
## Workflow
1. Add your model to `project/models` folder. You need to create folder with `lightning_module.py` file containing `LitModel` class
2. Add your datamodule to `project/data_modules` folder. You need to create folder with `datamodule.py` file containing `DataModule` class
3. Create new run config in [run_configs.yaml](project/run_configs.yaml) (specify there folders containing your model and datamodule)
3. Configure [project_config.yaml](project/project_config.yaml)
1. Create model in PyTorch Lightning
2. Create datamodule in PyTorch Lightning
3. Create new run config in [run_configs.yaml](project/run_configs.yaml)
- specify path to your model class
- specify path to your datamodule class
- you can add there and hyperparametrs you want!
3. Configure your project in [project_config.yaml](project/project_config.yaml)
4. Run training with chosen run config<br>
```bash
python train.py --run_config MNIST_CLASSIFIER_V1
python train.py --run_config SIMPLE_CONFIG_EXAMPLE_MNIST
```
<br><br>
@@ -192,7 +190,7 @@ Next, you can train model without logging
# train model without Weights&Biases
# choose run config from project/run_configs.yaml
cd project
python train.py --no_wandb --run_config MNIST_CLASSIFIER_V1
python train.py --no_wandb --run_config SIMPLE_CONFIG_EXAMPLE_MNIST
```
Or you can train model with Weights&Biases logging
@@ -207,10 +205,10 @@ loggers:
# train model with Weights&Biases
# choose run config from project/run_configs.yaml
cd project
python train.py --run_config MNIST_CLASSIFIER_V1
python train.py --run_config SIMPLE_CONFIG_EXAMPLE_MNIST
```
Optionally you can install project as package with [setup.py](setup.py)
Optionally you can install project as a package with [setup.py](setup.py)
```bash
pip install -e .
```