-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
292 lines (229 loc) · 8.96 KB
/
main.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
import numpy as np
from layers import FCLayer, ActivationLayer, Network
from activation_functions import relu, relu_prime, tanh, tanh_prime
from loss_functions import cross_entropy_softmax, cross_entropy_softmax_prime, mean_square_error, mean_square_error_prime
from keras.datasets import mnist
from keras.utils import np_utils
import time
import sys
import tkinter
def tanh_tanh_tanh_mse_network():
# The network provided from the site where I stole all this code from: Omar Aflak, https://towardsdatascience.com/math-neural-network-from-scratch-in-python-d6da9f29ce65
net = Network()
net.add(FCLayer(784, 100))
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(100, 50))
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(50, 10))
net.add(ActivationLayer(tanh, tanh_prime))
net.use(mean_square_error, mean_square_error_prime)
return net
# sample: 1000, epoch: 35
# 1. accuracy: 79.90%, time: 42.4911s
# 2. accuracy: 81.13%, time: 42.3893s
# 3. accuracy: 79.65%, time: 42.3747s
# sample: 2000, epoch: 35
# 1. accuracy: 86.61%, time: 84.7035s
# 2. accuracy: 85.04%, time: 85.1274s
# 3. accuracy: 84.22%, time: 84.0151s
# sample: 4000, epoch: 35
# 1. accuracy: 88.95%, time: 168.2937s
# 2. accuracy: 89.42%, time: 172.6208s
# 3. accuracy: 88.30%, time: 169.3265s
# sample: 8000, epoch: 35
# 1. accuracy: 91.50%, time: 335.0033s
# 2. accuracy: 91.09%, time: 337.2939s
# 3. accuracy: 91.56%, time: 333.9224s
# sample: 16000, epoch: 35
# 1. accuracy: 93.28%, time: 674.4695s
# 2. accuracy: 93.56%, time: 673.6062s
# 3. accuracy: 93.23%, time: 667.7904s
def relu_relu_softmax_cross_entropy_network():
# Network that almost every other tutorial uses
net = Network()
net.add(FCLayer(784, 100))
net.add(ActivationLayer(relu, relu_prime))
net.add(FCLayer(100, 50))
net.add(ActivationLayer(relu, relu_prime))
net.add(FCLayer(50, 10))
#softmax is supposed to be the last layer, but I combined it with the loss function
net.use(cross_entropy_softmax, cross_entropy_softmax_prime)
return net
# sample: 1000, epoch: 35
# 1. accuracy: 78.39%, time: 44.8030s
# 2. accuracy: 79.67%, time: 44.7586s
# 3. accuracy: 79.22%, time: 44.2722s
# sample: 2000, epoch: 35
# 1. accuracy: 85.27%, time: 91.7738s
# 2. accuracy: 85.00%, time: 89.7378s
# 3. accuracy: 86.01%, time: 90.1282s
# sample: 4000, epoch: 35
# 1. accuracy: 88.31%, time: 181.8210s
# 2. accuracy: 88.45%, time: 177.0105s
# 3. accuracy: 88.66%, time: 177.7447s
# sample: 8000, epoch: 35
# 1. accuracy: 90.98%, time: 353.3294s
# 2. accuracy: 91.83%, time: 356.6858s
# 3. accuracy: 91.32%, time: 358.8333s
# sample: 16000, epoch: 35
# 1. accuracy: 93.70%, time: 709.4794s
# 2. accuracy: 94.13%, time: 715.7597s
# 3. accuracy: 94.37%, time: 705.6836s
# sample: 32000, epoch: 35
# 1. accuracy: 95.38%, time: 1421.4080s
def get_data():
# Most of the code was from: Omar Aflak, https://towardsdatascience.com/math-neural-network-from-scratch-in-python-d6da9f29ce65
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28 * 28)
x_train = x_train.astype('float')
x_train /= 255
y_train = np_utils.to_categorical(y_train)
x_test = x_test.reshape(x_test.shape[0], 28 * 28)
x_test = x_test.astype('float')
x_test /= 255
y_test = np_utils.to_categorical(y_test)
return (x_train, y_train), (x_test, y_test)
def train():
target_network = sys.argv[2]
output_file = sys.argv[3]
start_index = int(sys.argv[4])
end_index = int(sys.argv[5])
epochs = int(sys.argv[6])
learning_rate = float(sys.argv[7])
net = globals()[target_network]()
(x_train, y_train), (x_test, y_test) = get_data()
start_time = time.time()
net.train(x_train[start_index:end_index], y_train[start_index:end_index], epochs, learning_rate)
elapsed_time = time.time() - start_time
print(f"time={elapsed_time:.4f}")
net.save(output_file)
def predict():
target_network = sys.argv[2]
input_file = sys.argv[3]
start_index = int(sys.argv[4])
end_index = int(sys.argv[5])
net = globals()[target_network]()
net.load(input_file)
(x_train, y_train), (x_test, y_test) = get_data()
matches = 0
samples = end_index - start_index
for i in range(start_index, end_index):
guess = np.argmax(net.predict(x_test[i]))
actual = np.argmax(y_test[i])
print(f"guess={guess}, actual={actual}, match={guess==actual}")
if actual == guess:
matches += 1
print(f"accuracy={matches * 100/samples:.2f}%")
def draw():
def increase_thickness(event):
if event.char != 't': return
nonlocal thickness
nonlocal max_thickness
thickness += 1
if thickness > max_thickness:
thickness = max_thickness
thickness_label.config(text=f"THICKNESS: {thickness}")
def decrease_thickness(event):
if event.char != 'r': return
nonlocal thickness
nonlocal min_thickness
thickness -= 1
if thickness < min_thickness:
thickness = min_thickness
thickness_label.config(text=f"THICKNESS: {thickness}")
def switch_erasing(event):
if event.char != 'e': return
nonlocal is_erasing
is_erasing = not is_erasing
erasing_label.config(text=f"IS ERASING: {is_erasing}")
def turn_on_draw(event):
nonlocal hold_down
hold_down = True
def turn_off_draw(event):
nonlocal hold_down
hold_down = False
def draw_on_canvas(event):
nonlocal hold_down
if not hold_down: return
x_index = event.x // 10
y_index = event.y // 10
nonlocal thickness
start_x_index = x_index - (thickness // 2)
start_y_index = y_index - (thickness // 2)
for y_index in range(start_y_index, start_y_index + thickness):
for x_index in range(start_x_index, start_x_index + thickness):
if y_index >= 0 and y_index < 28 and x_index >= 0 and x_index < 28:
x_coord = x_index * 10
y_coord = y_index * 10
nonlocal is_erasing
nonlocal saved_points
if not is_erasing:
canvas.create_rectangle(x_coord, y_coord, x_coord + 10, y_coord + 10, fill="white")
saved_points[y_index][x_index] = 1
else:
# This is not great, but I rarely need to erase, soooo.....
# enjoy a laggy experience until you clear :D
canvas.create_rectangle(x_coord, y_coord, x_coord + 10, y_coord + 10, fill="black")
saved_points[y_index][x_index] = 0
def predict_from_canvas(event):
if event.char != 'p': return
x = saved_points.flatten()
predictions = net.predict(x)
guess = np.argmax(predictions)
predict_label.config(text=f"PREDICTION: {guess}")
print(saved_points)
print(predictions)
print(guess)
def clear_canvas(event):
if event.char != 'c': return
canvas.delete("all")
nonlocal saved_points
saved_points = np.zeros((28, 28))
target_network = sys.argv[2]
input_file = sys.argv[3]
net = globals()[target_network]()
net.load(input_file)
is_erasing = False
hold_down = False
saved_points = np.zeros((28, 28))
thickness = 2
max_thickness = 4
min_thickness = 1
root = tkinter.Tk()
root.title("Predict Digit Draw")
controls_label = tkinter.Label(root, text="CONTROLS\nMouse-1: Draw, Mouse-1 Release: Stop Draw\nT: increase thickness, R: decrease thickness\nC: Clear, E: switch erase/draw, P: predict")
controls_label.pack()
erasing_label = tkinter.Label(root, text=f"IS ERASING: {is_erasing}")
erasing_label.pack()
thickness_label = tkinter.Label(root, text=f"THICKNESS: {thickness}")
thickness_label.pack()
predict_label = tkinter.Label(root, text="PREDICTION: ?", fg='red')
predict_label.pack()
canvas = tkinter.Canvas(root, bg="black", height=280, width=280)
canvas.pack()
root.bind("<ButtonPress-1>", lambda e: (turn_on_draw(e), draw_on_canvas(e)))
root.bind("<ButtonRelease-1>", turn_off_draw)
root.bind("<Motion>", draw_on_canvas)
root.bind("<Key>", lambda e: (predict_from_canvas(e), clear_canvas(e), increase_thickness(e), decrease_thickness(e), switch_erasing(e)))
root.mainloop()
if __name__ == "__main__":
if sys.argv[1] == "train":
# 1. target_network
# 2. output_file
# 3. start_index
# 4. end_index
# 5. epochs
# 6. learning_rate
train()
elif sys.argv[1] == "predict":
# 1. target_network
# 2. input_file
# 3. start_index
# 4. end_index
predict()
elif sys.argv[1] == "draw":
# 1. target_network
# 2. input_file
draw()
else:
print("You did not provide a valid command!")