Tensorflow + Docker = Production ready AI product
Everyone is talking about training the Deep Learning models and fine tuning them but very few talks about the deployment and the scalability aspects.
In BotSupply, we focus not only on building accurate Machine Learning models, but also on delivering them to the clients with the greater efficiency.
As well said by Giovanni Toschi :
“Delivery and consistency make the difference, on top of the intrinsic quality of the solution” — Giovanni Toschi, COO BotSupply
In this article, we will learn to deploy a sentiment analysis model trained on “Character-level Convolutional Networks for Text Classification” (Xiang Zhang, Junbo Zhao, Yann LeCun) which uses character-level ConvNet networks for text classification.
Mostly reused code from https://github.com/dennybritz/cnn-text-classification-tf which was posted by Denny Britz. Check out his great blog post on CNN classification.
In this post we will implement a model similar to Kim Yoon's Convolutional Neural Networks for Sentence Classification…www.wildml.com
As explained in the above blog about the training process, I am pre-assuming that you have already trained your sentiment analysis model.
- Creating inference of sentiment analysis model
- Creating RESTful API of the model using Web.py
- Packaging your model into Docker containers
- Basic machine learning understanding
- Basic RESTful understanding
- Basic understanding of Docker (https://www.docker.com/get-docker)
Now we will create an inference to load the sentiment analysis model with Tensorflow which holds the models pre-trained weights.
The important thing to remember while creating an inference of the model is to load the exact graph which we created during the training process.
To accomplish this we load the model components such as placeholders by calling
Once we have all the graph nodes ready in memory we’ll create a prediction function engine() with accepts few parameters as defined below.
def engine(query = '' , senderid = '', senderName = '')
Few pre-processing is performed on the given query to transform it into vectors which will be passed into the graph nodes as input to make sentiment predictions.
Here is the complete code gist for the inference.
With the model loaded, create a web server that can accept strings using webpy.
We now have sentiment analysis as a service! Let’s test it out locally before deploying. First, start the server. Then make a GET request to the prediction service.
$ python sentiment_api.py
(in another terminal window)
$ curl -X GET \
'http://localhost:8080/sentiment?message=i really like this product' \
-H 'cache-control: no-cache' \
-H 'postman-token: f9870fd3-90c6-2a5b-fad6-55cbcb8a7398'
If everthing is working correctly, you’ll see a response back
“Response”: “Thank you for your positive response.”
We’ve got the model created and generating predictions. Time to deploy the model and package it into the docker image. Below, is a dockerfile file to set up and serve the prediction api.
MAINTAINER Rahul Kumar, www.hellorahulk.com
COPY sentiment/ /usr/local/python/
RUN pip install -r requirements.txt
CMD python sentiment_api.py 8180
Save this file named as
Dockerfile as shown below.
Once we are all setup with the code, its time to test the end-to-end package. To do so follow the below steps:
- Install Docker
- Open docker terminal and navigate to
docker build -t sentiment-api .
docker run -p 8180:8180 sentiment-api
http://0.0.0.0:8180/sentiment?message=i love itfrom your browser.
Voila !!! We just created a highly efficient, low latency and easy to distribute AI product which can run on any operating system.
I have created a GitHub repository with all the above mentioned steps.
tf-sentiment-docker — A docker image for sentiment analysis on tensorflowgithub.com
In my future post, I will explain how to create a flexible, high-performance serving system for machine learning models using TensorFlow Serving API’s (https://www.tensorflow.org/serving).
Please feel free to add any suggestions or any problems that you faced while working on the about steps.