Building ML models have become child’s play in recent days. With the advent of machine learning frameworks such as
scikit-learn, the ability to build a machine learning model has been brought down from days to minutes. In this article, we will be demonstrating how to productionize ML models using Python-based micro web framework
This post isn’t yet another post about MNIST Handwritten Digits Prediction. There are about hundreds of tutorials available on-line to tutor. Here’s a quick introduction, to understand all the mechanics of the prediction process in tensorflow for the MNIST datasets to help you get up and running.
Now that we are done with the basics, it is time to head over to https://github.com/datawrangl3r/mnistProduction and clone the project.
We are about to deploy an image prediction RESTful API, powered by Flask microframework.
The code in the repo was written and tested on Python V2.7.
## Step 1: > python 1_modelCreate.py ## Step 2 > python 2_predict.py
Step 1 mentioned above, creates the model to identify the handwritten digits using
Tensorflow. The output of the model is
mnist_model.ckpt which is used by the second script
2_predict.py. The devised model can be used to test the pictures of input digits found in the same repository. In the code, the
/predictint route, is attached to the function which predicts the number of the image. The
2_predict.py script is an API by itself served on port 5000. The
GET requests take an argument
imageName corresponding to the name of the image.
The project directory contains the
numerals_sample.jpg, from which one may crop the required digits out. For this demo, we shall look at
numba9.jpg present in the same directory.
2_predict.py script running, fire up the browser, and hit the following URL to test our model with the file
The model has a pretty good accuracy rate. The response is given as number 6.
Let’s evaluate the model with the image -
You may have noticed the response to be as 7, correctly predicted by the model as a response.
Let’s try out the image:
The response is received as number 5, which is weird for input as 9.
The prediction is wrong. However, Five does look a lot like 9 to the human eyes.
The model can be further trained and improved by adding more datasets to train on. Thus, our API can be really helpful in deploying the created model to production.
In this article, we have learned how to deploy the devised model to production by creating an API that loads the model and predicts based on the input. The accuracy of the model can be improved over time by training with more datasets. A separate route can be written to facilitate this.