From 23e9c4fecf2012371b7e49d05106e9e555e68d0a Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Sat, 12 Dec 2020 22:39:40 +0100 Subject: [PATCH] more comments and explanations --- README.md | 34 +++++++++++-------- .../mnist_datamodule/datamodule.py | 4 +-- .../datamodules/mnist_datamodule/datasets.py | 5 +-- .../mnist_datamodule/transforms.py | 3 ++ .../lightning_module.py | 4 +-- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5b0eb6f..189978d 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@ A convenient starting template for most deep learning projects. Built with Py Click on "Use this template" button above to initialize new repository.
## Features -- All advantages of PyTorch Lightning - Predefined folder structure - Storing project configuration in a convenient way ([project_config.yaml](project/project_config.yaml)) - Storing many run configurations in a convenient way ([run_configs.yaml](project/run_configs.yaml)) +- All advantages of PyTorch Lightning +- Weights&Biases integration: + - Automatically stores all relevant code, configs and model checkpoints in Weights&Biases cloud + - Hyperparameter search with Weights&Biases sweeps ([execute_sweep.py](project/template_utils/execute_sweep.py)) - Automates the whole training process and initialization, you only need to create `model` and `datamodule` and specify them in [run_configs.yaml](project/run_configs.yaml) -- Automatically stores all relevant code, configs and model checkpoints in Weights&Biases cloud -- Hyperparameter search with Weights&Biases sweeps ([execute_sweep.py](project/template_utils/execute_sweep.py)) - Scheduling execution of many experiments ([execute_all_runs.py](project/template_utils/execute_all_runs.py)) - Built in requirements ([requirements.txt](requirements.txt)) - Built in conda environment initialization ([conda_env.yaml](conda_env.yaml)) @@ -36,16 +37,16 @@ The directory structure of new project looks like this: │ │ ├── init_utils.py <- Useful initializers │ │ └── predict_example.py <- Example of inference with trained model │ │ -│ ├── data_modules <- All your data modules should be located here! -│ │ ├── example_datamodule <- Each datamodule should be located in separate folder! +│ ├── datamodules <- All your datamodules should be located here! +│ │ ├── example_datamodule <- It's best to locate each datamodule in separate folder! │ │ │ ├── datamodule.py <- Contains class of type 'LightningDataModule' -│ │ │ ├── datasets.py <- Optional file -│ │ │ └── transforms.py <- Optional file +│ │ │ ├── transforms.py <- Some optional file +│ │ │ └── ... │ │ ├── ... │ │ └── ... │ │ │ ├── models <- All your models should be located here! -│ │ ├── example_model <- Each model should be located in separate folder! +│ │ ├── example_model <- It's best to locate each model in separate folder! │ │ │ ├── lightning_module.py <- Contains class of type 'LightningModule' │ │ │ └── models.py <- Model architectures used by 'LightningModule' │ │ ├── ... @@ -93,6 +94,9 @@ printing: progress_bar_refresh_rate: 5 <- refresh rate of training bar in terminal weights_summary: "top" <- print summary of model (alternatively "full") profiler: False <- set True if you want to see execution time profiling + +data_dir: "data/" <- path to data folder +logs_dir: "logs/" <- path to logs folder ```
@@ -129,20 +133,20 @@ SIMPLE_CONFIG_EXAMPLE_MNIST: To start training with this configuration run: `python train.py --run_config_name SIMPLE_CONFIG_EXAMPLE_MNIST` Each run configuration needs to contain sections `trainer`, `model` and `datamodule`.
-Sections `callbacks`, `wandb` and `resume_training` are optional and can be removed.
+Sections `seed`, `callbacks`, `wandb` and `resume_training` are optional and can be removed (see advanced config example in [run_configs.yaml](project/run_configs.yaml)).
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)).
-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)).
+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_datamodule](project/datamodules/mnist_datamodule/datamodule.py)).

## Workflow -1. Create model in PyTorch Lightning -2. Create datamodule in PyTorch Lightning +1. Create PyTorch Lightning model +2. Create PyTorch Lightning datamodule 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! + - you can add there and hyperparameters you want! 3. Configure your project in [project_config.yaml](project/project_config.yaml) 4. Run training with chosen run config
```bash @@ -166,8 +170,8 @@ Every parameter specified in datamodule hparams section will be passed to your d -## Description -What it does +## Description +What it does ## How to run First, install dependencies diff --git a/project/datamodules/mnist_datamodule/datamodule.py b/project/datamodules/mnist_datamodule/datamodule.py index 25e35b4..3470394 100644 --- a/project/datamodules/mnist_datamodule/datamodule.py +++ b/project/datamodules/mnist_datamodule/datamodule.py @@ -9,9 +9,9 @@ class MNISTDataModule(LightningDataModule): This is example of datamodule for MNIST dataset. The path to datamodule should be specified in your run config. (run_configs.yaml) - The 'hparams' dict contains your hparams specified in run config! (run_configs.yaml) - + The 'hparams' dict contains your hparams specified in run config. (run_configs.yaml) """ + def __init__(self, data_dir: str, hparams: dict): super().__init__() diff --git a/project/datamodules/mnist_datamodule/datasets.py b/project/datamodules/mnist_datamodule/datasets.py index 2a97b97..14076d6 100644 --- a/project/datamodules/mnist_datamodule/datasets.py +++ b/project/datamodules/mnist_datamodule/datasets.py @@ -5,8 +5,9 @@ import os class TestDataset(Dataset): """ - Example dataset class for loading images from folder. - 'Dataset' classes can be used by 'DataModule' class. + Example dataset class for loading images from folder and converting them to monochromatic. + Can be used to perform inference with trained MNIST model. + 'Dataset' type classes can also be used to create 'DataLoader' type classes which are used by datamodules. """ def __init__(self, img_dir, transform): diff --git a/project/datamodules/mnist_datamodule/transforms.py b/project/datamodules/mnist_datamodule/transforms.py index ca1ff4c..7b22102 100644 --- a/project/datamodules/mnist_datamodule/transforms.py +++ b/project/datamodules/mnist_datamodule/transforms.py @@ -1,3 +1,6 @@ +""" +Example file containing data transformations which can be used by datamodule. +""" from torchvision import transforms diff --git a/project/models/simple_mnist_classifier/lightning_module.py b/project/models/simple_mnist_classifier/lightning_module.py index 9bcf4c0..7a981b2 100644 --- a/project/models/simple_mnist_classifier/lightning_module.py +++ b/project/models/simple_mnist_classifier/lightning_module.py @@ -3,7 +3,7 @@ import pytorch_lightning as pl import torch.nn.functional as F import torch -# custom models +# import custom models from models.simple_mnist_classifier.models import * @@ -12,7 +12,7 @@ class LitModel(pl.LightningModule): This is example of lightning model for MNIST digits classification. The path to model should be specified in your run config. (run_configs.yaml) - The 'hparams' dict contains your hparams specified in run config! (run_configs.yaml) + The 'hparams' dict contains your hparams specified in run config. (run_configs.yaml) This class enables you to specify what happens during training, validation and test step. You can just remove 'validation_step()' or 'test_step()' methods if you don't want to have them during training.