-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.py
executable file
·295 lines (215 loc) · 10.2 KB
/
mnist.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import pickle as pkl
import struct
import seaborn as sn
import pandas as pd
import time
from scipy.spatial import distance
from sklearn.cluster import KMeans
def read_idx(filename):
with open(filename, 'rb') as f:
zero, data_type, dims = struct.unpack('>HBB', f.read(4))
shape = tuple(struct.unpack('>I', f.read(4))[0] for d in range(dims))
return np.fromstring(f.read(), dtype=np.uint8).reshape(shape)
################################################
# Functions for loading data #
################################################
# Load the first num_samples images from train dataset with corresponding labels:
def load_train_dataset(num_samples=10000):
# Maximum 60 000 samples
MAX_TRAIN_SAMPLES = 60000
num_samples = num_samples % MAX_TRAIN_SAMPLES
# Read from file
raw_train = read_idx('./mnist_datasets/train-images.idx3-ubyte')
train_labels = read_idx('./mnist_datasets/train-labels.idx1-ubyte')
# Flatten images
train_data = np.reshape(raw_train, (MAX_TRAIN_SAMPLES, 28*28))
# Truncate size to num_samples
train_data = train_data[:num_samples]
train_labels = train_labels[:num_samples]
return (train_data, train_labels)
# Load the first num_samples images from test dataset with corresponding labels:
def load_test_dataset(num_samples):
# Maximum 10000 samples
MAX_TEST_SAMPLES = 10000
num_samples = num_samples % MAX_TEST_SAMPLES
# Read from file
raw_test = read_idx('./mnist_datasets/t10k-images.idx3-ubyte')
test_labels = read_idx('./mnist_datasets/t10k-labels.idx1-ubyte')
# Flatten images
test_data = np.reshape(raw_test, (MAX_TEST_SAMPLES, 28*28))
# Truncate size to num_samples
test_data = test_data[:num_samples]
test_labels = test_labels[:num_samples]
return (test_data, test_labels)
##############################################
# Functions for calculations #
##############################################
def classify_img(nearest):
nearest_number_counts = [0]*10
highest = 0
number = -1
for i in range(len(nearest)):
index = nearest[i]
nearest_number_counts[index] += 1
for i in range(len(nearest_number_counts)):
if nearest_number_counts[i] > highest:
number = i
return number
def k_nearest_neighbours(data, data_labels, image, k_neighbours):
dist_and_label_list = []
for i in range (len(data)):
dist = distance.euclidean(data[i], image)
dist_and_label_list.append((dist, data_labels[i]))
dist_and_label_list = sorted(dist_and_label_list, key=lambda dist: dist[0])
labels = [ dist_and_label[1] for dist_and_label in dist_and_label_list[:k_neighbours] ]
return labels
def get_clusters(dataset, labels, classes, n_clusters=64):
label_and_clusters = [] # [ (label, clusters=[]) ]
for curr_class in classes:
indices = np.where(labels == curr_class)[0]
data_of_curr_class = dataset[indices]
clusters = KMeans(n_clusters=n_clusters).fit(data_of_curr_class).cluster_centers_
label_and_clusters.append( (curr_class, clusters) )
return label_and_clusters
def get_cluster_dataset(dataset, labels, n_clusters=64):
classes = range(10)
n_classes = len(classes)
label_and_clusters = get_clusters(dataset, labels, classes, n_clusters)
train_data = []
train_labels = [ [curr_class]*n_clusters for curr_class in classes ]
train_labels = np.array(train_labels).reshape(n_clusters*n_classes)
for (label, clusters) in label_and_clusters:
train_data.extend(clusters)
return (train_data, train_labels)
def get_confusion_matrix(labels_predicted, labels_true, classes):
confusion_matrix = []
for predicted_class in classes:
row = []
for true_class in classes:
# All occurences of current true_class in labels_true:
true_indices = np.where(labels_true == np.uint8(true_class))[0]
# All occurences of current predicted_class in labels_predicted:
predicted_indices = np.where(labels_predicted == np.uint8(predicted_class))[0]
# We want to find the number of elements where these two matches:
num_occurences = len( np.intersect1d(true_indices, predicted_indices) )
row.append(num_occurences)
confusion_matrix.append(row)
return np.array(confusion_matrix)
################################################
# Functions for plotting #
################################################
def plot_misclassified_images(images, labels_true, labels_predicted, num_images=12):
# Indices of some misclassified images
image_indices = np.where(labels_true != labels_predicted)[0]
image_indices = image_indices[:num_images]
# We want a square layout of the images
num_cols = np.uint16(np.floor(np.sqrt(num_images)))
num_rows = np.uint16(num_images / num_cols)
fig = plt.figure(figsize=(5, 5))
fig.suptitle("Some misclassified images")
for row in range(num_rows):
for col in range(num_cols):
i = (row * num_cols) + col
image_index = image_indices[i]
ax = fig.add_subplot(num_rows, num_cols, i+1)
ax.axis("off")
ax.set_title(f'T:{labels_true[image_index]}; P:{labels_predicted[image_index]}')
image = np.reshape(images[image_index, :], (28,28))
img_plot = ax.imshow(image)
img_plot.set_interpolation("nearest")
def plot_correctly_classified_images(images, labels_true, labels_predicted, num_images=12):
# Indices of some correctly classified images
image_indices = np.where(labels_true == labels_predicted)[0]
image_indices = image_indices[:num_images]
# We want a square layout of the images
num_cols = np.uint16(np.floor(np.sqrt(num_images)))
num_rows = np.uint16(num_images / num_cols)
fig = plt.figure(figsize=(5, 5))
fig.suptitle("Some correctly classified images")
for row in range(num_rows):
for col in range(num_cols):
i = (row * num_cols) + col
image_index = image_indices[i]
ax = fig.add_subplot(num_rows, num_cols, i+1)
ax.axis("off")
ax.set_title(f'T:{labels_true[image_index]}; P:{labels_predicted[image_index]}')
image = np.reshape(images[image_index, :], (28,28))
img_plot = ax.imshow(image)
img_plot.set_interpolation("nearest")
def plot_confusion_matrix(confusion_matrix, name):
df_cm = pd.DataFrame(confusion_matrix, index = [i for i in "0123456789"],
columns = [i for i in "0123456789"])
fig = plt.figure(figsize = (10,7))
fig.suptitle(name, fontsize=16)
sn.heatmap(df_cm, annot=True)
##########################################################
# Runtime functions: #
##########################################################
def run_nearest_neighbour_classifier(train_data, train_labels, test_data, test_labels):
start = time.time()
num_errors = 0
test_predicted = []
for i in range(len(test_data)):
nearest_neighbours = k_nearest_neighbours(train_data, train_labels, test_data[i], 1)
predicted_number = classify_img(nearest_neighbours)
test_predicted.append(predicted_number)
if predicted_number != test_labels[i]:
num_errors += 1
end = time.time()
print("Time used NN ", end - start)
error_rate = num_errors/len(test_data)*100
print("Error rate for NN using whole dataset:", error_rate, "%")
confusion_matrix = get_confusion_matrix(test_labels, test_predicted, classes=range(10))
plot_confusion_matrix(confusion_matrix, name="CM for NN using whole dataset")
plot_correctly_classified_images(test_data, test_labels, test_predicted)
plot_misclassified_images(test_data, test_labels, test_predicted)
def run_k_nearest_neighbour_classifier(train_data, train_labels, test_data, test_labels):
start = time.time()
num_errors = 0
test_predicted = []
NUM_NEIGHBOURS = 4
for i in range(len(test_data)):
nearest_neighbours = k_nearest_neighbours(train_data, train_labels, test_data[i], NUM_NEIGHBOURS)
predicted_number = classify_img(nearest_neighbours)
test_predicted.append(predicted_number)
if predicted_number != test_labels[i]:
num_errors += 1
end = time.time()
print("Time used KNN ", end - start)
error_rate = num_errors/len(test_data)*100
print("Error rate for KNN with k =",NUM_NEIGHBOURS, "using whole dataset:", error_rate,"%")
confusion_matrix = get_confusion_matrix(test_labels, test_predicted, classes=range(10))
plot_confusion_matrix(confusion_matrix, name="CM FOR KNN using whole dataset")
def run_cluster_classifier(train_data, train_labels, test_data, test_labels):
errors = 0
start_clustering = time.time()
train_cluster, train_labels = get_cluster_dataset(train_data, train_labels)
end_clustering = time.time()
print("Clustering time ", end_clustering -start_clustering)
test_predicted = []
start = time.time()
for i in range(len(test_data)):
k_nearest = k_nearest_neighbours(train_cluster, train_labels, test_data[i], 1)
predicted_number = classify_img(k_nearest)
test_predicted.append(predicted_number)
if predicted_number != test_labels[i]:
errors +=1
end = time.time()
print("NN time", end - start)
error_rate = errors/len(test_data)*100
print("Error rate for NN using cluster:", error_rate, "%")
confusion_matrix = get_confusion_matrix(test_labels, test_predicted, classes=range(10))
plot_confusion_matrix(confusion_matrix, name="CM for NN using cluster")
def main():
TRAIN_SIZE = 5000
TEST_SIZE = 100
train_data, train_labels = load_train_dataset(num_samples=TRAIN_SIZE)
test_data, test_labels = load_test_dataset(num_samples=TEST_SIZE)
run_nearest_neighbour_classifier(train_data, train_labels, test_data, test_labels)
run_k_nearest_neighbour_classifier(train_data, train_labels, test_data, test_labels)
run_cluster_classifier(train_data, train_labels, test_data, test_labels)
plt.show()
main()