Hand Writing Using - CNN
Hand Writing Using - CNN
AIM:
To write a python program for Character recognition using CNN.
ALGORITHM:
Import all the modules that we are going to need for training our model.
The mnist.load_data() method returns us the training data, its labels and also the
testing data and its labels.
The image data cannot be fed directly into the model so we need to perform
some operations and process the data to make it ready for our neural network.
The CNN model will require one more dimension so we reshape the matrix to
shape (60000,28,28,1).
The model.fit() function of Keras will start the training of the model. It takes the
training data, validation data, epochs, and batch size.
10,000 images in our dataset which will be used to evaluate how good our
model works.
The testing data was not involved in the training of the data therefore, it is new
data for our model.
The MNIST dataset is well balanced so we can get around 99% accuracy.
PROGRAM
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import pandas as pd
import numpy as np
import matplotlib as plt
data.shape
(60000, 785)
# create the train, test & validate split using list slicing notation.
trainData = data[:numTrain]
testData = data[numTrain:]
print (trainData.shape)
print (testData.shape)
42000
(42000, 785)
(18000, 785)
1 4699
7 4420
3 4297
9 4196
6 4156
4 4133
0 4124
2 4100
8 4076
5 3799
Name: 0, dtype: int64
trainY
trainY[0]
y_train[0]
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], dtype=float32)
## Model Creation.
batch_size = 128
# num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,
3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer='rm
sprop',metrics=['accuracy'])
## Model Fit
hist = model.fit(x_train,
y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=
(x_test, y_test))
print("The model has successfully trained")
Epoch 1/10
329/329 [==============================] - 134s 400ms/step - loss:
0.2518 - accuracy: 0.9218 - val_loss: 0.0637 - val_accuracy: 0.9797
Epoch 2/10
329/329 [==============================] - 129s 393ms/step - loss:
0.0815 - accuracy: 0.9747 - val_loss: 0.0430 - val_accuracy: 0.9862
Epoch 3/10
329/329 [==============================] - 125s 381ms/step - loss:
0.0575 - accuracy: 0.9823 - val_loss: 0.0410 - val_accuracy: 0.9871
Epoch 4/10
329/329 [==============================] - 109s 333ms/step - loss:
0.0457 - accuracy: 0.9861 - val_loss: 0.0429 - val_accuracy: 0.9870
Epoch 5/10
329/329 [==============================] - 115s 350ms/step - loss:
0.0380 - accuracy: 0.9885 - val_loss: 0.0317 - val_accuracy: 0.9899
Epoch 6/10
329/329 [==============================] - 116s 351ms/step - loss:
0.0305 - accuracy: 0.9906 - val_loss: 0.0296 - val_accuracy: 0.9911
Epoch 7/10
329/329 [==============================] - 124s 377ms/step - loss:
0.0276 - accuracy: 0.9917 - val_loss: 0.0301 - val_accuracy: 0.9908
Epoch 8/10
329/329 [==============================] - ETA: 0s - loss: 0.0242 -
accuracy: 0.9922
result