-
Notifications
You must be signed in to change notification settings - Fork 35
/
vision_ui.py
2688 lines (2273 loc) · 106 KB
/
vision_ui.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
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets
import ipywidgets as widgets
from IPython.display import display,clear_output
import pandas as pd
from fastai.vision import *
from fastai.widgets import *
from fastai.callbacks import*
from fastai.widgets import ClassConfusion
import matplotlib.pyplot as plt
from tkinter import Tk
from tkinter import filedialog
from tkinter.filedialog import askdirectory
import webbrowser
from IPython.display import YouTubeVideo
import warnings
warnings.filterwarnings('ignore')
import xresnet2 #for xrresnet usability
#widget layouts
layout = {'width':'90%', 'height': '50px', 'border': 'solid', 'fontcolor':'lightgreen'}
layout_two = {'width':'100px', 'height': '200px', 'border': 'solid', 'fontcolor':'lightgreen'}
style_green = {'handle_color': 'green', 'readout_color': 'red', 'slider_color': 'blue'}
style_blue = {'handle_color': 'blue', 'readout_color': 'red', 'slider_color': 'blue'}
############################
## Modules for Data 1 tab ##
############################
def dashboard_one():
"""GUI for architecture selection as well as batch size, image size, pre-trained values
as well as checking system info and links to fastai, fastai forum and asvcode github page"""
import fastai
import psutil
print ('>> Vision_UI Update: 12/23/2019')
style = {'description_width': 'initial'}
button = widgets.Button(description='System')
display(button)
out = widgets.Output()
display(out)
def on_button_clicked_info(b):
with out:
clear_output()
print(f'Fastai Version: {fastai.__version__}')
print(f'Cuda: {torch.cuda.is_available()}')
print(f'GPU: {torch.cuda.get_device_name(0)}')
print(f'Python version: {sys.version}')
print(psutil.cpu_percent())
print(psutil.virtual_memory()) # physical memory usage
print('memory % used:', psutil.virtual_memory()[2])
button.on_click(on_button_clicked_info)
dashboard_one.norma = widgets.ToggleButtons(
options=['Imagenet', 'Custom', 'Cifar', 'Mnist'],
description='Normalization:',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Imagenet stats', 'Create your own', 'Cifar stats', 'Mnist stats'],
style=style
)
dashboard_one.archi = widgets.ToggleButtons(
options=['alexnet', 'BasicBlock', 'densenet121', 'densenet161', 'densenet169', 'densenet201', 'resnet18',
'resnet34', 'resnet50', 'resnet101', 'resnet152', 'squeezenet1_0', 'squeezenet1_1', 'vgg16_bn',
'vgg19_bn', 'xresnet18', 'xresnet34', 'xresnet50', 'xresnet101', 'xresnet152'],
description='Architecture:',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltips=[],
)
layout = widgets.Layout(width='auto', height='40px') #set width and height
dashboard_one.pretrain_check = widgets.Checkbox(
options=['Yes', "No"],
description='Pretrained:',
disabled=False,
value=True,
box_style='success',
button_style='lightgreen', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Default: Checked = use pretrained weights, Unchecked = No pretrained weights'],
)
dashboard_one.method = widgets.ToggleButtons(
options=['cnn_learner', 'unet_learner'],
description='Method:',
disabled=True,
value='cnn_learner',
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Under construction'],
style=style
)
dashboard_one.f=widgets.FloatSlider(min=8,max=64,step=8,value=32, continuous_update=False, layout=layout, style=style_green, description="Batch size")
dashboard_one.m=widgets.FloatSlider(min=0, max=360, step=16, value=128, continuous_update=False, layout=layout, style=style_green, description='Image size')
display(dashboard_one.norma, dashboard_one.archi, dashboard_one.pretrain_check, dashboard_one.method, dashboard_one.f, dashboard_one.m)
print ('>> Resources')
button_two = widgets.Button(description='Fastai Docs')
button_three = widgets.Button(description='Fastai Forums')
button_four = widgets.Button(description='Vision_UI github')
but_two = widgets.HBox([button_two, button_three, button_four])
display(but_two)
def on_doc_info(b):
webbrowser.open('https://docs.fast.ai/')
button_two.on_click(on_doc_info)
def on_forum(b):
webbrowser.open('https://forums.fast.ai/')
button_three.on_click(on_forum)
def vision_utube(b):
webbrowser.open('https://github.com/asvcode/Vision_UI')
button_four.on_click(vision_utube)
##################################
## Modules for Augmentation Tab ##
##################################
def dashboard_two():
"""GUI for augmentations"""
choice_button = widgets.Button(description='Augmentation Image')
button = widgets.Button(description="View")
c_button = widgets.Button(description="View Code")
display(choice_button)
print('>> Augmentations\n')
dashboard_two.doflip = widgets.ToggleButtons(
options=['True', 'False'],
value=None,
description='Do Flip:',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.dovert = widgets.ToggleButtons(
options=['True', "False"],
value=None,
description='Do Vert:',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.three = widgets.FloatSlider(min=0.1,max=4,step=0.1,value=0.1, description='Max Zoom', orientation='vertical', style=style_green)
dashboard_two.four = widgets.FloatSlider(min=0.25, max=1.0, step=0.1, value=0.75, description='p_affine', orientation='vertical', style=style_green)
dashboard_two.five = widgets.FloatSlider(min=0.2,max=0.99, step=0.1,value=0.2, description='Max Lighting', orientation='vertical', style=style_blue)
dashboard_two.six = widgets.FloatSlider(min=0.25, max=1.1, step=0.1, value=0.75, description='p_lighting', orientation='vertical', style=style_blue)
dashboard_two.seven = widgets.FloatSlider(min=0.1, max=0.9, step=0.1, value=0.1, description='Max warp', orientation='vertical', style=style_green)
dashboard_two.cut = widgets.ToggleButtons(
options=['True', 'False'],
value='False',
description='Cutout:',
disabled=False,
icon='check',
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.jit = widgets.ToggleButtons(
options=['True', 'False'],
value='False',
description='Jitter:',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.contrast = widgets.ToggleButtons(
options=['True', 'False'],
description='Contrast:',
value='False',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=[''],
)
dashboard_two.bright = widgets.ToggleButtons(
options=['True', 'False'],
description='Brightness:',
value='False',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=[''],
)
dashboard_two.rotate = widgets.ToggleButtons(
options=['True', 'False'],
value='False',
description='Rotate:',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.sym_warp = widgets.ToggleButtons(
options=['True', 'False'],
value='False',
description='S. Warp:',
disabled=False,
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
dashboard_two.pad = widgets.ToggleButtons(
options=['zeros', 'border', 'reflection'],
value='reflection',
description='Padding:',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltips=['Description of slow', 'Description of regular', 'Description of fast'],
)
ui2 = widgets.VBox([dashboard_two.doflip, dashboard_two.dovert])
ui = widgets.HBox([dashboard_two.three, dashboard_two.seven, dashboard_two.four,dashboard_two.five, dashboard_two.six])
ui3 = widgets.VBox([ui2, ui])
ui4 = widgets.VBox([dashboard_two.cut, dashboard_two.jit, dashboard_two.contrast, dashboard_two.bright,
dashboard_two.rotate, dashboard_two.sym_warp])
ui5 = widgets.HBox([ui3, ui4])
ui6 = widgets.VBox([ui5, dashboard_two.pad])
display (ui6)
print ('>> Press button to view augmentations.')
display(button)
def on_choice_button(b):
image_choice()
choice_button.on_click(on_choice_button)
out_aug = widgets.Output()
display(out_aug)
def on_button_clicked(b):
with out_aug:
clear_output()
additional_aug_choices()
button.on_click(on_button_clicked)
def cutout_choice():
"""Helper for cutout augmentations"""
cut_out = dashboard_two.cut.value
if cut_out == 'True':
cutout_choice.one = widgets.FloatSlider(min=0,max=10,step=1,value=0, description='Start', orientation='vertical', style=style_green)
cutout_choice.two = widgets.FloatSlider(min=0, max=40, step=1, value=0, description='End', orientation='vertical', style=style_green)
cutout_choice.three = widgets.FloatSlider(min=0,max=50, step=1,value=10, description='Length', orientation='vertical', style=style_blue)
cutout_choice.four = widgets.FloatSlider(min=0, max=50, step=1, value=10, description='Height', orientation='vertical', style=style_blue)
ui = widgets.HBox([cutout_choice.one, cutout_choice.two, cutout_choice.three, cutout_choice.four])
print('>> Cutout')
display(ui)
else:
cutout_choice.one = widgets.FloatSlider(value=0)
cutout_choice.two = widgets.FloatSlider(value=0)
cutout_choice.three = widgets.FloatSlider(value=0)
cutout_choice.four = widgets.FloatSlider(value=0)
def jit_choice():
"""Helper for jittery augmentations"""
jit = dashboard_two.jit.value
if jit == 'True':
jit_choice.one = widgets.FloatSlider(min=0.01,max=1.0,step=0.02,value=0.01, description='magnitude', orientation='vertical', style=style_green)
jit_choice.two = widgets.FloatSlider(min=0, max=1.0, step=0.1, value=0.1, description='p', orientation='vertical', style=style_green)
ui = widgets.HBox([jit_choice.one, jit_choice.two])
print('>> Jittery')
display(ui)
else:
jit_choice.one = widgets.FloatSlider(min=0, value=0)
jit_choice.two = widgets.FloatSlider(min=0, value=0)
def contrast_choice():
"""Helper for contrast augmentations"""
contr = dashboard_two.contrast.value
if contr == 'True':
contrast_choice.one = widgets.FloatSlider(min=0,max=10,step=0.1,value=1, description='scale1', orientation='vertical', style=style_green)
contrast_choice.two = widgets.FloatSlider(min=0, max=10, step=0.1, value=1, description='scale2', orientation='vertical', style=style_blue)
ui = widgets.HBox([contrast_choice.one, contrast_choice.two])
print('>> Contrast')
display(ui)
else:
contrast_choice.one = widgets.FloatSlider(value=1)
contrast_choice.two = widgets.FloatSlider(value=1)
def bright_choice():
"""Helper for brightness augmentations"""
bright = dashboard_two.bright.value
if bright == 'True':
bright_choice.one = widgets.FloatSlider(min=0,max=1,step=0.1,value=0.1, description='b1', orientation='vertical', style=style_green)
bright_choice.two = widgets.FloatSlider(min=0, max=1, step=0.1, value=0.1, description='b2', orientation='vertical', style=style_green)
bright_choice.three = widgets.FloatSlider(min=0, max=1, step=0.1, value=0, description='p', orientation='vertical', style=style_green)
ui = widgets.HBox([bright_choice.one, bright_choice.two, bright_choice.three])
print('>> Brightness')
display(ui)
else:
bright_choice.one = widgets.FloatSlider(value=0)
bright_choice.two = widgets.FloatSlider(value=0)
bright_choice.three = widgets.FloatSlider(value=0)
def rotate_choice():
"""Helper for rotation augmentations"""
rotate = dashboard_two.rotate.value
if rotate == 'True':
rotate_choice.one = widgets.FloatSlider(min=-90,max=90,step=5,value=-30, description='Degree1', orientation='vertical', style=style_green)
rotate_choice.two = widgets.FloatSlider(min=-90, max=90, step=5, value=30, description='Degree2', orientation='vertical', style=style_green)
rotate_choice.three = widgets.FloatSlider(min=0, max=10, step=0.1, value=0, description='p', orientation='vertical', style=style_blue)
ui = widgets.HBox([rotate_choice.one, rotate_choice.two, rotate_choice.three])
print('>> Rotate')
display(ui)
else:
rotate_choice.one = widgets.FloatSlider(value=0)
rotate_choice.two = widgets.FloatSlider(value=0)
rotate_choice.three = widgets.FloatSlider(value=0)
def sym_w():
"""Helper for Symmetric Warp Augmentations"""
sym = dashboard_two.sym_warp.value
if sym == 'True':
sym_w.one = widgets.FloatSlider(min=-90,max=90,step=5,value=-30, description='mag1', orientation='vertical', style=style_green)
sym_w.two = widgets.FloatSlider(min=-90, max=90, step=5, value=30, description='mag2', orientation='vertical', style=style_green)
sym_w.three = widgets.FloatSlider(min=0, max=1, step=0.1, value=0, description='p', orientation='vertical', style=style_blue)
ui = widgets.HBox([sym_w.one, sym_w.two, sym_w.three])
print('>> Symmetric Warp')
display(ui)
else:
sym_w.one = widgets.FloatSlider(value=0)
sym_w.two = widgets.FloatSlider(value=0)
sym_w.three = widgets.FloatSlider(value=0)
def additional_aug_choices():
"""Helper for additional augmentation choices"""
cut_c = dashboard_two.cut.value
jit_c = dashboard_two.jit.value
cont_c = dashboard_two.contrast.value
bright_c = dashboard_two.bright.value
rotate_c = dashboard_two.rotate.value
sym_c = dashboard_two.sym_warp.value
if cut_c == 'True':
cutout_choice()
else:
cutout_choice()
if jit_c == 'True':
jit_choice()
else:
jit_choice()
if cont_c == 'True':
contrast_choice()
else:
contrast_choice()
if bright_c == 'True':
bright_choice()
else:
bright_choice()
if rotate_c == 'True':
rotate_choice()
else:
rotate_choice()
if sym_c == 'True':
sym_w()
else:
sym_w()
display_augs(image_choice)
def display_augs(image_path):
"""Helper to display augmentations"""
button = widgets.Button(description='View Augmentations')
display(button)
dis_out = widgets.Output()
display(dis_out)
def on_button_clicked(b):
with dis_out:
clear_output()
image_path = image_choice.path
get_image(image_path)
image_d = open_image(image_path)
print(f'Augmentation Image: {image_choice.path}')
print(f'Image Size: {image_d} \n')
def get_ex(): return open_image(image_path)
path = path_choice.path #checkiing to see if needed
print('Getting augmentations.....')
#Helper for getting do_flip and flip_vert to work correctly
flip = ''
vert = ''
if dashboard_two.doflip.value == 'True':
flip = 'True'
else:
flip = ''
if dashboard_two.dovert.value == 'True':
vert = 'True'
else:
vert = ''
flip_val = dashboard_two.doflip.value #to use for print statements only
vert_val = dashboard_two.dovert.value #to use for print statements only
max_zoom=dashboard_two.three.value
max_warp=dashboard_two.seven.value
max_light=dashboard_two.five.value
p_affine=dashboard_two.four.value
p_light=dashboard_two.six.value
cut1 = int(cutout_choice.one.value)
cut2 = int(cutout_choice.two.value)
length = int(cutout_choice.three.value)
height = int(cutout_choice.four.value)
jit1 = float(jit_choice.one.value)
jit2 = float(jit_choice.two.value)
scale1 = float(contrast_choice.one.value)
scale2 = float(contrast_choice.two.value)
b1 = float(bright_choice.one.value)
b2 = float(bright_choice.two.value)
b3 = float(bright_choice.three.value)
degree1 = float(rotate_choice.one.value)
degree2 = float(rotate_choice.two.value)
deg_p = float(rotate_choice.three.value)
s_war = float(sym_w.one.value)
s_war2 = float(sym_w.two.value)
p_sym = float(sym_w.three.value)
xtra_tfms = [cutout(n_holes=(cut1, cut2), length=(length, height), p=1.), jitter(magnitude=jit1,p=jit2),
contrast(scale=(scale1, scale2), p=1.), brightness(change=(b1, b2), p=b3),
rotate(degrees=(degree1,degree2), p=deg_p), symmetric_warp(magnitude=(s_war,s_war2), p=p_sym)]
tfms = get_transforms(do_flip=flip, flip_vert=vert, max_zoom=max_zoom, p_affine=p_affine,
max_lighting=max_light, p_lighting=p_light, max_warp=max_warp, xtra_tfms=xtra_tfms)
print('\n>> Augmentations')
print(f'do_flip: {flip_val}| flip_vert: {vert_val}| max_zoom: {max_zoom}| max_warp: {max_warp}| '
f'p_affine: {p_affine}| max_lighting: {max_light}| p_lighthing: {p_light}')
print('\n>> Additional Augmentations')
print(f'cutout(n_holes=({cut1}, {cut2}, length=({length}, {height}), p=1.), jitter(magnitude={jit1},'
f'p={jit2}), contrast(scale=({scale1}, {scale2}, p=1.), brightness(change=({b1}, {b2}), p={b3}),'
f'rotate(degrees({degree1}, {degree2}), p={deg_p}), symmetric_warp(magnitude=({s_war}, {s_war2}, p={p_sym})))')
_, axs = plt.subplots(2,4,figsize=(12,6))
for ax in axs.flatten():
img = get_ex().apply_tfms(tfms[0], get_ex(), size=224, padding_mode=dashboard_two.pad.value)
img.show(ax=ax)
button.on_click(on_button_clicked)
##########################
##Modules for data 2 tab##
##########################
def get_image(image_path):
"""Get choosen image"""
#print(image_path)
def path_choice():
"""Choose the data path"""
root = Tk()
path_choice.path = askdirectory(title='Select Folder')
root.destroy()
path = Path(path_choice.path)
print('Folder path:', path)
path_ls = path.ls()
data_in()
return path_choice.path
def image_choice():
"""Choose image for augmentations"""
root = Tk()
image_choice.path = filedialog.askopenfilename(title='Choose Image')
root.destroy()
return image_choice.path
def df_choice():
"""Helper to choose the csv file for using with data in datafolder"""
root = Tk()
df_choice.path = filedialog.askopenfilename(title='Choose File')
root.destroy()
return df_choice.path
def in_folder_test():
"""Helper to choose folder option """
root = Tk()
in_folder_test.path = askdirectory(title='Select Folder')
root.destroy()
path = Path(in_folder_test.path)
def in_folder_train():
"""Helper to choose folder option """
root = Tk()
in_folder_train.path = askdirectory(title='Select Folder')
root.destroy()
path = Path(in_folder_train.path)
def in_folder_valid():
"""Helper to choose folder option """
root = Tk()
in_folder_valid.path = askdirectory(title='Select Folder')
root.destroy()
path = Path(in_folder_valid.path)
def pct_metrics():
print('>> Specify train./valid split:')
pct_metrics.f=widgets.FloatSlider(min=0,max=1,step=0.1,value=0.2, continuous_update=False, style=style_green, description="valid_pct")
ui2 = widgets.VBox([pct_metrics.f])
display(ui2)
def csv_folder_choice():
"""Helper to choose folder option """
root = Tk()
csv_folder_choice.path = askdirectory(title='Select Folder')
root.destroy()
path = Path(csv_folder_choice.path)
def button_f():
"""Helper for folder_choices"""
print('>> Do you need to specify train and/or valid folder locations:')
print('>> Leave unchecked for default fastai values')
button_fs = widgets.Button(description='Confirm')
button_f.train = widgets.Checkbox(
value=False,
description='Specify Train folder location',
disabled=False
)
button_f.valid = widgets.Checkbox(
value=False,
description='Specify Valid folder location',
disabled=False
)
ui = widgets.HBox([button_f.train, button_f.valid])
display(ui)
display(button_fs)
out = widgets.Output()
display(out)
def on_button_clicked(b):
with out:
clear_output()
folder_choices()
button_fs.on_click(on_button_clicked)
def folder_choices():
"""Helper for in_folder choices"""
button_fc = widgets.Button(description='Choice')
button_tv = widgets.Button(description='Train and Valid folder')
button_v = widgets.Button(description='Valid Folder')
button_t = widgets.Button(description='Train folder')
if button_f.train.value == True and button_f.valid.value == True:
ui = widgets.HBox([button_tv])
elif button_f.train.value == False and button_f.valid.value == True:
ui = widgets.HBox([button_v])
elif button_f.train.value == True and button_f.valid.value == False:
ui = widgets.HBox([button_t])
else:
ui = None
print("Using default values of 'train' and 'valid' folders")
in_folder_train.path = 'train'
in_folder_valid.path = 'valid'
pct_metrics()
out = widgets.Output()
display(out)
display(ui)
def on_button_clicked_tv(b):
clear_output()
in_folder_train()
in_folder_valid()
print(f'Train folder: {in_folder_train.path}')
print(f'Valid folder: {in_folder_valid.path}')
pct_metrics()
button_tv.on_click(on_button_clicked_tv)
def on_button_clicked_t(b):
clear_output()
in_folder_train()
print(f'Train folder: {in_folder_train.path}')
in_folder_valid.path = 'valid'
pct_metrics()
button_t.on_click(on_button_clicked_t)
def on_button_clicked_v(b):
clear_output()
in_folder_valid()
print(f'Valid folder: {in_folder_valid.path}\n')
in_folder_train.path = 'train'
pct_metrics()
button_v.on_click(on_button_clicked_v)
def button_g():
"""Helper for csv_choices"""
print('Do you need to specify suffix and folder location:')
button = widgets.Button(description='Confirm')
button_g.suffix = widgets.Checkbox(
value=False,
description='Specify suffix',
disabled=False
)
button_g.folder = widgets.Checkbox(
value=False,
description='Specify folder',
disabled=False
)
ui = widgets.HBox([button_g.folder, button_g.suffix])
display(ui)
display(button)
out = widgets.Output()
display(out)
def on_button_clicked(b):
with out:
clear_output()
csv_choices()
button.on_click(on_button_clicked)
def csv_choices():
"""Helper for in_csv choices"""
print(f'Choose image suffix, location of training folder and csv file')
button_s = widgets.Button(description='Folder')
button_f = widgets.Button(description='CSV file')
button_c = widgets.Button(description='Confirm')
csv_choices.drop = widgets.Dropdown(
options=[None,'.jpg', '.png', '.jpeg'],
value=None,
description='Suffix:',
disabled=False,
)
if button_g.folder.value == True and button_g.suffix.value == True:
ui = widgets.HBox([csv_choices.drop, button_s, button_f])
elif button_g.folder.value == False and button_g.suffix.value == False:
ui = widgets.HBox([button_f])
elif button_g.folder.value == True and button_g.suffix.value == False:
ui = widgets.HBox([button_s, button_f])
elif button_g.folder.value == False and button_g.suffix.value == True:
ui = widgets.HBox([csv_choices.drop, button_f])
display(ui)
display(button_c)
out = widgets.Output()
display(out)
def on_button_s(b):
csv_folder_choice()
button_s.on_click(on_button_s)
def on_button_f(b):
df_choice()
button_f.on_click(on_button_f)
def on_button_c(b):
if button_g.folder.value == True and button_g.suffix.value == True:
print(csv_folder_choice.path)
csv_choices.folder_csv = (csv_folder_choice.path.rsplit('/', 1)[1])
print(f'folder: {csv_choices.folder_csv}\n')
print(f'CSV file location: {df_choice.path}')
csv_choices.file_name = (df_choice.path.rsplit('/', 1)[1])
print (f'CSV file name: {csv_choices.file_name}\n')
print(f'Image suffix: {csv_choices.drop.value}\n')
if button_g.folder.value == False and button_g.suffix.value == False:
print(f'CSV file location: {df_choice.path}')
csv_choices.folder_csv = 'train'
csv_choices.file_name = (df_choice.path.rsplit('/', 1)[1])
print (f'CSV file name: {csv_choices.file_name}\n')
print(f'Image suffix: {csv_choices.drop.value}\n')
if button_g.folder.value == True and button_g.suffix.value == False:
csv_choices.folder_csv = (csv_folder_choice.path.rsplit('/', 1)[1])
csv_choices.file_name = (df_choice.path.rsplit('/', 1)[1])
if button_g.folder.value == False and button_g.suffix.value == True:
csv_choices.folder_csv = 'train'
csv_choices.file_name = (df_choice.path.rsplit('/', 1)[1])
pct_metrics()
button_c.on_click(on_button_c)
def ds():
"""Choose from various data options, either from a custom dataset on computer or from """
"""easy to install datasets"""
button = widgets.Button(description='Location')
style = {'description_width': 'initial'}
ds.datas = widgets.ToggleButtons(
options=['Custom', 'CATS&DOGS', 'IMAGENETTE',
'IMAGENETTE_160', 'IMAGENETTE_320', 'IMAGEWOOF', 'IMAGEWOOF_160', 'IMAGEWOOF_320',
'CIFAR', 'CIFAR_100', 'MNIST', 'MNIST_SAMPLE', 'MNIST_TINY', 'FLOWERS', 'FOOD', 'CARS', 'CALTECH' ],
description='Choose',
value=None,
disabled=False,
button_style='info',
tooltips=['Choose your folder', ' Cats&Dogs: 25000 images, 819MB',
'Imagenette: A subset of 10 easily classified classes from Imagenet, 18000 images, 1.48GB', 'Imagenette_160: 18000 images, 127MB',
'Imagenette_320: 18000 images, 358MB', 'ImageWoof: A subset of 10 harder to classify classes from Imagenet, 18000 images, 1.28GB',
'ImageWoof_160: 18000 images, 119MB', 'ImageWoof_320: 18000 images, 343MB', 'Cifar: 60000 images, 234MB',
'Cifar_100: 100 classes, 60000 images, 234MB', 'Mnist', 'Mnist 14434 images', 'Mnist 1428 images'
'A 102 category dataset consisting of 102 flower categories', '101 food categories, with 101,000 images',
'16,185 images of 196 classes of cars, Pictures of objects belonging to 101 categories'],
style=style
)
display(ds.datas)
display(button)
out_three = widgets.Output()
display(out_three)
def on_button_clicked_info2(b):
with out_three:
clear_output()
ds_choice()
button.on_click(on_button_clicked_info2)
def ds_choice():
"""Helper for dataset choices"""
print('Choose how the data is saved')
if ds.datas.value == 'Custom':
path_choice()
elif ds.datas.value == 'CATS&DOGS':
path_choice.path = untar_data(URLs.DOGS)
data_in()
elif ds.datas.value == 'IMAGENETTE':
path_choice.path = untar_data(URLs.IMAGENETTE)
data_in()
elif ds.datas.value == 'IMAGENETTE_160':
path_choice.path = untar_data(URLs.IMAGENETTE_160)
data_in()
elif ds.datas.value == 'IMAGENETTE_320':
path_choice.path = untar_data(URLs.IMAGENETTE_320)
data_in()
elif ds.datas.value == 'IMAGEWOOF':
path_choice.path = untar_data(URLs.IMAGEWOOF)
data_in()
elif ds.datas.value == 'IMAGEWOOF_160':
path_choice.path = untar_data(URLs.IMAGEWOOF_160)
data_in()
elif ds.datas.value == 'IMAGEWOOF_320':
path_choice.path = untar_data(URLs.IMAGEWOOF_320)
data_in()
elif ds.datas.value == 'CIFAR':
path_choice.path = untar_data(URLs.CIFAR)
data_in()
elif ds.datas.value == 'CIFAR_100':
path_choice.path = untar_data(URLs.CIFAR_100)
data_in()
elif ds.datas.value == 'MNIST':
path_choice.path = untar_data(URLs.MNIST)
data_in()
elif ds.datas.value == 'MNIST_SAMPLE':
path_choice.path = untar_data(URLs.MNIST_SAMPLE)
data_in()
elif ds.datas.value == 'MNIST_TINY':
path_choice.path = untar_data(URLs.MNIST_TINY)
data_in()
elif ds.datas.value == 'FLOWERS':
path_choice.path = untar_data(URLs.FLOWERS)
data_in()
elif ds.datas.value == 'FOOD':
path_choice.path = untar_data(URLs.FOOD)
data_in()
elif ds.datas.value == 'CARS':
path_choice.path = untar_data(URLs.CARS)
data_in()
elif ds.datas.value == 'CALTECH':
path_choice.path = untar_data(URLs.CALTECH_101)
data_in()
def data_in():
"""Helper to determine if the data is in a folder, csv or dataframe"""
style = {'description_width': 'initial'}
button = widgets.Button(description='Data In')
data_in.datain = widgets.ToggleButtons(
options=['from_folder', 'from_csv'],
description='Data In:',
value=None,
disabled=False,
button_style='success',
tooltips=['Data in folder', 'Data in csv format'],
)
display(data_in.datain)
display(button)
disp_out = widgets.Output()
display(disp_out)
def on_choice_button(b):
with disp_out:
clear_output()
if data_in.datain.value == 'from_folder':
print('From Folder')
#folder_choices()
#pct_metrics()
button_f()
#TO DO
#if data_in.datain.value == 'from_df':
# print('From DF')
# df_choice()
# print(f'CSV file location: {df_choice.path}')
# file_name = (df_choice.path.rsplit('/', 1)[1])
# print (f'CSV file name: {file_name}')
if data_in.datain.value == 'from_csv':
print('From CSV')
button_g()
button.on_click(on_choice_button)
def get_data():
"""Helper to get the data from the folder, df or csv"""
if data_in.datain.value == 'from_folder':
Data_in.in_folder()
#TO DO
#elif data_in.datain.value == 'from_df':
# Data_in.in_df()
elif data_in.datain.value == 'from_csv':
Data_in.in_csv()
class Data_in():
def in_folder():
print('\n>> In Folder')
path = path_choice.path
"""Helpers for correctly selecting transforms and extra transforms"""
#Helper for getting do_flip and flip_vert to work correctly
flip = ''
vert = ''
if dashboard_two.doflip.value == 'True':
flip = 'True'
else:
flip = ''
if dashboard_two.dovert.value == 'True':
vert = 'True'
else:
vert = ''
batch_val = int(dashboard_one.f.value) # batch size
image_val = int(dashboard_one.m.value) # image size
flip_val = dashboard_two.doflip.value #to use for print statements only
vert_val = dashboard_two.dovert.value #to use for print statements only
max_zoom=dashboard_two.three.value
max_warp=dashboard_two.seven.value
max_light=dashboard_two.five.value
p_affine=dashboard_two.four.value
p_light=dashboard_two.six.value
cut1 = int(cutout_choice.one.value)
cut2 = int(cutout_choice.two.value)
length = int(cutout_choice.three.value)
height = int(cutout_choice.four.value)
jit1 = float(jit_choice.one.value)
jit2 = float(jit_choice.two.value)
scale1 = float(contrast_choice.one.value)
scale2 = float(contrast_choice.two.value)
b1 = float(bright_choice.one.value)
b2 = float(bright_choice.two.value)
b3 = float(bright_choice.three.value)
degree1 = float(rotate_choice.one.value)
degree2 = float(rotate_choice.two.value)
deg_p = float(rotate_choice.three.value)
s_war = float(sym_w.one.value)
s_war2 = float(sym_w.two.value)
p_sym = float(sym_w.three.value)
r = dashboard_one.pretrain_check.value
#values for saving model
value_mone = str(dashboard_one.archi.value)
value_mtwo = str(dashboard_one.pretrain_check.value)
value_mthree = str(round(dashboard_one.f.value))
value_mfour = str(round(dashboard_one.m.value))
if button_f.train.value == False:
train_choice = 'train'
else:
train_choice = (in_folder_train.path.rsplit('/', 1)[1])
print(train_choice)
if button_f.valid.value == False:
valid_choice = 'valid'
else:
valid_choice = (in_folder_valid.path.rsplit('/', 1)[1])
Data_in.in_folder.from_code = ''
xtra_tfms = [cutout(n_holes=(cut1, cut2), length=(length, height), p=1.), jitter(magnitude=jit1,p=jit2),
contrast(scale=(scale1, scale2), p=1.), brightness(change=(b1, b2), p=b3),
rotate(degrees=(degree1,degree2), p=deg_p), symmetric_warp(magnitude=(s_war,s_war2), p=p_sym)]
tfms = get_transforms(do_flip=flip, flip_vert=vert, max_zoom=max_zoom, p_affine=p_affine,
max_lighting=max_light, p_lighting=p_light, max_warp=max_warp, xtra_tfms=xtra_tfms)
data = ImageDataBunch.from_folder(path,
train=train_choice,
valid=valid_choice,
ds_tfms=tfms,
bs=batch_val,
size=image_val,
valid_pct=pct_metrics.f.value,
padding_mode=dashboard_two.pad.value)
if display_ui.tab.selected_index == 3: #Batch
data.show_batch(rows=4, figsize=(10,10))
#if display_ui.tab.selected_index == 4: #Model
if display_ui.tab.selected_index == 6 :#Train
print('FOLDER')
button_LR = widgets.Button(description='LR')
button_T = widgets.Button(description='Train')
disp = widgets.HBox([button_LR, button_T])
display(disp)
out_fol = widgets.Output()
display(out_fol)
def on_button_clicked(b):
with out_fol:
clear_output()
a, b = metrics_list(mets_list, mets_list_code)
learn = cnn_learner(data, base_arch=arch_work.info, pretrained=r, metrics=a, custom_head=None)
learn.lr_find()
learn.recorder.plot()
button_LR.on_click(on_button_clicked)
def on_button_clicked_2(b):
with out_fol:
button = widgets.Button(description='Train_N')
clear_output()
training_ds()
display(button)
def on_button_clicked_3(b):
lr_work()
a, b = metrics_list(mets_list, mets_list_code)
b_ = b[0]
learn = cnn_learner(data, base_arch=arch_work.info, pretrained=r, metrics=a, custom_head=None)
print(f'Training in folder......{b}')
cycle_l = int(training_ds.cl.value)
#save model
file_model_name = value_mone + '_pretrained_' + value_mtwo + '_batch_' + value_mthree + '_image_' + value_mfour
learn.fit_one_cycle(cycle_l,
slice(lr_work.info),
callbacks=[SaveModelCallback(learn, every='improvement', monitor=b_, name='best_'+ file_model_name)])
learn.save(file_model_name)
button.on_click(on_button_clicked_3)
button_T.on_click(on_button_clicked_2)
if display_ui.tab.selected_index == 8: #Code
print(f'data = ImageDataBunch.from_folder(path, ds_tfms=tfms, bs={batch_val}, size={image_val},'
f'padding_mode={dashboard_two.pad.value})')
def in_df():
print('\n>> In Data frame')
path = path_choice.path
"""Helpers for correctly selecting transforms and extra transforms"""
#Helper for getting do_flip and flip_vert to work correctly