-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCR_WITH_BIASES.py
415 lines (407 loc) · 20 KB
/
OCR_WITH_BIASES.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from PIL import Image
import os
import numpy as np
import sys
from PIL import ImageDraw
from PIL import ImageFont
import random
from random import randint
import time
import os
from text_window import thandle
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
import threading
from queue import Queue
np.set_printoptions(suppress=True, precision=2, linewidth=140)
class Neural_Net():
def __init__(self,parent):
self.parent=parent
self.load()
self.number_trained = 0
self.weights0_shape = (256,100)
self.weights1_shape = (100,25)
self.weights2_shape = (25,10)
self.biases0_shape = (self.weights0_shape[1],1)
self.biases1_shape = (self.weights1_shape[1],1)
self.biases2_shape = (self.weights2_shape[1],1)
self.mean_error = 0
self.bias_coefficient = .1
self.stop_training = False
def init_network(self):
self.weights0 = np.random.normal(0,1,self.weights0_shape)
self.biases0 = np.random.normal(0,1,self.biases0_shape)
self.weights1 = np.random.normal(0,1,self.weights1_shape)
self.biases1 = np.random.normal(0,1,self.biases1_shape)
self.weights2 = np.random.normal(0,1,self.weights2_shape)
self.biases2 = np.random.normal(0,1,self.biases2_shape)
self.parent.text_object.write("\n\nNew network loaded.")
self.show_shapes()
self.parent.window.new_network(start=False)
def load(self):
self.parent.text_object.write("\n")
try:
self.weights0 = np.load(os.getcwd().replace('\\','/') + '/network_save/weights0.npy')
self.parent.text_object.write("\nweights0 loaded from file! Shape = " + str(self.weights0.shape))
except:
self.parent.text_object.write("\nLoading weights0 failed -- initializing randomly")
self.weights0 = np.random.normal(0,1,self.weights0_shape)
try:
self.biases0 = np.load(os.getcwd().replace('\\','/') + '/network_save/biases0.npy')
self.parent.text_object.write("\nbiases0 loaded from file! Shape = " + str(self.biases0.shape))
except:
self.parent.text_object.write("\nLoading biases0 failed -- initializing randomly")
self.biases0 = np.random.normal(0,1,self.biases0_shape)
####
try:
self.weights1 = np.load(os.getcwd().replace('\\','/') + '/network_save/weights1.npy')
self.parent.text_object.write("\nweights1 loaded from file! Shape = " + str(self.weights1.shape))
except:
self.parent.text_object.write("\nLoading weights1 failed -- initializing randomly")
self.weights1 = np.random.normal(0,1,self.weights1_shape)
try:
self.biases1 = np.load(os.getcwd().replace('\\','/') + '/network_save/biases1.npy')
self.parent.text_object.write("\nbiases1 loaded from file! Shape = " + str(self.biases1.shape))
except:
self.parent.text_object.write("\nLoading biases1 failed -- initializing randomly")
self.biases1 = np.random.normal(0,1,self.biases1_shape)
####
try:
self.weights2 = np.load(os.getcwd().replace('\\','/') + '/network_save/weights2.npy')
self.parent.text_object.write("\nweights2 loaded from file! Shape = " + str(self.weights2.shape))
except:
self.parent.text_object.write("\nLoading weights2 failed -- initializing randomly")
self.weights2 = np.random.normal(0,1,self.weights2_shape)
try:
self.biases2 = np.load(os.getcwd().replace('\\','/') + '/network_save/biases2.npy')
self.parent.text_object.write("\nbiases2 loaded from file! Shape = " + str(self.biases2.shape))
except:
self.parent.text_object.write("\nLoading biases2 failed -- initializing randomly")
self.biases2 = np.random.normal(0,1,self.biases2_shape)
self.parent.window.load_it(start=False)
return
def sigmoid(self, x, deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
def show_shapes(self):
self.parent.text_object.write("\n\nweights0 shape: " + str(self.weights0.shape))
self.parent.text_object.write("\nbiases0 shape: " + str(self.biases0.shape))
self.parent.text_object.write("\nweights1 shape: " + str(self.weights1.shape))
self.parent.text_object.write("\nbiases1 shape: " + str(self.biases1.shape))
self.parent.text_object.write("\nweights2 shape: " + str(self.weights2.shape))
self.parent.text_object.write("\nbiases2 shape: " + str(self.biases2.shape))
return
def train_list(self, images_list):
debug_shape = False
length_list = len(images_list)
count = 1
global mean_error
self.parent.text_object.write("\nRunning images...")
for im in images_list:
image_name = "images_individual/" + im
layer0 = self.get_input_row(image_name)
if debug_shape: print("layer0.shape: " + str(layer0.shape))
layer1 = self.sigmoid(np.dot(layer0.T,self.weights0).T + self.biases0)
if debug_shape: print("layer1.shape: " + str(layer1.shape))
layer2 = self.sigmoid(np.dot(layer1.T,self.weights1).T + self.biases1)
layer3 = self.sigmoid(np.dot(layer2.T,self.weights2).T + self.biases2)
if debug_shape: print("layer3.shape: " + str(layer3.shape))
answer = self.get_correct_answer(im)
#### BACKPROPAGATION ####
if debug_shape: print("answer.shape: " + str(answer.shape))
layer3_error = answer - layer3
guess = np.argmax(layer3)
if debug_shape: print("layer3_error.shape: " + str(layer3_error.shape))
layer3_delta = layer3_error * self.sigmoid(layer3, deriv=True)
if debug_shape: print("layer3_delta.shape: " + str(layer3_delta.shape))
layer2_error = layer3_delta.T.dot(self.weights2.T).T
if debug_shape: print("layer2_error.shape: " + str(layer2_error.shape))
layer2_delta = layer2_error * self.sigmoid(layer2, deriv=True)
layer1_error = layer2_delta.T.dot(self.weights1.T).T
layer1_delta = layer1_error * self.sigmoid(layer1, deriv=True)
self.weights2 += layer2.dot(layer3_delta.T)
if debug_shape: print("self.weights2.shape: " + str(self.weights2.shape))
self.weights1 += layer1.dot(layer2_delta.T)
self.weights0 += layer0.dot(layer1_delta.T)
self.biases2 -= self.bias_coefficient * layer3_delta
self.biases1 -= self.bias_coefficient * layer2_delta
self.biases0 -= self.bias_coefficient * layer1_delta
if debug_shape: print("self.biases0.shape: " + str(self.biases0.shape))
error = np.mean(np.abs(layer3_error))
if self.number_trained == 0: self.mean_error = error
else: self.mean_error = (self.mean_error * (self.number_trained - 1) + error) / self.number_trained
self.parent.text_object.overwrite("Images ran: " + str(count) + " out of " + str(length_list) + "; Error: " + str(self.mean_error))
self.number_trained += 1
if count % 255 == 0:
try:
self._im = ImageTk.PhotoImage(Image.open(image_name).resize((250,250), Image.ANTIALIAS))
self.parent.window.canvas.create_image((0,0), anchor=NW, image=self._im)
self.parent.window.guess_entry.delete(0, END)
self.parent.window.guess_entry.insert(END, str(guess))
except:
print("Can't open images_individual/" + image + " for the display because the OS thinks it's in use.")
count+=1
return
def save_weights(self):
if not os.path.exists(os.getcwd().replace('\\','/') + '/network_save'):
os.mkdir(os.getcwd().replace('\\','/') + '/network_save')
np.save(os.getcwd().replace('\\','/') + '/network_save/weights0',self.weights0)
np.save(os.getcwd().replace('\\','/') + '/network_save/weights1',self.weights1)
np.save(os.getcwd().replace('\\','/') + '/network_save/weights2',self.weights2)
np.save(os.getcwd().replace('\\','/') + '/network_save/biases0',self.biases0)
np.save(os.getcwd().replace('\\','/') + '/network_save/biases1',self.biases1)
np.save(os.getcwd().replace('\\','/') + '/network_save/biases2',self.biases2)
self.parent.text_object.write("\n\nNetwork saved!")
self.parent.window.save_it(start=False)
return
def continuous_train(self, batch_size = 1024):
if self.stop_training: self.stop_training = False
self.parent.window.stop_button.config(state=NORMAL)
debug = False
if debug: print("check1")
images_list = os.listdir("images_individual")
if debug: print("check2")
for image in images_list: os.remove("images_individual/" + image)
if debug: print("check3")
self.parent.text_object.write("\n\nTraining...")
if debug: print("check4")
while not self.stop_training:
self.make(batch_size)
images_list = os.listdir("images_individual")
np.random.shuffle(images_list)
self.train_list(images_list)
for image in images_list:
try:
os.remove("images_individual/" + image)
except:
print("Can't delete images_individual/" + image + " because the OS thinks it's in use.")
self.parent.text_object.write("\nNumber trained: " + str(self.number_trained))
self.stop_training = False
self.parent.window.train_thread(start=False)
self.parent.window.stop_button.config(state=NORMAL)
return
def make(self, how_many):
images_list = os.listdir("images_individual")
for image in images_list: os.remove("images_individual/" + image)
del(images_list)
count = 1
font_list = os.listdir("fonts")
name_count = {}
while count <= how_many:
# self.white_on_black = bool(random.getrandbits(1))
self.white_on_black = True
if self.white_on_black: picture = Image.new("RGB", (160,160), (0,0,0))
else: picture = Image.new("RGB", (160,160), (255,255,255))
draw = ImageDraw.Draw(picture)
choice = randint(1, len(font_list))
font_location = "fonts/" + font_list[choice - 1]
my_font = ImageFont.truetype(font_location, 130)
number = randint(0,9)
if self.white_on_black: draw.text((25,0), str(number), (255,255,255), font=my_font)
else: draw.text((25,0), str(number), (0,0,0), font=my_font)
filename = "images_individual/" + str(number) + ".jpg"
if os.path.isfile(filename):
if number not in name_count:
count1 = 1
while os.path.isfile(filename):
name_count[number] = count1
filename = "images_individual/" + str(number) + "_" + str(count1) + ".jpg"
count1 += 1
else:
name_count[number] += 1
filename = "images_individual/" + str(number) + "_" + str(name_count[number]) + ".jpg"
picture = picture.resize((16,16), Image.ANTIALIAS)
picture = self.move_number(picture, self.where_number(picture))
picture.save(filename)
if count==1: self.parent.text_object.write("\n\nCreating " + str(how_many) + " images...")
count += 1
return
def where_number(self, image):
image = image.convert('RGB')
width, height = image.size
max_y = 0
max_x = 0
min_y = height
min_x = width
for column in range(0,width):
for row in range(0,height):
pixel = (column, row)
R, G, B = image.getpixel(pixel)
if ((R + G + B > 48) and self.white_on_black) or ((R + G + B < 665) and not self.white_on_black):
if row < min_y:
min_y = row
if row > max_y:
max_y = row
if column < min_x:
min_x = column
if column > max_x:
max_x = column
if min_x > 0:
min_x -= 1
if min_y > 0:
min_y -= 1
if max_x < width:
max_x += 1
if max_y < height:
max_y += 1
return (min_x, min_y, max_x, max_y)
def move_number(self, image, bounds):
snip = image.crop((bounds))
if self.white_on_black: image = Image.new("RGB", (16,16), (0,0,0))
else: image = Image.new("RGB", (16,16), (255,255,255))
snip_width = bounds[2] - bounds[0]
snip_height = bounds[3] - bounds[1]
new_position = randint(0,int((16-snip_width))), randint(0,int(16-snip_height))
for row in range(1,snip_height):
for column in range(1,snip_width):
pixel = (column, row)
pix_loc = (new_position[0] + column, new_position[1] + row)
try:
image.putpixel(pix_loc, snip.getpixel(pixel))
except:
wait = input("\nPix loc: " + str(pix_loc) + "\nRGB Value: " + str(snip.getpixel(pixel)) + "\nNew position: " + str(new_position))
return image
def get_input_row(self, pic):
try:
picture = Image.open(pic)
except:
print("Couldn't open image " + pic + " because the OS thinks its in use.")
return np.zeros((256,1))
the_array = np.array(picture)
the_array = the_array[...,0]
the_array = the_array.reshape(256,1)/256
del(picture)
return the_array
def get_correct_answer(self, filename):
if "_" in filename:
filename = filename[0:filename.index("_")]
else:
filename = filename[0:filename.index(".")]
the_array = np.zeros(shape=(10,1))
the_array[int(filename),0] = 1
return the_array
class window:
def __init__(self, parent):
self.parent = parent
self.main_font = ("Courier", 16)
self.setup_window()
def setup_window(self):
self.master = Tk()
self.master.wm_title("Optical Character Recognition")
self.master.minsize(width=100, height=30)
self.master.maxsize(width=1900, height=930)
#
self.text_frame = ttk.Frame(self.master)
self.text_frame.grid(row=0, column=0, rowspan = 15, sticky=NSEW)
textbox=Text(self.text_frame, background="#000000",foreground="#00AA00",width=60,height=38)
textbox.config(font = self.main_font)
textbox.pack()
self.parent.text_object = thandle(textbox)
#
image_and_guess_frame = ttk.Frame(self.master)
image_and_guess_frame.grid(row=0, column=1, sticky=N)
self.default_image = ImageTk.PhotoImage(Image.open(os.getcwd().replace('\\','/') + '/source/default.jpg').resize((250,250), Image.ANTIALIAS))
canvas_label = Label(image_and_guess_frame, text="Input Image:")
canvas_label.config(font=self.main_font)
canvas_label.grid(row=0,column=0)
self.canvas = Canvas(image_and_guess_frame)
self.canvas.config(width=250, height=250)
self.canvas.create_image((0,0), anchor=NW, image=self.default_image)
self.canvas.grid(row=1, column=0)
guess_label = Label(image_and_guess_frame, text="Guess:")
guess_label.config(font=self.main_font)
guess_label.grid(row=0,column=1)
self.guess_entry = Entry(image_and_guess_frame)
self.guess_entry.config(font=("Courier", 160),width=1)
self.guess_entry.grid(row=1, column=1)
# BUTTONS
control_panel_frame = ttk.Frame(self.master)
control_panel_frame.grid(row=2,column=1, rowspan=10, sticky=E)
#
self.new_network_button = Button(control_panel_frame, text = 'New Network', command = lambda: self.new_network())
self.new_network_button.grid(row=0, column=1, sticky=NSEW)
self.new_network_button.config(font=self.main_font)
#
check_shapes = Button(control_panel_frame, text = 'Check Shapes', command = lambda: self.parent.neural_net.show_shapes())
check_shapes.grid(row=2, column=1, sticky=NSEW)
check_shapes.config(font=self.main_font)
#
self.train_button = Button(control_panel_frame, text = 'Train Network', command = lambda: self.train_thread())
self.train_button.grid(row=3, column=1, sticky=NSEW)
self.train_button.config(font=self.main_font)
#
self.stop_button = Button(control_panel_frame, text = 'Stop', command = lambda: self.stop_it())
self.stop_button.grid(row=4, column=1,sticky=NSEW)
self.stop_button.config(font=self.main_font)
#
self.save_button = Button(control_panel_frame, text = 'Save', command = lambda: self.save_it())
self.save_button.grid(row=5, column=1,sticky=NSEW)
self.save_button.config(font=self.main_font)
#
self.load_button = Button(control_panel_frame, text = 'Load', command = lambda: self.load_it())
self.load_button.grid(row=6, column=1,sticky=NSEW)
self.load_button.config(font=self.main_font)
#
self.quit_button = Button(control_panel_frame, text = 'Quit', command = lambda: self.quit())
self.quit_button.grid(row=8, column=1, sticky=NSEW)
self.quit_button.config(font=self.main_font)
def train_thread(self, start=True):
if start:
self.train_button.config(state=DISABLED)
training_thread = threading.Thread(target=self.parent.neural_net.continuous_train)
training_thread.daemon = True
training_thread.start()
else:
self.train_button.config(state=NORMAL)
def stop_it(self):
self.stop_button.config(state=DISABLED)
self.parent.neural_net.stop_training = True
def save_it(self, start=True):
if start:
self.save_button.config(state=DISABLED)
self.stop_it()
self.parent.main_queue.put(lambda: self.parent.neural_net.save_weights())
else:
self.save_button.config(state=NORMAL)
def load_it(self, start=True):
if start:
self.load_button.config(state=DISABLED)
self.stop_it()
self.parent.main_queue.put(lambda: self.parent.neural_net.load())
else:
self.load_button.config(state=NORMAL)
def new_network(self, start=True):
if start:
self.new_network_button.config(state=DISABLED)
self.stop_it()
self.parent.main_queue.put(lambda: self.parent.neural_net.init_network())
else:
self.new_network_button.config(state=NORMAL)
def quit(self):
self.quit_button.config(state=DISABLED)
self.stop_it()
self.parent.quit = True
class parent:
def __init__(self):
self.window = window(self)
self.neural_net = Neural_Net(self)
self.main_queue = Queue()
self.quit = False
main_queue_thread = threading.Thread(target=lambda: self. main_queue_thread())
main_queue_thread.daemon = True
main_queue_thread.start()
mainloop()
def main_queue_thread(self):
while not self.quit: # handle objects in the queue until game_lost
time.sleep(.25)
try:
next_action = self.main_queue.get(False)
next_action()
except:
pass
self.main_queue.queue.clear()
self.window.master.destroy()
if __name__ == '__main__':
main_object = parent()