Technology

SAP Data Intelligence ML Scenario: Digit Recognition using Tensor flow

In this post, we will use the famous MNIST Dataset to construct simple neural networks capable of classifying handwritten digits. In other words, when our algorithm is given an input, it will print what kind of digit this input represents.

For example if we pass the image of digit 7 as a input to the code we will get the name of digit in output .

In our case, the target is the digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and the observations are the intensity and the relative position of the pixels. After some training, a “function” that maps inputs (digit image) to the desired outputs can be generated (type of digit). The only concern is how well this map process is going to happen. While attempting to produce this “function” the training process continues until the model achieves the desired level of accuracy of the training data.

What is MNIST?

The MNIST is a handwritten number database with a training set of 60,000 examples and a test set of 10,000 examples. It is a subset of NIST’s accessible larger package. The digits in a fixed-size image have been size-normalized and centered.

Import the MNIST dataset using TensorFlow built-in feature

import tensorflow as tf
from keras.datasets import mnist
# load dataset
(trainX, trainy), (testX, testy) = mnist.load_data()

Normalize the data

(x_train, y_train),(x_test, y_test)= tf.keras.datasets.mnist.load_data()
x_train = x_train/ 255.0,
x_test = x_test/ 255.0

Train the Model

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape =(28,28)),
    tf.keras.layers.Dense(128,activation ='relu'),
    tf.keras.layers.Dense(10,activation = 'softmax')])
model.compile(optimizer ='adam', loss ='sparse_categorical_crossentropy',
              metrics = ['accuracy'])
model.fit(x_train,y_train, validation_data = (x_test, y_test), epochs = 10)

Make Prediction

To make a prediction on fresh images, we can use our model.

The model assumes that new images are grayscale, that they have been aligned in such a way that one image includes one handwritten centered digit, and that the image size is 28 to 28 pixels square.

# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
 
# load and prepare the image
def load_image(filename):
	# load the image
	img = load_img(filename, grayscale=True, target_size=(28, 28))
	# convert to array
	img = img_to_array(img)
	# reshape into a single sample with 1 channel
	img = img.reshape(1, 28, 28)
	# prepare pixel data
	img = img.astype('float32')
	img = img / 255.0
	return img
 
# load an image and predict the class
def run_example():
    # load the image
    img = load_image('/vrep/vflow/zero.jpg')
    #print(img)
    # load model
    #model = load_model('final_model.h5')
    # predict the class
    digit = model.predict_classes(img)
    print('number is :')
    print(digit[0])
 
# entry point, run the example
run_example();

We can load the picture first, force it to be in grayscale format, and force the size to be 28 pixels by 28 pixels. You can then resize the loaded image to have a single channel and reflect in a dataset a single sample. This is implemented by the load image() function and will return the loaded image ready for classification.

Input image for Prediction:

Output result:

We’re going to pretend that this is an entirely new and unseen image, prepared in the way needed, and see if we might use our model to predict the integer represented by the image (e.g. we expect “5”).

Now we will implement the digit classification code in SAP Data Intelligence

First we will use the Python operator with tag “Tensorflow” after grouping

Copy the code into python operator

from keras.datasets import mnist
import tensorflow as tf
import numpy as np
# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import io
def on_input(data):
    # load dataset
    (trainX, trainy), (testX, testy) = mnist.load_data()
    
    #nomaise the data
    (x_train, y_train),(x_test, y_test)= tf.keras.datasets.mnist.load_data()
    x_train = x_train/ 255.0,
    x_test = x_test/ 255.0
    
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape =(28,28)),
        tf.keras.layers.Dense(128,activation ='relu'),
        tf.keras.layers.Dense(10,activation = 'softmax')])
    model.compile(optimizer ='adam', loss ='sparse_categorical_crossentropy',
                  metrics = ['accuracy'])
    model.fit(x_train,y_train, validation_data = (x_test, y_test), epochs = 10)
    
    # load and prepare the image
    def load_image(filename):
    	# load the image
    	img = load_img(filename, grayscale=True, target_size=(28, 28))
    	# convert to array
    	img = img_to_array(img)
    	# reshape into a single sample with 1 channel
    	img = img.reshape(1, 28, 28)
    	# prepare pixel data
    	img = img.astype('float32')
    	img = img / 255.0
    	return img
     
    # load an image and predict the class
    def run_example():
        # load the image
        img = load_image((data))
        #print(img)
       
        # predict the class
        digit = model.predict_classes(img)
        api.send("output",'Predicted digit is :'+ str(digit[0]))
      
    # entry point, run the example
    run_example()
              
api.set_port_callback("input", on_input)

Now connect the read file operator with python operator, Using read file we will pass the source input image path to the python operator.

We can upload the image in DI_DATA_LAKE or under Workspace . In this blog we have placed the images under workspace:

Read file operator configuration

Final Pipeline:

When you execute the pipeline , you will get the output in wiretap operator.As shown in read operator figure we have used “zero digit” image as a input .

Input Image:

Wiretap output:

Dataset images on which we tested:

After reading this article , hope you have got the basic understanding of doing image classification in SAP DI Using tensor flow