forked from PacktPublishing/Data-Augmentation-with-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluto_chapter_4.py
More file actions
811 lines (749 loc) · 26.4 KB
/
pluto_chapter_4.py
File metadata and controls
811 lines (749 loc) · 26.4 KB
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
# create an object
# First, importing the basic library
import torch
import pandas
import numpy
import matplotlib
import pathlib
import PIL
import datetime
import sys
import psutil
# create class/object
class PacktDataAug(object):
#
# initialize the object
def __init__(self, name="Pluto", is_verbose=True,*args, **kwargs):
super(PacktDataAug, self).__init__(*args, **kwargs)
self.author = "Duc Haba"
self.version = 1.0
self.name = name
if (is_verbose):
self._ph()
self._pp("Hello from class", str(self.__class__) + " Class: " + str(self.__class__.__name__))
self._pp("Code name", self.name)
self._pp("Author is", self.author)
self._ph()
#
return
#
# pretty print output name-value line
def _pp(self, a, b):
print("%28s : %s" % (str(a), str(b)))
return
#
# pretty print the header or footer lines
def _ph(self):
print("-" * 28, ":", "-" * 28)
return
# ---end of class
#
# Hack it! Add new method as needed.
# add_method() is copy from Michael Garod's blog,
# https://medium.com/@mgarod/dynamically-add-a-method-to-a-class-in-python-c49204b85bd6
# AND correction by: Филя Усков
#
import functools
def add_method(cls):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
setattr(cls, func.__name__, wrapper)
return func
return decorator
#
pluto = PacktDataAug("Pluto")
@add_method(PacktDataAug)
def say_sys_info(self):
self._ph()
now = datetime.datetime.now()
self._pp("System time", now.strftime("%Y/%m/%d %H:%M"))
self._pp("Platform", sys.platform)
self._pp("Pluto Version (Chapter)", self.version)
self._pp("Python (3.7.10)", 'actual: ' + ''.join(str(sys.version).splitlines()))
self._pp("PyTorch (1.11.0)", 'actual: ' + str(torch.__version__))
self._pp("Pandas (1.3.5)", 'actual: ' + str(pandas.__version__))
self._pp("PIL (9.0.0)", 'actual: ' + str(PIL.__version__))
self._pp("Matplotlib (3.2.2)", 'actual: ' + str(matplotlib.__version__))
#
try:
val = psutil.cpu_count()
self._pp("CPU count", val)
val = psutil.cpu_freq()
if (None != val):
val = val._asdict()
self._pp("CPU speed", (str(round((val["current"] / 1000), 2)) + " GHz"))
self._pp("CPU max speed", (str(round((val["max"] / 1000), 2)) + " GHz"))
else:
self._pp("*CPU speed", "NOT available")
except:
pass
self._ph()
return
pluto.version = 2.0
@add_method(PacktDataAug)
def remember_kaggle_access_keys(self,username,key):
self.kaggle_username = username
self.kaggle_key = key
return
@add_method(PacktDataAug)
def _write_kaggle_credit(self):
creds = '{"username":"'+self.kaggle_username+'","key":"'+self.kaggle_key+'"}'
kdirs = ["~/.kaggle/kaggle.json", "./kaggle.json"]
#
for k in kdirs:
cred_path = pathlib.Path(k).expanduser()
cred_path.parent.mkdir(exist_ok=True)
cred_path.write_text(creds)
cred_path.chmod(0o600)
import kaggle
#
return
#
@add_method(PacktDataAug)
def fetch_kaggle_comp_data(self,cname):
#self._write_kaggle_credit() # need to run only once.
path = pathlib.Path(cname)
kaggle.api.competition_download_cli(str(path))
zipfile.ZipFile(f'{path}.zip').extractall(path)
return
#
#
@add_method(PacktDataAug)
def fetch_kaggle_dataset(self,url,dest="kaggle"):
#self._write_kaggle_credit() # need to run only once.
opendatasets.download(url,data_dir=dest)
return
import zipfile
import os
import zipfile
import os
@add_method(PacktDataAug)
def fetch_df(self, csv):
df = pandas.read_csv(csv, encoding='latin-1')
return df
@add_method(PacktDataAug)
def build_sf_fname(self, df):
root = 'state-farm-distracted-driver-detection/imgs/train/'
df["fname"] = root + df.classname + '/' + df.img
return
# set internal counter for image to be zero, e.g. pluto0.jpg, pluto1.jpg, etc.
pluto.fname_id = 0
@add_method(PacktDataAug)
def _drop_image(self,canvas, fname=None,format=".jpg",dname="Data-Augmentation-with-Python/pluto_img"):
if (fname is None):
self.fname_id += 1
if not os.path.exists(dname):
os.makedirs(dname)
fn = dname + "/pluto" + str(self.fname_id) + format
else:
fn = fname
canvas.savefig(fn, bbox_inches="tight", pad_inches=0.25)
return
#
@add_method(PacktDataAug)
def draw_batch(self,df_filenames, disp_max=10,is_shuffle=False, figsize=(16,8)):
disp_col = 5
disp_row = int(numpy.round((disp_max/disp_col)+0.4, 0))
_fns = list(df_filenames)
if (is_shuffle):
numpy.random.shuffle(_fns)
k = 0
clean_fns = []
if (len(_fns) >= disp_max):
canvas, pic = matplotlib.pyplot.subplots(disp_row,disp_col, figsize=figsize)
for i in range(disp_row):
for j in range(disp_col):
try:
im = PIL.Image.open(_fns[k])
pic[i][j].imshow(im)
pic[i][j].set_title(pathlib.Path(_fns[k]).name)
clean_fns.append(_fns[k])
except:
pic[i][j].set_title(pathlib.Path(_fns[k]).name)
k += 1
canvas.tight_layout()
self._drop_image(canvas)
canvas.show()
else:
print("**Warning: the length should be more then ", disp_max, ". The given length: ", len(_fns))
return clean_fns
@add_method(PacktDataAug)
def build_shoe_fname(self, start_path):
df = pandas.DataFrame()
for root, dirs, files in os.walk(start_path, topdown=False):
for name in files:
f = os.path.join(root, name)
p = pathlib.Path(f).parent.name
d = pandas.DataFrame({'fname': [f], 'label': [p]})
df = df.append(d, ignore_index=True)
#
# clean it up
df = df.reset_index(drop=True)
return df
#
# create the same with a generic function name
@add_method(PacktDataAug)
def make_dir_dataframe(self, start_path):
return self.build_shoe_fname(start_path)
@add_method(PacktDataAug)
def print_batch_text(self,df_orig, disp_max=10, cols=["title", "description"]):
df = df_orig[cols]
with pandas.option_context("display.max_colwidth", None):
display(df.sample(disp_max))
return
@add_method(PacktDataAug)
def count_word(self, df, col_dest="description"):
df['wordc'] = df[col_dest].apply(lambda x: len(x.split()))
return
@add_method(PacktDataAug)
def draw_word_count(self,df, wc='wordc'):
canvas, pic = matplotlib.pyplot.subplots(1,2, figsize=(16,5))
df.boxplot(ax=pic[0],column=[wc],vert=False,color="black")
df[wc].hist(ax=pic[1], color="cornflowerblue", alpha=0.9)
#
title=["Description BoxPlot", "Description Histogram"]
yaxis=["Description", "Stack"]
x1 = "Word Count: Mean: " + str(round(df[wc].mean(), 2)) + ", Min: " + str(df[wc].min()) + ", Max: " + str(df[wc].max())
xaxis=[x1, "Word Count"]
#
pic[0].set_title(title[0], fontweight ="bold")
pic[1].set_title(title[1], fontweight ="bold")
pic[0].set_ylabel(yaxis[0])
pic[1].set_ylabel(yaxis[1])
pic[0].set_xlabel(xaxis[0])
pic[1].set_xlabel(xaxis[1])
#
canvas.tight_layout()
self._drop_image(canvas)
#
canvas.show()
return
import re
@add_method(PacktDataAug)
def _strip_punc(self,s):
p = re.sub(r'[^\w\s]','',s)
return(p)
#
@add_method(PacktDataAug)
def check_spelling(self,df, col_dest='description'):
spell = spellchecker.SpellChecker()
df["misspelled"] = df[col_dest].apply(lambda x: spell.unknown(self._strip_punc(x).split()))
df["misspelled_count"] = df["misspelled"].apply(lambda x: len(x))
return
# STOP: if failed the import below, you need to run:
# !pip install -Uqq fastai
#
pluto.version = 3.0
from fastcore.all import *
import fastai
import fastai.vision
import fastai.vision.core
import fastai.vision.augment
import numpy
print("\nfastai version (should be 2.6.3 or higher): ", fastai.__version__)
import PIL
import PIL.ImageOps
@add_method(PacktDataAug)
def draw_image_flip_pil(self,fname):
img = PIL.Image.open(fname)
mirror_img = PIL.ImageOps.mirror(img)
display(img, mirror_img)
return
@add_method(PacktDataAug)
def _make_data_loader(self,df, tfms, i_tfms=None):
dls = fastai.vision.data.ImageDataLoaders.from_df(df,
fn_col="fname",label_col="label",
item_tfms=i_tfms,
batch_tfms=tfms,
valid_pct=0.2,
bs=32)
return dls
#
# fastai.vision.augment.RandomCrop
# fastai.vision.augment.CropPad(480)
# import fastai.vision.augment. (to see all other option like flip, hue, etc)
@add_method(PacktDataAug)
def draw_image_flip(self,df,bsize=15):
aug = [fastai.vision.augment.Flip(p=0.8)]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def draw_image_flip_both(self,df,bsize=15,pad_mode='zeros'):
aug = fastai.vision.augment.Dihedral(p=0.8,pad_mode=pad_mode)
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def draw_image_crop(self,df,bsize=15,pad_mode="zeros",isize=480):
aug = fastai.vision.augment.CropPad(isize,pad_mode=pad_mode)
itfms = fastai.vision.augment.CropPad(isize, pad_mode=pad_mode)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def draw_image_rotate(self,df,bsize=15,max_rotate=45.0,pad_mode='zeros'):
aug = [fastai.vision.augment.Rotate(max_rotate,p=0.75,pad_mode=pad_mode)]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def draw_image_warp(self,df,bsize=15,magnitude=0.2,pad_mode='zeros'):
aug = [fastai.vision.augment.Warp(magnitude=magnitude, pad_mode=pad_mode,p=0.75)]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def draw_image_shift_pil(self,fname, x_axis, y_axis=0):
img = PIL.Image.open(fname)
shift_img = PIL.ImageChops.offset(img,x_axis,y_axis)
display(img, shift_img)
return
try:
pluto._ph()
import albumentations
pluto._pp("albumentations 1.2.1", "actual " + albumentations.__version__)
# import cat2
except ImportError as e:
pluto._ph()
pluto._pp("**Error", e)
pluto._ph()
@add_method(PacktDataAug)
def _draw_image_album(self,df,aug_album,bsize=5):
if (bsize == 2):
ncol = 2
nrow = 1
w = 16
h = 8
else:
ncol = 5
nrow = int(numpy.ceil(bsize/ncol))
w = 14
h = int(4 * nrow)
#
canvas, pic = matplotlib.pyplot.subplots(nrow, ncol, figsize=(w, h))
pics = pic.flatten()
# select random images
samp = df.sample(int(ncol * nrow))
samp.reset_index(drop=True, inplace=True)
for i, ax in enumerate(pics):
# convert to an array
img_numpy = numpy.array(PIL.Image.open(samp.fname[i]))
label = df.label[i]
# perform the transformation using albumentations
img = aug_album(image=img_numpy)['image']
# display the image in batch modde
ax.imshow(img)
ax.set_title(label)
canvas.tight_layout()
canvas.show()
return
@add_method(PacktDataAug)
def draw_image_brightness(self,df,brightness=0.2,bsize=5):
aug_album = albumentations.ColorJitter(brightness=brightness,
contrast=0.0, saturation=0.0,hue=0.0,always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_grayscale(self,df,bsize=5):
aug_album = albumentations.ToGray(p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_contrast(self,df,contrast=0.2,bsize=5):
aug_album = albumentations.ColorJitter(brightness=0.0,
contrast=contrast, saturation=0.0,hue=0.0,always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_saturation(self,df,saturation=0.2,bsize=5):
aug_album = albumentations.ColorJitter(brightness=0.0,
contrast=0.0, saturation=saturation,hue=0.0,always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_hue(self,df,hue=0.2,bsize=5):
aug_album = albumentations.ColorJitter(brightness=0.0,
contrast=0.0, saturation=0.0,hue=hue,always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_noise(self,df,var_limit=(10.0, 50.0),bsize=5):
aug_album = albumentations.GaussNoise(var_limit=var_limit,
always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_sunflare(self,df,flare_roi=(0, 0, 1, 0.5),src_radius=400,bsize=2):
aug_album = albumentations.RandomSunFlare(flare_roi=flare_roi,
src_radius=src_radius, always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_rain(self,df,drop_length=20, drop_width=1,blur_value=1,bsize=2):
aug_album = albumentations.RandomRain(drop_length=drop_length, drop_width=drop_width,
blur_value=blur_value,always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_sepia(self,df,bsize=5):
aug_album = albumentations.ToSepia(always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_fancyPCA(self,df,alpha=0.1,bsize=5):
aug_album = albumentations.FancyPCA(alpha=alpha, always_apply=True, p=1.0)
self._draw_image_album(df,aug_album,bsize)
return
@add_method(PacktDataAug)
def draw_image_erasing(self,df,bsize=8,max_count=5):
aug = [fastai.vision.augment.RandomErasing(p=1.0,max_count=max_count)]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def print_safe_parameters(self):
data = [['Horizontal Flip','NA','Yes','Yes','Yes','Yes','Yes',],
['Vertical Flip','NA','NA','NA','Yes','Yes','NA',],
['Croping and Padding','NA','pad=border','pad=border','pad=reflection','pad=reflection','pad=zeros',],
['Rotation','NA','max_rotate=25.0','max_rotate=25.0','max_rotate=180.0','max_rotate=180.0','max_rotate=16.0',],
['Warping','NA','magnitude=0.3','magnitude=0.3','magnitude=0.4','magnitude=0.4','magnitude=0.3',],
['Lighting','brightness=0.2','brightness=0.3','brightness=0.3','brightness=0.4','brightness=0.4','brightness=0.3',],
['Grayscale','NA','NA','NA','NA','NA','Yes',],
['Contrast','contrast=0.1','contrast=0.3','contrast=0.3','contrast=0.3','contrast=0.4','contrast=0.4',],
['Saturation','NA','saturation=3.5','saturation=2.0','saturation=3.0','saturation=3.0','saturation=2.5',],
['Hue Shifting','NA','NA','NA','hue=0.15','hue=0.2','hue=0.2',],
['Noise Injection','limit=(100.0, 300.0)','limit=(300.0, 500.0)','limit=(200.0, 400.0)','limit=(200.0, 400.0)','limit=(300.0, 400.0)','limit=(300.0, 500.0)',],
['Sun Flare','NA','NA','radius=200','NA','NA','NA',],
['Rain','NA','NA','length=20','NA','NA','NA',],
['Sepia','NA','Yes','NA','NA','NA','NA',],
['FancyPCA','NA','alpha=0.5','alpha=0.5','alpha=0.5','alpha=0.5','NA',],
['Random Erasing','NA','max_count=3','max_count=3','max_count=4','max_count=4','NA',]]
# Create the pandas DataFrame
df = pandas.DataFrame(data, columns=['Filter','Covid-19', 'People', 'Fungi', 'Sea Animal', 'Food', 'Mall Crowd'])
display(df)
return
class AlbumentationsTransform(DisplayedTransform):
split_idx,order=0,2
def __init__(self, train_aug): store_attr()
def encodes(self, img: fastai.vision.core.PILImage):
aug_img = self.train_aug(image=numpy.array(img))['image']
return fastai.vision.core.PILImage.create(aug_img)
@add_method(PacktDataAug)
def _fetch_album_covid19(self):
return albumentations.Compose([
albumentations.GaussNoise(var_limit=(100.0, 300.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_covid19(self,df,bsize=15):
aug = [
fastai.vision.augment.Brightness(max_lighting=0.3,p=0.5),
fastai.vision.augment.Contrast(max_lighting=0.4, p=0.5),
AlbumentationsTransform(self._fetch_album_covid19())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def _fetch_album_people(self):
return albumentations.Compose([
albumentations.ColorJitter(brightness=0.3, contrast=0.4, saturation=3.5,hue=0.0, p=0.5),
albumentations.ToSepia(p=0.5),
albumentations.FancyPCA(alpha=0.5, p=0.5),
albumentations.GaussNoise(var_limit=(300.0, 500.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_people(self,df,bsize=15):
aug = [
fastai.vision.augment.Flip(p=0.5),
fastai.vision.augment.Rotate(25.0,p=0.5,pad_mode='border'),
fastai.vision.augment.Warp(magnitude=0.3, pad_mode='border',p=0.5),
fastai.vision.augment.RandomErasing(p=0.5,max_count=2),
AlbumentationsTransform(self._fetch_album_people())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def _fetch_album_fungi(self):
return albumentations.Compose([
albumentations.ColorJitter(brightness=0.3, contrast=0.4, saturation=2.0,hue=0.0, p=0.5),
albumentations.FancyPCA(alpha=0.5, p=0.5),
albumentations.RandomSunFlare(flare_roi=(0, 0, 1, 0.5),src_radius=200, always_apply=True, p=0.5),
albumentations.RandomRain(drop_length=20, drop_width=1.1,blur_value=1.1,always_apply=True, p=0.5),
albumentations.GaussNoise(var_limit=(200.0, 400.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_fungi(self,df,bsize=15):
aug = [
fastai.vision.augment.Flip(p=0.5),
fastai.vision.augment.Rotate(25.0,p=0.5,pad_mode='border'),
fastai.vision.augment.Warp(magnitude=0.3, pad_mode='border',p=0.5),
fastai.vision.augment.RandomErasing(p=0.5,max_count=2),
AlbumentationsTransform(self._fetch_album_fungi())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def _fetch_album_sea_animal(self):
return albumentations.Compose([
albumentations.ColorJitter(brightness=0.4, contrast=0.4, saturation=2.0,hue=1.5, p=0.5),
albumentations.FancyPCA(alpha=0.5, p=0.5),
albumentations.GaussNoise(var_limit=(200.0, 400.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_sea_animal(self,df,bsize=15):
aug = [
fastai.vision.augment.Dihedral(p=0.5,pad_mode='reflection'),
fastai.vision.augment.Rotate(180.0,p=0.5,pad_mode='reflection'),
fastai.vision.augment.Warp(magnitude=0.3, pad_mode='reflection',p=0.5),
fastai.vision.augment.RandomErasing(p=0.5,max_count=2),
AlbumentationsTransform(self._fetch_album_sea_animal())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def _fetch_album_food(self):
return albumentations.Compose([
albumentations.ColorJitter(brightness=0.4, contrast=0.4, saturation=2.0,hue=1.5, p=0.5),
albumentations.FancyPCA(alpha=0.5, p=0.5),
albumentations.GaussNoise(var_limit=(200.0, 400.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_food(self,df,bsize=15):
aug = [
fastai.vision.augment.Dihedral(p=0.5,pad_mode='reflection'),
fastai.vision.augment.Rotate(180.0,p=0.5,pad_mode='reflection'),
fastai.vision.augment.Warp(magnitude=0.3, pad_mode='reflection',p=0.5),
fastai.vision.augment.RandomErasing(p=0.5,max_count=2),
AlbumentationsTransform(self._fetch_album_food())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
@add_method(PacktDataAug)
def _fetch_album_crowd(self):
return albumentations.Compose([
albumentations.ColorJitter(brightness=0.3, contrast=0.4, saturation=3.5,hue=0.0, p=0.5),
albumentations.ToSepia(p=0.5),
albumentations.FancyPCA(alpha=0.5, p=0.5),
albumentations.GaussNoise(var_limit=(300.0, 500.0), p=0.5)
])
#
@add_method(PacktDataAug)
def draw_augment_crowd(self,df,bsize=15):
aug = [
fastai.vision.augment.Flip(p=0.5),
fastai.vision.augment.Rotate(25.0,p=0.5,pad_mode='zeros'),
fastai.vision.augment.Warp(magnitude=0.3, pad_mode='zeros',p=0.5),
fastai.vision.augment.RandomErasing(p=0.5,max_count=2),
AlbumentationsTransform(self._fetch_album_crowd())
]
itfms = fastai.vision.augment.Resize(480)
dsl_org = self._make_data_loader(df, aug,itfms)
dsl_org.show_batch(max_n=bsize)
return dsl_org
pluto.version = 4.0
@add_method(PacktDataAug)
def _make_df_mask_name(self,fname):
p = pathlib.Path(fname)
return (str(p.parent.parent) + '/' + str(p.parent.name) + '_labels/' + str(p.stem)+'_L' + str(p.suffix))
#
@add_method(PacktDataAug)
def make_df_mask_name(self,df):
df['mask_name'] = df.fname.apply(self._make_df_mask_name)
return
@add_method(PacktDataAug)
def _make_batch_segmentation(self,df, disp_max=8,is_shuffle=False):
# get random or not
if (is_shuffle):
_fns = df.sample(disp_max)
_fns.reset_index(drop=True, inplace=True)
else:
_fns = df.head(disp_max)
# merge the list
fname = []
fmask = []
for i in range(disp_max):
fname.append(str(_fns.fname[i]))
fname.append(str(_fns.mask_name[i]))
#
fmask.append(str(pathlib.Path(_fns.fname[i]).name))
fmask.append('Mask: ' +str(pathlib.Path(_fns.mask_name[i]).name))
#
return fname, fmask
#
@add_method(PacktDataAug)
def draw_batch_segmentation(self,df, disp_max=8,is_shuffle=False, figsize=(16,8), disp_col=4):
disp_row = int(numpy.round((disp_max/disp_col)+0.4, 0))
#
fname, fmask = self._make_batch_segmentation(df, disp_max,is_shuffle)
canvas, pic = matplotlib.pyplot.subplots(disp_row,disp_col, figsize=figsize)
#
_pics = pic.flatten()
# display it
for i, ax in enumerate(_pics):
try:
im = PIL.Image.open(fname[i])
ax.imshow(im)
ax.set_title(fmask[i])
except:
self._pp("Error, invalid image", fname[i])
canvas.tight_layout()
self._drop_image(canvas)
canvas.show()
return
@add_method(PacktDataAug)
def _make_df_mask_name_aerial(self,fname):
p = pathlib.Path(fname)
return (str(p.parent.parent) + '/masks/' + str(p.stem) + '.png')
#
@add_method(PacktDataAug)
def make_df_mask_name_aerial(self,df):
i = df[df['label'] =='masks'].index
df.drop(i, inplace=True)
df.reset_index(drop=True, inplace=True)
df['mask_name'] = df.fname.apply(self._make_df_mask_name_aerial)
return
@add_method(PacktDataAug)
def draw_image_flip_pil_segmen(self,fname):
img = PIL.Image.open(fname)
mirror_img = PIL.ImageOps.mirror(img)
canvas, pic = matplotlib.pyplot.subplots(1,2, figsize=(16,6))
#display(img, mirror_img)
pic[0].imshow(img)
pic[0].set_title(pathlib.Path(fname).name)
pic[1].imshow(mirror_img)
pic[1].set_title('Horizontal Flip')
#
canvas.tight_layout()
self._drop_image(canvas)
canvas.show()
return
@add_method(PacktDataAug)
def _draw_image_album_segmentation(self,df,aug_album,label_name):
bsize = 2
ncol = 4
nrow = 2
w = 18
h = 8
#
canvas, pic = matplotlib.pyplot.subplots(nrow, ncol, figsize=(w, h))
pics = pic.flatten()
# select random images
samp = df.sample(bsize)
samp.reset_index(drop=True, inplace=True)
#
_img = []
_label = []
for i in range(2):
img = PIL.Image.open(samp.fname[i])
imask = PIL.Image.open(samp.mask_name[i])
_img.append(img)
_img.append(imask)
img_numpy = numpy.array(img)
imask_numpy = numpy.array(imask)
album = aug_album(image=img_numpy,mask=imask_numpy)
_img.append(album['image'])
_img.append(album['mask'])
#
_label.append(str(pathlib.Path(samp.fname[i]).name))
_label.append('Mask:')
_label.append(label_name)
_label.append(label_name + ': Mask:')
#
for i, ax in enumerate(pics):
ax.imshow(_img[i])
ax.set_title(_label[i])
canvas.tight_layout()
self._drop_image(canvas)
canvas.show()
return
@add_method(PacktDataAug)
def draw_image_flip_segmen(self,df):
aug_album = albumentations.HorizontalFlip(p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Horizontal Flip')
return
@add_method(PacktDataAug)
def draw_image_flip_both_segmen(self,df):
aug_album = albumentations.Flip(p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Vertical Flip')
return
@add_method(PacktDataAug)
def draw_image_rotate_segmen(self,df):
aug_album = albumentations.Rotate(limit=45, p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Rotate')
return
@add_method(PacktDataAug)
def draw_image_resize_segmen(self,df):
aug_album = albumentations.RandomSizedCrop(min_max_height=(500, 600), height=500, width=500, p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Resize')
return
@add_method(PacktDataAug)
def draw_image_transpose_segmen(self,df):
aug_album = albumentations.Transpose(p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Transpose')
return
@add_method(PacktDataAug)
def draw_image_brightness_segmen(self,df,brightness=0.5):
aug_album = albumentations.ColorJitter(brightness=brightness,
contrast=0.0, saturation=0.0,hue=0.0,always_apply=True, p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Brightness')
return
@add_method(PacktDataAug)
def draw_image_contrast_segmen(self,df,contrast=0.5):
aug_album = albumentations.ColorJitter(brightness=0.0,
contrast=contrast, saturation=0.0,hue=0.0,always_apply=True, p=1.0)
self._draw_image_album_segmentation(df,aug_album,'Contrast')
return
@add_method(PacktDataAug)
def draw_image_fancyPCA_segmen(self,df,alpha=0.3):
aug_album = albumentations.FancyPCA(alpha=alpha, always_apply=True, p=1.0)
self._draw_image_album_segmentation(df,aug_album,'FancyPCA')
return
@add_method(PacktDataAug)
def print_safe_parameters_segmen(self):
data = [
['Horizontal Flip','Yes','Yes','Yes','Yes'],
['Vertical Flip','Yes','Yes','Yes','Yes'],
['Resize and Crop','Yes','Yes','Yes','Yes'],
['Rotation','Yes','Yes','Yes','Yes'],
['Transpose','Yes','Yes','Yes','Yes'],
['Lighting','Yes','No','Yes','No'],
['FancyPCA','Yes','No','Yes','No']]
# Create the pandas DataFrame
df = pandas.DataFrame(data, columns=['filters','CamVid','CamVid Mask', 'Aerial', 'Aerial Mask'])
display(df)
return
@add_method(PacktDataAug)
def draw_uber_segmen(self,df,contrast=0.5):
aug_album = albumentations.Compose([
albumentations.ColorJitter(brightness=0.5,contrast=0.0, saturation=0.0,hue=0.0,p=0.5),
albumentations.HorizontalFlip(p=0.5),
albumentations.Flip(p=0.5),
albumentations.Rotate(limit=45, p=0.5),
albumentations.RandomSizedCrop(min_max_height=(500, 600), height=500, width=500,p=0.5),
albumentations.Transpose(p=0.5),
albumentations.FancyPCA(alpha=0.2, p=0.5)
])
self._draw_image_album_segmentation(df,aug_album,'Combine')
return