-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_functions.py
143 lines (124 loc) · 5.97 KB
/
main_functions.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
import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np
from cnn import get_mini_batch, fc, relu, conv, pool2x2, flattening
from cnn import train_slp_linear, train_slp, train_mlp, train_cnn
def main_slp_linear():
mnist_train = sio.loadmat('./mnist_train.mat')
mnist_test = sio.loadmat('./mnist_test.mat')
im_train, label_train = mnist_train['im_train'], mnist_train['label_train']
im_test, label_test = mnist_test['im_test'], mnist_test['label_test']
batch_size = 32
im_train, im_test = im_train / 255.0, im_test / 255.0
mini_batch_x, mini_batch_y = get_mini_batch(im_train, label_train, batch_size)
w, b = train_slp_linear(mini_batch_x, mini_batch_y)
sio.savemat('slp_linear.mat', mdict={'w': w, 'b': b})
acc = 0
confusion = np.zeros((10, 10))
num_test = im_test.shape[1]
for i in range(num_test):
x = im_test[:, [i]]
y = fc(x, w, b)
l_pred = np.argmax(y)
confusion[l_pred, label_test[0, i]] = confusion[l_pred, label_test[0, i]] + 1
if l_pred == label_test[0, i]:
acc = acc + 1
accuracy = acc / num_test
for i in range(10):
confusion[:, i] = confusion[:, i] / np.sum(confusion[:, i])
label_classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
visualize_confusion_matrix(confusion, accuracy, label_classes, 'Single-layer Linear Perceptron Confusion Matrix')
def main_slp():
mnist_train = sio.loadmat('./mnist_train.mat')
mnist_test = sio.loadmat('./mnist_test.mat')
im_train, label_train = mnist_train['im_train'], mnist_train['label_train']
im_test, label_test = mnist_test['im_test'], mnist_test['label_test']
batch_size = 32
im_train, im_test = im_train / 255.0, im_test / 255.0
mini_batch_x, mini_batch_y = get_mini_batch(im_train, label_train, batch_size)
w, b = train_slp(mini_batch_x, mini_batch_y)
sio.savemat('slp.mat', mdict={'w': w, 'b': b})
acc = 0
confusion = np.zeros((10, 10))
num_test = im_test.shape[1]
for i in range(num_test):
x = im_test[:, [i]]
y = fc(x, w, b)
l_pred = np.argmax(y)
confusion[l_pred, label_test[0, i]] = confusion[l_pred, label_test[0, i]] + 1
if l_pred == label_test[0, i]:
acc = acc + 1
accuracy = acc / num_test
for i in range(10):
confusion[:, i] = confusion[:, i] / np.sum(confusion[:, i])
label_classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
visualize_confusion_matrix(confusion, accuracy, label_classes, 'Single-layer Perceptron Confusion Matrix')
def main_mlp():
mnist_train = sio.loadmat('./mnist_train.mat')
mnist_test = sio.loadmat('./mnist_test.mat')
im_train, label_train = mnist_train['im_train'], mnist_train['label_train']
im_test, label_test = mnist_test['im_test'], mnist_test['label_test']
batch_size = 32
im_train, im_test = im_train / 255.0, im_test / 255.0
mini_batch_x, mini_batch_y = get_mini_batch(im_train, label_train, batch_size)
w1, b1, w2, b2 = train_mlp(mini_batch_x, mini_batch_y)
sio.savemat('mlp.mat', mdict={'w1': w1, 'b1': b1, 'w2': w2, 'b2': b2})
acc = 0
confusion = np.zeros((10, 10))
num_test = im_test.shape[1]
for i in range(num_test):
x = im_test[:, [i]]
pred1 = fc(x, w1, b1)
pred2 = relu(pred1)
y = fc(pred2, w2, b2)
l_pred = np.argmax(y)
confusion[l_pred, label_test[0, i]] = confusion[l_pred, label_test[0, i]] + 1
if l_pred == label_test[0, i]:
acc = acc + 1
accuracy = acc / num_test
for i in range(10):
confusion[:, i] = confusion[:, i] / np.sum(confusion[:, i])
label_classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
visualize_confusion_matrix(confusion, accuracy, label_classes, 'Multi-layer Perceptron Confusion Matrix')
def main_cnn():
mnist_train = sio.loadmat('./mnist_train.mat')
mnist_test = sio.loadmat('./mnist_test.mat')
im_train, label_train = mnist_train['im_train'], mnist_train['label_train']
im_test, label_test = mnist_test['im_test'], mnist_test['label_test']
batch_size = 32
im_train, im_test = im_train / 255.0, im_test / 255.0
mini_batch_x, mini_batch_y = get_mini_batch(im_train, label_train, batch_size)
w_conv, b_conv, w_fc, b_fc = train_cnn(mini_batch_x, mini_batch_y)
sio.savemat('cnn.mat', mdict={'w_conv': w_conv, 'b_conv': b_conv, 'w_fc': w_fc, 'b_fc': b_fc})
# could use following two lines to replace above two lines if only want to check results
# data = sio.loadmat('cnn.mat')
# w_conv, b_conv, w_fc, b_fc = data['w_conv'], data['b_conv'], data['w_fc'], data['b_fc']
acc = 0
confusion = np.zeros((10, 10))
num_test = im_test.shape[1]
for i in range(num_test):
x = im_test[:, [i]].reshape((14, 14, 1), order='F')
pred1 = conv(x, w_conv, b_conv) # (14, 14, 3)
pred2 = relu(pred1) # (14, 14, 3)
pred3 = pool2x2(pred2) # (7, 7, 3)
pred4 = flattening(pred3) # (147, 1)
y = fc(pred4, w_fc, b_fc) # (10, 1)
l_pred = np.argmax(y)
confusion[l_pred, label_test[0, i]] = confusion[l_pred, label_test[0, i]] + 1
if l_pred == label_test[0, i]:
acc = acc + 1
accuracy = acc / num_test
for i in range(10):
confusion[:, i] = confusion[:, i] / np.sum(confusion[:, i])
label_classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
visualize_confusion_matrix(confusion, accuracy, label_classes, 'CNN Confusion Matrix')
def visualize_confusion_matrix(confusion, accuracy, label_classes, name):
plt.title("{}, accuracy = {:.3f}".format(name, accuracy))
plt.imshow(confusion)
ax, fig = plt.gca(), plt.gcf()
plt.xticks(np.arange(len(label_classes)), label_classes)
plt.yticks(np.arange(len(label_classes)), label_classes)
ax.set_xticks(np.arange(len(label_classes) + 1) - .5, minor=True)
ax.set_yticks(np.arange(len(label_classes) + 1) - .5, minor=True)
ax.tick_params(which="minor", bottom=False, left=False)
plt.show()