-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeuralNetwork.py
282 lines (174 loc) · 6.26 KB
/
NeuralNetwork.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# coding: utf-8
# # Importing the modules
# In[1]:
from sklearn.cluster import KMeans
import tensorflow as tf
import numpy as np
import csv
import math
import matplotlib.pyplot
from itertools import islice
from matplotlib import pyplot as plt
import random
import pandas as pd
from keras.utils import np_utils
import pickle
import gzip
# # Extracting features and labels from the dataset
# In[2]:
filename = 'mnist.pkl.gz'
f = gzip.open(filename, 'rb')
training_data, validation_data, test_data = pickle.load(f, encoding='latin1')
f.close()
x = np_utils.to_categorical(np.array(training_data[1]),10)
valTarget = np_utils.to_categorical(np.array(validation_data[1]),10)
valFeature = validation_data[0]
testFeature = test_data[0]
testTarget = np_utils.to_categorical(np.array(test_data[1]),10)
# # Confusion Matrix Customization
# In[3]:
# import numpy as np
def plot_confusion_matrix(cm,
title='Confusion matrix',
cmap=None,
normalize=True):
import matplotlib.pyplot as plt
import numpy as np
import itertools
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
# # Keras Model
# In[4]:
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.callbacks import EarlyStopping, TensorBoard
import numpy as np
input_size = 784
drop_out = 0.2 # to remove overfitting we use dropout
first_dense_layer_nodes = 256
second_dense_layer_nodes = 10
def get_model():
model = Sequential()
model.add(Dense(first_dense_layer_nodes, input_dim=input_size))
model.add(Activation('relu'))
model.add(Dropout(drop_out))
model.add(Dense(second_dense_layer_nodes))
model.add(Activation('softmax'))
model.summary()
model.compile(optimizer='Adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
model=get_model()
# # Running the model on training data
# In[5]:
#hyper parameters
# validation_data = 0.1
num_epochs = 300
model_batch_size = 2048
tb_batch_size = 64
early_patience =100
tensorboard_cb = TensorBoard(log_dir='logs', batch_size= tb_batch_size, write_graph= True)
earlystopping_cb = EarlyStopping(monitor='val_loss', verbose=1, patience=early_patience, mode='min')
history = model.fit(training_data[0]
, x
, validation_data=(valFeature,valTarget)
, epochs=num_epochs
, batch_size=model_batch_size
, callbacks = [tensorboard_cb,earlystopping_cb]
)
# In[6]:
import pandas as pd
get_ipython().run_line_magic('matplotlib', 'inline')
df = pd.DataFrame(history.history)
df.plot(subplots=True, grid=True, figsize=(10,15))
# # Testing on MNIST validation and testing datasets
# In[7]:
from sklearn.metrics import confusion_matrix
test_loss, test_acc = model.evaluate(testFeature, testTarget)
print('Test Accuracy for test data: ' +str(test_acc))
print('Test Loss for test data: ' +str(test_loss))
val_loss, val_acc = model.evaluate(valFeature, valTarget)
print('Test Accuracy for validation data: ' +str(val_acc))
print('Test Loss for validation target: ' +str(val_loss))
Neural_prediction_mnist = []
for i,j in zip(testFeature,testTarget):
y = model.predict(np.array(i).reshape(-1,784))
Neural_prediction_mnist.append((y.argmax()))
# # Confusion Matrix of MNIST dataset
# In[8]:
conf_mat = confusion_matrix(test_data[1],Neural_prediction_mnist )
plot_confusion_matrix(cm = conf_mat,
normalize = False,
title = "Confusion Matrix")
# # Testing on USPS dataset
#
# In[9]:
from PIL import Image
import os
import numpy as np
image_size = 28
num_labels = 10
USPSMat = []
USPSTar = []
curPath = 'USPSdata/Numerals'
savedImg = []
for j in range(0,10):
curFolderPath = curPath + '/' + str(j)
imgs = os.listdir(curFolderPath)
for img in imgs:
curImg = curFolderPath + '/' + img
if curImg[-3:] == 'png':
img = Image.open(curImg,'r')
img = img.resize((28, 28))
savedImg = img
imgdata = (255-np.array(img.getdata()))/255
USPSMat.append(imgdata)
USPSTar.append(j)
def reformat(labels):
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return labels
USPSMat = np.array(USPSMat)
USPSTar= np.array(USPSTar)
test = np_utils.to_categorical(np.array(USPSTar),10)
# # Testing and Confusion Matrix of USPS dataset
# In[20]:
test_loss1, test_acc1 = model.evaluate(USPSMat, test)
print('Test Accuracy for USPS dataset: ' +str(test_acc1))
print('Test Loss for USPS dataset: ' +str(test_loss1))
Neural_prediction_usps = []
for i,j in zip(USPSMat,USPSTar):
y = model.predict(np.array(i).reshape(-1,784))
Neural_prediction_usps.append((y.argmax()))
conf_mat1 = confusion_matrix(USPSTar,Neural_prediction_usps )
plot_confusion_matrix(cm = conf_mat1,
normalize = False,
title = "Confusion Matrix")
# In[21]:
from sklearn.metrics import classification_report
print(classification_report(Neural_prediction_usps,
USPSTar))
# In[ ]: