0% found this document useful (0 votes)
13 views5 pages

Hand Writing Using - CNN

Uploaded by

mrprs17122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Hand Writing Using - CNN

Uploaded by

mrprs17122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex.

No 2 : CHARACTER RECOGNITION USING CNN

AIM:
To write a python program for Character recognition using CNN.

ALGORITHM:

1. Import the libraries and load the dataset

 Import all the modules that we are going to need for training our model.

 The Keras library already contains some datasets and MNIST.

 The mnist.load_data() method returns us the training data, its labels and also the
testing data and its labels.

2. Preprocess the data

 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 dimension of the training data is (60000,28,28).

 The CNN model will require one more dimension so we reshape the matrix to
shape (60000,28,28,1).

3. Create the model

Create CNN model in Python data science project.

4. Train the model

 The model.fit() function of Keras will start the training of the model. It takes the
training data, validation data, epochs, and batch size.

5. Evaluate the model

 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

# Load the mnist data set


data = pd.read_csv("c:\\users\\mnist_train.csv", header = None)

data.shape

(60000, 785)

## split into train and test data


# Calculate the size of each partition.
numTrain = int(0.7 * data.shape[0])
print (numTrain)

#fix a seed for reproducibility


np.random.seed(123)

# Randomly shuffle the original dataset.


np.random.shuffle(data.values)

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

## So totally there are 10 classes available


trainData.loc[:, 0].value_counts()

1 4699
7 4420
3 4297
9 4196
6 4156
4 4133
0 4124
2 4100
8 4076
5 3799
Name: 0, dtype: int64

## spliting the training data into X and Y


trainX = trainData.loc[:, 1:].values
trainY = trainData.loc[:, 0].values

## spliting the test data into X and Y


testX = testData.loc[:, 1:].values
testY = testData.loc[:, 0].values

trainY

array([4, 9, 8, ..., 1, 3, 3], dtype=int64)

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


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

# convert class vectors to binary class matrices


from tensorflow.python.keras.utils.np_utils import to_categorical
num_classes = 10
y_train = to_categorical(trainY, num_classes)
y_test = to_categorical(testY, num_classes)

trainY[0]

y_train[0]

array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], dtype=float32)

## Re-Shaping the data to fit the model

input_shape = (28, 28, 1)


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

x_train shape: (42000, 28, 28, 1)


42000 train samples
18000 test samples

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

## Evaluation of the Model

score = model.evaluate(x_test, y_test, verbose=0)


print('Test loss:', score[0])
print('Test accuracy:', score[1])

## Model Training is done here.

testX = testData.loc[:, 1:].values


testX.shape

## Sample prediction for the first item ( so i am taking x_test[0],


you can change the numbers 1 ,2, 3 to look at different items)
result = model.predict(x_test[0].reshape(1,28,28,1))

result

## Predicte number :: Gettin g the value with max probability


np.argmax(result)

## So the predicted number is "0",, lets check if in the data we had


ZERO. for the data.

## Printing the a number based on the

# plot a sample image.


sampleRow = testData.loc[42000, 1:].values
np.shape(sampleRow)
imageData = sampleRow.reshape(28, 28)
print (sampleRow.shape)

from matplotlib import pyplot as plt


from matplotlib.pyplot import imshow

print ("Label: ", testData.loc[42000, 0])


# plot()
# title("label: " + str(trainData.loc[0, 0]))
imshow(imageData, cmap='Greys_r',
interpolation='None')

## you can continue to change and keep printing new variables.

You might also like