# 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
``````

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)
``````

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()
``````

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'])
``````

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.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)
plt.present()
image_number += 1
``````

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

``````predict_img(mannequin)
``````

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

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

``````mannequin.save('hand_digit.h5')