Giter Site home page Giter Site logo

charl-ai / lightning-template Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 102 KB

Template for machine learning projects using pytorch lightning (my flavour of the official lightning template).

License: MIT License

Python 91.17% Jupyter Notebook 8.83%
deep-learning template-repository pytorch-lightning pytorch

lightning-template's Introduction

lightning-template

04.04.22: This Template has now been archived and will no longer be maintained. It has been a useful template, but I now mostly use JAX for my research projects. At the time of writing, there are several other templates which seem to be very well made for use with Pytorch Lightning.

Template for machine learning projects with Pytorch Lightning, focussed on allowing research projects to get set up quickly. The template includes an MNIST example, logging with weights and biases, and a recommended style guide. This README serves as a style guide and gives some general advice for getting the most out of this template.

To start a new project, create a new repo with the "Use this template" button. Upon doing do, the initial-commit.yml action will run - deleting this README and replacing it with project-README.md

Template features

This template is designed to give the user the minimum amount of useful features to get started with their research project quickly. Naturally, there is a tradeoff between adding features and introducing bloat and I have strived to make the template as minimal as possible, whilst also providing some example code and some boilerplate code. Here are the main features this template comes with:

  1. MNIST + ResNet example. By default, the template will include an MNIST DataModule and a ResNet LightningModule (and running train.py will use these out-of-the-box). These are common enough that they should be useful, and even if not, they provide useful examples of how to properly make models and datasets in Pytorch Lightning. Feel free to remove them if not needed.

  2. Logging with Weights and Biases. By default, train.py initialises a Weights and Biases logger and uses the two included logging callbacks to log some useful plots for image classification. Note: I intend to create a package with lots of useful logging callbacks at some point in the future, so these may be moved.

  3. Black format check GitHub action. On pushes and pull requests to main an action will run to check if the code is black formatted.

  4. Auto-generated README. When creating a repo with this template, this README will be deleted and replaced with project-specific-README.md. The action also performs a find and replace to insert the actual name of the repository where "REPO-NAME" is currently. The Action then self-destructs to ensure it never runs again and does not use up your actions minutes. If you have any ideas for anything else that would be useful to trigger when the repo is created, let me know :).

Note on CI testing: this template used to contain some standard tests for checking that your models are working properly. These have since been removed, and in the future, I may create a separate package for ML model and dataset testing. If you want some inspiration for setting up CI testing for you model, the most recent commit which still has these tests is f8887bf512f37ad099781e30b29d3c726cbfa967.

Using the CLI

We use the Python argument parser to create a CLI for training models. It is recommended to use this as much as possible, as it makes it easier to run hyperparameter sweeps and reduces the need to keep editing code between runs. The MNIST + ResNet examples demonstrate how to use argparse args with datasets and models. This is done with the add_argparse_args classmethods. Note: it is technically possible to remove the named arguments from the class initialsers and simply pass vars(args) into __init__(**kwargs), however, this template opts to keep the named arguments and pass args.named_argument to make it more explicit when initialising the classes.

The pl.Trainer module also has a powerful CLI and lets you control things like GPUs, floating point precision, logging and more from the command line.

Using the MNIST+ResNet example

It is simple to use the default example model out-of-the-box; for training, show available options by running:

python src/train.py --help

This will give a list of command line arguments you can use in the program - e.g. python src/train.py --max_epochs 50 --batch_size 8 --log_every_n_steps 5 --learning_rate 0.01.

This project integrates with Weights and Biases for logging and it is strongly recommended to use it (it's free!). By default, including the --logger True flag in the CLI will use Weights and Biases. When using Weights and Biases on a new machine, run wandb login in the terminal, and paste the API key from your weights and biases account when prompted to set it up.

lightning-template's People

Contributors

charl-ai avatar

Watchers

 avatar

lightning-template's Issues

Use Lightning add/from_argparse_args

Is your feature request related to a problem? Please describe.
Currently, we define our own add_argprse_args classmethods for both LightningModule and LightningDataModule. It turns out the latter has this built-in (the former does not but we can copy the same code).

Describe the solution you'd like
Change the classes to use the default functionality. LightningModule can replicate this functionality too.

Additional context
I've already made this change in my Kaggle-Knowledge-Competitions repo, so mostly just need to copy the code over

Callback memory leak

Severity: [high]

Describe the bug
Memory leak in model where all step outputs are stored. This causes every image,target and logits, tensor from an epoch to be cached, so memory increases linearly over an epoch. Crashes when working with larger images.

Expected behavior
Only tensors from a single batch should be cached. Memory used should remain roughly constant throughout an epoch.

Screenshots
image

Set up Pylint

Is your feature request related to a problem? Please describe.
We could use Pylint as part of CI and set up vscode to lint on save.

Describe the solution you'd like
Pylint setup with custom settings to prevent it from giving too many false positives

Describe alternatives you've considered
Could use an alternative such as flake8, or stick to current system which just uses black in CI and pylance on save.

Rename 'master' to 'main'

Is your feature request related to a problem? Please describe.
Master branch is no longer the preferred convention.

Describe the solution you'd like
Rename the branch to 'main'

Describe alternatives you've considered
Other names have been suggested but aren't as widely adopted as 'main'

Multiple template branches

Is your feature request related to a problem? Please describe.
It's hard to strike a balance between a minimal template and one with too many features... It's annoying to have to delete code when cloning the template, but it's also annoying to have to rewrite code often.

Describe the solution you'd like
Multiple template branches. main would be the most minimal, with other branches offering things like tests etc...
When cloning the project, you could either clone the main branch only for a minimal template, or clone all branches and merge or delete as necessary.

Describe alternatives you've considered
Could also remain with one branch and try to strike the perfect balance between features and minimalism. Moving some features to separate repositories and setting them up as packages could also work.

Implement dataset tests

Is your feature request related to a problem? Please describe.
Currently unit tests for datasets raise notimplemented errors.

Describe the solution you'd like
Modular test suite for dataset modules

Describe alternatives you've considered
N/A

Additional context
N/A

Create devcontainer

Is your feature request related to a problem? Please describe.
Currently using python venvs for requirements/reproducibility but it might be nice to use containers as well to pin OS/CUDA requirements.

Describe the solution you'd like
Would be nice to use docker with devcontainers as a method for reproducibility. You could still use the current method of creating a python venv and installing from requirements.txt, but you could also create a container to run the project. This can integrate with vscode devcontainers, so all someone needs to do is 'clone repo in container volume'.

Include .vscode folder

Is your feature request related to a problem? Please describe.
Would be nice to include a standard .vscode folder with standard settings, extensions, and debugging setup

Describe the solution you'd like
.vscode folder with preset files

Describe alternatives you've considered
Could set settings on a per-user per-project basis, but this seems like a waste of time

Boilerplate code for changing path in Notebooks doesn't always work

Severity: [medium]

Describe the bug
The boilerplate code provided for changing sys.path in notebooks doesn't work in all cases. On JupyterHub remotely it fails.

To Reproduce
Steps to reproduce the behavior:

  1. Run cell on JupyerHub RCS

Expected behavior
Correct sys.path and path variable

Screenshots
image

Additional context
This could be solved using the newly created helper function in utils.py

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.