Deploying a Deep Learning Image Classification Model with Fastai, Python, and NodeJS
TL|DR: Use this to easily deploy a FastAI Python model using NodeJS.
You’ve processed your data and trained your model and now it’s time to move it to the cloud.
If you’ve used a Python-based framework like fastai to build your model, there are several excellent solutions for deployment like Django or Starlette. But many web devs prefer to work in NodeJS, especially if your model is only part of a broader application.
I extended Navjot’s shared NodeJS/Python environment into a simple, minimal boilerplate for a NodeJS deployment of an image classification model. The deep learning model was made with the fastai library. Although fastai and our model were built in Python, we can expose the model to users from NodeJS.
How does it work?
The deployment works using three main modules:
Our Express server in
server.js provides a standard, RESTful API to the outside world. Nothing new here!
But our Express server also references our
PythonConnector, which serves as a middle man between the worlds of NodeJS and Python.
const PythonConnector = require('./PythonConnector.js');
Specifically, on startup
PythonConnector spawns a python3 process that sets up our
PythonServer. It also negotiates and maintains a socket connection to
PythonServer through zerorpc.
When the client makes a request to a given endpoint from our
server.js, such as
/predict, our Express server commands the
PythonConnector middleman to invoke a function in our Python environment via zerorpc.
Our Python environment returns some JSON, which can be processed and forwarded along to our client.
Here’s how that looks when a user sends an image for classification.
First our NodeJS server gets the request…
… and calls out to
… which makes a prediction from our fastai model:
You can use the boilerplate out of the box. The included model classifies black bears, teddy bears, and grizzly bears.
If you’d like to use the boilerplate for your own project, you can customize the model with instructions available on Github.
Thanks for reading! You can check out other writing and projects at my website.