This repo covers deployment of an image classification model(ResNet50) using FastAPI, Redis and Docker.
For an in-depth project on Residual Networks: Residual Networks
For a more detailed FastAPI project: Using FastAPI to build a full-fledged social media backend
This repo is based on this medium article: Medium Article
- First docker-compose
docker-compose up
- Test with sample image provided
curl -X POST [email protected] http://localhost/predict
- Response if everything run correctly.
{"success":true,
"predictions":[
{"label":"dingo","probability":0.5237663984298706},
{"label":"Pembroke","probability":0.178588405251503},
{"label":"basenji","probability":0.1512373834848404},
{"label":"Eskimo_dog","probability":0.03126605227589607},
{"label":"bath_towel","probability":0.011432502418756485}]}
- Building a web server using FastAPI (with Uvicorn) to serve as a machine learning endpoint.
- Building a machine learning model server that serves a Keras image classification model (ResNet50 trained on ImageNet).
- Using Redis as a message queue to pass queries and responses between the web server and model server.
- Using Docker Compose to spin them all up!
The main function of the web server is to serve a /predict endpoint through which other applications will call our machine learning model.
When the endpoint is called, the web server routes the request to the Redis, which acts as an in-memory message queue for many concurrent requests.
The model server simply polls the Redis message queue for a batch of images, classifies the batch of images, then returns the results to Redis. The web server picks up the results and returns that.
Contains code for the webserver built using FastAPI Dockerfile - Docker file for copies the fastapi image and installing requirements. main.py - runs the FastAPI server, exposing the/predict endpoint which takes the uploaded image, serializes it, pushes it to Redis and polls for the resulting predictions. requirements.txt- contains all the requirements for the webserver.
FastAPI uses an ASGI(Asynchronous Server Gateway Interface) which means that requests are handled asynchronously. This means that they don't have to wait for others before them to finish doing their tasks.
FastAPI is also a micro web framework with many advantages, including out-of-the-box support for asynchronous code using the Python async and await keywords, and much more.