Demystifying AI/ML Microservice with TensorFlow
Join the DZone community and get the full member experience.Join For Free
This tutorial will walk you through how to build and deploy a sample microservice application using Red Hat OpenShift Container Platform. The scope of this service is to perform prediction of handwritten digit from 0 to 9 by accepting image pixel array as a parameter. This sample application also provides a HTML file that offer you a canvas to draw your number and convert it into an image pixel array. The intention of this article is to give you a high level idea, the same concept can be taken to the next level for addressing many other complex use cases.
Image recognition is one of the major capabilities of deep learning. In this article, we will be identifying our own hand written digit. However, in order to accurately predict what digit is it, learning has to be performed, so that it understands the different characteristics of each digit as well as the subtle variations in writing the same digit. Thus, we need to train the model with a dataset of labelled handwritten digit. This is where MNIST dataset comes handy.
MNIST dataset is comprised of 60,000 small 28x28 square pixel gray scale images and 10,000 test images. These are handwritten single digit images from 0 to 9. Instead of downloading it manually, we can download it using Tensorflow Keras API
We will be performing following steps in this exercise
- Build and train the model using MNIST dataset
- Saving the learned model in persistent storage
- Build and launch microservice for prediction using the provided dockerfile
- Generate image pixel array of your handwritten digit using the provided HTML file
- Perform prediction using microservice by providing image pixel array as a parameter to this service.
You can find the dockerfile and python code by clicking the following git repository.
Below is the python code that performs building, compiling, training, and saving the model based on MNIST dataset. This code can also be executed in Jupyter Notebook. It is important to save this model in a persistent storage once the model training is successfully completed so that it can be used by the microservice application for prediction. Also make sure that saved model file should be accessible by the container running the microservice.
Prediction Microservice With Flask
Following instructions will help you with building the microservice using Red Hat OpenShift Container Platform. For the sake of simplicity, it’s a single source file and written in python. This source file is copied inside the container image via Dockerfile during the container image build process, which will be discussed later in this article. The sole purposes of this microservice to is to predict the handwritten digit using the already learned model. It simply takes the image pixel array as a parameter and predict it.
Below is the dockerfile that will be used to initiate the image build and launch the container in OpenShift platform. requirements.txt file contains the pre-requisite packages for this microservice to work. requirements.txt file contains the following.
Launch the Microservice
Now login to your Red Hat OpenShift cluster. Click Developer and then click Add to create an application. Click “From Dockerfile”. This will import your dockerfile from your git repo and initiate the image build and deploy the container.
Supply the Git Repo URL. The dockerfile for this project is located at https://github.com/mafzal786/tensorflow-microservice.git. Also give name to your application. Click create.
This will create the build config and build will start. To view the logs for your build, click “Builds” under Administrator tab and click the build as shown below. Once the build is completed, container image is pushed to your configured image registry.
After the build is completed and image is pushed to the registry, OpenShift will launch the container.
OpenShift will create a route for this service to be exposed by giving an externally reachable hostname. Service end point will be available in Networking →Routes tab. Clicking on the Location as shown below will launch the microservice in the browser.
The below figure shows when the microservice is launched in the browser.
Or run the following CLI as below
Canvas for Writing Digits
Below html file offers you a canvas to draw digit. Copy below code and save it as html on your computer and then launch it in the browser. Click “Convert” button to convert the image into image pixel array. Then click “Copy Image Pixel Array in Clipboard” button to copy the pixel array in your clipboard for providing it to the microservice as a parameter for prediction.
Draw Your Number
Perform Prediction Using the Microservice
Now pass this image pixel array to the microservice as a parameter as show below.
You can run the same in the browser as follows.
Published at DZone with permission of Muhammad Afzal. See the original article here.
Opinions expressed by DZone contributors are their own.