-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_handwritten_digit_classification_using_deep_learning.py
207 lines (131 loc) · 4.79 KB
/
mnist_handwritten_digit_classification_using_deep_learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
"""MNIST Handwritten Digit Classification using Deep Learning.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1HXw77JnfQfFHAXrxmgkoilRbbBemiMpb
**About the MNIST dataset:**
---> It consists of 60,000 images with 28X28 dimension and 10,000 test image data. It is a type of grascale images and the image processing and cross vlidation part is already done in this dataset.
**Importing the Dependencies:(Libraries)**
"""
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
from google.colab.patches import cv2_imshow
from PIL import Image
import tensorflow as tf
tf.random.set_seed(3)
from tensorflow import keras
from keras.datasets import mnist
from tensorflow.math import confusion_matrix
"""**Loading the data from keras.datasets:**"""
(X_train, Y_train),(X_test, Y_test) = mnist.load_data()
type(X_train)
# shape of the numpy arrays:
print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)
# printing the 10th image
print(X_train[10])
# display the image fromm numpy array using matoltlib library:
plt.imshow(X_train[10])
plt.show()
# printing the corresponding label of the image
print(Y_train[10])
# display the image from numpy array using OpenCV Library:
cv2_imshow(X_train[10])
# printing the corresponding lebel to the image
print(Y_train[10])
"""**Image Label analysis:**"""
print(Y_train.shape, Y_test.shape)
"""We can use the labels like this or we can also use One Hot Encoding"""
# unique values in Y_train
print(np.unique(Y_train))
# unique values in Y_test
print(np.unique(Y_test))
# Scaling (Normalization) the values:
X_train = X_train/255
X_test = X_test/255
"""**Building the Nueral Network using Tensorflow and Keras:**"""
# Setting up the layers of the Neural Network
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(50, activation='relu'),
keras.layers.Dense(50, activation='relu'),
keras.layers.Dense(10, activation='sigmoid')
])
# compiling the neral network
model.compile(optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy']
)
# training the neural network
model.fit(X_train,Y_train,epochs=10)
"""Accuracy on our trining data = 98.8%
**Model Evaluation: (accuracy on test data to check if our neral network is overfitted or not.**
"""
loss, accuracy = model.evaluate(X_test,Y_test)
print(accuracy)
"""Accuracy on our testing data=96.8%"""
print(X_test.shape)
# display image using matplotlib library
plt.imshow(X_test[0])
plt.show()
# print the cooresponding lebel
print(Y_test[0])
# display image using OpenCV library
cv2_imshow(X_test[5])
# print the cooresponding lebel
print(Y_test[5])
Y_pred = model.predict(X_test)
print(Y_pred.shape)
print(Y_pred[0])
"""In case of ML, model.predict() will give us the labels directly. But, in DL, model.predict() gives the predictions probability of each class(labels) for that data point"""
# converting the prediction probabilities to class label
label_for_first_test_img = np.argmax(Y_pred[0])
print(label_for_first_test_img)
print(Y_pred)
Y_pred_labels = [np.argmax(i) for i in Y_pred]
print(Y_pred_labels)
"""Y_test ---> actual/true labels of our test data
Y_pred_labels ----> predicted labels of our test data
"""
conf_mat = confusion_matrix(Y_test, Y_pred_labels)
print(conf_mat)
plt.figure(figsize=(15,7))
sns.heatmap(conf_mat, annot=True, fmt='d', cmap='Blues')
plt.ylabel('True Labels')
plt.xlabel('Predicted Labels')
"""**Building a Predictive System:**"""
input_img_path ='/content/MNIST_img.jpg'
input_img = cv2.imread(input_img_path)
type(input_img)
print(input_img)
# display/ visualize the image
cv2_imshow(input_img)
print(input_img.shape)
#converting into grayscale image
grayscale = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY)
grayscale.shape
# resizing the image using OpenCV library
img_resize = cv2.resize(grayscale, (28,28))
print(img_resize.shape)
cv2_imshow(img_resize)
#Scaling the image
input_img_scaled = img_resize/255
print(input_img_scaled)
img_reshape = np.reshape(input_img_scaled,[1,28,28])
input_prediction = model.predict(img_reshape)
print(input_prediction)
# converting prediction probabilty of the image to class label
pred_img_label = np.argmax(input_prediction)
print(pred_img_label)
"""**Presictive System:**"""
nput_img_path =input('Path of the image to be predicted: ')
input_img = cv2.imread(input_img_path)
cv2_imshow(input_img)
grayscale = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY)
img_resize = cv2.resize(grayscale, (28,28))
input_img_scaled = img_resize/255
img_reshape = np.reshape(input_img_scaled,[1,28,28])
input_prediction = model.predict(img_reshape)
pred_img_label = np.argmax(input_prediction)
print('The Handwritten image is recognised as: ',pred_img_label)