Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

Hand Written digits classification using deep learning with keras?

Description

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

Input

MNIST data set. (keras inbuilt data set).

Output

Classification report
Accuracy score
Confusion matrix

Process

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.

  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 numpy as np
%matplotlib inline
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation,Flatten
from sklearn.metrics import confusion_matrix,classification_report, accuracy_score

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)

#print(X_train.shape)
#print(y_train.shape)

#Build the neural network model
model = Sequential()

#add layers to the model
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation=’sigmoid’))
model.add(Dense(50, activation=’sigmoid’))
model.add(Dense(10, activation=’sigmoid’))

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

#Fit the model
model.fit(X_train, y_train, epochs=15)

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

#Predict the test results
prediction = model.predict_classes(X_test)
length = len(prediction)
y_label = np.array(y_test)
predict_label = np.array(prediction)

#confusion matrix and classification report
print(“\n”)
print(“Confusion Matrix\n”,confusion_matrix(y_label,predict_label))
print(“\n”)
print(“Classification Report\n”,classification_report(y_label,predict_label))
print(“\n”)
print(“Accuracy : “,accuracy_score(y_label,predict_label)*100)

Screenshots
Hand Written digits classification using deep learning with keras
import necessary libraries
import matplotlib.pyplot as plt
classifying hand written digits using deep learning with keras
Load the data set from keras