Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to build a Convolution neural Network for hand written digits classification using deep learning with keras?

Description

To build a Deep learning model for classifying hand written digits using CNN with keras

Input

MNIST data set. (keras inbuilt data set).

Output

Prediction result.

Accuracy score.

Process

  Load the data set from keras.

  Its the inbuilt data set, has 60,000 images for training and 10,000 for testing.

  Build the model.v

  Add the layers (Input layer, hidden layer and output layer).

  Fit the train data in model.

  Evaluate the model using test data.

  Apply the basic metrics.

  Print the results.

Sample Code

#import necessary libraries
import warnings
warnings.filterwarnings(“ignore”)
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten

#load mnist data set from keras
mnist = keras.datasets.mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

class_names = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’,
‘5’, ‘6’, ‘7’, ‘8’, ‘9’]

print(“Train images\n\n”)
def plot_train(train):
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train[i])
plt.show()
plot_train(X_train)

print(“Testing images\n\n”)
def plot_test(test):
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test[i])
plt.show()
plot_test(X_test)

#reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)

from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

#create model
model = Sequential()

#add model layers
model.add(Conv2D(64, kernel_size=3, activation=’relu’, input_shape=(28,28,1)))
model.add(Conv2D(32, kernel_size=3, activation=’relu’))
model.add(Flatten())
model.add(Dense(10, activation=’softmax’))

#compile the model
model.compile(optimizer=’adam’,
loss=’categorical_crossentropy’,
metrics=[‘accuracy’])

#Fit the model
model.fit(X_train, y_train, batch_size = 1000,epochs=4)

#Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test)

#Predict the test results
print(“Prediction results\n”)
prediction = model.predict(X_test[:10])
plt.imshow(prediction)
plt.show()
print(“Accuracy of the model\n”,test_acc*100)

Screenshots
build a Convolution neural Network for hand written digits classification using deep learning with keras
import necessary libraries
Load the data set from keras
Prediction result