-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
1163 lines (1004 loc) · 51.9 KB
/
app.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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import queue
import webbrowser
import base64
import time
from PIL import Image
import io
from itertools import chain
from io import BytesIO
from matplotlib.figure import Figure
from flask import Flask, request, jsonify
from flask_socketio import SocketIO, emit
from flask import render_template, request, jsonify
import numpy as np
import torch.nn as nn
from torch import manual_seed, Tensor
from torch.optim import Optimizer, SGD
import torch
import matplotlib.pyplot as plt
#from sklearn.neural_network import MLPClassifier
import matplotlib
matplotlib.use('Agg')
from ml_utils.model import Adjustable_model
from ml_utils.network_drawer import Neuron, Layer, NeuralNetwork, DrawNN
from ml_utils.layer_representor import layer_box_representation
#from ml_utils.model import ConvolutionalNeuralNetwork
import math
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import gradio as gr
import numpy as np
import datetime
import plotly.express as px
import pandas as pd
from queue import Queue
import sys
sys.path.append("ml_utils")
import cv2
import torch
# try:
from ml_utils.data import get_data_loaders
from ml_utils.evaluate import accuracy
# except:
# from data import get_data_loaders
# from evaluate import accuracy
# from model import ConvolutionalNeuralNetwork
from torchvision import models
import torchvision.datasets as datasets
from torchsummary import summary
import warnings
warnings.filterwarnings('ignore')
#import cv2
# app = Flask(__name__)
# socketio = SocketIO(app)
from torch.nn import Module, functional as F
from torch.cuda import empty_cache
# Initialize variables
seed = 42
acc = -1
loss = 0.1
text = ""
loss_fn=nn.CrossEntropyLoss()
n_epochs = 10
epoch = -1
epoch_losses = dict.fromkeys(range(n_epochs))
stop_signal = False
break_signal = False
data_image = base64.b64encode(b"").decode("ascii")
loss_img_url = f"data:image/png;base64,{data_image}"
loss_img_url = f"data:image/png;base64,{data_image}"
lr = 0.3
batch_size = 256
q_acc = queue.Queue()
q_loss = queue.Queue()
q_stop_signal = queue.Queue()
q_epoch = queue.Queue()
q_break_signal = queue.Queue()
q_text = queue.Queue()
visible_plots = gr.LinePlot()
accs = []
losses = []
epochs = []
current_model = None
def prepare_training(file_name: str,
n_epochs: int,
start_epoch: int,
batch_size: int,
q_acc: Queue = None,
q_loss: Queue = None,
q_epoch: Queue = None,
q_break_signal:Queue = None,
q_stop_signal: Queue = None,
learning_rate: float = 0.001,
seed: int = 42,
loss_fn: str = "CrossEntropyLoss"):
manual_seed(seed)
np.random.seed(seed)
path = f"{file_name}.pt"
model = Adjustable_model()
checkpoint = load_checkpoint(model, path)
model = Adjustable_model(linear_layers = checkpoint['lin_layers'], convolutional_layers = checkpoint['conv_layers'])
opt = SGD(model.parameters(), lr=learning_rate, momentum=0.5)
#model = load_checkpoint(model, path)
model.load_state_dict(checkpoint['model_state_dict'])
#opt.load_state_dict(checkpoint['optimizer_state_dict'])
loss_fns = {"CrossEntropyLoss": nn.CrossEntropyLoss(), "NLLLoss": nn.NLLLoss(), "MSELoss": nn.MSELoss(), "L1Loss": nn.L1Loss()}
loss_fn = loss_fns[loss_fn]
training(model=model,
optimizer=opt,
cuda=False,
n_epochs=n_epochs,
start_epoch=start_epoch,
batch_size=batch_size,
q_acc=q_acc,
q_loss=q_loss,
q_epoch=q_epoch,
q_break_signal = q_break_signal,
q_stop_signal=q_stop_signal,
file_name=file_name, lin_layers = checkpoint['lin_layers'], conv_layers = checkpoint['conv_layers'],
loss_fn=loss_fn)
def train_step(model: Module, optimizer: Optimizer, loss_fn: nn, data: Tensor,
target: Tensor, cuda: bool):
model.train()
if cuda:
data, target = data.cuda(), target.cuda()
prediction = model(data)
if (str(loss_fn) == "NLLLoss()"):
prediction = F.log_softmax(prediction, dim=1)
loss = loss_fn(prediction, target)
loss.backward()
optimizer.step()
optimizer.zero_grad()
# For advanced model creator:
boxes_of_layers = layer_box_representation()
def training(model: Module, optimizer: Optimizer, loss_fn: nn, cuda: bool, n_epochs: int,
start_epoch: int, batch_size: int, q_acc: Queue = None, q_loss: Queue = None,
q_epoch: Queue = None,
q_break_signal:Queue = None,
q_stop_signal: Queue = None,
file_name: str = None,
lin_layers: int = 0, conv_layers: int = 0):
train_loader, test_loader = get_data_loaders(batch_size=batch_size)
global q_text
stop = False
if cuda:
model.cuda()
timestr = time.strftime("%Y%m%d-%H%M%S")
file = cv2.FileStorage(f"{file_name}.yml", cv2.FILE_STORAGE_READ)
model_name = file.getNode("Name").string()
plots = np.array(file.getNode("Plot").mat())
if plots.size == 1:
plots = np.empty((3, 0), float)
if q_acc is not None:
for acc in plots[2]:
q_acc.put(acc)
if q_loss is not None:
for loss in plots[1]:
q_loss.put(loss)
if q_epoch is not None:
for epoch in plots[0]:
q_epoch.put(epoch)
for epoch in range(start_epoch, n_epochs):
#q_epoch.put(epoch)
print(f"Epoch {epoch} starts...")
q_text.put(f"Epoch {epoch} starts...")
path=f"{model_name}_{timestr}_{epoch}.pt"
batch_counter = 0
for batch in train_loader:
data, target = batch
if(str(loss_fn) == "MSELoss()" or str(loss_fn) == "L1Loss()"):
target = F.one_hot(target, 10).float()
train_step(model=model, optimizer=optimizer, loss_fn=loss_fn, cuda=cuda, data=data,
target=target)
batch_counter = batch_counter+1
if batch_counter %10 == 0:
print(f"{batch_counter} batches done!")
if q_stop_signal.empty():
continue
if q_stop_signal.get():
q_break_signal.put(True)
stop=True
break
test_loss, test_acc = accuracy(model, test_loader, loss_fn, cuda)
if q_acc is not None:
q_acc.put(test_acc)
if q_loss is not None:
q_loss.put(test_loss)
if q_epoch is not None:
q_epoch.put(epoch)
#print(plots)
plots = np.append(plots, np.array([[epoch, test_loss, test_acc]]).transpose(), axis=1)
print(f"epoch{epoch} is done!")
q_text.put(f"epoch{epoch} is done!")
print(f"epoch={epoch}, test accuracy={test_acc}, loss={test_loss}")
save_checkpoint(model, optimizer, epoch, test_loss, loss_fn, test_acc, lin_layers, conv_layers, path, False)
print(f"The checkpoint for epoch: {epoch} is saved!")
q_text.put(f"The checkpoint for epoch: {epoch} is saved!")
#print(plots)
file = cv2.FileStorage(f"{model_name}_{timestr}_{epoch}.yml", cv2.FILE_STORAGE_WRITE)
file.write("Plot", np.array(plots))
file.write("Name", model_name)
file.write("Parent", file_name)
file.release()
if stop:
print("successfully stopped")
q_text.put("successfully stopped!")
break
if cuda:
empty_cache()
def save_checkpoint(model, optimizer, epoch, loss, acc, loss_fn, lin_layers, conv_layers, path, print_info):
checkpoint = {
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
'acc': acc,
'loss_fn': loss_fn,
# Add any other information you want to save
'lin_layers': lin_layers,
'conv_layers': conv_layers
}
torch.save(checkpoint, path)
if(print_info):
print(f"The checkpoint for epoch: {epoch} is saved!")
# Print model's state_dict
print("Model's state_dict:")
for param_tensor in model.state_dict():
print(param_tensor, "\t", model.state_dict()[param_tensor].size())
print()
print("Optimizer's state_dict:")
for var_name in optimizer.state_dict():
print(var_name, "\t", optimizer.state_dict()[var_name])
def load_checkpoint(model, path):
checkpoint = torch.load(path)
model.eval()
return checkpoint
def listener():
global q_acc, q_loss, q_stop_signal, q_break_signal, q_epoch, \
epoch, acc, loss, stop_signal, loss_img_url, epoch_losses
while True:
acc = q_acc.get()
loss = q_loss.get()
epoch = q_epoch.get()
while((epoch_losses.get(epoch) is None) & (epoch != -1)):
epoch_losses[epoch] = loss
# loss_img_url = loss_plot_url()
q_acc.task_done()
q_loss.task_done()
q_epoch.task_done()
#def simple_model_creator(conv_layer_num = 2, lin_layer_num = 1, conv_layer_size = 32, lin_layer_size = 32):
def simple_model_creator(model_name, conv_layer_num = 2, lin_layer_num = 1, conv_layer_size = 32, lin_layer_size = 32):
#global current_model
if model_name == "":
print("model needs a name")
model_name = "unnamed"
conv_layers_proto = [{'size' : conv_layer_size, 'kernel_size' : 8, 'stride' : 2, 'padding' : 2},
{'size' : conv_layer_size, 'kernel_size' : 4, 'stride' : 1, 'padding' : 0}]
if conv_layer_num > len(conv_layers_proto):
conv_layers_proto = conv_layers_proto + [{'size' : conv_layer_size} for i in range(conv_layer_num - len(conv_layers_proto))]
lin_layers = [{"linear_cells":lin_layer_size} for i in range(lin_layer_num)]
conv_layers = [conv_layers_proto[i % 2] for i in range(conv_layer_num)]
current_model = Adjustable_model(linear_layers = lin_layers, convolutional_layers = conv_layers)
checkpoint = {
'epoch': 0,
'model_state_dict': current_model.state_dict(),
'optimizer_state_dict': current_model.state_dict(),
'loss': 1,
'acc': 0,
'model_name': model_name,
'lin_layers': lin_layers,
'conv_layers': conv_layers
# Add any other information you want to save
}
#timestr = time.strftime("%Y%m%d-%H%M%S")
path=f"{model_name}.pt"
torch.save(checkpoint, path)
file = cv2.FileStorage(f"{model_name}.yml", cv2.FILE_STORAGE_WRITE)
file.write("Plot", np.array([]))
file.write("Name", model_name)
file.release()
return make_img(conv_layer_num = conv_layer_num, lin_layer_num = lin_layer_num, conv_layer_size = conv_layer_size, lin_layer_size = lin_layer_size)
def simple_model_drawer(conv_layer_num = 2, lin_layer_num = 1, conv_layer_size = 32, lin_layer_size = 32):
inp = [1]
for i in range(conv_layer_num):
inp.append(conv_layer_size)
for i in range(lin_layer_num):
inp.append(lin_layer_size)
inp.append(10)
print(inp)
network = DrawNN( inp, conv_layer_num )
return network.draw()
def fig2img(fig):
buf = io.BytesIO()
fig.savefig(buf, bbox_inches='tight')
buf.seek(0)
img = Image.open(buf)
return img
def make_img(conv_layer_num = 2, lin_layer_num = 1, conv_layer_size = 32, lin_layer_size = 32):
fig = simple_model_drawer(conv_layer_num = conv_layer_num, lin_layer_num = lin_layer_num, conv_layer_size = conv_layer_size, lin_layer_size = lin_layer_size)
img = fig2img(fig)
# Save image with the help of save() Function.
img.save('network.png')
#return os.path.join(os.path.dirname(__file__), "network.png")
return img
# complex_model_creator:
# create base box layout (only input and output boxes) to start:
def base_boxes():
fig = boxes_of_layers.display_current_boxes()
img = fig2img(fig)
img.save('layer_boxes.png')
base_boxes()
def add_conv_layer(convolutional_cells=32, kernel_size=3, padding=0, stride=1, output_function="Tanh", pooling="off" ):
conv_layer = {"size" : convolutional_cells,
"kernel_size" : kernel_size,
"padding" : padding,
"stride" : stride,
"output_function" : output_function,
"pooling" : pooling}
boxes_of_layers.add_conv_layer(conv_layer)
fig = boxes_of_layers.display_current_boxes()
img = fig2img(fig)
img.save('layer_boxes.png')
return img
def delete_last_conv_layer():
if len(boxes_of_layers.get_conv_layers()) >= 1:
boxes_of_layers.remove_conv_layer()
fig = boxes_of_layers.display_current_boxes()
img = fig2img(fig)
img.save('layer_boxes.png')
return img
def add_lin_layer(linear_cells=32, output_function="Tanh"):
lin_layer = {"linear_cells" : linear_cells, "output_function" : output_function}
boxes_of_layers.add_lin_layer(lin_layer)
fig = boxes_of_layers.display_current_boxes()
img = fig2img(fig)
img.save('layer_boxes.png')
return img
def delete_last_lin_layer():
if len(boxes_of_layers.get_lin_layers()) >= 1:
boxes_of_layers.remove_lin_layer()
fig = boxes_of_layers.display_current_boxes()
img = fig2img(fig)
img.save('layer_boxes.png')
return img
def draw_complex_model():
inp = [1]
conv_layers = boxes_of_layers.get_conv_layers()
lin_layer_dicts = boxes_of_layers.get_lin_layers()
lin_layers = [i["linear_cells"] for i in lin_layer_dicts]
for i in conv_layers:
inp.append(i["size"])
for i in lin_layers:
inp.append(i)
inp.append(10)
print(inp)
network = DrawNN( inp, len(conv_layers) )
img = fig2img(network.draw())
# Save image with the help of save() Function.
img.save('network.png')
#return os.path.join(os.path.dirname(__file__), "network.png")
return img
def complex_model_creator(model_name):
global current_model
if model_name == "":
print("model needs a name")
model_name = "unnamed"
conv_layers = boxes_of_layers.get_conv_layers()
lin_layer_dicts = boxes_of_layers.get_lin_layers()
#lin_layers = [i["linear_cells"] for i in lin_layer_dicts]
current_model = Adjustable_model(linear_layers = lin_layer_dicts, convolutional_layers = conv_layers)
checkpoint = {
'epoch': 0,
'model_state_dict': current_model.state_dict(),
'optimizer_state_dict': current_model.state_dict(),
'loss': 1,
'acc': 0,
'lin_layers': lin_layer_dicts,
'conv_layers': conv_layers
# Add any other information you want to save
}
#timestr = time.strftime("%Y%m%d-%H%M%S")
path=f"{model_name}.pt"
print(current_model)
torch.save(checkpoint, path)
file = cv2.FileStorage(f"{model_name}.yml", cv2.FILE_STORAGE_WRITE)
file.write("Plot", np.array([]))
file.write("Name", model_name)
file.release()
return draw_complex_model()
def start_training(model_name, seed, lr, batch_size, n_epochs, loss_fn):
global q_acc, q_loss, stop_signal, q_stop_signal, q_break_signal, epoch, epoch_losses, loss, current_model
accs, losses, epochs = [], [], []
if model_name == None:
q_text.put("Select a model first please!")
manual_seed(seed)
np.random.seed(seed)
print("Starting training with:")
print(f"Seed: {seed}")
print(f"Learning rate: {lr}")
print(f"Number of epochs: {n_epochs}")
print(f"Batch size: {batch_size}")
print(f"loss function: {loss_fn}")
# execute training
prepare_training(file_name=model_name[:(len(model_name)-3)],
n_epochs=n_epochs,
start_epoch = 0,
batch_size=batch_size,
q_acc=q_acc,
q_loss=q_loss,
q_epoch=q_epoch,
q_break_signal = q_break_signal,
q_stop_signal=q_stop_signal,
learning_rate=lr,
seed=seed,
loss_fn=loss_fn)
# return jsonify({"success": True})
# @app.route("/stop_training", methods=["POST"])
def stop_training():
global break_signal
if not break_signal:
q_stop_signal.put(True)
# set block to true to wait for item if the queue is empty
break_signal = q_break_signal.get(block=True)
if break_signal:
print("Training breaks!")
q_text.put("Training breaks!")
def resume_training(model_name, seed, lr, batch_size, n_epochs, loss_fn):
global break_signal, epoch, q_acc, q_loss, q_epoch, q_stop_signal
manual_seed(seed)
np.random.seed(seed)
break_signal = False
print(f"Resume from epoch {epoch+1}")
q_text.put(f"Resume from epoch {epoch+1}")
if epoch != -1:
print(f"Epoch {epoch} loaded, ready to resume training!")
q_text.put(f"Epoch {epoch} loaded, ready to resume training!")
prepare_training(file_name=model_name[:(len(model_name)-3)],
n_epochs=n_epochs,
start_epoch=epoch+1,
batch_size=batch_size,
q_acc=q_acc,
q_loss=q_loss,
q_epoch=q_epoch,
q_break_signal = q_break_signal,
q_stop_signal=q_stop_signal,
learning_rate=lr,
seed=seed,
loss_fn=loss_fn)
else:
prepare_training(file_name=model_name[:(len(model_name)-3)],
n_epochs=n_epochs,
start_epoch=0,
batch_size=batch_size,
q_acc=q_acc,
q_loss=q_loss,
q_epoch=q_epoch,
q_break_signal = q_break_signal,
q_stop_signal=q_stop_signal,
learning_rate=lr,
seed=seed,
loss_fn=loss_fn)
return gr.update()
# def new_resume_training(model_name, seed, lr, batch_size, n_epochs, loss_fn):
# global q_acc, q_loss, stop_signal, q_stop_signal, q_break_signal, epoch, epoch_losses, loss, current_model, accs, losses, epochs
# accs, losses, epochs = [], [], []
# manual_seed(seed)
# np.random.seed(seed)
# print("Starting training with:")
# print(f"Seed: {seed}")
# print(f"Learning rate: {lr}")
# print(f"Number of epochs: {n_epochs}")
# print(f"Batch size: {batch_size}")
# prepare_training(file_name=model_name[:(len(model_name)-3)],
# n_epochs=n_epochs,
# batch_size=batch_size,
# q_acc=q_acc,
# q_loss=q_loss,
# q_epoch=q_epoch,
# q_break_signal = q_break_signal,
# q_stop_signal=q_stop_signal,
# learning_rate=lr,
# seed=seed,
# loss_fn=loss_fn)
# return gr.update() #jsonify({"success": True})
#app.route("/revert_to_last_epoch", methods=["GET", "POST"])
def revert_to_last_epoch():
global break_signal, epoch, loss, lr, q_epoch
print("We're at revert")
q_text.put("Reverting to last epoch...")
# check if the training is already stopped, if not, stop first
if not break_signal:
q_stop_signal.put(True)
break_signal = q_break_signal.get(block=True)
if break_signal:
print("Training breaks!")
q_text.put("Training breaks!")
time.sleep(10)
try:
q_epoch.put(epoch-1)
q_loss.put(epoch_losses[epoch-1])
loss = q_loss.get()
epoch = q_epoch.get()
for i in range(epoch+1, n_epochs):
while epoch_losses.get(i) is not None:
epoch_losses[i] = None
print(f"After revert epoch is {epoch}")
q_text.put(f"After revert epoch is {epoch}")
print(f"current epoch_losses:{epoch_losses}")
# call loss_plot to draw the new plot
except:
print("You couldn't revert from epoch 0! You can resume(restart) from epoch 0 now.")
q_text.put("You couldn't revert from epoch 0! You can resume(restart) from epoch 0 now.")
#loss_img_url = loss_plot_url()
return gr.update()
# @app.route("/update_seed", methods=["POST"])
def update_seed():
global seed
seed = int(request.form["seed"])
return jsonify({"seed": seed})
#adjust learning rate
# @app.route("/update_learningRate", methods=["POST"])
def update_learningRate():
global lr
lr = float(request.form["lr"])
return jsonify({"lr": lr})
#adjust number of epochs
# @app.route("/update_numEpochs", methods=["POST"])
def update_numEpochs():
global n_epochs
n_epochs = int(request.form["n_epochs"])
return jsonify({"n_epochs": n_epochs})
#adjust batch_size
# @app.route("/update_batch_size", methods=["POST"])
def update_batch_size():
global batch_size
batch_size = int(request.form["batch_size"])
return jsonify({"batch_size": batch_size})
#adjust loss_fn
#@app.route("/update_loss_fn", methods=["POST"])
def update_loss_fn():
global loss_fn
loss_dict = {"CrossEntropy":nn.CrossEntropyLoss(),
"NegativeLog":nn.NLLLoss(),
"L1":nn.L1Loss(),
"MSE":nn.MSELoss()}
loss_fn = loss_dict[str(request.form["loss_fn"])]
return jsonify({"success": True})
#@app.route("/get_accuracy")
def get_accuracy():
global acc
return jsonify({"acc": acc})
# @app.route("/get_loss")
def get_loss():
global loss
return jsonify({"loss": loss})
# @app.route("/get_epoch")
def get_epoch():
global epoch
return jsonify({"epoch": epoch})
# @app.route("/get_epoch_losses")
def get_epoch_losses():
global epoch_losses
return jsonify({"epoch_losses": epoch_losses})
# @app.route("/get_dict")
def get_dict():
dictTest = dict({"one": "1", "two": "2"})
return jsonify({"dictTest": dictTest})
# # @app.route("/get_loss_image")
# def get_loss_image():
# global loss_img_url
# return jsonify({"loss_img_url": loss_img_url})
"""
if __name__ == "__main__":
host = "127.0.0.1"
port = 5001
print("App started")
threading.Thread(target=listener, daemon=True).start()
webbrowser.open_new_tab(f"http://{host}:{port}")
socketio.run(app, host=host, port=port, debug=True)
"""
def get_loss():
global loss, q_loss
if q_loss is not None and q_loss.qsize() > 0:
loss = q_loss.get()
q_loss.task_done()
return loss
def get_accuracy():
global acc, q_acc
if q_acc is not None and q_acc.qsize() > 0:
acc = q_acc.get()
q_acc.task_done()
return acc
def get_statistics():
global loss, q_loss, acc, q_acc, epoch, q_epoch, accs, losses, epochs
if q_loss is not None and q_loss.qsize() > 0:
loss = q_loss.get()
q_loss.task_done()
losses.append(loss)
if q_acc is not None and q_acc.qsize() > 0:
acc = q_acc.get()
q_acc.task_done()
accs.append(acc)
if q_epoch is not None and q_epoch.qsize() > 0:
epoch = q_epoch.get()
q_epoch.task_done()
epochs.append(epoch)
return f"""
Epoch:     {int(epoch)}<br />
Accuracy:   {acc}<br />
Loss:       {loss}
"""
#str("Epoch: " + str(epoch) + "\n" + "Accuracy: " + str(acc) + "\n" + "Loss: " + str(loss))
def get_text():
global q_text, text
if q_text is not None and q_text.qsize() > 0:
text = q_text.get()
q_text.task_done()
return f"""{text}"""
labels_rp, epochs_rp, values_rp = [], [], []
labels_ap, epochs_ap, values_ap = [], [], []
def make_plot():
global accs, losses, epochs, labels_rp, epochs_rp, values_rp, labels_ap, epochs_ap, values_ap
#training_steps = []
max_len = min([len(accs), len(losses), len(epochs)])
#for j in range(2):
# for i in range(max_len):
# training_steps.append(i + 1)
#plot = gr.LinePlot(value=pd.DataFrame({"Epoch": training_steps, "Accuracy": accs, "Loss": losses}), x="Epoch", y="Accuracy")
#plot = gr.LinePlot(value=pd.DataFrame({"Labels": ["Accuracy" for _ in range(max_len)] + ["Loss" for _ in range(max_len)], "Values": accs[:max_len] + losses[:max_len], "Epochs": training_steps}), x="Epochs", y="Values", color="Labels")
"""
out_labels = np.concatenate([np.array(["Accuracy - Current Run" for _ in range(max_len)] + ["Loss - Current Run" for _ in range(max_len)]), labels_rp])
out_values = np.concatenate([np.array(accs[:max_len] + losses[:max_len]), values_rp])
out_epochs = np.concatenate([np.array(epochs[:max_len] + epochs[:max_len]), epochs_rp])
print(len(out_labels), len(out_values), len(out_epochs))
print(out_labels)
print(out_values)
print(out_epochs)
"""
plot = gr.LinePlot(value=pd.DataFrame({"Labels": np.concatenate([np.array(["Accuracy - Current Run" for _ in range(max_len)] + ["Loss - Current Run" for _ in range(max_len)]), labels_rp]),
"Values": np.concatenate([np.array(accs[:max_len] + losses[:max_len]), values_rp]),
"Epochs": np.concatenate([np.array(epochs[:max_len] + epochs[:max_len]), epochs_rp])}),
x="Epochs", y="Values", color="Labels")
return plot
def load_graph(file_names: [str]):
global labels_rp, epochs_rp, values_rp
labels_rp, epochs_rp, values_rp = [], [], []
for file_name in file_names:
file = cv2.FileStorage(file_name, cv2.FILE_STORAGE_READ)
plots = np.array(file.getNode("Plot").mat())
if plots.size == 1:
plots = np.empty((3, 0), float)
#print(plots)
data_points = len(plots[0])
basename = os.path.basename(file_name)
labels_rp = np.append(labels_rp, np.concatenate([[f"Loss - {basename}" for _ in range(data_points)], [f"Accuracy - {basename}" for _ in range(data_points)]]))
epochs_rp = np.append(epochs_rp, np.concatenate([plots[0], plots[0]]))
values_rp = np.append(values_rp, np.concatenate([plots[1], plots[2]]))
#print(plots[-1])
#print(plots[-1][0])
#print(plots[-1][1])
#print(plots[-1][2])
#max_len = min([len(plots[-1][0]), len(plots[-1][1])])
#print(["Accuracy" for _ in range(max_len)] + ["Loss" for _ in range(max_len)])
#print(plots[-1][2] + plots[-1][1])
#print(plots[-1][0] + plots[-1][0])
#plot = gr.LinePlot(value=pd.DataFrame({"Labels": np.concatenate([["Accuracy" for _ in range(max_len)], ["Loss" for _ in range(max_len)]]), "Values": np.concatenate([plots[-1][2], plots[-1][1]]), "Epochs": np.concatenate([plots[-1][0], plots[-1][0]])}), x="Epochs", y="Values", color="Labels")
#plot = gr.LinePlot(value=pd.DataFrame({"Labels": labels_rp, "Values": values_rp, "Epochs": epochs_rp}), x="Epochs", y="Values", color="Labels")
#global visible_plots
#visible_plots = plot
#return plot
#def make_example_graphs():
# file = cv2.FileStorage("test.yml", cv2.FILE_STORAGE_WRITE)
# file.write("graph", )
# file.release()
"""
mnist_trainset = datasets.MNIST(root='./data', train=True, download=True, transform=None)
mnist_testset = datasets.MNIST(root='./data', train=False, download=True, transform=None)
x_train=mnist_trainset.data.numpy()
x_test=mnist_testset.data.numpy()
y_train=mnist_trainset.targets.numpy()
y_test=mnist_testset.targets.numpy()
x_train=x_train.reshape(60000,784)/255.0
x_test=x_test.reshape(10000,784)/255.0
mlp = MLPClassifier(hidden_layer_sizes=(32,32))
mlp.fit(x_train, y_train)
print("Training Accuracy:", mlp.score(x_train, y_train))
print("Testing Accuracy:", mlp.score(x_test, y_test))
def predictIt(img):
img = img.reshape(1,784)/255.0
prediction = mlp.predict(img)[0]
return int(prediction)
"""
def predict(path, img):
#print(img)
#print(img.shape)
img = img['composite']
#img = img.reshape((28,28))
#print(img)
#print(img.shape)
new_img = []
for x in range(len(img)):
new_img.append([])
for y in range(len(img[x])):
#if img[x][y][3] == 0:
# new_img[x].append(255)
#else:
# new_img[x].append(0)
new_img[x].append(img[x][y][3])
img = new_img
#print(img)
model = Adjustable_model()
#model = Adjustable_model(linear_layers = lin_layers, convolutional_layers = conv_layers)
#opt = SGD(model.parameters(), lr=learning_rate, momentum=0.5)
checkpoint = load_checkpoint(model, path)
model = Adjustable_model(linear_layers = checkpoint['lin_layers'], convolutional_layers = checkpoint['conv_layers'])
#opt = SGD(model.parameters(), lr=learning_rate, momentum=0.5)
#model = load_checkpoint(model, path)
model.load_state_dict(checkpoint['model_state_dict'])
#model.eval()
#print(model)
#img.reshape((1, 28, 28, 1)).astype('float32') / 255.0
#img = img.resize((28,28))
img = np.array(cv2.resize(np.array(img).astype('uint8'), (28,28)))
#print(img)
transform = transforms.Compose([transforms.ToTensor()]) #transforms.Resize(28),
img_tensor = transform(img).unsqueeze(0)
#print(img_tensor.shape)
#img_tensor = img_tensor.view(img_tensor.size(0), -1)
#img_tensor = torch.tensor(img, dtype=float)
#img_tensor = img_tensor.float()
#print(img_tensor)
#print(img_tensor.shape)
#img = np.array(cv2.resize(np.array(img), (28,28))) #img.reshape((1, 28, 28, 1)).astype('float32') / 255.0
prediction = model(img_tensor).data
#print(prediction)
pred_out, pred_index = torch.max(prediction, 1)
#print(pred_out, pred_index)
return pred_index.item() #str(model(np.array(cv2.resize(np.array(img['layers'][0]), (28,28)))))
img = np.array(cv2.resize(np.array(img), (28,28))) #img.reshape((1, 28, 28, 1)).astype('float32') / 255.0
return model(torch.from_numpy(img)) #str(model(np.array(cv2.resize(np.array(img['layers'][0]), (28,28)))))
def aaa():
return np.zeros((28,28))
# def bbb():
# return gr.update()
visibleee = True
embed_html = '<iframe width="560" height="315" src="https://www.youtube.com/embed/bfmFfD2RIcg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
def clear_saved_files():
app_dir = os.path.dirname(os.path.abspath(__file__))
for root, dirs, files in os.walk(app_dir):
for file in files:
if file.endswith(".pt"):
os.remove(os.path.join(root, file))
with gr.Blocks() as demo:
with gr.Tab("Info"):
gr.Markdown("<h1 align='center'>Introduction to Machine Learning</h1>")
with gr.Row():
with gr.Column(scale=1):
pass
with gr.Column(scale=7):
gr.HTML(embed_html)
gr.Markdown(
"""
<span style="font-weight:300;font-size:20px;text-align:justify">
In order to explain the term machine learning, we must first deal with the term artificial intelligence.
Artificial intelligence is a scientific discipline that focuses on the research and algorithmization of
preferably human intelligence in the form of automatically usable perception and "mind power".
Artificial intelligence (AI for short) is therefore a machine that can replicate the cognitive abilities of a human being,
i.e. automates human intelligence. Philosophers and psychologists have been discussing what exactly
intelligence is for thousands of years, but the ability to learn is a generally recognized component.\n
This brings us to the next term, "machine learning". Just as a person only becomes intelligent through lifelong learning,
a machine only becomes intelligent through learning processes. The advantage of using such processes is that machines learn
independently how to solve certain problems. This becomes particularly advantageous if the problem cannot be described in concrete
terms or demonstrates such variability that a clearly definable solution proves challenging to pinpoint.\n\n
Trainable programs are often implemented in the form of neural networks. Neural networks are a set of algorithms,
modeled loosely after the human brain, that are designed to recognize patterns.
Neurons within neural networks can be envisioned as interconnected information nodes. They receive input data and process it,
aiming to generate an output that closely matches the desired result or ideally achieves it precisely.
To simplify, think of neurons as tiny decision-makers. When the network achieves a good outcome, these neurons adjust
themselves to provide more similar decisions in the future. However, if the outcome is not satisfactory,
the neurons recalibrate to offer different decisions /output next time.
It's like refining the network's judgment based on whether it's geting things right or wrong.</span>
""")
gr.Image("NN.png",width=450, show_label=False) #"https://www.researchgate.net/profile/Anna-Meiliana/publication/334845867/figure/fig1/AS:787149469270017@1564682466919/A-deep-neural-network-simplified40-Adapted-with-permission-from-Springer-Nature.png"
gr.Markdown(
"""
<span style="font-weight:300;font-size:20px;text-align:justify">
This tool focuses on image recognition with neural networks. The data, the tool works with, is the MNIST-dataset,
which is a collection of handwritten digits from 0 to 9 widely used for training various machine learning models.
Each image is labeled with the number it shows. The trained models should be able to classifiy the images and recognize the displayed number.
Neural networks, that are used specifically to learn grid-like data such as images, are called Convolutional Neural Networks (CNN).
Image pixels contain values that indicate the color each pixel should be.
The MNIST images are grayscale, therefore each pixel has a value between 0 (black) and 255 (white).
</span>"""
)
gr.Image("pixel-values-matrix.png",width=450, show_label=False) #"source:https://www.researchgate.net/figure/Representation-of-value-three-in-the-MNIST-dataset-and-its-equivalent-matrix_fig1_361444345"
gr.Markdown(
"""
<span style="font-weight:300;font-size:20px;text-align:justify">
These pixel values serve as input to the CNN. Simplified, they are processed by several convolutional layers followed by linear layers.
The convolutional layers detect features/patterns in data. The layers are structured to initially recognize simpler patterns like lines and curves,
progressing to identify more complex patterns such as faces and objects as they advance. The deeper (more convLayers) the better the pattern
recognition, but it also extends the training duration and processing load.
</span>"""
)
gr.Image("hierarchy.png",width=450, show_label=False) #https://www.ibm.com/content/dam/connectedassets-adobe-cms/worldwide-content/creative-assets/s-migr/ul/g/41/0f/hierarchy.png
gr.Markdown(
"""
<span style="font-weight:300;font-size:20px;text-align:justify">
The linear layers aid in determining the correct class for the image, which corresponds to the digits 0 through 9.
In the end the predicted output result of the model is compared to the actual target labels.
A loss function (such as cross-entropy loss) is used to quantify the difference between the predicted output and the true labels.
Based on the loss it changes learnable parameters in the neurons accordingly and repeats the process.
</span>
"""
)
#gr.Image("https://miro.medium.com/v2/resize:fit:1400/format:webp/1*XdCMCaHPt-pqtEibUfAnNw.png",width=450, show_label=False) #https://miro.medium.com/v2/resize:fit:1400/format:webp/1*XdCMCaHPt-pqtEibUfAnNw.png
gr.Markdown("""More detailed resources:
https://towardsdatascience.com/convolutional-neural-networks-explained-9cc5188c4939
https://towardsdatascience.com/simple-introduction-to-convolutional-neural-networks-cdf8d3077bac
""")
with gr.Column(scale=1):
pass
""" with gr.Row():
with gr.Column(min_width=50):
pass
with gr.Column(min_width=50):
gr.HTML(embed_html)
#gr.Video("https://www.youtube.com/embed/bfmFfD2RIcg")
with gr.Column(min_width=50):
pass"""
with gr.Tab("Train/Test"):
with gr.Row():
with gr.Column():
with gr.Tab("Select Model"):
gr.Markdown("<h1>Select Model</h1>")
gr.Markdown("Select an already created or trained model to train or test it.")
button_refresh = gr.Button(value="Refresh File Explorers")
button_clear = gr.Button(value="Clear all saved files")
button_clear.click(clear_saved_files)
gr.Markdown("Your models will be stored as files every epoch. Because you can revert to an earlier epoch and therefore e.g. train the second epoch multiple times, the filename format is <b>modelName_date-time_lastEpoch.pt</b> (date and time refer to the point in time button start was clicked). <br/> Untrained models do not have an epoch number at the end.")
select_model = gr.FileExplorer("**/*.pt", label="Select Model", file_count="single", interactive=True)
button_refresh.click(None, js="window.location.reload()")
with gr.Tab("Create Model"):
with gr.Tab("Beginner Model Creator"):
gr.Markdown("Here you can create a new model. Once a model has been created, its structure can no longer be changed. A new model must be created for that purpose.")
in_model_name = gr.Textbox(label="Model Name", value="unnamed")
in_convolutional_layers = gr.Slider(label="Convolutional Layers", value=2, minimum=0, maximum=5, step=1, info="extract features and patterns from input data. Many layers can lead to better accuracy but lengthen the training duration. ")
in_cells_per_conv = gr.Slider(label="Cells per convolutional layer", value=32, minimum=1, maximum=128, step=1, info="influence the capacity and learning ability of the neural network")
in_linear_layers = gr.Slider(label="Linear Layers", value=1, minimum=0, maximum=5, step=1, info="commonly used for learning complex relationships between features extracted by convolutional layers")
in_cells_per_lin = gr.Slider(label="Cells per linear layer", value=32, minimum=1, maximum=128, step=1, info="influence the capacity and learning ability of the neural network")
button_display = gr.Button(value="Display Model")
button_create_model = gr.Button(value="Create Model")
network_img = gr.Image(type='filepath', value='network.png')#type="pil")
button_create_model.click(simple_model_creator, inputs=[in_model_name, in_convolutional_layers, in_linear_layers, in_cells_per_conv, in_cells_per_lin], outputs=network_img)
#network_plot = gr.Plot()
button_display.click(make_img, inputs = [in_convolutional_layers, in_linear_layers, in_cells_per_conv, in_cells_per_lin], outputs=network_img)
#gr.Interface(make_img, gr.Image(type="pil", value=None), "image")