• Stars
    star
    1,252
  • Rank 37,538 (Top 0.8 %)
  • Language
    Python
  • Created almost 7 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A best practice for deep learning project template architecture.

PyTorch Project Template

A simple and well designed structure is essential for any Deep Learning project, so after a lot practice and contributing in pytorch projects here's a pytorch project template that combines simplicity, best practice for folder structure and good OOP design. The main idea is that there's much same stuff you do every time when you start your pytorch project, so wrapping all this shared stuff will help you to change just the core idea every time you start a new pytorch project.

So, hereโ€™s a simple pytorch template that help you get into your main project faster and just focus on your core (Model Architecture, Training Flow, etc)

In order to decrease repeated stuff, we recommend to use a high-level library. You can write your own high-level library or you can just use some third-part libraries such as ignite, fastai, mmcv โ€ฆ etc. This can help you write compact but full-featured training loops in a few lines of code. Here we use ignite to train mnist as an example.

Requirements

  • yacs (Yet Another Configuration System)
  • PyTorch (An open source deep learning platform)
  • ignite (High-level library to help with training neural networks in PyTorch)

Table Of Contents

In a Nutshell

In a nutshell here's how to use this template, so for example assume you want to implement ResNet-18 to train mnist, so you should do the following:

  • In modeling folder create a python file named whatever you like, here we named it example_model.py . In modeling/__init__.py file, you can build a function named build_model to call your model
from .example_model import ResNet18

def build_model(cfg):
    model = ResNet18(cfg.MODEL.NUM_CLASSES)
    return model
  • In engine folder create a model trainer function and inference function. In trainer function, you need to write the logic of the training process, you can use some third-party library to decrease the repeated stuff.
# trainer
def do_train(cfg, model, train_loader, val_loader, optimizer, scheduler, loss_fn):
 """
 implement the logic of epoch:
 -loop on the number of iterations in the config and call the train step
 -add any summaries you want using the summary
 """
pass

# inference
def inference(cfg, model, val_loader):
"""
implement the logic of the train step
- run the tensorflow session
- return any metrics you need to summarize
 """
pass
  • In tools folder, you create the train.py . In this file, you need to get the instances of the following objects "Model", "DataLoaderโ€, โ€œOptimizerโ€, and config
# create instance of the model you want
model = build_model(cfg)

# create your data generator
train_loader = make_data_loader(cfg, is_train=True)
val_loader = make_data_loader(cfg, is_train=False)

# create your model optimizer
optimizer = make_optimizer(cfg, model)
  • Pass the all these objects to the function do_train , and start your training
# here you train your model
do_train(cfg, model, train_loader, val_loader, optimizer, None, F.cross_entropy)

You will find a template file and a simple example in the model and trainer folder that shows you how to try your first model simply.

In Details

โ”œโ”€โ”€  config
โ”‚    โ””โ”€โ”€ defaults.py  - here's the default config file.
โ”‚
โ”‚
โ”œโ”€โ”€  configs  
โ”‚    โ””โ”€โ”€ train_mnist_softmax.yml  - here's the specific config file for specific model or dataset.
โ”‚ 
โ”‚
โ”œโ”€โ”€  data  
โ”‚    โ””โ”€โ”€ datasets  - here's the datasets folder that is responsible for all data handling.
โ”‚    โ””โ”€โ”€ transforms  - here's the data preprocess folder that is responsible for all data augmentation.
โ”‚    โ””โ”€โ”€ build.py  		   - here's the file to make dataloader.
โ”‚    โ””โ”€โ”€ collate_batch.py   - here's the file that is responsible for merges a list of samples to form a mini-batch.
โ”‚
โ”‚
โ”œโ”€โ”€  engine
โ”‚   โ”œโ”€โ”€ trainer.py     - this file contains the train loops.
โ”‚   โ””โ”€โ”€ inference.py   - this file contains the inference process.
โ”‚
โ”‚
โ”œโ”€โ”€ layers              - this folder contains any customed layers of your project.
โ”‚   โ””โ”€โ”€ conv_layer.py
โ”‚
โ”‚
โ”œโ”€โ”€ modeling            - this folder contains any model of your project.
โ”‚   โ””โ”€โ”€ example_model.py
โ”‚
โ”‚
โ”œโ”€โ”€ solver             - this folder contains optimizer of your project.
โ”‚   โ””โ”€โ”€ build.py
โ”‚   โ””โ”€โ”€ lr_scheduler.py
โ”‚   
โ”‚ 
โ”œโ”€โ”€  tools                - here's the train/test model of your project.
โ”‚    โ””โ”€โ”€ train_net.py  - here's an example of train model that is responsible for the whole pipeline.
โ”‚ 
โ”‚ 
โ””โ”€โ”€ utils
โ”‚    โ”œโ”€โ”€ logger.py
โ”‚    โ””โ”€โ”€ any_other_utils_you_need
โ”‚ 
โ”‚ 
โ””โ”€โ”€ tests					- this foler contains unit test of your project.
     โ”œโ”€โ”€ test_data_sampler.py

Future Work

Contributing

Any kind of enhancement or contribution is welcomed.

Acknowledgments

More Repositories

1

pytorch-beginner

pytorch tutorial for beginners
Python
2,977
star
2

code-of-learn-deep-learning-with-pytorch

This is code of book "Learn Deep Learning with PyTorch"
Jupyter Notebook
2,781
star
3

Roadmap-of-DL-and-ML

Roadmap of DL and ML, some courses, study notes and paper summary
746
star
4

deploy-pytorch-model

Python
143
star
5

Char-RNN-PyTorch

ไฝฟ็”จPyTorchๅฎž็ŽฐChar RNN็”Ÿๆˆๅค่ฏ—ๅ’Œๅ‘จๆฐไผฆ็š„ๆญŒ่ฏ
Jupyter Notebook
139
star
6

mxnet-tutorial

my simple tutorial for mxnet, a fast deep learning framework
Python
106
star
7

fcn.pytorch

Python
81
star
8

neural-transfer

Jupyter Notebook
59
star
9

Char-RNN-Gluon

ไฝฟ็”จMXNet็š„ๅŠจๆ€ๅ›พๆŽฅๅฃGluonๅฎž็ŽฐChar RNN็”Ÿๆˆๅค่ฏ—ๅ’Œๅ‘จๆฐไผฆ็š„ๆญŒ่ฏ
Python
58
star
10

cifar10-gluon

Gluon implement of Kaggle cifar10 competition
Jupyter Notebook
50
star
11

tensorflow-beginner

tensorflow learning according to CS20SI
Python
49
star
12

cs231n-assignment-solution

This is the cs231n assignment solution
Jupyter Notebook
47
star
13

kaggle-house-price

่ฟ™ๆ˜ฏkaggleๆˆฟไปท้ข„ๆต‹็š„baseline
Jupyter Notebook
38
star
14

Deep-Dream

PyTorch implement of Google Deep Dream
Jupyter Notebook
35
star
15

lenet

Jupyter Notebook
31
star
16

kaggle-cifar10

่ฟ™ๆ˜ฏkaggle-cifar10็š„baseline
Python
16
star
17

reid_baseline_gluon

SOTA results for reid baseline model (Gluon implementation)
Python
13
star
18

llm-infer-bench

Python
11
star
19

Digital-Image-Processing

Jupyter Notebook
9
star
20

cycle-GAN-for-Makeup

This is a cycle gan application about makeup remover.
Python
8
star
21

ai-challenger-scene-classification-baseline

PyTorch implement of AI Challenger scene classification baseline
Jupyter Notebook
7
star
22

seq2seq-translation

PyTorch implement of neural machine translation
Python
6
star
23

dockerfile

Vim Script
6
star
24

image-caption-project

this is image caption project reference code
Jupyter Notebook
6
star
25

L1aoXingyu.github.io_archive

where blog and notes place
JavaScript
6
star
26

AI-Challenger-image-caption

PyTorch implement of image caption
Python
6
star
27

leetcode-python

This is my leetcode solutions with python
6
star
28

machine-learning-by-Zhouzhihua

ๅ‘จๅฟ—ๅŽใ€Šๆœบๅ™จๅญฆไน ใ€‹
5
star
29

faster_rcnn.pytorch

my attempt to implement faster rcnn using pytorch
Python
5
star
30

dog-breed-identification-Gluon

Jupyter Notebook
5
star
31

kaggle_dog_vs_cat

Jupyter Notebook
4
star
32

deep-tesla

End-to-End Learning from Human and Autopilot Driving
Python
3
star
33

CarND-LaneLines-P1

udacity self-driving car nanodegree project 1: Laneline detection
Jupyter Notebook
2
star
34

distracted_driver_detection

detect distracted driver
Python
2
star
35

complete_process_pytorch

Jupyter Notebook
1
star
36

LearnCpp

learn cpp
C++
1
star
37

PDE_numerical

Jupyter Notebook
1
star
38

S4TF-tutorial

1
star
39

blogpages_fastai

HTML
1
star
40

blogpages

Astro
1
star
41

image-caption

PyTorch implement of simple image caption
1
star
42

udacity_CV_SLAM

Jupyter Notebook
1
star
43

udacity-deeplearning

Jupyter Notebook
1
star
44

Wasserstein-GAN

pytorch implement of Wasserstein GAN
1
star