Inspiration

We too often to see a beginner of Machine Learning feels overwhelmed with current getting started tutorials. Often, they get a too long jupyter notebook and guessing which part does something. They also expect to see how Machine Learning can help them, instead of just a graphic of loss and accuracy.

In this project, we create a simple, well-structured, PyTorch project with RestFul API and automation. A single command line and they are ready to go.

Some code that I use:

What it does

  • cookiecutter gh:tegarimansyah/pytorch2web_cookiecutter and you get all of your files
  • make install and you get all of your dependencies, including virtualenv in Jupyter Notebook
  • make serve and you get RestFul API in http://localhost:8081 with docs (Thanks to FastAPI)
  • make train and you can train your model

And this is how the folder structure :

{{cookiecutter.project_name}}
├── Makefile
├── README.md
├── app
│   ├── __init__.py
│   ├── architecture
│   │   ├── __init__.py        -> Data initiation (Datatrain, Datatest)
│   │   ├── NN.py              -> NN Architecture (LeNet)
│   │   └── train.py           -> Training Mechanism
│   ├── main.py                -> FastAPI Instance
│   └── predict                -> HTTP handler for prediction
│       ├── __init__.py
│       ├── service
│       │   ├── __init__.py
│       │   └── prediction.py   -> Logic for prediction
│       └── views.py            -> Routing for /predict
├── data                        -> Dataset and model folder
│   └── README.md
└── requirements.txt

So, if you just want to change the NN architecture, you only need to change NN.py. If you just want to change how you handle prediction from RestFul API, you only need to change prediction.py and so on.

How I built it

  • Learn how the PyTorch works
  • Separate the logic in several python files
  • Integrate with web framework (FastAPI)

Challenges I ran into

Prediction with real-world image needs some adjustment. And I need to learn more about how to transform some random image to fits our model.

Accomplishments that I'm proud of

I love automation, and I am satisfied with how I use makefile and cookiecutter for cutting down some hour to create a new ready-to-go ML project with PyTorch.

What I learned

I never tried PyTorch before, but I already learned Caffe, Tensorflow, and Keras. It's nice to know that PyTorch uses a pythonic way to build Machine Learning. It's very easy to integrate with FastAPI and PyTest.

What's next for pytorch2web

  • Prediction with real image via RestFul API
  • Integration with TensorBoard
  • Easily switching from jupyter notebook to python module
  • Front end and live prediction

Built With

  • cookiecutter
  • fastapi
  • makefile
  • pytoch
  • tensorboard
Share this project:

Updates