• Stars
    star
    303
  • Rank 132,873 (Top 3 %)
  • Language
    Jupyter Notebook
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

A Pytorch Computer Vision template to quick start your next project! πŸš€πŸš€

Pytorch Deep Learning Template

A clean and simple template to kick start your next dl project πŸš€πŸš€

Francesco Saverio Zuppichini

In this article, we present you a deep learning template based on Pytorch. This template aims to make it easier for you to start a new deep learning computer vision project with PyTorch. The main features are:

  • modularity: we split each logic piece into a different python submodule
  • data-augmentation: we included imgaug
  • ready to go: by using poutyne a Keras-like framework you don't have to write any train loop.
  • torchsummary to show a summary of your models
  • reduce the learning rate on a plateau
  • auto-saving the best model
  • experiment tracking with comet
  • logging using python logging module
  • a playground notebook to quick test/play around

Installation

Clone the repo and go inside it. Then, run:

pip install -r requirements.txt

Motivation

Let's face it, usually data scientists are not software engineers and they usually end up with spaghetti code, most of the time on a big unusable Jupiter-notebook. With this repo, I have proposed a clean example of how your code should be split and modularized to make scalability and sharability possible. In this example, we will try to classify Darth Vader and Luke Skywalker. We have 100 images per class gathered using google images. The dataset is here. You just have to extract it in this folder and run main.py. We are fine-tuning resnet18 and it should be able to reach > 90% accuracy in 5/10 epochs.

Structure

The template is inside ./template.

.
β”œβ”€β”€ callbacks // here you can create your custom callbacks
β”œβ”€β”€ checkpoint // were we store the trained models
β”œβ”€β”€ data // here we define our dataset
β”‚ └── transformation // custom transformation, e.g. resize and data augmentation
β”œβ”€β”€ dataset // the data
β”‚ β”œβ”€β”€ train
β”‚ └── val
β”œβ”€β”€ logger.py // were we define our logger
β”œβ”€β”€ losses // custom losses
β”œβ”€β”€ main.py
β”œβ”€β”€ models // here we create our models
β”‚ β”œβ”€β”€ MyCNN.py
β”‚ β”œβ”€β”€ resnet.py
β”‚ └── utils.py
β”œβ”€β”€ playground.ipynb // a notebook that can be used to fast experiment with things
β”œβ”€β”€ Project.py // a class that represents the project structure
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ test // you should always perform some basic testing
β”‚ └── test_myDataset.py
└── utils.py // utilities functions

We strongly encourage to play around with the template

Keep your structure clean and concise

Every deep learning project has at least three mains steps:

  • data gathering/processing
  • modeling
  • training/evaluating

Project

One good idea is to store all the paths at an interesting location, e.g. the dataset folder, in a shared class that can be accessed by anyone in the folder. You should never hardcode any paths and always define them once and import them. So, if you later change your structure you will only have to modify one file. If we have a look at Project.py we can see how we defined the data_dir and the checkpoint_dir once for all. We are using the 'new' Path APIs that support different OS out of the box, and also makes it easier to join and concatenate paths. alt For example, if we want to know the data location we canΒ :

from Project import Project
project = Project() 
print(project.data_dir) # /foo/baa/…/dataset

Data

In the data package you can define your own Dataset, as always by subclassing torch.data.utils.Dataset, exposing transformations and utilities to work with your data. In our example, we directly used ImageDataset from torchvision but we included a skeleton for a custom Dataset in /data/MyDataset

Transformation

You usually have to do some preprocessing on the data, e.g. resize the images and apply data augmentation. All your transformation should go inside .data.trasformation. In our template, we included a wrapper for imgaug alt

Dataloaders

As you know, you have to create a Dataloader to feed your data into the model. In the data.__init__.py file we expose a very simple function get_dataloaders to automatically configure the train, val and test data loaders using few parameters alt

Losses

Sometimes you may need to define your custom losses, you can include them in the ./losses package. For example alt

Metrics

Sometimes you may need to define your custom metrics. For example alt

Logging

We included python logging module. You can import and use it by:

from logger import logging
logging.info('print() is for noobs')

Models

All your models go inside models, in our case, we have a very basic cnn and we override the resnet18 function to provide a frozen model to finetune.

alt

Train/Evaluation

In our case we kept things simple, all the training and evaluation logic is inside .main.py where we used poutyne as the main library. We already defined a useful list of callbacks:

  • learning rate scheduler
  • auto-save of the best model
  • early stopping Usually, this is all you need! alt

Callbacks

You may need to create custom callbacks, with poutyne is very easy since it support Keras-like API. You custom callbacks should go inside ./callbacks. For example, we have created one to update Comet every epoch. alt

Track your experiment

We are using comet to automatically track our models' results. This is what comet's board looks like after a few models run. alt Running main.py produces the following output: alt

Utils

We also created different utilities function to plot both dataset and dataloader. They are in utils.py. For example, calling show_dl on our train and val dataset produces the following outputs. alt alt As you can see data-augmentation is correctly applied on the train set

Conclusions

I hope you found some useful information and hopefully it this template will help you on your next amazing projectΒ :)

Let me know if you have some ideas/suggestions to improve it.

Thank you for reading

TODO

  • one example for lightning
  • one example with fastai
  • show how to setup anaconda

More Repositories

1

glasses

High-quality Neural Networks for Computer Vision 😎
Jupyter Notebook
360
star
2

Pytorch-how-and-when-to-use-Module-Sequential-ModuleList-and-ModuleDict

Code for my medium article
Jupyter Notebook
283
star
3

mirror

Visualisation tool for CNNs in pytorch
Jupyter Notebook
242
star
4

A-journey-into-Convolutional-Neural-Network-visualization-

A journey into Convolutional Neural Network visualization
Jupyter Notebook
235
star
5

ViT

Implementing Vi(sion)T(transformer)
213
star
6

ResNet

Clean, scalable and easy to use ResNet implementation in Pytorch
Jupyter Notebook
160
star
7

Tensorflow-Dataset-Tutorial

Notebook for my medium article about how to use Dataset API in TensorFlow
Jupyter Notebook
159
star
8

skeleton-card-vuejs

A reusable skeleton card component written in Vuejs
Vue
143
star
9

Reinforcement-Learning-Cheat-Sheet

Reinforcement Learning Cheat Sheet
TeX
131
star
10

API-Class

A utility class for calling apis CRUD methods
JavaScript
89
star
11

LinkedInGPT

Skynet
Python
83
star
12

DrawIo2Vuejs

A faster way to create a Vuejs app by using draw.io
JavaScript
82
star
13

Modern-Python-Doc-Example

mkdocs + material + cool stuff
Python
63
star
14

my-spaces

Run hugging face spaces locally with one command!
Python
49
star
15

linkedin_python

Python package to create posts on LinkedIn
Python
41
star
16

ConvNext

Implementing ConvNext in PyTorch
39
star
17

PytorchModuleStorage

A easy to use API to store outputs from forward/backward hooks in Pytorch
Jupyter Notebook
35
star
18

gradioGPT

Easy to hack template for your next chatGPT app with Gradio and Langchain
Python
35
star
19

TensorFlow-Serving-Example

Example of how to use TensorFlow serving
Python
35
star
20

purgIn

Chrome Extension to remove LinkedIn posts containing user defined words
JavaScript
32
star
21

torchserve-tryout

Deploy a CNN with torchserve using a custom handler
Python
31
star
22

drawIoToVuejs

Python
31
star
23

dynamic-batching-asyncio

Python
26
star
24

FairytaleDJ

You got a friend in me
Python
26
star
25

how-to-use-chatgpt-with-python

Tutorial about using ChatGPT APIs in Python
Python
26
star
26

Loading-huge-PyTorch-models-with-linear-memory-consumption

Little article showing how to load pytorch's models with linear memory consumption
21
star
27

search-all

Python
19
star
28

pytorch-2.0-benchmark

Benchmarking PyTorch 2.0 different models
Python
18
star
29

SegFormer

Implementation of SegFormer in PyTorch
17
star
30

DropPath

Implementing DropPath/StochasticDepth in PyTorch
16
star
31

Flue

Yep, another Flux implementation for Vuejs. Docs: https://francescosaveriozuppichini.github.io/Flue/header.html
JavaScript
14
star
32

detector

Python
13
star
33

BottleNeck-InvertedResidual-FusedMBConv-in-PyTorch

A little walk-trough different types of the block with their corresponding implementation in PyTorch
Jupyter Notebook
13
star
34

RepVgg

Implementing RepVGG in PyTorch
Python
11
star
35

non-max-suppression-in-pytorch

How to implement Non Max Suppression (NMS) in PyTorch
10
star
36

http-streaming-fastapi-js-playground

http-streaming-playground
TypeScript
10
star
37

yolov10

Python
10
star
38

LSTM-Text-Generator

LSTM written in Tensorflow that generates text
Python
8
star
39

is-3090-good-for-computer-vision

A collection of benchmarks I've run
Python
8
star
40

torchlego

High level building blocks for Neural Networks with examples
Python
8
star
41

DeiT

DeiT: Data-efficient Image Transformers
8
star
42

Face-Unlock

Face Unlock with Deep Learning
Jupyter Notebook
7
star
43

Resource

A more convenient way to store your state data using a map
JavaScript
6
star
44

DropBlock

Implementing DropBlock a better Dropout for Conv Nets in PyTorch!
6
star
45

Object365-download

Zero Dependencies script to download Object365
Python
5
star
46

chatgpt-action-fastapi

chatgpt-action-fastapi
Python
5
star
47

End2End-DataScienceProject

Jupyter Notebook
4
star
48

model-version-with-hf-hub

What if we use hf hub to do versioning of a model?
Python
4
star
49

Search-COVID-papers-with-Deep-Learning

A semantic browser using deep learning to search in COVID papers
Jupyter Notebook
4
star
50

local-youtube-rag

Local YouTube RAG with Qdrant and Ollama
Python
4
star
51

Paxos

Distributed Algorithm 2018 Project - USI
Python
3
star
52

any-inference

Run inference in any model using a message broker.
Python
3
star
53

StopWatchElectron

JavaScript
3
star
54

OneNet

OneNet
Python
3
star
55

How-To-Embed-in-TensorFlow

Code for my medium article
Jupyter Notebook
3
star
56

pytorch-distributed-collective-communication

Code and visualisations about PyTorch distributed collective communication
Python
3
star
57

auto_model_card

Little utility to create model card for huggingface hub
Python
3
star
58

Distributed-Algorithm-USI-2018

notes for Distributed Algorithm course
3
star
59

data-gradients-hf-datasets

Using data-gradients with hugging face datasets
Jupyter Notebook
2
star
60

tips_pytorch

Tips for PyTorch
2
star
61

HuggingFaceAutoDocstring

Mustache
2
star
62

PytorchModulePCA

An easy to use API to visualize the latent space of CNN in Pytorch
Jupyter Notebook
2
star
63

.dotfiles

Hosting my home pc configuration
2
star
64

Emotions-Detection

Detection of emotions (happiness, sadness) from a face photo using Deep Learning
Jupyter Notebook
2
star
65

GraphAppCreator

JavaScript
2
star
66

COVID-19-Map-Storytelling

Covid-19 story told by maps
JavaScript
2
star
67

brainyquote-Web-Scraper

A easy to use web scraper to get quotes from brainyquote
JavaScript
2
star
68

Faster-RCNN-tryout

Let's try out Faster-RCNN
Jupyter Notebook
2
star
69

Estimator

A predicatable way to train your deep learning model
Python
2
star
70

redux-promise-action-middleware

JavaScript
2
star
71

playground-python

1
star
72

yolov42

Python
1
star
73

LinkedIn-posts

Repo holding my LinkedIn posts πŸ’™
1
star
74

glasses-webapp

Webapp for my computer vision library glasses
JavaScript
1
star
75

FrancescoSaverioZuppichini

1
star
76

spammer

Python
1
star
77

Physical-Computing-Project

JavaScript
1
star
78

yolov36

Python
1
star
79

auto-convert-notebooks-to-markdown-github-action

Python
1
star
80

yolov_-n-1-

hold code for yolov_{n+1}
Python
1
star
81

company-xyxy-challenge

Jupyter Notebook
1
star
82

yolov11

Python
1
star
83

Activities

A vanilla bootstrap GUI for activity filtering based on 0-1 knapsack problem
JavaScript
1
star
84

glasses-2.0

Python
1
star
85

Fool-Object-Classifier

Repo used for my medium article
JavaScript
1
star
86

Master-Thesis

Repository for my Master Thesis @IDSIA and @USI
Jupyter Notebook
1
star
87

MarkDownToMediumThisNameAlreadyExists

Python
1
star
88

yolov100

Python
1
star
89

mobileone-segmentation-models-pytorch

lazy and raw work around to use mobile one in segmentation models pytorch
Python
1
star
90

yolov99

Python
1
star
91

Mobile-Computing-Project

Application for Mobile Computing
1
star
92

simebot

Python
1
star
93

TFGraphConvertible

Jupyter Notebook
1
star
94

Advance-Topics-In-Machine-Learning-Project

Project for the Advance Topics in Machine Learning course - USI 2018
Python
1
star
95

Robotics-2018

Python
1
star
96

fastapi-template

A little template with loguru and some sort of json logging
Python
1
star