forked from aleju/imgaug-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_main_repo_readme_images.py
1331 lines (1227 loc) · 62.2 KB
/
generate_main_repo_readme_images.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, division
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
import imageio
from skimage import data
import matplotlib.pyplot as plt
import tempfile
import six.moves as sm
import re
import os
from collections import defaultdict
import PIL.Image
try:
from cStringIO import StringIO as BytesIO
except ImportError:
from io import BytesIO
np.random.seed(44)
ia.seed(44)
#IMAGES_DIR = "docs/readme_images"
IMAGES_DIR = "readme_images"
def main():
draw_small_overview()
#draw_single_sequential_images()
#draw_per_augmenter_videos()
def draw_small_overview():
ia.seed(44)
image = ia.quokka(size=0.2)
heatmap = ia.quokka_heatmap(size=0.2)
segmap = ia.quokka_segmentation_map(size=0.2)
kps = ia.quokka_keypoints(size=0.2)
bbs = ia.quokka_bounding_boxes(size=0.2)
polys = ia.quokka_polygons(size=0.2)
batch = ia.Batch(
images=[image],
heatmaps=[heatmap.invert()],
segmentation_maps=[segmap],
keypoints=[kps],
bounding_boxes=[bbs],
polygons=[polys]
)
augs = []
augs.append(("noop", iaa.Noop()))
augs.append(("non_geometric", iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=(0, 20)),
iaa.ContrastNormalization(1.2),
iaa.Sharpen(alpha=1.0, lightness=1.5)
])))
augs.append(("affine", iaa.Affine(rotate=0, translate_percent={"x": 0.1}, scale=1.3, mode="constant", cval=25)))
augs.append(("cropandpad", iaa.CropAndPad(percent=(-0.05, 0.2, -0.05, -0.2), pad_mode="maximum")))
augs.append(("fliplr_perspective", iaa.Sequential([
iaa.Fliplr(1.0),
iaa.PerspectiveTransform(scale=0.15)
])))
for name, aug in augs:
result = list(aug.augment_batches([batch]))[0]
image_aug = result.images_aug[0]
image_aug_heatmap = result.heatmaps_aug[0].draw(cmap=None)[0]
image_aug_segmap = result.segmentation_maps_aug[0].draw_on_image(image_aug, alpha=0.8)
image_aug_kps = result.keypoints_aug[0].draw_on_image(image_aug, color=[0, 255, 0], size=7)
image_aug_bbs = result.bounding_boxes_aug[0].clip_out_of_image().draw_on_image(image_aug, size=3)
# add polys for now to BBs image to save (screen) space
image_aug_bbs = result.polygons_aug[0].clip_out_of_image().draw_on_image(
image_aug_bbs, color=[0, 128, 0], color_points=[0, 128, 0], alpha=0.0,
alpha_points=1.0, alpha_lines=0.5)
imageio.imwrite(os.path.join(IMAGES_DIR, "small_overview", "%s_image.jpg" % (name,)), image_aug, quality=90)
imageio.imwrite(os.path.join(IMAGES_DIR, "small_overview", "%s_heatmap.jpg" % (name,)), image_aug_heatmap, quality=90)
imageio.imwrite(os.path.join(IMAGES_DIR, "small_overview", "%s_segmap.jpg" % (name,)), image_aug_segmap, quality=90)
imageio.imwrite(os.path.join(IMAGES_DIR, "small_overview", "%s_kps.jpg" % (name,)), image_aug_kps, quality=90)
imageio.imwrite(os.path.join(IMAGES_DIR, "small_overview", "%s_bbs.jpg" % (name,)), image_aug_bbs, quality=90)
def draw_single_sequential_images():
ia.seed(44)
#image = ia.imresize_single_image(imageio.imread("quokka.jpg", pilmode="RGB")[0:643, 0:643], (128, 128))
image = ia.quokka_square(size=(128, 128))
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
seq = iaa.Sequential(
[
# apply the following augmenters to most images
iaa.Fliplr(0.5), # horizontally flip 50% of all images
iaa.Flipud(0.2), # vertically flip 20% of all images
# crop images by -5% to 10% of their height/width
sometimes(iaa.CropAndPad(
percent=(-0.05, 0.1),
pad_mode=ia.ALL,
pad_cval=(0, 255)
)),
sometimes(iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
rotate=(-45, 45), # rotate by -45 to +45 degrees
shear=(-16, 16), # shear by -16 to +16 degrees
order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
cval=(0, 255), # if mode is constant, use a cval between 0 and 255
mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
# execute 0 to 5 of the following (less important) augmenters per image
# don't execute all of them, as that would often be way too strong
iaa.SomeOf((0, 5),
[
sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
# search either for all edges or for directed edges,
# blend the result with the original image using a blobby mask
iaa.SimplexNoiseAlpha(iaa.OneOf([
iaa.EdgeDetect(alpha=(0.5, 1.0)),
iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
iaa.Invert(0.05, per_channel=True), # invert color channels
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
# either change the brightness of the whole image (sometimes
# per channel) or change the brightness of subareas
iaa.OneOf([
iaa.Multiply((0.5, 1.5), per_channel=0.5),
iaa.FrequencyNoiseAlpha(
exponent=(-4, 0),
first=iaa.Multiply((0.5, 1.5), per_channel=True),
second=iaa.ContrastNormalization((0.5, 2.0))
)
]),
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
iaa.Grayscale(alpha=(0.0, 1.0)),
sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
],
random_order=True
)
],
random_order=True
)
grid = seq.draw_grid(image, cols=8, rows=8)
imageio.imwrite(os.path.join(IMAGES_DIR, "examples_grid.jpg"), grid)
def draw_per_augmenter_images():
print("[draw_per_augmenter_images] Loading image...")
#image = ia.imresize_single_image(imageio.imread("quokka.jpg", pilmode="RGB")[0:643, 0:643], (128, 128))
image = ia.quokka_square(size=(128, 128))
keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)] # left ear, right ear, mouth
keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]
print("[draw_per_augmenter_images] Initializing...")
rows_augmenters = [
(0, "Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]),
(0, "Crop\n(top, right,\nbottom, left)", [(str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
(0, "Pad\n(top, right,\nbottom, left)", [(str(vals), iaa.Pad(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]),
(0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]),
(0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]),
(0, "Superpixels\np_replace=1", [("n_segments=%d" % (n_segments,), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]),
(0, "Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace,), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]),
(0, "Invert", [("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]),
(0, "Invert\n(per_channel)", [("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]),
(0, "Add", [("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]),
(0, "Add\n(per channel)", [("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]),
(0, "AddToHueAndSaturation", [("value=%d" % (val,), iaa.AddToHueAndSaturation(val)) for val in [-45, -25, 0, 25, 45]]),
(0, "Multiply", [("value=%.2f" % (val,), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]),
(1, "Multiply\n(per channel)", [("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
(0, "GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
(0, "AverageBlur", [("k=%d" % (k,), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
(0, "MedianBlur", [("k=%d" % (k,), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]),
(0, "BilateralBlur\nsigma_color=250,\nsigma_space=250", [("d=%d" % (d,), iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250)) for d in [1, 3, 5, 7, 9]]),
(0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
(0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
(0, "EdgeDetect", [("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
(0, "DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]]),
(0, "AdditiveGaussianNoise", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
(0, "AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
(0, "Dropout", [("p=%.2f" % (p,), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
(0, "Dropout\n(per channel)", [("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
(3, "CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
(0, "CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
(0, "SaltAndPepper", [("p=%.2f" % (p,), iaa.SaltAndPepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
(0, "Salt", [("p=%.2f" % (p,), iaa.Salt(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
(0, "Pepper", [("p=%.2f" % (p,), iaa.Pepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
(0, "CoarseSaltAndPepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSaltAndPepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
(0, "CoarseSalt\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseSalt(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
(0, "CoarsePepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarsePepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
(0, "ContrastNormalization", [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
(0, "ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]),
(0, "Grayscale", [("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
(6, "PerspectiveTransform", [("scale=%.3f" % (scale,), iaa.PerspectiveTransform(scale=scale)) for scale in [0.025, 0.05, 0.075, 0.10, 0.125]]),
(0, "PiecewiseAffine", [("scale=%.3f" % (scale,), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]),
(0, "Affine: Scale", [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]),
(0, "Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y})) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]),
(0, "Affine: Rotate", [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]),
(0, "Affine: Shear", [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]),
(0, "Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]),
(0, "Affine: cval", [("%d" % (int(cval*255),), iaa.Affine(translate_px=-32, cval=int(cval*255), mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]),
(
2, "Affine: all", [
(
"",
iaa.Affine(
scale={"x": (0.5, 1.5), "y": (0.5, 1.5)},
translate_px={"x": (-32, 32), "y": (-32, 32)},
rotate=(-45, 45),
shear=(-32, 32),
mode=ia.ALL,
cval=(0.0, 1.0)
)
)
for _ in sm.xrange(5)
]
),
(1, "ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]),
(0, "Alpha\nwith EdgeDetect(1.0)", [("factor=%.1f" % (factor,), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0))) for factor in [0.0, 0.25, 0.5, 0.75, 1.0]]),
(4, "Alpha\nwith EdgeDetect(1.0)\n(per channel)", [("factor=(%.2f, %.2f)" % (factor[0], factor[1]), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0), per_channel=0.5)) for factor in [(0.0, 0.2), (0.15, 0.35), (0.4, 0.6), (0.65, 0.85), (0.8, 1.0)]]),
(15, "SimplexNoiseAlpha\nwith EdgeDetect(1.0)", [("", iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0))) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
(9, "FrequencyNoiseAlpha\nwith EdgeDetect(1.0)", [("exponent=%.1f" % (exponent,), iaa.FrequencyNoiseAlpha(exponent=exponent, first=iaa.EdgeDetect(1.0), size_px_max=16, upscale_method="linear", sigmoid=False)) for exponent in [-4, -2, 0, 2, 4]])
]
print("[draw_per_augmenter_images] Augmenting...")
rows = []
for (row_seed, row_name, augmenters) in rows_augmenters:
ia.seed(row_seed)
#for img_title, augmenter in augmenters:
# #aug.reseed(1000)
# pass
row_images = []
row_keypoints = []
row_titles = []
for img_title, augmenter in augmenters:
aug_det = augmenter.to_deterministic()
row_images.append(aug_det.augment_image(image))
row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
row_titles.append(img_title)
rows.append((row_name, row_images, row_keypoints, row_titles))
# matplotlib drawin routine
"""
print("[draw_per_augmenter_images] Plotting...")
width = 8
height = int(1.5 * len(rows_augmenters))
fig = plt.figure(figsize=(width, height))
grid_rows = len(rows)
grid_cols = 1 + 5
gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1])
axes = []
for i in sm.xrange(grid_rows):
axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)])
fig.tight_layout()
#fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22)
#fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02)
fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02)
for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows):
axes_row = axes[row_idx]
for col_idx in sm.xrange(grid_cols):
ax = axes_row[col_idx]
ax.cla()
ax.axis("off")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
if col_idx == 0:
ax.text(0, 0.5, row_name, color="black")
else:
cell_image = row_images[col_idx-1]
cell_keypoints = row_keypoints[col_idx-1]
cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5)
ax.imshow(cell_image_kp)
x = 0
y = 145
#ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6)
ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7)
fig.savefig("examples.jpg", bbox_inches="tight")
#plt.show()
"""
# simpler and faster drawing routine
"""
output_image = ExamplesImage(128, 128, 128+64, 32)
for (row_name, row_images, row_keypoints, row_titles) in rows:
row_images_kps = []
for image, keypoints in zip(row_images, row_keypoints):
row_images_kps.append(keypoints.draw_on_image(image, size=5))
output_image.add_row(row_name, row_images_kps, row_titles)
imageio.imwrite("examples.jpg", output_image.draw())
"""
# routine to draw many single files
seen = defaultdict(lambda: 0)
markups = []
for (row_name, row_images, row_keypoints, row_titles) in rows:
output_image = ExamplesImage(128, 128, 128+64, 32)
row_images_kps = []
for image, keypoints in zip(row_images, row_keypoints):
row_images_kps.append(keypoints.draw_on_image(image, size=5))
output_image.add_row(row_name, row_images_kps, row_titles)
if "\n" in row_name:
row_name_clean = row_name[0:row_name.find("\n")+1]
else:
row_name_clean = row_name
row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower())
row_name_clean = row_name_clean.strip("_")
if seen[row_name_clean] > 0:
row_name_clean = "%s_%d" % (row_name_clean, seen[row_name_clean] + 1)
fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean,))
#imageio.imwrite(fp, output_image.draw())
save(fp, output_image.draw())
seen[row_name_clean] += 1
markup_descr = row_name.replace('"', '') \
.replace("\n", " ") \
.replace("(", "") \
.replace(")", "")
markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr)
markups.append(markup)
for markup in markups:
print(markup)
class ExamplesImage(object):
def __init__(self, image_height, image_width, title_cell_width, subtitle_height):
self.rows = []
self.image_height = image_height
self.image_width = image_width
self.title_cell_width = title_cell_width
self.cell_height = image_height + subtitle_height
self.cell_width = image_width
def add_row(self, title, images, subtitles):
assert len(images) == len(subtitles)
images_rs = []
for image in images:
images_rs.append(ia.imresize_single_image(image, (self.image_height, self.image_width)))
self.rows.append((title, images_rs, subtitles))
def draw(self):
rows_drawn = [self.draw_row(title, images, subtitles) for title, images, subtitles in self.rows]
grid = np.vstack(rows_drawn)
return grid
def draw_row(self, title, images, subtitles):
title_cell = np.zeros((self.cell_height, self.title_cell_width, 3), dtype=np.uint8) + 255
title_cell = ia.draw_text(title_cell, x=2, y=12, text=title, color=[0, 0, 0], size=16)
image_cells = []
for image, subtitle in zip(images, subtitles):
image_cell = np.zeros((self.cell_height, self.cell_width, 3), dtype=np.uint8) + 255
image_cell[0:image.shape[0], 0:image.shape[1], :] = image
image_cell = ia.draw_text(image_cell, x=2, y=image.shape[0]+2, text=subtitle, color=[0, 0, 0], size=11)
image_cells.append(image_cell)
row = np.hstack([title_cell] + image_cells)
return row
def slugify(s):
return re.sub(r"[^a-z0-9]+", "_", s.lower()).strip("_")
def draw_per_augmenter_videos():
class _Descriptor(object):
def __init__(self, module, title, augmenters, subtitles, seed=0, affects_geometry=False, comment=None):
self.module = module
self.title = title
self.augmenters = augmenters
self.subtitles = subtitles
self.seed = seed
self.affects_geometry = affects_geometry
self.comment = comment
@property
def title_markup(self):
return self.title.replace('"', '') \
.replace("\n", " ") \
.replace("(", "") \
.replace(")", "")
@classmethod
def from_augsubs(cls, module, title, augsubs, seed=0, affects_geometry=False, comment=None):
return _Descriptor(module=module, title=title,
augmenters=[el[1] for el in augsubs],
subtitles=[el[0] for el in augsubs],
seed=seed, affects_geometry=affects_geometry, comment=comment)
def generate_frames(self, image, keypoints, bounding_boxes, polygons, heatmap, segmap, subtitle_height):
frames_images = []
frames_kps = []
frames_bbs = []
frames_heatmap = []
frames_segmap = []
any_subtitle = any([len(subtitle) > 0 for subtitle in self.subtitles])
for i, (augmenter, subtitle) in enumerate(zip(self.augmenters, self.subtitles)):
# print("seeding", augmenter.name, self.seed+i)
augmenter.localize_random_state_(recursive=True)
augmenter.reseed(random_state=self.seed+i)
def _subt(img, toptitle):
if self.affects_geometry:
#return self._draw_cell(img, subtitle, subtitle_height if any_subtitle else 0, toptitle, 16)
return self._draw_cell(img, subtitle, subtitle_height, toptitle, 16)
else:
#return self._draw_cell(img, subtitle, subtitle_height if any_subtitle else 0, "", 0)
return self._draw_cell(img, subtitle, subtitle_height, "", 16)
aug_det = augmenter.to_deterministic()
image_aug = aug_det.augment_image(image)
kps_aug = aug_det.augment_keypoints([keypoints])[0]
bbs_aug = aug_det.augment_bounding_boxes([bounding_boxes])[0]
polys_aug = aug_det.augment_polygons([polygons])[0]
heatmap_aug = aug_det.augment_heatmaps([heatmap])[0]
segmap_aug = aug_det.augment_segmentation_maps([segmap])[0]
if self.affects_geometry:
image_with_coordsaug = _subt(
polys_aug.draw_on_image(
bbs_aug.draw_on_image(
kps_aug.draw_on_image(image_aug, size=5)
),
color_lines=(0, 128, 0),
color_points=(0, 128, 0),
alpha=0,
alpha_lines=0.5,
alpha_points=1.0
),
"IMG, KPs, BBs, Polys"
)
frames_images.append(image_with_coordsaug)
#frames_kps.append(_subt(kps_aug.draw_on_image(image_aug, size=5), "keypoints"))
#frames_bbs.append(_subt(bbs_aug.draw_on_image(image_aug), "bounding boxes"))
#frames_kps.append(_subt(
# bbs_aug.draw_on_image(kps_aug.draw_on_image(image_aug, size=5)),
# "Keypoints + BBs"
#))
frames_heatmap.append(_subt(heatmap_aug.draw_on_image(image_aug)[0], "Heatmaps"))
frames_segmap.append(_subt(segmap_aug.draw_on_image(image_aug), "Segmentation Maps"))
else:
frames_images.append(_subt(image_aug, "Images"))
return frames_images, frames_kps, frames_bbs, frames_heatmap, frames_segmap
@classmethod
def _draw_cell(cls, image, subtitle, subtitle_height, toptitle, toptitle_height):
cell_height, cell_width = image.shape[0:2]
image_cell = np.zeros((toptitle_height + cell_height + subtitle_height, cell_width, 3), dtype=np.uint8) + 255
image_cell[toptitle_height:toptitle_height+image.shape[0], 0:image.shape[1], :] = image
image_cell = ia.draw_text(image_cell, x=2, y=toptitle_height + image.shape[0]+2, text=subtitle, color=[0, 0, 0], size=9)
if toptitle != "":
image_cell = ia.draw_text(image_cell, x=2, y=2, text=toptitle, color=[0, 0, 0], size=9)
return image_cell
class _MarkdownTableCell(object):
def __init__(self, descriptor, markup_images, markup_kps, markup_bbs, markup_hm, markup_segmap):
self.descriptor = descriptor
self.markup_images = markup_images
self.markup_kps = markup_kps
self.markup_bbs = markup_bbs
self.markup_hm = markup_hm
self.markup_segmap = markup_segmap
@property
def colspan(self):
#only_images = len(self.markup_kps) == 0 and len(self.markup_bbs) == 0 and len(self.markup_hm) == 0 and len(self.markup_segmap) == 0
only_images = not self.descriptor.affects_geometry
return 1 if only_images else 2
def render_title(self):
#return '<td colspan="%d">\n<small>\n%s\n</small>\n</td>' % (self.colspan, self.descriptor.title.replace("\n", "<br/>"))
return '<td colspan="%d"><sub>%s</sub></td>' % (self.colspan, self.descriptor.title.replace("\n", "<br/>"))
def render_main(self):
#return '<td colspan="%d">\n\n%s%s%s%s%s\n\n</td>' % (self.colspan, self.markup_images, self.markup_kps, self.markup_bbs, self.markup_hm, self.markup_segmap)
return '<td colspan="%d">%s%s%s%s%s</td>' % (self.colspan, self.markup_images, self.markup_kps, self.markup_bbs, self.markup_hm, self.markup_segmap)
def render_comment(self):
if self.descriptor.comment is not None:
#return '<td colspan="%d">\n<small>\n\n%s\n\n</small>\n</td>' % (self.colspan, self.descriptor.comment,)
return '<td colspan="%d"><sub>%s</sub></td>' % (self.colspan, self.descriptor.comment,)
else:
return '<td colspan="%d"> </td>' % (self.colspan,)
class _MarkdownTable(object):
ROW_SIZE = 5 # in columns
def __init__(self):
self.cells = []
def render(self):
current_module = None
first_row_in_module = True
markup = []
cells = self.cells
while len(cells) > 0:
current_row_size = 0
row_title = []
row_main = []
row_comment = []
any_comment = False
while current_row_size < self.ROW_SIZE and len(cells) > 0:
cell = cells[0]
if current_module is None:
current_module = cell.descriptor.module
elif current_module != cell.descriptor.module:
if current_row_size == 0:
current_module = cell.descriptor.module
first_row_in_module = True
else:
break
if cell.colspan > (self.ROW_SIZE - current_row_size):
break
row_title.append(cell.render_title())
row_main.append(cell.render_main())
row_comment.append(cell.render_comment())
if cell.descriptor.comment is not None and len(cell.descriptor.comment) > 0:
any_comment = True
current_row_size += cell.colspan
cells = cells[1:]
while current_row_size < self.ROW_SIZE:
row_title.append("<td> </td>")
row_main.append("<td> </td>")
row_comment.append("<td> </td>")
current_row_size += 1
if first_row_in_module:
#markup.append('<tr>\n<td colspan="3">\n\n**%s**\n\n</td>\n</tr>' % (current_module,))
markup.append('<tr><td colspan="%d"><strong>%s</strong></td></tr>' % (self.ROW_SIZE, current_module,))
first_row_in_module = False
markup.append("<tr>\n%s\n</tr>\n<tr>\n%s\n</tr>%s" % (
"\n".join(row_title),
"\n".join(row_main),
"" if not any_comment else "\n<tr>\n%s\n</tr>" % ("\n".join(row_comment),)
))
return "<table>\n\n%s\n\n</table>" % ("\n".join(markup),)
def append(self, descriptor, markup_images, markup_kps, markup_bbs, markup_hm, markup_segmap):
self.cells.append(_MarkdownTableCell(descriptor, markup_images, markup_kps, markup_bbs, markup_hm, markup_segmap))
print("[draw_per_augmenter_videos] Loading image...")
# image = ia.imresize_single_image(imageio.imread("quokka.jpg", pilmode="RGB")[0:643, 0:643], (128, 128))
h, w = 100, 100
h_subtitle = 32
image = ia.quokka_square(size=(h, w))
keypoints = ia.quokka_keypoints(size=(h, w), extract="square")
bbs = ia.quokka_bounding_boxes(size=(h, w), extract="square")
polygons = ia.quokka_polygons(size=(h, w), extract="square")
heatmap = ia.quokka_heatmap(size=(h, w), extract="square")
segmap = ia.quokka_segmentation_map(size=(h, w), extract="square")
image_landscape = imageio.imread("https://upload.wikimedia.org/wikipedia/commons/8/89/Kukle%2CCzech_Republic..jpg", format="jpg")
# os.path.join(os.path.dirname(os.path.abspath(__file__)), "landscape.jpg")
image_landscape = ia.imresize_single_image(image_landscape, (96, 128))
print("[draw_per_augmenter_videos] Initializing...")
descriptors = []
# ###
# meta
# ###
descriptors.extend([
# Augmenter (base class for all augmenters)
# Sequential
# SomeOf
# OneOf
# Sometimes
# WithChannels
_Descriptor.from_augsubs(
"meta",
"Noop",
[("", iaa.Noop()) for _ in sm.xrange(1)]),
# Lambda
# AssertLambda
# AssertShape
_Descriptor.from_augsubs(
"meta",
"ChannelShuffle",
[("p=1.0", iaa.ChannelShuffle(p=1.0)) for _ in sm.xrange(5)]
)
])
# ###
# arithmetic
# ###
descriptors.extend([
_Descriptor.from_augsubs(
"arithmetic",
"Add",
[("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Add\n(per_channel=True)",
[("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True))
for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]
),
# AddElementwise
_Descriptor.from_augsubs(
"arithmetic",
"AdditiveGaussianNoise",
[("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255))
for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]
),
_Descriptor.from_augsubs(
"arithmetic",
"AdditiveGaussianNoise\n(per_channel=True)",
[("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True))
for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]
),
_Descriptor.from_augsubs(
"arithmetic",
"AdditiveLaplaceNoise",
[("scale=%.2f*255" % (scale,), iaa.AdditiveLaplaceNoise(scale=scale * 255))
for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]
),
_Descriptor.from_augsubs(
"arithmetic",
"AdditiveLaplaceNoise\n(per_channel=True)",
[("scale=%.2f*255" % (scale,), iaa.AdditiveLaplaceNoise(scale=scale * 255, per_channel=True))
for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]
),
_Descriptor.from_augsubs(
"arithmetic",
"AdditivePoissonNoise",
[("lam=%.2f" % (lam,), iaa.AdditivePoissonNoise(lam=lam))
for lam in [4.0, 8.0, 16.0, 32.0, 64.0]]
),
_Descriptor.from_augsubs(
"arithmetic",
"AdditivePoissonNoise\n(per_channel=True)",
[("lam=%.2f" % (lam,), iaa.AdditivePoissonNoise(lam=lam, per_channel=True))
for lam in [4.0, 8.0, 16.0, 32.0, 64.0]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Multiply",
[("value=%.2f" % (val,), iaa.Multiply(val))
for val in [0.25, 0.5, 1.0, 1.25, 1.5]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Multiply\n(per_channel=True)",
[("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True))
for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]
),
# MultiplyElementwise
_Descriptor.from_augsubs(
"arithmetic",
"Dropout",
[("p=%.2f" % (p,), iaa.Dropout(p=p))
for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Dropout\n(per_channel=True)",
[("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True))
for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"CoarseDropout\n(p=0.2)",
[("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2))
for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]
),
_Descriptor.from_augsubs(
"arithmetic",
"CoarseDropout\n(p=0.2, per_channel=True)",
[("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2))
for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]
),
# ReplaceElementwise
_Descriptor.from_augsubs(
"arithmetic",
"ImpulseNoise",
[("p=%.2f" % (p,), iaa.ImpulseNoise(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"SaltAndPepper",
[("p=%.2f" % (p,), iaa.SaltAndPepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Salt",
[("p=%.2f" % (p,), iaa.Salt(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Pepper",
[("p=%.2f" % (p,), iaa.Pepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]
),
_Descriptor.from_augsubs(
"arithmetic",
"CoarseSaltAndPepper\n(p=0.2)",
[("size_percent=%.2f" % (size_percent,), iaa.CoarseSaltAndPepper(p=0.2, size_percent=size_percent, min_size=2))
for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]
),
_Descriptor.from_augsubs(
"arithmetic",
"CoarseSalt\n(p=0.2)",
[("size_percent=%.2f" % (size_percent,), iaa.CoarseSalt(p=0.2, size_percent=size_percent, min_size=2))
for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]
),
_Descriptor.from_augsubs(
"arithmetic",
"CoarsePepper\n(p=0.2)",
[("size_percent=%.2f" % (size_percent,), iaa.CoarsePepper(p=0.2, size_percent=size_percent, min_size=2))
for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Invert",
[("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 1]]
),
_Descriptor.from_augsubs(
"arithmetic",
"Invert\n(per_channel=True)",
[("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]
),
#_Descriptor.from_augsubs(
# "arithmetic",
# "ContrastNormalization",
# [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]
#),
#_Descriptor.from_augsubs(
# "arithmetic",
# "ContrastNormalization\n(per channel)",
# [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True))
# for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]
#),
_Descriptor.from_augsubs(
"arithmetic",
"JpegCompression",
[("compression=%d" % (compression,), iaa.JpegCompression(compression=compression))
for compression in np.linspace(50, 100, num=5)]
)
])
# ###
# blur
# ###
descriptors.extend([
_Descriptor.from_augsubs(
"blur",
"GaussianBlur",
[("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma))
for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]
),
_Descriptor.from_augsubs(
"blur",
"AverageBlur",
[("k=%d" % (k,), iaa.AverageBlur(k=k))
for k in [1, 3, 5, 7, 9]]
),
_Descriptor.from_augsubs(
"blur",
"MedianBlur",
[("k=%d" % (k,), iaa.MedianBlur(k=k))
for k in [1, 3, 5, 7, 9]]
),
_Descriptor.from_augsubs(
"blur",
"BilateralBlur\n(sigma_color=250,\nsigma_space=250)",
[("d=%d" % (d,), iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250))
for d in [1, 3, 5, 7, 9]]
),
_Descriptor.from_augsubs(
"blur",
"MotionBlur\n(angle=0)",
[("k=%d" % (k,), iaa.MotionBlur(k=k, angle=0)) for k in [3, 5, 7, 11, 13]]
),
_Descriptor.from_augsubs(
"blur",
"MotionBlur\n(k=5)",
[("angle=%d" % (angle,), iaa.MotionBlur(k=5, angle=angle))
for angle in np.linspace(0, 360-360/5, num=5)]
)
])
# ####
# color
# ####
descriptors.extend([
# WithColorspace
_Descriptor.from_augsubs(
"color",
"AddToHueAndSaturation",
[("value=%d" % (val,), iaa.AddToHueAndSaturation(val)) for val in [-45, -25, 0, 25, 45]]
),
_Descriptor.from_augsubs(
"color",
"Grayscale",
[("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]
)
# ChangeColorspace
])
# ####
# contrast
# ####
descriptors.extend([
_Descriptor.from_augsubs(
"contrast",
"GammaContrast",
[("gamma=%.2f" % (gamma,), iaa.GammaContrast(gamma=gamma)) for gamma in np.linspace(0.5, 1.75, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"GammaContrast\n(per_channel=True)",
[("gamma=(0.5, 1.75)", iaa.GammaContrast(gamma=(0.5, 1.75), per_channel=True)) for _ in range(5)]
),
_Descriptor.from_augsubs(
"contrast",
"SigmoidContrast\n(cutoff=0.5)",
[("gain=%.1f" % (gain,), iaa.SigmoidContrast(gain=gain, cutoff=0.5))
for gain in np.linspace(5, 17.5, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"SigmoidContrast\n(gain=10)",
[("cutoff=%.2f" % (cutoff,), iaa.SigmoidContrast(gain=10, cutoff=cutoff))
for cutoff in np.linspace(0.0, 1.0, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"SigmoidContrast\n(per_channel=True)",
[("gain=(5, 15),\ncutoff=(0.0, 1.0)", iaa.SigmoidContrast(gain=(5, 15), cutoff=(0.0, 1.0), per_channel=True))
for _ in range(5)]
),
_Descriptor.from_augsubs(
"contrast",
"LogContrast",
[("gain=%.2f" % (gain,), iaa.LogContrast(gain=gain)) for gain in np.linspace(0.5, 1.0, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"LogContrast\n(per_channel=True)",
[("gain=(0.5, 1.0)", iaa.LogContrast(gain=(0.5, 1.0), per_channel=True)) for _ in range(5)]
),
_Descriptor.from_augsubs(
"contrast",
"LinearContrast",
[("alpha=%.2f" % (alpha,), iaa.LinearContrast(alpha=alpha)) for alpha in np.linspace(0.25, 1.75, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"LinearContrast\n(per_channel=True)",
[("alpha=(0.25, 1.75)", iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=True)) for _ in range(5)]
),
_Descriptor.from_augsubs(
"contrast",
"AllChannels-\nHistogramEqualization",
[("", iaa.AllChannelsHistogramEqualization()) for _ in range(1)]
),
_Descriptor.from_augsubs(
"contrast",
"HistogramEqualization",
[("to_colorspace=%s" % (to_colorspace,), iaa.HistogramEqualization(to_colorspace=to_colorspace))
for to_colorspace
in [iaa.HistogramEqualization.Lab, iaa.HistogramEqualization.HSV, iaa.HistogramEqualization.HLS]]
),
_Descriptor.from_augsubs(
"contrast",
"AllChannelsCLAHE",
[("clip_limit=%d" % (int(clip_limit),), iaa.AllChannelsCLAHE(clip_limit=int(clip_limit)))
for clip_limit
in np.linspace(1, 20, num=5)]
),
_Descriptor.from_augsubs(
"contrast",
"AllChannelsCLAHE\n(per_channel=True)",
[("clip_limit=(1, 20)", iaa.AllChannelsCLAHE(clip_limit=(1, 20), per_channel=True)) for _ in range(5)],
seed=4
),
_Descriptor.from_augsubs(
"contrast",
"CLAHE",
[("clip_limit=%d,\nto_colorspace=%s" % (int(clip_limit), to_colorspace),
iaa.CLAHE(clip_limit=int(clip_limit), to_colorspace=to_colorspace))
for to_colorspace, clip_limit
in zip([iaa.CLAHE.Lab] * 5, np.linspace(1, 20, num=5))]
),
])
# ###
# convolutional
# ###
descriptors.extend([
# Convolve
_Descriptor.from_augsubs(
"convolutional",
"Sharpen\n(alpha=1)",
[("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness))
for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
_Descriptor.from_augsubs(
"convolutional",
"Emboss\n(alpha=1)",
[("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength))
for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
_Descriptor.from_augsubs(
"convolutional",
"EdgeDetect",
[("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha))
for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
_Descriptor.from_augsubs(
"convolutional",
"DirectedEdgeDetect\n(alpha=1)",
[("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction))
for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]])
])
# ###
# flip
# ###
descriptors.extend([
_Descriptor.from_augsubs(
"flip",
"Fliplr",
[("p=%.1f" % (p,), iaa.Fliplr(p)) for p in [0, 1]],
affects_geometry=True),
_Descriptor.from_augsubs(
"flip",
"Flipud",
[("p=%.1f" % (p,), iaa.Flipud(p)) for p in [0, 1]],
affects_geometry=True)
])
# ###
# geometric
# ###
descriptors.extend([
_Descriptor.from_augsubs(
"geometric",
"Affine",
[("", iaa.Affine(scale={"x": (0.5, 1.5), "y": (0.5, 1.5)}, translate_px={"x": (-32, 32), "y": (-32, 32)}, rotate=(-45, 45), shear=(-32, 32), mode=["constant", "edge"], cval=(0.0, 1.0)))
for _ in sm.xrange(5)],
affects_geometry=True
),
#_Descriptor.from_augsubs(
# "geometric",
# "Affine: Scale",
# [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]],
# affects_geometry=True),
#_Descriptor.from_augsubs(
# "geometric",
# "Affine: Translate",
# [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y}))
# for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]],
# affects_geometry=True),
#_Descriptor.from_augsubs(
# "geometric",
# "Affine: Rotate",
# [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]],
# affects_geometry=True),
#_Descriptor.from_augsubs(
# "geometric",
# "Affine: Shear",
# [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]],
# affects_geometry=True),
_Descriptor.from_augsubs(
"geometric",
"Affine: Modes",
[("mode=%s" % (mode,), iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]],
affects_geometry=True
#comment='Augmentation of heatmaps and segmentation maps is currently always done with mode="constant" '
# + 'for consistency with keypoint and bounding box augmentation. It may be resonable to use '
# + 'mode="constant" for images too when augmenting heatmaps or segmentation maps.'
),
_Descriptor.from_augsubs(
"geometric",
"Affine: cval",
[("cval=%d" % (int(cval*255),), iaa.Affine(translate_px=-32, cval=int(cval*255), mode="constant"))
for cval in [0.0, 0.25, 0.5, 0.75, 1.0]],
affects_geometry=True),
# AffineCv2
_Descriptor.from_augsubs(
"geometric",