cover

Increase your productivity with your own PyTorch template

Over the last years I’ve built several deep learning projects in PyTorch, either for work or for academic projects. What I realized was that I was actually writing and writing the same things, except for the dataset class, model architecture, custom losses and a few minor things.

Therefore, in order to avoid writing always the same pipeline, we can actually our own PyTorch template, that can be reused with minimal modifications.

In this blog post, we will discuss the benefits of having your own PyTorch template for deep learning pipelines and how it can increase your productivity.

Benefits

Below I will report three major benefits of having your own template:

  • Faster experimentation: this allows you to quickly experiment different dataset, architectures and loss functions with a relatively low effort.
  • Reproducibility: with a robust and consistent template, you can ensure that your experiments will be reproducible.
  • Customization: if you build your own template, tailored for your needs, you are free to add all the methods, plots and callbacks that you need on your day-to-day projects, avoiding to copy and paste the same snippets of code between older repositories.

Of course there will be more than three benefits, and I am looking forward to hearing from you!

My personal setup

I shared my personal template in this repository. As you can see, the overall structure is as follows:

└─── assets/ : store results, images or major checkpoints
|
└─── dataset/ : store the dataset involved on your tests
|
└─── src/ 
└───────── data/ 
└───────────────── dataset.py : dataset class
└───────────────── transformations.py : data augmentation pipeline
└───────── losses/
└───────── metrics/ 
└───────── models/
└───────────────── model.py : custom model
└───────────────── trainer.py : trainer class
└───────── utils/ 
└───────────────── plots.py : plots sample of images, metrics and so on
└───────────────── utils.py : set seed, early stopping and so on
|
└─── .gitignore
└─── README.md
└─── settings.yaml : settings for the current experiment 
└─── train.py : training pipeline with logging, cuda and tensorboard support

I was inspired by the wonderful work of Francesco Saverio Zuppichini and I will keep my template up-to-date with further features along the way!

Conclusions

This blogpost reported the main advantages of building your PyTorch or Tensorflow template instead of copying and pasting many times from your older projects. My personl setup is still a work-in-progress and I would be happy to receive comments and feedbacks based on your professional experience!

References

Related articles:

    background

    05 December 2022

    avatar

    Francesco Di Salvo

    45 min

    30 Days of Machine Learning Engineering

    30 Days of Machine Learning Engineering

    background

    16 January 2023

    avatar

    Daniele Moltisanti

    6 min

    Advanced Data Normalization Techniques for Financial Data Analysis

    In the financial industry, data normalization is an essential step in ensuring accurate and meaningful analysis of financial data.

    background

    17 January 2023

    avatar

    Francesco Di Salvo

    10 min

    AI for breast cancer diagnosis

    Analysis of AI applications for fighting breast cancer.

    background

    14 November 2022

    avatar

    Francesco Di Gangi

    5 min

    Artificial Intelligence in videogames

    Artificial Intelligence is a giant world where we can find everything. Also videogames when we don't even notice...

    background

    18 November 2022

    avatar

    Gabriele Albini

    15 min

    Assortativity Coefficients in social networks data

    Assortativity helps analysing pattern of connections in networks. Let’s use it to confirm if people tend to connect to similar people.

JoinUS