martes, 1 de noviembre de 2022

Handwritten Digit Prediction using MNIST Dataset in Keras - By Jison M Johnson

https://valueml.com/handwritten-digit-prediction-using-mnist-dataset-in-keras/

Handwritten Digit Prediction using MNIST Dataset in Keras

In this tutorial, we will learn to predict handwritten digit using MNIST dataset in Keras with the corresponding Python code. We will use Keras API for this purpose.

There will be the following sections:

  1.  Importing Libraries
  2.  Importing Dataset
  3.  Data Preprocessing
  4.  Building the model
  5.  Training the model on the Dataset
  6.  Predicting the test results

We will be using CNN(Convolutional Neural Network).

What is Convolutional Neural Network?

Convolutional Neural Networks are a special type of neural network which comes under Deep Learning. This neural network is majorly used for image-related datasets. CNNs are used in a lot of domains, especially in the medical field. eg: The respiratory detection system can be built using CNN.

HANDWRITTEN DIGIT PREDICTION Using MNIST DATASET

We will build a model predicting the handwritten digit.

1. IMPORTING LIBRARIES

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
from keras.datasets import mnist

Pandas: A Python package which is a fast, powerful, and open-source data manipulation tool.
Numpy: A Python package used for scientific computing.
Sequential: The model used will be sequential.
Dense, Conv2D, Flatten, MaxPooling2D: These are the layers we will use.

So, these are the initial libraries you need to have. You can find documentation of pandas, NumPy, and keras.layers libraries at the end of this tutorial.

2. IMPORTING DATASET

We have already imported the mnist dataset from keras. We need to load the data.

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape)

OUTPUT:

(60000, 28, 28)

3. DATA PREPROCESSING

First, we will transform our dataset,i.e. reshape.

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
print(x_train.shape)

OUTPUT:

(60000, 28, 28, 1)

We will normalize our data between 0 and 1.

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

Now, we need to take care of the class variables. For that, we will use to_categorical function from np_utils class.

Originally:

print(y_train.shape)
print(y_train[:10])

OUTPUT:

(60000,)
[5 0 4 1 9 2 1 3 1 4]

After using the function:

from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
print(y_train.shape)
print(y_train)

OUTPUT:

(60000, 10)
[[0. 0. 0. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 1. 0.]]

4. BUILDING THE MODEL

We will build a CNN model.

model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), activation = 'relu', input_shape = (28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dense(10,activation = 'softmax'))

We have used one Conv2D layer, one Maxpooling layer, and two dense layers. In between, we have used a flatten layer.

Next, we will compile the model:

model.compile(optimizer='adam', loss = 'mean_squared_error', metrics=['accuracy'])

Thus, the model has been built.

5. TRAINING THE MODEL

To train our model with the dataset, we will pass x_train and y_train into the fit() function.

model.fit(x_train, y_train, epochs = 5)

I have given just five epochs. You can change according to your convenience.

OUTPUT:

Epoch 1/5
60000/60000 [==============================] - 38s 626us/step - loss: 0.0072 - accuracy: 0.9536
Epoch 2/5
60000/60000 [==============================] - 37s 623us/step - loss: 0.0028 - accuracy: 0.9823
Epoch 3/5
60000/60000 [==============================] - 37s 622us/step - loss: 0.0018 - accuracy: 0.9882
Epoch 4/5
60000/60000 [==============================] - 37s 619us/step - loss: 0.0015 - accuracy: 0.9910
Epoch 5/5
60000/60000 [==============================] - 37s 622us/step - loss: 0.0011 - accuracy: 0.9930
<keras.callbacks.callbacks.History at 0x7fca275eafd0>

Thus, we have trained our model with our dataset.

6. PREDICTING THE TEST RESULTS

image_index = 5555
plt.imshow(x_test[image_index].reshape(28, 28),cmap='Greys')
pred = model.predict(x_test[image_index].reshape(1, 28, 28, 1))
print(pred.argmax())

OUTPUT:

3
digit prediction

Thus, we have built a model using mnist dataset from Keras that will predict the handwritten digits.

Also, you may visit:

Thank you for reaching till here.

No hay comentarios:

Publicar un comentario