Hydra Lightning Template for Structured Configs

Overview

Hydra Lightning Template for Structured Configs

Template for creating projects with pytorch-lightning and hydra.

How to use this template?

Create your own project on GitHub with this template by clicking the Use this template button.

You now have to only add your own dataloader, dataset, model, optimizer and loss and you should be ready to go. To see if you have all modules installed and everything works fine, you should run the unit tests!

How to add my own module?

For this tutorial it is expected that you already know pytorch (and best also some pytorch-lightning). If you don't know hydra that should be fine, but definitely check out their docs.

If you encounter any problems have a look at the my_simple_model branch of this repo, where I played through this complete tutorial. So you can find all files there.

Lets explore how to use hydra and this template by showcasing how one would add a simple own CNN to this repo. For the tests I used MNIST as dataset so we will just continue using that. But if you know how to write a pytorch-lightning Dataloader and a torch Dataset it should be just as easy to replace them after this small tutorial.

To add our own model we have to do the following steps:

  1. in the folder src/models we create a new file containing our torch model (a torch.nn.Module).
  2. Add the model in the hydra config library by adding it to the src/lib/model.py file.
  3. Register the model in the hydra global-config-register by following the pattern in src/lib/config.py and creating a new entry there.
  4. (Optional) Create a yaml file for the model. This makes sense if the model is used with a lot of different settings. So we can give those settings individual names, which makes them easier to call.
  5. Add an experiment using that model

1. Creating the simplest model:

Create the file src/models/my_simple_model.py with the following content:

import torch.nn as nn
import torch.nn.functional as F


class MySimpleModel(nn.Module):
    def __init__(self, input_channels=1, num_classes=10):
        super(MySimpleModel, self).__init__()

        # When the image enters the net at conv1 it has a size of 28x28x1, because there is a single color channel
        self.conv1 = nn.Conv2d(input_channels, 16, kernel_size=3, stride=1, padding=1, bias=True)
        # Since we are using padding the size of the image does not change after the conv layer
        self.max_pool = nn.MaxPool2d(kernel_size=2, stride=2)
        # due to the maxpooling shape and stride our image is now 14x14
        self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1, bias=True)
        # still 14x14
        # We will again use maxpool so now it is 7x7
        self.fully_connected = nn.Linear(16 * 7 * 7, num_classes, bias=True)

    def forward(self, x):
        x = self.conv1(x)
        x = self.max_pool(x)
        x = self.conv2(x)
        x = self.max_pool(x)
        x = x.flatten(start_dim=1)  # To use a fully connected layer in the end we need to have a 1D array
        x = self.fully_connected(x)
        return F.softmax(x)  #  we apply a softmax here to return probabilities between 0 and 1

2. Add the model to the lib:

Change the file src/lib/model.py to add our model there. Just add the following lines:

@dataclass
class MySimpleModelLib:
    _target_: str = "src.models.my_simple_model.MySimpleModel"
    input_channels: int = 1
    num_classes: int = 10

A few pittfalls to avoid are:

  • Do not forget to decorate your class with @dataclass !
  • do not forget to specify the type !
  • Have a look at other lib files to see how to implement None as default and use the Any type.
  • do not forget any inputs to the actual model (like forget the parameter input_channels) because you will never be able to override the input channels from outside the source code.

3. Register the model in hydra:

For hydra to know about your model, you have to register it. We do this in the file src/lib/config.py. All we have to do here is adding 2 lines.

  1. We have to import the library model. So at the imports we add:
from src.lib.model import MySimpleModelLib
  1. Register the model by using the hydra ConfigStore. Best keep the code clean, so find the section where the models are defined and add:
cs.store(name="my_simple_model_base", node=MySimpleModelLib, group=model_group)

I like to append the _base her to later distinguish between the yaml-config and the structured-config. If you want to know more about this you will probably have to read the hydra documentation.

4. Add a yaml config file:

This step is not necessary. We could already use our model in hydra now, which would at this point go under the name my_simple_model_base. But for the sake of completion lets create a yaml config as well.

For this we will have to create this file: conf/model/my_simple_model.yaml

The content of this file should be

defaults:
  - my_simple_model_base  # this is the name of the registered model that we would like to extend
  - _self_  # adding this BELOW!! the registered name means, that everything in this yaml file will override the defaults

# you can only specify values here that are also in the registered model (src/lib/model/MySimpleModelLib)
num_classes: 10
input_channels: 1

If you want, you can of course drop the comments.

Why did we create this config file? Lets say you would like to also have t he same model, but with 3 input channels when you do predictions on colored images. All you would have to do is either change the value input_channels: 3 of the file conf/model/my_simple_model.yaml. But if you want to give it a distiguishable name (which makes sense for more complex usecases) then you can just create another file conf/model/my_simple_model_rgb.yaml for example, which has the content

defaults:
  - my_simple_model_base
  - _self_

num_classes: 10
input_channels: 3  # <- this is the only thing that changed

Now you could from a command line very easily switch between the 2 configs without remembering any specific numbers.

5. Add an experiment using that model:

There are 2 ways to use your model now in a training run.

  1. From the command line: All you have to do is keep everything with the defaults and just exchanging the model from the command line using hydras command line interface:
python main.py model=my_simple_model

or

python main.py model=my_simple_model_rgb

or if you did not create the yaml-file:

python main.py model=my_simple_model_base

From the command line we could also specify different inputs to our model:

python main.py model=my_simple_model_base model.input_channels=3
  1. We can create an experiment using this model. This definitely is preferable when the setups get more complex. For this, we have to create a new yaml file in the experiment folder. So lets create the file conf/experiment/my_simple_model_experiment.yaml with the following content:
# @package _global_

defaults:
  - override /lightning_module: default
  - override /datamodule: mnist
  - override /datamodule/dataset: mnist
  - override /loss: nll_loss
  - override /datamodule/train_transforms: no_transforms
  - override /datamodule/valid_transforms: no_transforms
  - override /model: my_simple_model  # <- this is the line where we add our own model to the experiment
  - override /optimizer: sgd
  - override /loss: nll_loss
  - override /strategy: null
  - override /logger/tensorboard: tensorboard
  - override /callbacks/checkpoint: model_checkpoint
  - override /callbacks/early_stopping: early_stopping
  - override /callbacks/lr_monitor: lr_monitor

  - override /hydra/launcher: local
  - _self_

output_dir_base_path: ./outputs
random_seed: 7
print_config: true
log_level: "info"

trainer:
  fast_dev_run: false
  num_sanity_val_steps: 3
  max_epochs: 3
  gpus: 0
  limit_train_batches: 3
  limit_val_batches: 3

datamodule:
  num_workers: 0
  batch_size: 4

Most settings here are the same as in the defaults, which are specified in conf/config.yaml but for this tutorial I think explicit is easier to understand the implicit.

To use the experiment we run our model with

python main.py +experiment=my_simple_model_experiment

Again we can also change all set values from the command line

python main.py +experiment=my_simple_model_experiment datamodule.num_workers=20

It should be easy now to follow the same steps to include your own datamodule, dataset, transforms, optimizers or whatever else you might need.

Owner
Model-driven Machine Learning
Model-driven Machine Learning
Library for 8-bit optimizers and quantization routines.

bitsandbytes Bitsandbytes is a lightweight wrapper around CUDA custom functions, in particular 8-bit optimizers and quantization functions. Paper -- V

Facebook Research 687 Jan 04, 2023
A graph-to-sequence model for one-step retrosynthesis and reaction outcome prediction.

Graph2SMILES A graph-to-sequence model for one-step retrosynthesis and reaction outcome prediction. 1. Environmental setup System requirements Ubuntu:

29 Nov 18, 2022
Project code for weakly supervised 3D object detectors using wide-baseline multi-view traffic camera data: WIBAM.

WIBAM (Work in progress) Weakly Supervised Training of Monocular 3D Object Detectors Using Wide Baseline Multi-view Traffic Camera Data 3D object dete

Matthew Howe 10 Aug 24, 2022
This is the code for our KILT leaderboard submission to the T-REx and zsRE tasks. It includes code for training a DPR model then continuing training with RAG.

KGI (Knowledge Graph Induction) for slot filling This is the code for our KILT leaderboard submission to the T-REx and zsRE tasks. It includes code fo

International Business Machines 72 Jan 06, 2023
Tutorial page of the Climate Hack, the greatest hackathon ever

Tutorial page of the Climate Hack, the greatest hackathon ever

UCL Artificial Intelligence Society 12 Jul 02, 2022
An unofficial implementation of "Unpaired Image Super-Resolution using Pseudo-Supervision." CVPR2020

UnpairedSR An unofficial implementation of "Unpaired Image Super-Resolution using Pseudo-Supervision." CVPR2020 turn RCAN(modified) -- xmodel(xilinx

JiaKui Hu 10 Oct 28, 2022
[ICCV 2021] Deep Hough Voting for Robust Global Registration

Deep Hough Voting for Robust Global Registration, ICCV, 2021 Project Page | Paper | Video Deep Hough Voting for Robust Global Registration Junha Lee1,

Junha Lee 10 Dec 02, 2022
A pytorch reprelication of the model-based reinforcement learning algorithm MBPO

Overview This is a re-implementation of the model-based RL algorithm MBPO in pytorch as described in the following paper: When to Trust Your Model: Mo

Xingyu Lin 93 Jan 05, 2023
The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dealing with medical images.

The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dea

MIC-DKFZ 1.2k Jan 04, 2023
Implementation of paper "Graph Condensation for Graph Neural Networks"

GCond A PyTorch implementation of paper "Graph Condensation for Graph Neural Networks" Code will be released soon. Stay tuned :) Abstract We propose a

Wei Jin 66 Dec 04, 2022
Combinatorial model of ligand-receptor binding

Combinatorial model of ligand-receptor binding The binding of ligands to receptors is the starting point for many import signal pathways within a cell

Mobolaji Williams 0 Jan 09, 2022
YOLOX-CondInst - Implement CondInst which is a instances segmentation method on YOLOX

YOLOX CondInst -- YOLOX 实例分割 前言 本项目是自己学习实例分割时,复现的代码. 通过自己编程,让自己对实例分割有更进一步的了解。 若想

DDGRCF 16 Nov 18, 2022
Bridging Vision and Language Model

BriVL BriVL (Bridging Vision and Language Model) 是首个中文通用图文多模态大规模预训练模型。BriVL模型在图文检索任务上有着优异的效果,超过了同期其他常见的多模态预训练模型(例如UNITER、CLIP)。 BriVL论文:WenLan: Bridgi

235 Dec 27, 2022
a Pytorch easy re-implement of "YOLOX: Exceeding YOLO Series in 2021"

A pytorch easy re-implement of "YOLOX: Exceeding YOLO Series in 2021" 1. Notes This is a pytorch easy re-implement of "YOLOX: Exceeding YOLO Series in

91 Dec 26, 2022
A Python library for common tasks on 3D point clouds

Point Cloud Utils (pcu) - A Python library for common tasks on 3D point clouds Point Cloud Utils (pcu) is a utility library providing the following fu

Francis Williams 622 Dec 27, 2022
Tom-the-AI - A compound artificial intelligence software for Linux systems.

Tom the AI (version 0.82) WARNING: This software is not yet ready to use, I'm still setting up the GitHub repository. Should be ready in a few days. T

2 Apr 28, 2022
Rethinking the U-Net architecture for multimodal biomedical image segmentation

MultiResUNet Rethinking the U-Net architecture for multimodal biomedical image segmentation This repository contains the original implementation of "M

Nabil Ibtehaz 308 Jan 05, 2023
This is an official pytorch implementation of Lite-HRNet: A Lightweight High-Resolution Network.

Lite-HRNet: A Lightweight High-Resolution Network Introduction This is an official pytorch implementation of Lite-HRNet: A Lightweight High-Resolution

HRNet 675 Dec 25, 2022
Ros2-voiceroid2 - ROS2 wrapper package of VOICEROID2

ros2_voiceroid2 ROS2 wrapper package of VOICEROID2 Windows Only Installation Ins

Nkyoku 1 Jan 23, 2022
Working demo of the Multi-class and Anomaly classification model using the CLIP feature space

👁️ Hindsight AI: Crime Classification With Clip About For Educational Purposes Only This is a recursive neural net trained to classify specific crime

Miles Tweed 2 Jun 05, 2022