Hand Digit Prediction using CNN and Tensorflow

On this weblog, I am going to present you methods to practice a neural community mannequin utilizing the MNIST dataset, in addition to methods to predict the digits in them utilizing photographs.

Be sure you have the Tensorflow bundle put in, as it will likely be utilised as a Deep Studying library right here.

Let’s import all packages that are needed for us

import os  
import cv2 
import matplotlib.pyplot as plt 
import tensorflow as tf 
import numpy as np
Enter fullscreen mode

Exit fullscreen mode

After we have imported all the important packages, we’ll have to import the MNIST dataset, which is able to allow us to practice our mannequin. We’ll cut up the dataset into coaching and testing after it is imported.

mnist = tf.keras.datasets.mnist 
(x_train , y_train) , (x_test , y_test) = mnist.load_data() 
x_train = tf.keras.utils.normalize(x_train , axis = 1)
x_test = tf.keras.utils.normalize(x_test , axis=1) 
Enter fullscreen mode

Exit fullscreen mode

It is now time to place our neural community mannequin collectively. Let’s make our Community mannequin with Keras.

We’ll use a Flatten layer to reshape our picture’s dimensions to a single dimension. A Dense layer of 128 neurons , 128 neurons and 64 neurons will represent the second , third and fourth layers, respectively. The ultimate output layer is the Dense layer, which has 10 neurons as a result of our MNIST dataset has ten completely different lessons.

mannequin = tf.keras.Sequential() 
mannequin.add(tf.keras.layers.Flatten(input_shape = (28 , 28))) 
mannequin.add(tf.keras.layers.Dense(128 , activation='relu'))
mannequin.add(tf.keras.layers.Dense(128 , activation='relu'))
mannequin.add(tf.keras.layers.Dense(64 , activation='relu'))
mannequin.add(tf.keras.layers.Dense(10 , activation='softmax')) 
Enter fullscreen mode

Exit fullscreen mode

Later let’s compile the community mannequin by specifying adam for optimizer , accuracy for metrices and sparse_categorical_crossentropy for loss

mannequin.compile(optimizer='adam' , loss='sparse_categorical_crossentropy' , metrics=['accuracy']) 
Enter fullscreen mode

Exit fullscreen mode

Lastly, we’ll use the match operate to coach our mannequin.

We should check our mannequin after it has been educated. We’ll be offering enter photographs that had been generated with figma.
Let’s make a folder referred to as digits after which put our digit photographs inside it as digit1.png, digit2.png, and so forth.

def predict_img(mannequin):  
    image_number = 1
    whereas os.path.isfile(f"digits/digit{image_number}.png"):
        img = cv2.imread(f"digits/digit{image_number}.png")[:,:,0] 
        img = cv2.resize(img,(28 , 28))     
        img = np.invert(np.array([img]))
        prediction = mannequin.predict(img) 
        print(f"This digit might be a {np.argmax(prediction)}") 
        plt.imshow(img[0] , cmap = plt.cm.binary)
        image_number += 1 
Enter fullscreen mode

Exit fullscreen mode

Let’s use the mannequin as a parameter to name this operate.

Enter fullscreen mode

Exit fullscreen mode

Because of this, you will get a sequence of images like those under

digit 1

digit 2

Now, let’s attempt in a special method by saving our mannequin as hand_digit.h5 and utilizing that mannequin to make our predictions.


loaded_model = tf.keras.fashions.load_model('hand_digit.h5') 

loss , accuracy = mannequin.consider(x_test , y_test ) 

Enter fullscreen mode

Exit fullscreen mode

Because of this, our remaining output will seem like this



Because of this, I’ve developed different networks and labored on them. The jupyter file may be discovered on my github repository.

Github Repo : https://github.com/ndrohith09/Hand-digit-classificaiton-using-MNIST-dataset-and-CNN

Hope i feel this weblog lets you create your personal CNN mannequin for Hand Digit Recognition

Add a Comment

Your email address will not be published. Required fields are marked *