-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorGUI.py
969 lines (769 loc) · 39 KB
/
TensorGUI.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
import os
from tkinter import *
from tkinter import messagebox, filedialog
from tkinter import ttk
from LayersGUI import DenseLayerWindow, FlattenLayerWindow, ConvConfigureWindow
from InputWindow import InputLayerWindow
import tensorflow as tf
from tensorflow import keras
import json
from Utils import Combobox_colour_config
from matplotlib import pyplot as plt
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Loading the colour configurations
with open("Colours.json") as f:
colourConfig = json.load(f)
Input_colour_config = colourConfig["Input"]
Widget_ColourConfig = colourConfig["Widget"]
Label_ColourConfig = colourConfig["Label"]
Button_ColourConfig = colourConfig["Button"]
Entry_ColourConfig = colourConfig["Entry"]
Combobox_ColourConfig = colourConfig["Combobox"]
canvas_colour_config = colourConfig["Canvas"]
class AdamConfigObj:
def __init__(self):
self.epsilon = 1e-07
self.beta_1 = 0.9
self.beta_2 = 0.999
def set_values(self, epsilon=1e-07, beta_1=0.9, beta_2=0.999):
self.epsilon = epsilon
self.beta_1 = beta_1
self.beta_2 = beta_2
def get_optimizer(self):
return tf.keras.optimizers.Adam(beta_1=self.beta_1,
beta_2=self.beta_2,
epsilon=self.epsilon)
class AdamConfigWindow:
def __init__(self, par, ConfigObj):
self.Config = ConfigObj
epsilon = ConfigObj.epsilon
beta_1 = ConfigObj.beta_1
beta_2 = ConfigObj.beta_2
self.root = Toplevel(par)
self.root.title("Adam Parameters")
pad_x = (5, 5)
pad_y = (5, 5)
# epsilon
label = Label(self.root, text='epsilon: ')
label.grid(row=0, column=0, padx=pad_x, pady=pad_y)
self.eps_entry = Entry(self.root)
self.eps_entry.insert(0, str(epsilon))
self.eps_entry.grid(row=0, column=1, padx=pad_x, pady=pad_y)
# beta 1
label = Label(self.root, text='beta 1: ')
label.grid(row=1, column=0, padx=pad_x, pady=pad_y)
self.beta1_entry = Entry(self.root)
# self.beta1_entry.delete(0, END)
self.beta1_entry.insert(0, str(beta_1))
self.beta1_entry.grid(row=1, column=1, padx=pad_x, pady=pad_y)
# beta 2
label = Label(self.root, text='beta 2: ')
label.grid(row=2, column=0, padx=pad_x, pady=pad_y)
self.beta2_entry = Entry(self.root)
self.beta2_entry.insert(0, str(beta_2))
self.beta2_entry.grid(row=2, column=1, padx=pad_x, pady=pad_y)
# cancel button
cancel_button = Button(self.root, text="Cancel", command=self.root.destroy)
cancel_button.grid(row=3, column=0, padx=pad_x, pady=pad_y)
# save button
save_button = Button(self.root, text="Save", command=self.save)
save_button.grid(row=3, column=1, sticky=E, padx=pad_x, pady=pad_y)
def save(self):
epsilon = float(self.eps_entry.get())
beta_1 = float(self.beta1_entry.get())
beta_2 = float(self.beta2_entry.get())
self.Config.set_values(epsilon=epsilon, beta_1=beta_1, beta_2=beta_2)
self.root.destroy()
class AddLayerWindow:
"""Window in which the user will choose what type of layer
to add in the network.
Contains:
- Combobox with the choices for the layers.
- Button for going to the next window where the user will
choose the configuration of the layer.
- Button for quiting the window.
"""
def __init__(self, parent, canvas, layers, SummaryFrame):
self.Layers = layers
self.parent = parent
self.canvas = canvas
self.summaryFrame = SummaryFrame
self.Window = Toplevel(parent)
self.label = Label(self.Window, text="Choose a layer type : ")
self.label.grid(row=0, column=0, sticky=W)
self.combo = ttk.Combobox(self.Window, value=["...", "Foully Connected", "Flatten", "Convolution"])
self.combo.current(0)
self.combo.grid(row=0, column=1)
self.CancelButton = Button(self.Window, text="Cancel", command=self.Window.destroy)
self.NextButton = Button(self.Window, text="Next", command=self.nextWindow)
self.CancelButton.grid(row=1, column=0)
self.NextButton.grid(row=1, column=1)
def nextWindow(self):
if self.combo.get() == "Foully Connected":
self.Window.destroy()
DenseLayerWindow(self.parent, self.canvas, self.Layers, self.summaryFrame).Create()
elif self.combo.get() == "Flatten":
self.Window.destroy()
FlattenLayerWindow(self.parent, self.canvas, self.Layers, self.summaryFrame).Create()
elif self.combo.get() == "Convolution":
self.Window.destroy()
ConvConfigureWindow(self.parent, self.canvas, self.Layers, self.summaryFrame).Create()
class LoadingBarWindow:
def __init__(self, parent):
self.parent = parent
def show(self, total_epochs, total_batches, train_size):
# loading bar
self.loadingBar_window = Toplevel(self.parent, bg=colourConfig["TopLevel"]["bg"])
self.loadingBar_window.geometry("550x130")
# Loading bar for each epoch
self.epoch_loading_bar = ttk.Progressbar(self.loadingBar_window, orient=HORIZONTAL, length=400,
mode='determinate')
self.epoch_label = Label(self.loadingBar_window, text=f'Epoch: 0 /{total_epochs}',
**colourConfig["TopLevel"]["Labels"])
# Loading bar for each bach
self.batch_loading_bar = ttk.Progressbar(self.loadingBar_window, orient=HORIZONTAL, length=400,
mode='determinate')
self.batch_label = Label(self.loadingBar_window, text=f'Batch 0/{train_size/ total_batches}',
**colourConfig["TopLevel"]["Labels"])
self.epoch_end_loss_label = Label(self.loadingBar_window, text='Loss: 0.0',
**colourConfig["TopLevel"]["Labels"])
self.epoch_end_loss_label.grid(row=3, column=0, sticky=W)
self.batch_end_loss_label = Label(self.loadingBar_window, text='Accuracy: 0.0',
**colourConfig["TopLevel"]["Labels"])
self.batch_end_loss_label.grid(row=4, column=0, sticky=W)
return self
def load_bar_on_epoch_end(self, epoch, logs, total_epochs):
self.epoch_loading_bar['value'] = ((epoch + 1) / total_epochs) * 100
self.epoch_label.config(text=f'Epoch: {epoch + 1} /{total_epochs}')
self.epoch_end_loss_label.config(text=f'Loss: {logs["loss"]}')
self.batch_end_loss_label.config(text=f'Accuracy: {logs["accuracy"]}')
self.loadingBar_window.update_idletasks()
def pack_loading_bar(self):
self.epoch_loading_bar.grid(row=0, column=0, columnspan=2, pady=(5, 10), padx=(10, 10))
self.epoch_label.grid(row=0, column=3, columnspan=2, sticky=W, pady=(5, 10))
self.batch_loading_bar.grid(row=1, column=0, columnspan=2, pady=(5, 10), padx=(10, 10))
self.batch_label.grid(row=1, column=3, columnspan=2, sticky=W, pady=(5, 10))
self.loadingBar_window.update_idletasks()
def load_bar_on_bach_end(self, logs, batch, train_size, total_batches):
batches = train_size / total_batches
self.batch_loading_bar["value"] = ((batch + 1) / int(batches)) * 100
if (batch + 1) % ((train_size / total_batches) / 30) == 0: # print the loss every ~50 batches
self.batch_label.config(text=f'Batch: {batch + 1} /{train_size / total_batches}')
self.loadingBar_window.update_idletasks()
class App:
"""The main window of the app.
Contains:
- Toolbar with a button to add new layers and a button to train the network.
- A Compiler frame with the options for the compiling and training of
the model: optimization algorithms, accuracy metrics, loss function and
number of epochs.
- The Canvas with the visual representation of the layers.
Inputs:
- Layers: A list of layers in the form: {"name": the layers name, "layer": the keras layer}
The App will draw in the Canvas the layers the input when it is constructed.
"""
def __init__(self):
self.ModelSaved = False
self.ModelCompiled = False
self.SaveModelDirectory = None
self.SaveProjectDirectory = None
self.CreateNewWindow = False
Input = InputLayerWindow()
self.Input = Input
self.Layers = Layers(self)
if not Input.NextPressed:
return
if Input.OpenProject_RadioVar.get() == 0:
self.CreateNewWindow = True
projectDir = Input.ProjectFilePath
self._load_project(direction=projectDir)
self.Create()
return
(train_X, train_Y) = Input.ProjectWindow.data["train"]
(test_X, test_Y) = Input.ProjectWindow.data["test"]
keras_Input = tf.keras.Input(shape=train_X.shape[1:])
Input_Layer = {"name": "Input",
"type": "Input",
"shape": keras_Input.shape[1:],
"parameters": 0,
"configuration":
{
"shape": keras_Input.shape
},
"keras_layer": keras_Input}
self.Layers.Append(Input_Layer)
self.data = Input.ProjectWindow.data
self.Create()
def Create(self):
App_W = 1200
App_H = 700
self.root = Tk()
self.root.geometry(str(App_W)+"x"+str(App_H))
self.root.columnconfigure(0, weight=1)
self.root.columnconfigure(1, weight=1)
self.root.rowconfigure(2, weight=1)
self.CreateNewWindow = False
# ===== Toolbar ===== #
# top frame gray5
# Compiler gray20
# ------- Top Frame ------- #
self.TopFrame = Frame(self.root, heigh=50, bg="gray5", width=App_W, height=App_H/16)
self.TopFrame.grid_propagate(0)
self.TopFrame.grid(row=0, columnspan=2, column=0, sticky="we")
# ------ Middle Frame ------ #
self.CompileFrame = Frame(self.root, heigh=100, bg="gray20", width=App_W, height=App_H/11)
self.CompileFrame.config(highlightbackground="white", highlightthickness=1)
# self.CompileFrame.pack(side=TOP, fill='x', expand=0)
self.CompileFrame.grid_propagate(0)
self.CompileFrame.grid(row=1, column=0, columnspan=2, sticky="we")
# # ------- Bottom frame --------- #
BottomFrame_H = App_H*(1 - (1/16) - (1/11))
self.BottomFrame = Frame(self.root, bg="black", width=App_W, height=BottomFrame_H)
self.BottomFrame.columnconfigure(0, weight=1)
self.BottomFrame.rowconfigure(0, weight=1)
self.BottomFrame.grid(row=2, column=0, columnspan=2, sticky=W+E+N+S)
# ------> Canvas Frame
CanvasFrame_W = App_W-250
CanvasFrame_H = BottomFrame_H
self.CanvasFrame = Frame(self.BottomFrame, bg="green", width=CanvasFrame_W, height=CanvasFrame_H)
self.CanvasFrame.grid(row=0, column=0, sticky=W+E+N+S)
# ------> Model summary Frame
SummaryFrame_W = 200
SummaryFrame_H = CanvasFrame_H
self.SummaryFrame = Frame(self.BottomFrame, width=SummaryFrame_W, height=SummaryFrame_H,
**colourConfig["ModelSummary"]["Frame"])
self.SummaryFrame.grid_propagate(0)
self.SummaryFrame.grid(row=0, column=1, sticky=E+N+S)
# Canvas and scrollbar
self.canvas = Canvas(self.CanvasFrame, width=App_W, height=App_H*(149/176), scrollregion=(0, 0, 20000, 20000),
bg=canvas_colour_config["canvas_bg"])
# Scrollbar for the canvas
s = Scrollbar(self.CanvasFrame, orient=HORIZONTAL, bg="red")
s.pack(side=BOTTOM, fill='x')
self.canvas.config(xscrollcommand=s.set)
s.config(command=self.canvas.xview)
# Canvas configuration
# self.canvas.config(width=300, height=300)
self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
# ================================================================== #
self.TopFrame.config(highlightbackground="white", highlightthickness=1)
SavePhotoImage = PhotoImage(file="Icons/Save/Save1_x22.png")
self.addButton = Button(self.TopFrame, text="Save", image=SavePhotoImage, command=self.Add_layer,
bg="gray5", highlightbackground="gray5",
activebackground="gray15", bd=0,
width=30, height=30).grid(row=0, column=0, padx=(2, 5), pady=(5, 2))
# self.saveButton = Button(self.TopFrame, text="Load", command=self.save,
# **Button_ColourConfig).grid(row=0, column=1, padx=(2, 2), pady=(5, 2))
RunPhotoImage = PhotoImage(file="Icons/Run/play1_x22.png")
# RunPhotoImage = RunPhotoImage.subsample(2, 2)
self.trainButton = Button(self.TopFrame, text="Train", image=RunPhotoImage, command=self.train,
bg="gray5", highlightbackground="gray5", activebackground="gray15", bd=0,
width=30, height=30)
self.trainButton.grid_propagate(0)
self.trainButton.grid(row=0, column=2, padx=(2, 5), pady=(5, 2))
ttk.Separator(self.TopFrame, orient=VERTICAL).grid(row=0, column=3, sticky=N+S)
Label(self.TopFrame, text="Saved ", bg="gray5", fg="white").grid(row=0, column=4, padx=(2, 2), pady=(5, 2))
self.saveFrame = Frame(self.TopFrame, width=10, heigh=10, bg="red")
self.saveFrame.grid_propagate(0)
self.saveFrame.grid(row=0, column=5, padx=(0, 2), pady=(5, 2))
Label(self.TopFrame, text="Compiled ", bg="gray5", fg="white").grid(row=0, column=6, padx=(2, 2), pady=(5, 2))
self.compileFrame = Frame(self.TopFrame, width=10, heigh=10, bg="red")
self.compileFrame.grid_propagate(0)
self.compileFrame.grid(row=0, column=7, padx=(0, 2), pady=(5, 2))
# ================================================================== #
self.Menubar = Menu(self.root)
self.root.config(menu=self.Menubar)
# ----> File drop down menu
def _new_model():
self.Layers.LayersList = [self.Layers.LayersList[0]]
self.model = None
from LayersGraphs import paint_layers
paint_layers(self.root, self.canvas, self.Layers, self.SummaryFrame)
def _save_project_as():
filename = filedialog.asksaveasfilename()
try:
os.mkdir(filename)
self.model.save(filename+"/Model.h5")
with open(filename + "/InputConf.json", "w") as file:
file.write(json.dumps(self.Input.ProjectWindow.Configuration, indent=2))
self.SaveProjectDirectory = filename
except AttributeError:
messagebox.showerror("Saving Error", message="You must compile a model before shaving")
except OSError:
messagebox.showerror(title="Saving Error",
message="Creation of the directory %s failed" % filename)
def _save_project():
if self.SaveProjectDirectory:
try:
self.model.save(self.SaveProjectDirectory + "/Model.h5")
with open(self.SaveProjectDirectory + "/InputConf.json", "w") as file:
file.write(json.dumps(self.Input.ProjectWindow.Configuration, indent=2))
except AttributeError:
messagebox.showerror("Shaving Error", message="You must compile a model before shaving")
else:
_save_project_as()
def _save_model_as():
if os.path.exists("/"):
initial_dir = "/"
elif os.path.exists("C:"):
initial_dir = "C:"
else:
raise ValueError("Unknown file system")
filePath = filedialog.asksaveasfilename(initialdir=initial_dir, title="Select file",
filetypes=(("HDF5", "*.h5"),
("SavedModel", "*"),
("all files", "*")))
try:
self.model.save(str(filePath))
self.SaveModelDirectory = str(filePath)
except AttributeError:
messagebox.showerror("Shaving Error", message="You must compile a model before shaving")
def _save_model():
if self.SaveModelDirectory:
try:
self.model.save(self.SaveModelDirectory)
except AttributeError:
messagebox.showerror("Shaving Error", message="You must compile a model before shaving")
else:
_save_model_as()
def _load_model():
if os.path.exists("/"):
initial_dir = "/"
elif os.path.exists("C:"):
initial_dir = "C:"
else:
raise ValueError("Unknown file system")
loading_filePath = filedialog.askopenfilename(initialdir=initial_dir, title="Select file",
filetypes=(("HDF5", "*.h5"),
("SavedModel", "*"),
("all files", "*")))
if loading_filePath == (): # if Cancel is pressed in the askdirectory dialog
return
try:
model = keras.models.load_model(str(loading_filePath))
self.Layers.LayersFromModelConfig(model)
self.model = model
from LayersGraphs import paint_layers
paint_layers(self.root, self.canvas, self.Layers, self.SummaryFrame)
self.SaveModelDirectory = str(loading_filePath)
except OSError:
messagebox.showerror("Loading Model Error", "Model is corrupted or missing")
FileMenu = Menu(self.Menubar, tearoff=0)
FileMenu.add_command(label="New Model", command=_new_model)
FileMenu.add_separator()
FileMenu.add_command(label="Save Project...", command=_save_project)
FileMenu.add_command(label="Save Project As...", command=_save_project_as)
FileMenu.add_command(label="Load Project...", command=self._load_project)
FileMenu.add_separator()
FileMenu.add_command(label="Save Model...", command=_save_model)
FileMenu.add_command(label="Save Model As...", command=_save_model_as)
FileMenu.add_command(label="Load Model...", command=_load_model)
self.Menubar.add_cascade(label="File", menu=FileMenu)
# ----> Run drop down menu
RunMenu = Menu(self.Menubar, tearoff=0)
RunMenu.add_command(label="Train", command=self.train)
RunMenu.add_command(label="Compile", command=self.compile)
RunMenu.add_separator()
RunMenu.add_command(label="Re-initialize", command=self.re_initialize)
self.Menubar.add_cascade(label="Run", menu=RunMenu)
# ----> Add a layer drop down menu
def _add_Dense():
DenseLayerWindow(self.root, self.canvas, self.Layers, self.SummaryFrame).Create()
def _add_Flatten():
FlattenLayerWindow(self.root, self.canvas, self.Layers, self.SummaryFrame).Create()
def _add_Conv2D():
ConvConfigureWindow(self.root, self.canvas, self.Layers, self.SummaryFrame).Create()
AddMenu = Menu(self.Menubar, tearoff=0)
AddMenu.add_command(label="Dense", command=_add_Dense)
AddMenu.add_command(label="Flatten", command=_add_Flatten)
AddMenu.add_command(label="Conv2D", command=_add_Conv2D)
self.Menubar.add_cascade(label="Add layer", menu=AddMenu)
# ----> Weights drop down menu
def _save_weights():
if os.path.exists("/"):
initial_dir = "/"
elif os.path.exists("C:"):
initial_dir = "C:"
else:
raise ValueError("Unknown file system")
filePath = filedialog.asksaveasfilename(initialdir=initial_dir, title="Select file",
filetypes=(("HDF5", "*.h5"),
("TensorFlow Checkpoint", "*.tf"),
("all files", "*")))
try:
self.model.save_weights(str(filePath), save_format="h5")
except ValueError as error:
print("Save Error: ", error)
def _load_weights():
if os.path.exists("/"):
initial_dir = "/"
elif os.path.exists("C:"):
initial_dir = "C:"
else:
raise ValueError("Unknown file system")
filePath = filedialog.askopenfilename(initialdir=initial_dir, title="Select file",
filetypes=(("HDF5", "*.h5"),
("TensorFlow Checkpoint", "*.tf"),
("all files", "*")))
try:
self.model.load_weights(str(filePath))
except ValueError as error:
print("Load Error: ", error)
WeightsMenu = Menu(self.Menubar, tearoff=0)
WeightsMenu.add_command(label="Save weights", command=_save_weights)
WeightsMenu.add_command(label="Load weights", command=_load_weights)
self.Menubar.add_cascade(label="Weights", menu=WeightsMenu)
# ================================================================== #
# ===== Compiler ===== #
labels_bg = "gray20"
labels_fg = "white"
button_bg = "dark green"
button_fg = "white"
# Optimization Algorithms
OptAlgorithms = ['adam']
self.OptConfigs = {"adam": AdamConfigObj()}
Label(self.CompileFrame, text="Optimization Algorithm:",
bg=labels_bg, fg=labels_fg).grid(row=0, column=0, sticky=W, pady=(5, 0))
style = ttk.Style()
Combobox_colour_config(self.root, Combobox_ColourConfig, style)
self.OptCombobox = ttk.Combobox(self.CompileFrame, value=OptAlgorithms, state="normal", style='TCombobox')
self.OptCombobox["state"] = "readonly"
self.OptCombobox.current(0)
self.OptCombobox.grid(row=1, column=0, sticky=W, pady=(2, 10), padx=(2, 2))
opt_config = Button(self.CompileFrame, text="config.", command=self.config_optimizer, bg=button_bg,
fg=button_fg, highlightbackground="black")
opt_config.config(height=1, width=3)
opt_config.grid(row=1, column=1, pady=(0, 10))
# Separator
ttk.Separator(self.CompileFrame, orient=VERTICAL).grid(row=0, column=2, rowspan=2, sticky='ns',
padx=(10, 10))
# Loss Functions
Label(self.CompileFrame, text="Losses:", bg=labels_bg,
fg=labels_fg).grid(row=0, column=3, sticky=W, pady=(5, 0))
self.losses = {'SparseCategoricalCrossentropy': tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)}
self.LossCombobox = ttk.Combobox(self.CompileFrame, value=list(self.losses.keys()), state="normal", width=27,
style='TCombobox')
self.LossCombobox["state"] = "readonly"
self.LossCombobox.current(0)
self.LossCombobox.grid(row=1, column=3, sticky=W, pady=(2, 10), padx=(2, 2))
# Separator
ttk.Separator(self.CompileFrame, orient=VERTICAL).grid(row=0, column=4, rowspan=2, sticky='ns',
padx=(10, 10))
# Metrics
Label(self.CompileFrame, text="Metrics:", bg=labels_bg,
fg=labels_fg).grid(row=0, column=5, sticky=W, pady=(5, 0))
metrics = ['accuracy']
self.MetricsCombobox = ttk.Combobox(self.CompileFrame, value=metrics, state="normal", style='TCombobox')
self.MetricsCombobox["state"] = "readonly"
self.MetricsCombobox.current(0)
self.MetricsCombobox.grid(row=1, column=5, sticky=W, pady=(2, 10), padx=(2, 2))
# Separator
ttk.Separator(self.CompileFrame, orient=VERTICAL).grid(row=0, column=6, rowspan=2, sticky='ns',
padx=(10, 10))
# Epochs
Label(self.CompileFrame, text="Epochs:", bg=labels_bg,
fg=labels_fg).grid(row=0, column=7, sticky=W, pady=(5, 0))
self.epochsEntry = Entry(self.CompileFrame)
self.epochsEntry.grid(row=1, column=7, sticky=W, pady=(2, 10), padx=(2, 2))
import LayersGraphs
LayersGraphs.paint_layers(self.root, self.canvas, self.Layers, self.SummaryFrame)
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
self.root.quit()
self.root.destroy()
self.root.protocol("WM_DELETE_WINDOW", on_closing)
self.root.update()
# height = self.canvas.winfo_height()
self.root.mainloop()
def _load_project(self, direction=None):
"""
Function for loading an existing, previously saved, project.
Every project is a file with the saved model and a JSON file
witch contains the input data configuration for the project.
NOTE: The actual input data are not saved in the project file.
The JSON file contains the direction of the data in the host
machine.
:param direction: The projects direction. If None a filedialog will be open
and the user will be able to choose the direction of the
project.
:return: Nothing
"""
self.Layers.LayersList = []
if direction is None:
filename = filedialog.askdirectory()
if filename == (): # if Cancel is pressed in the askdirectory dialog
return
else:
filename = direction
try:
with open(filename + "/InputConf.json") as file:
InputConf = json.load(file)
# Asserting that the Input configuration file has the correct format.
# It must be of the form:
# {
# "Use_Keras_Data": true or false,
# "Keras_data": {
# "Data_library": for example "boston_housing"
# },
# "Use_npz_Data": true or false,
# "Reshape": {
# "Do_reshape": true or false,
# "Shape": for example "(28, 28, 1)"
# }
# }
assert "Use_Keras_Data" in InputConf.keys()
assert "Keras_data" in InputConf.keys()
assert "Data_library" in InputConf["Keras_data"].keys()
assert "Use_npz_Data" in InputConf.keys()
assert "Reshape" in InputConf.keys()
assert "Do_reshape" in InputConf["Reshape"].keys()
assert "Shape" in InputConf["Reshape"].keys()
assert InputConf["Use_Keras_Data"] in [True, False]
assert InputConf["Keras_data"]["Data_library"] in ["boston_housing", "cifar10", "cifar100",
"fashion_mnist", "imdb", "mnist", "reuters", None]
assert InputConf["Use_npz_Data"] in [True, False]
assert InputConf["Reshape"]["Do_reshape"] in [True, False]
self.Input.load_the_data(InputConf=InputConf)
model = keras.models.load_model(filename + "/Model.h5")
self.model = model
self.data = self.Input.data
(trainData_X, trainData_Y) = self.Input.data["train"]
(testData_X, testData_Y) = self.Input.data["test"]
keras_InputLayer = tf.keras.Input(shape=trainData_X.shape[1:])
Input_layer = {"name": "Input",
"type": "Input",
"shape": keras_InputLayer.shape[1:],
"parameters": 0,
"configuration":
{
"shape": keras_InputLayer.shape
},
"keras_layer": keras_InputLayer}
self.Layers.Append(Input_layer)
self.Layers.LayersFromModelConfig(model)
print("====>>> ", self.CreateNewWindow)
if self.CreateNewWindow:
# self.root.quit()
# self.root.destroy()
self.Create()
from LayersGraphs import paint_layers
paint_layers(self.root, self.canvas, self.Layers, self.SummaryFrame)
except AssertionError:
messagebox.showerror(title="Loading Error",
message="Input configuration files are corrupted or missing")
except FileNotFoundError:
messagebox.showerror(title="Loading Error",
message="Some files are corrupted or missing")
except KeyError:
messagebox.showerror(title="Loading Error",
message="Input Configuration files are corrupted or missing")
except OSError as error:
messagebox.showerror(title="Loading Error",
message="Some files are corrupted or missing.\n" + str(error))
def Add_layer(self):
AddLayerWindow(self.root, self.canvas, self.Layers, self.SummaryFrame)
def config_optimizer(self):
ConfigClasses = {"adam": AdamConfigWindow}
optimizer_str = self.OptCombobox.get()
configClass = ConfigClasses[optimizer_str]
configClass(self.root, self.OptConfigs[optimizer_str])
def train(self):
(train_X, train_Y) = self.data["train"]
train_size = train_X.shape[0]
loadingBar = LoadingBarWindow(self.root)
# custom callback for saving logs in a json file
json_logs = open("loss_log.json", "wt")
logDict = {"logs": []}
json_logging_callback = keras.callbacks.LambdaCallback(
on_epoch_end=lambda epoch, logs: logDict["logs"].append({"epoch": epoch,
"loss": logs["loss"],
"accuracy": logs["accuracy"]}),
on_train_end=lambda logs: json_logs.write(json.dumps(logDict, indent=2))
)
# callback for updating the loading bar
logging_callback = keras.callbacks.LambdaCallback(
on_epoch_end=lambda epoch, logs: loadingBar.load_bar_on_epoch_end(epoch, logs, epochs),
on_train_begin=lambda logs: loadingBar.pack_loading_bar(),
on_batch_end=lambda batch, logs: loadingBar.load_bar_on_bach_end(logs, batch, train_size, batch_N)
)
try:
epochs = int(self.epochsEntry.get())
batch_N = 32
loadingBar.show(epochs, batch_N, train_size)
results = self.model.fit(train_X, train_Y, epochs=epochs,
batch_size=batch_N,
callbacks=[logging_callback,
json_logging_callback])
ResultsWindow().create(self.root, results.history)
except AttributeError as error:
try:
loadingBar.loadingBar_window.destroy()
except AttributeError:
pass
error_message = "Unknown error: " + str(error)
if not hasattr(self, "model"):
error_message = "The model must be saved and compiled"
messagebox.showerror("AttributeError", error_message)
except ValueError as error:
try:
loadingBar.loadingBar_window.destroy()
except AttributeError:
pass
error_message = "Unknown error"
if str(error) == f"invalid literal for int() with base 10: '{str(self.epochsEntry.get())}'":
error_message = f"Invalid value for the number of epochs: '{str(self.epochsEntry.get())}'"
messagebox.showerror("ValueError", error_message)
except RuntimeError as error:
try:
loadingBar.loadingBar_window.destroy()
except AttributeError:
pass
loadingBar.loadingBar_window.destroy()
errorWindow = Toplevel(self.root)
Label(errorWindow, text="RuntimeError"+str(error)).pack()
json_logs.close()
def re_initialize(self):
self.Layers.Re_initialize()
def compile(self):
self.Keras_Layers = self.Layers.get_KerasLayers()
try:
self.model = keras.Sequential(self.Keras_Layers)
self.saveFrame.config(bg="green")
except ValueError as error:
errorWindow = Toplevel(self.root)
Label(errorWindow, text=str(error)).pack()
# get the optimizer
optimizer_str = self.OptCombobox.get()
optimizer_config = self.OptConfigs[optimizer_str]
optimization = optimizer_config.get_optimizer()
loss = self.LossCombobox.get()
metric = self.MetricsCombobox.get()
try:
self.model.compile(optimizer=optimization,
loss=self.losses[loss],
metrics=[metric])
self.compileFrame.config(bg="green")
except AttributeError as error:
errorWindow = Toplevel(self.root)
Label(errorWindow, text=str(error)).pack()
except ValueError as error:
errorWindow = Toplevel(self.root)
Label(errorWindow, text=str(error)).pack()
class ResultsWindow:
def create(self, root, logDict):
# self.Window = Toplevel(root, bg=colourConfig["TopLevel"]["bg"])
# losses = [logs["loss"] for logs in logDict["logs"]]
# accuracy = [logs["accuracy"] for logs in logDict["logs"]]
losses = logDict["loss"]
accuracy = logDict["accuracy"]
lossesFig = plt.figure(0, figsize=(4, 3), dpi=100)
lossesAx = lossesFig.subplots()
lossesAx.plot(losses)
accuracyFig = plt.figure(1, figsize=(4, 3), dpi=100)
accuracyAx = accuracyFig.subplots()
accuracyAx.plot(accuracy)
ResultWindow = Toplevel(root, bg=colourConfig["TopLevel"]["bg"])
ResultWindow.wm_title("Results")
fig = plt.Figure(figsize=(4, 3), dpi=100)
ax = fig.add_subplot()
ax.plot(accuracy)
canvas1Frame = Frame(ResultWindow)
canvas1 = FigureCanvasTkAgg(fig, master=canvas1Frame) # A tk.DrawingArea.
canvas1.draw()
canvas1.get_tk_widget().pack(side=TOP, expand=True)
toolbar = NavigationToolbar2Tk(canvas1, canvas1Frame)
toolbar.update()
canvas1.get_tk_widget().pack(side=TOP, expand=True)
canvas1Frame.grid(row=0, column=0)
canvas2Frame = Frame(ResultWindow)
canvas2 = FigureCanvasTkAgg(lossesFig, master=canvas2Frame)
canvas2.draw()
canvas2.get_tk_widget().pack(side=TOP, expand=True)
toolbar = NavigationToolbar2Tk(canvas2, canvas2Frame)
toolbar.update()
canvas1.get_tk_widget().pack(side=TOP, expand=True)
canvas2Frame.grid(row=0, column=1)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas1, toolbar)
canvas1.mpl_connect("key_press_event", on_key_press)
def _quit():
# Window.quit() # stops mainloop
ResultWindow.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL
button = Button(master=ResultWindow, text="Quit", command=_quit)
button.grid(row=0, column=2)
# plt.show()
class Layers:
def __init__(self, Application):
self.LayersList = []
self._layer_types = {"Flatten": tf.keras.layers.Flatten,
"Dense": tf.keras.layers.Dense,
"Conv2D": tf.keras.layers.Conv2D}
self.App = Application
def Add_layer(self, layer, position):
"""
Add a layer in the list in a certain position.
Each layer must be of the form:
Layer = {"name": layers name,
"type": layers type,
"shape": layers shape,
"parameters": number of trainable parameters,
"configuration": {the configuration of the layer},
"keras_layer": the keras layer}
Each layer can go in certain positions only. For example
a Conv2D layer can't be placed after a Dense layer.
:param layer: the layer to be added in the above form
:param position: the position of the layer in the network
:return: Nothing
"""
assert isinstance(position, int)
assert position >= 0
assert "name" in layer.keys()
assert "type" in layer.keys()
assert "shape" in layer.keys()
assert "parameters" in layer.keys()
assert "configuration" in layer.keys()
assert "keras_layer" in layer.keys()
assert layer["type"] in self._layer_types.keys()
self.App.saveFrame.config(bg="red")
self.App.compileFrame.config(bg="red")
self.LayersList.insert(position, layer)
def Remove_layer(self, layer):
self.App.saveFrame.config(bg="red")
self.App.compileFrame.config(bg="red")
self.LayersList.remove(layer)
def Append(self, layer):
if hasattr(App, "saveFrame") and hasattr(App, "compileFrame"):
self.App.saveFrame.config(bg="red")
self.App.compileFrame.config(bg="red")
self.LayersList.append(layer)
def Re_initialize(self):
for layer in self.LayersList[1:]:
try:
del layer["keras_layer"]
layer["keras_layer"] = self._layer_types[layer["type"]](**layer["configuration"])
except KeyError:
pass
def get_KerasLayers(self):
return [layer["keras_layer"] for layer in self.LayersList]
def get_LayersNames(self):
return [layer["name"] for layer in self.LayersList]
def LayersFromModelConfig(self, model):
self.LayersList = [self.LayersList[0]]
keras_layers = model.layers
modelConfig = model.get_config()
for layer_config, keras_layer in zip(modelConfig["layers"], keras_layers):
AppLayer = {"name": layer_config["config"]["name"],
"type": layer_config["class_name"],
"parameters": 0,
"shape": (),
"configuration": layer_config["config"],
"keras_layer": keras_layer
}
self.Append(AppLayer)
if __name__ == "__main__":
Application = App()
# Application.Create()