-
Notifications
You must be signed in to change notification settings - Fork 51
/
common.py
946 lines (649 loc) · 40.1 KB
/
common.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
import numpy as np
import PIL.Image
import warnings
from collections import Counter
try:
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
except ImportError:
import keras
from keras_preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras import backend as K
from keras.utils import Sequence
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
try:
from tqdm import tqdm
except ImportError:
def tqdm(it, **kwargs):
for x in it:
yield x
class DataSequence(Sequence):
""" Helper class representing a sequence that can be passed to Keras functions expecting a generator. """
def __init__(self, data_generator, ids, labels, batch_size = 32, shuffle = False, oversample = False, repeats = 1,
batch_transform = None, batch_transform_kwargs = {}, **kwargs):
"""
# Arguments:
- data_generator: The data generator instance that created this sequence.
Must provide a `compose_batch` method that takes a list of image indices as first argument
and optionally any additional keyword arguments passed to this constructor and returns
a batch of images as numpy array.
- ids: List with IDs of all images.
- labels: List with labels corresponding to the images in `ids`.
- batch_size: The size of the batches provided by this sequence.
- shuffle: Whether to shuffle the order of images after each epoch.
- oversample: Whether to oversample smaller classes to the size of the largest one.
- repeats: Number of repeats per epoch. If this was set to 3, for example, a single epoch would actually
comprise 3 epochs.
- batch_transform: Optionally, a function that takes the inputs and targets of a batch and returns
transformed inputs and targets that will be provided by this sequence instead of
the original ones.
- batch_transform_kwargs: Additional keyword arguments passed to `batch_transform`.
"""
super(DataSequence, self).__init__()
self.data_generator = data_generator
self.ids = ids
self.labels = np.asarray(labels)
self.batch_size = batch_size
self.shuffle = shuffle
self.oversample = oversample
self.repeats = repeats
self.batch_transform = batch_transform
self.batch_transform_kwargs = batch_transform_kwargs
self.kwargs = kwargs
if self.oversample:
self.class_sizes = Counter(labels)
self.max_class_size = max(self.class_sizes.values())
self.class_members = { lbl : np.where(np.asarray(labels) == lbl)[0] for lbl in self.class_sizes.keys() }
self.permutations = [np.concatenate([
np.repeat(members, int(np.ceil(self.max_class_size / len(members))))[:self.max_class_size]
for lbl, members in self.class_members.items()
]) for i in range(self.repeats)]
self.epoch_len = int(np.ceil((len(self.class_sizes) * self.max_class_size) / self.batch_size))
else:
self.permutations = [np.arange(len(self.ids)) for i in range(self.repeats)]
self.epoch_len = int(np.ceil(len(self.ids) / self.batch_size))
self.on_epoch_end()
def __len__(self):
""" Returns the number of batches per epoch. """
return self.repeats * self.epoch_len
def __getitem__(self, idx):
""" Returns the batch with the given index. """
subepoch = idx // self.epoch_len
idx = idx % self.epoch_len
batch_ind = self.permutations[subepoch][idx*self.batch_size:(idx+1)*self.batch_size]
X = self.data_generator.compose_batch([self.ids[i] for i in batch_ind], **self.kwargs)
y = self.labels[batch_ind]
if self.batch_transform is not None:
return self.batch_transform(X, y, **self.batch_transform_kwargs) # pylint: disable=not-callable
else:
return X, y
def on_epoch_end(self):
""" Called by Keras after each epoch. Handles shuffling of the data if required. """
if self.shuffle:
if self.oversample:
self.permutations = [np.concatenate([
np.concatenate([
np.random.choice(members, len(members), replace = False)
for _ in range(int(np.ceil(self.max_class_size / len(members))))
])[:self.max_class_size]
for lbl, members in self.class_members.items()
]) for i in range(self.repeats)]
for i in range(self.repeats):
np.random.shuffle(self.permutations[i])
class FileDatasetGenerator(object):
""" Abstract base class for image generators. """
def __init__(self, root_dir, cropsize = (224, 224), default_target_size = -1,
randzoom_range = None, randrot_max = 0,
distort_colors = False, colordistort_params = {},
randerase_prob = 0.0, randerase_params = { 'sl' : 0.02, 'sh' : 0.4, 'r1' : 0.3, 'r2' : 1./0.3 },
color_mode = 'rgb'):
""" Abstract base class for image generators.
# Arguments:
- root_dir: Root directory of the dataset.
- cropsize: Tuple with width and height of crops extracted from the images.
- default_target_size: Int or tuple of ints. Specifies the default target size which images will be resized to (before cropping)
if not specified differently in calls to `flow_train/test` or `train/test_sequence`.
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
- randzoom_range: Tuple with minimum and maximum size of the smaller image dimension for random scale augmentation.
May either be given as integer specifying absolute pixel values or float specifying the relative scale of the image.
If set to `None`, no scale augmentation will be performed.
- randrot_max: Maximum number of degrees for random rotations.
- distort_colors: Boolean specifying whether to apply color distortions as data augmentation.
- colordistort_params: Parameters for color distortions, passed as keyword arguments to `distort_colors()`.
- randerase_prob: Probability for random erasing.
- randerase_params: Random erasing parameters (see Zhong et al. (2017): "Random erasing data augmentation.").
- color_mode: Image color mode, either "rgb" or "bgr".
"""
super(FileDatasetGenerator, self).__init__()
self.root_dir = root_dir
self.cropsize = cropsize
self.default_target_size = default_target_size
self.randzoom_range = randzoom_range
self.randrot_max = randrot_max
self.distort_colors = distort_colors
self.colordistort_params = colordistort_params
self.randerase_prob = randerase_prob
self.randerase_params = randerase_params
self.color_mode = color_mode.lower()
self.classes = []
self.train_img_files = []
self.test_img_files = []
self._train_labels = []
self._test_labels = []
warnings.filterwarnings('ignore', '.*[Cc]orrupt EXIF data.*', UserWarning)
def _compute_stats(self, mean = None, std = None):
""" Computes channel-wise mean and standard deviation of all images in the dataset.
If `mean` and `std` arguments are given, they will just be stored instead of being re-computed.
The channel order of both is always "RGB", independent of `color_mode`.
"""
if mean is None:
mean = 0
for fn in tqdm(self.train_img_files, desc = 'Computing channel mean'):
mean += np.mean(np.asarray(load_img(fn), dtype=np.float64), axis = (0,1))
mean /= len(self.train_img_files)
print('Channel-wise mean: {}'.format(mean))
self.mean = np.asarray(mean, dtype=np.float32)
if (mean is None) or (std is None):
std = 0
for fn in tqdm(self.train_img_files, desc = 'Computing channel variance'):
std += np.mean((np.asarray(load_img(fn), dtype=np.float64) - self.mean) ** 2, axis = (0,1))
std = np.sqrt(std / (len(self.train_img_files) - 1))
print('Channel-wise standard deviation: {}'.format(std))
self.std = np.asarray(std, dtype=np.float32)
def flow_train(self, batch_size = 32, include_labels = True, shuffle = True, target_size = None, augment = True):
""" A generator yielding batches of pre-processed and augmented training images.
# Arguments:
- batch_size: Number of images per batch.
- include_labels: If true, target labels will be yielded as well.
- shuffle: If True, the order of images will be shuffled after each epoch.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to (before cropping).
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
- augment: Whether data augmentation should be applied or not.
# Yields:
If `include_labels` is True, a tuple of inputs and targets for each batch.
Otherwise, only inputs will be yielded.
"""
return self._flow(self.train_img_files, self._train_labels if include_labels else None,
batch_size=batch_size, shuffle=shuffle, target_size=target_size,
normalize=True, hflip=augment, vflip=False, colordistort=self.distort_colors and augment,
randzoom=augment, randrot=augment, cropsize=self.cropsize, randcrop=augment, randerase=augment)
def flow_test(self, batch_size = 32, include_labels = True, shuffle = False, target_size = None, augment = False):
""" A generator yielding batches of pre-processed and augmented test images.
# Arguments:
- batch_size: Number of images per batch.
- include_labels: If true, target labels will be yielded as well.
- shuffle: If True, the order of images will be shuffled after each epoch.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to (before cropping).
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
- augment: Whether data augmentation should be applied or not.
# Yields:
If `include_labels` is True, a tuple of inputs and targets for each batch.
Otherwise, only inputs will be yielded.
"""
return self._flow(self.test_img_files, self._test_labels if include_labels else None,
batch_size=batch_size, shuffle=shuffle, target_size=target_size,
normalize=True, hflip=augment, vflip=False, colordistort=False,
randzoom=augment, randrot=augment, cropsize=self.cropsize, randcrop=augment, randerase=augment)
def train_sequence(self, batch_size = 32, shuffle = True, target_size = None, augment = True, batch_transform = None, batch_transform_kwargs = {}):
""" Creates a `DataSequence` with pre-processed and augmented training images that can be passed to the Keras methods expecting a generator for efficient and safe multi-processing.
# Arguments:
- batch_size: Number of images per batch.
- shuffle: If True, the order of images will be shuffled after each epoch.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to (before cropping).
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
- augment: Whether data augmentation should be applied or not.
- batch_transform: Optionally, a function that takes the inputs and targets of a batch and returns
transformed inputs and targets that will be provided by the sequence instead of
the original ones.
- batch_transform_kwargs: Additional keyword arguments passed to `batch_transform`.
# Returns:
a DataSequence instance
"""
return DataSequence(self, self.train_img_files, self._train_labels,
batch_size=batch_size, shuffle=shuffle,
target_size=target_size, normalize=True, hflip=augment, vflip=False, colordistort=self.distort_colors and augment,
randzoom=augment, randrot=augment, cropsize=self.cropsize, randcrop=augment, randerase=augment,
batch_transform=batch_transform, batch_transform_kwargs=batch_transform_kwargs)
def test_sequence(self, batch_size = 32, shuffle = False, target_size = None, augment = False, batch_transform = None, batch_transform_kwargs = {}):
""" Creates a `DataSequence` with pre-processed and augmented test images that can be passed to the Keras methods expecting a generator for efficient and safe multi-processing.
# Arguments:
- batch_size: Number of images per batch.
- shuffle: If True, the order of images will be shuffled after each epoch.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to (before cropping).
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
- augment: Whether data augmentation should be applied or not.
- batch_transform: Optionally, a function that takes the inputs and targets of a batch and returns
transformed inputs and targets that will be provided by the sequence instead of
the original ones.
- batch_transform_kwargs: Additional keyword arguments passed to `batch_transform`.
# Returns:
a DataSequence instance
"""
return DataSequence(self, self.test_img_files, self._test_labels,
batch_size=batch_size, shuffle=shuffle,
target_size=target_size, normalize=True, hflip=augment, vflip=False, colordistort=False,
randzoom=augment, randrot=augment, cropsize=self.cropsize, randcrop=augment, randerase=augment,
batch_transform=batch_transform, batch_transform_kwargs=batch_transform_kwargs)
def _flow(self, filenames, labels = None, batch_size = 32, shuffle = False, **kwargs):
""" A generator yielding batches of pre-processed and augmented images.
# Arguments:
- filenames: List with the filenames of all images to sample batches from.
- labels: Optionally, labels corresponding to the images specified by `filenames`.
- batch_size: Number of images per batch.
- shuffle: If True, the order of images will be shuffled after each epoch.
Remaining keyword arguments will be passed through to `compose_batch`.
# Yields:
If `labels` is not None, a tuple of inputs and targets for each batch.
Otherwise, only inputs will be yielded.
"""
ind = np.arange(len(filenames))
if shuffle:
np.random.shuffle(ind)
if labels is not None:
labels = np.asarray(labels)
offs = 0
while True:
if offs >= len(ind):
offs = 0
if shuffle:
np.random.shuffle(ind)
batch_ind = ind[offs:offs+batch_size]
offs += batch_size
X = self.compose_batch([filenames[i] for i in batch_ind], **kwargs)
if labels is not None:
yield X, labels[batch_ind]
else:
yield X
def compose_batch(self, filenames, cropsize = None, randcrop = False, data_format = None, **kwargs):
""" Composes a batch of augmented images given by their filenames.
# Arguments:
- filenames: List with image filenames to be contained in the batch.
- cropsize: Int or tuple of ints specifying the size which the images will be cropped to.
If a single int is given, a square crop will be extracted.
If set to None, the batch will be cropped to the median size of the images in the batch.
- randcrop: If True, a random crop will be extracted from each image, otherwise the center crop.
- data_format: The image data format (either 'channels_first' or 'channels_last'). Set to None for the default value.
Remaining keyword arguments will be passed through to `_load_and_transform`.
# Returns:
a batch of images as 4-dimensional numpy array.
"""
if data_format is None:
data_format = K.image_data_format()
if data_format == 'channels_first':
x_axis, y_axis = 2, 1
else:
x_axis, y_axis = 1, 0
X = [self._load_and_transform(fn, data_format=data_format, **kwargs) for fn in filenames]
if cropsize is not None:
crop_width, crop_height = cropsize
else:
crop_height = int(np.median([img.shape[y_axis] for img in X]))
crop_width = int(np.median([img.shape[x_axis] for img in X]))
for i, img in enumerate(X):
y_pad = x_pad = 0
if img.shape[y_axis] > crop_height:
y_offs = np.random.randint(img.shape[y_axis] - crop_height + 1) if randcrop else (img.shape[y_axis] - crop_height) // 2
img = img[:,y_offs:y_offs+crop_height,:] if data_format == 'channels_first' else img[y_offs:y_offs+crop_height,:,:]
elif img.shape[y_axis] < crop_height:
y_pad = np.random.randint(crop_height - img.shape[y_axis] + 1) if randcrop else (crop_height - img.shape[y_axis]) // 2
if img.shape[x_axis] > crop_width:
x_offs = np.random.randint(img.shape[x_axis] - crop_width + 1) if randcrop else (img.shape[x_axis] - crop_width) // 2
img = img[:,:,x_offs:x_offs+crop_width] if data_format == 'channels_first' else img[:,x_offs:x_offs+crop_width,:]
elif img.shape[x_axis] < crop_width:
x_pad = np.random.randint(crop_width - img.shape[x_axis] + 1) if randcrop else (crop_width - img.shape[x_axis]) // 2
X[i] = np.pad(
img,
((0,0), (y_pad, crop_height - img.shape[1] - y_pad), (x_pad, crop_width - img.shape[2] - x_pad)) if data_format == 'channels_first' else \
((y_pad, crop_height - img.shape[0] - y_pad), (x_pad, crop_width - img.shape[1] - x_pad), (0,0)),
'reflect'
)
return np.stack(X)
def _load_image(self, filename, target_size = None, randzoom = False):
""" Loads an image file.
# Arguments:
- filename: The path of the image file.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to.
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
The actual size may be modified further is `randzoom` is True.
- randzoom: If True and `self.randzoom_range` is not None, random zooming will be applied.
If `self.randzoom_range` is given as floats defining a range relative to the image size,
`target_size` will be used as reference if it is not None, otherwise the original image size.
# Returns:
the image as PIL image.
"""
img = load_img(filename)
if target_size is None:
target_size = self.default_target_size
if (target_size > 0) or (randzoom and (self.randzoom_range is not None)):
if target_size <= 0:
target_size = img.size
if randzoom and (self.randzoom_range is not None):
if isinstance(self.randzoom_range[0], float):
target_size = np.round(np.array(target_size) * np.random.uniform(self.randzoom_range[0], self.randzoom_range[1])).astype(int).tolist()
else:
target_size = np.random.randint(self.randzoom_range[0], self.randzoom_range[1])
if isinstance(target_size, int):
target_size = (target_size, round(img.size[1] * (target_size / img.size[0]))) if img.size[0] < img.size[1] else (round(img.size[0] * (target_size / img.size[1])), target_size)
img = img.resize(target_size, PIL.Image.BILINEAR)
return img
def _transform(self, img, normalize = True,
hflip = False, vflip = False, randrot = False, colordistort = False, randerase = False,
data_format = None):
""" Loads an image file and applies normalization and data augmentation.
# Arguments:
- img: the unnormalized and untransformed image as PIL image.
- normalize: If True, the image will be normalized by subtracting the channel-wise mean and dividing by the channel-wise standard deviation.
- hflip: If True, the image will be flipped horizontally with a chance of 50%.
- vflip: If True, the image will be flipped vertically with a chance of 50%.
- randerase: If True, random erasing will be applied with probability `self.randerase_prob`.
- data_format: The image data format (either 'channels_first' or 'channels_last'). Set to None for the default value.
# Returns:
the transformed image as 3-dimensional numpy array.
"""
if data_format is None:
data_format = K.image_data_format()
# Rotate image
if randrot and (self.randrot_max > 0):
angle = np.random.uniform(-self.randrot_max, self.randrot_max)
img = img.rotate(angle, PIL.Image.BILINEAR)
# Convert PIL image to array
img = img_to_array(img, data_format=data_format)
# Color distortions
if colordistort:
img = distort_color(img, data_format=data_format, **self.colordistort_params)
# Normalize image
if normalize:
img -= self.mean[:,None,None] if data_format == 'channels_first' else self.mean[None,None,:]
img /= self.std[:,None,None] if data_format == 'channels_first' else self.std[None,None,:]
# RGB -> BGR conversion
if self.color_mode == 'bgr':
img = img[::-1,:,:] if data_format == 'channels_first' else img[:,:,::-1]
# Random Flipping
if hflip and (np.random.random() < 0.5):
img = img[:,:,::-1] if data_format == 'channels_first' else img[:,::-1,:]
if vflip and (np.random.random() < 0.5):
img = img[:,::-1,:] if data_format == 'channels_first' else img[::-1,:,:]
# Random erasing
if randerase and (self.randerase_prob > 0) and (np.random.random() < self.randerase_prob):
while True:
se = np.random.uniform(self.randerase_params['sl'], self.randerase_params['sh']) * (img.shape[0] * img.shape[1])
re = np.random.uniform(self.randerase_params['r1'], self.randerase_params['r2'])
he, we = int(np.sqrt(se * re)), int(np.sqrt(se / re))
if (he < img.shape[0]) and (we < img.shape[1]):
break
xe, ye = np.random.randint(0, img.shape[1] - we), np.random.randint(0, img.shape[0] - he)
img[ye:ye+he,xe:xe+we,:] = (np.random.uniform(0., 255., (he, we, img.shape[-1])) \
- (self.mean[:,None,None] if data_format == 'channels_first' else self.mean[None,None,:])) \
/ (self.std[:,None,None] if data_format == 'channels_first' else self.std[None,None,:])
return img
def _load_and_transform(self, filename, target_size = None, normalize = True,
hflip = False, vflip = False, randzoom = False, randrot = False, colordistort = False, randerase = False,
data_format = None):
""" Loads an image file and applies normalization and data augmentation.
# Arguments:
- filename: The path of the image file.
- target_size: Int or tuple of ints. Specifies the target size which the image will be resized to.
If a single int is given, it specifies the size of the smaller side of the image and the aspect ratio will be retained.
If set to -1, the image won't be resized.
If set to None, the default_target_size passed to the constructor will be used.
The actual size may be modified further is `randzoom` is True.
- normalize: If True, the image will be normalized by subtracting the channel-wise mean and dividing by the channel-wise standard deviation.
- hflip: If True, the image will be flipped horizontally with a chance of 50%.
- vflip: If True, the image will be flipped vertically with a chance of 50%.
- randzoom: If True and `self.randzoom_range` is not None, random zooming will be applied.
If `self.randzoom_range` is given as floats defining a range relative to the image size,
`target_size` will be used as reference if it is not None, otherwise the original image size.
- randerase: If True, random erasing will be applied with probability `self.randerase_prob`.
- data_format: The image data format (either 'channels_first' or 'channels_last'). Set to None for the default value.
# Returns:
the image as 3-dimensional numpy array.
"""
return self._transform(
self._load_image(filename, target_size=target_size, randzoom=randzoom),
normalize=normalize, hflip=hflip, vflip=vflip, randrot=randrot, colordistort=colordistort, randerase=randerase, data_format=data_format
)
@property
def labels_train(self):
""" List with labels corresponding to the training files in `self.train_img_files`.
These are not the original labels, but automatically assigned numeric labels in the range `[0, num_classes-1]`.
The look-up table in `self.class_indices` can be used to obtain the original label for each class.
"""
return self._train_labels
@property
def labels_test(self):
""" List with labels corresponding to the test files in `self.test_img_files`.
These are not the original labels, but automatically assigned numeric labels in the range `[0, num_classes-1]`.
The look-up table in `self.class_indices` can be used to obtain the original label for each class.
"""
return self._test_labels
@property
def num_classes(self):
""" Number of unique classes in the dataset. """
return len(self.classes)
@property
def num_train(self):
""" Number of training images in the dataset. """
return len(self.train_img_files)
@property
def num_test(self):
""" Number of test images in the dataset. """
return len(self.test_img_files)
@property
def num_channels(self):
""" Number of channels (e.g., 3 for RGB, 1 for grayscale). """
return 3
class TinyDatasetGenerator(object):
""" Abstract base class for datasets with low-resolution images that fit entirely into memory (e.g., CIFAR). """
def __init__(self, X_train, X_test, y_train, y_test,
generator_kwargs = { 'featurewise_center' : True, 'featurewise_std_normalization' : True },
train_generator_kwargs = { 'horizontal_flip' : True, 'width_shift_range' : 0.15, 'height_shift_range' : 0.15 }):
""" Abstract base class for interfaces to datasets with low-resolution images that fit entirely into memory (e.g., CIFAR).
# Arguments:
- X_train: 4-D numpy array with the training images.
- X_test: 4-D numpy array with the test images.
- y_train: list with numeric labels for the training images.
- y_test: list with numeric labels for the test images.
- generator_kwargs: Dictionary with keyword arguments passed to Keras' ImageDataGenerator for both training and test.
- train_generator_kwargs: Dictionary with keyword arguments passed to Keras' ImageDataGenerator for the training set.
"""
super(TinyDatasetGenerator, self).__init__()
self.X_train = X_train
self.X_test = X_test
self.y_train = y_train
self.y_test = y_test
# Set up pre-processing
self.image_generator = ImageDataGenerator(**generator_kwargs, **train_generator_kwargs)
self.image_generator.fit(self.X_train)
self.test_image_generator = ImageDataGenerator(**generator_kwargs)
self.test_image_generator.fit(self.X_train)
def flow_train(self, batch_size = 32, include_labels = True, shuffle = True, augment = True):
""" A generator yielding batches of pre-processed and augmented training images.
# Arguments:
- batch_size: Number of images per batch.
- include_labels: If true, target labels will be yielded as well.
- shuffle: If True, the order of images will be shuffled after each epoch.
- augment: Whether data augmentation should be applied or not.
# Yields:
If `include_labels` is True, a tuple of inputs and targets for each batch.
Otherwise, only inputs will be yielded.
"""
image_generator = self.image_generator if augment else self.test_image_generator
return image_generator.flow(self.X_train, self.y_train if include_labels else None,
batch_size=batch_size, shuffle=shuffle)
def flow_test(self, batch_size = 32, include_labels = True, shuffle = False, augment = False):
""" A generator yielding batches of pre-processed and augmented test images.
# Arguments:
- batch_size: Number of images per batch.
- include_labels: If true, target labels will be yielded as well.
- shuffle: If True, the order of images will be shuffled after each epoch.
- augment: Whether data augmentation should be applied or not.
# Yields:
If `include_labels` is True, a tuple of inputs and targets for each batch.
Otherwise, only inputs will be yielded.
"""
image_generator = self.image_generator if augment else self.test_image_generator
return image_generator.flow(self.X_test, self.y_test if include_labels else None,
batch_size=batch_size, shuffle=shuffle)
def train_sequence(self, batch_size = 32, shuffle = True, augment = True, batch_transform = None, batch_transform_kwargs = {}):
""" Creates a `DataSequence` with pre-processed and augmented training images that can be passed to the Keras methods expecting a generator for efficient and safe multi-processing.
# Arguments:
- batch_size: Number of images per batch.
- shuffle: If True, the order of images will be shuffled after each epoch.
- augment: Whether data augmentation should be applied or not.
- batch_transform: Optionally, a function that takes the inputs and targets of a batch and returns
transformed inputs and targets that will be provided by the sequence instead of
the original ones.
- batch_transform_kwargs: Additional keyword arguments passed to `batch_transform`.
# Returns:
a DataSequence instance
"""
return DataSequence(self, np.arange(len(self.X_train)), self.y_train,
train=True, augment=augment,
batch_size=batch_size, shuffle=shuffle, batch_transform=batch_transform, batch_transform_kwargs=batch_transform_kwargs)
def test_sequence(self, batch_size = 32, shuffle = False, augment = False, batch_transform = None, batch_transform_kwargs = {}):
""" Creates a `DataSequence` with pre-processed and augmented test images that can be passed to the Keras methods expecting a generator for efficient and safe multi-processing.
# Arguments:
- batch_size: Number of images per batch.
- shuffle: If True, the order of images will be shuffled after each epoch.
- augment: Whether data augmentation should be applied or not.
- batch_transform: Optionally, a function that takes the inputs and targets of a batch and returns
transformed inputs and targets that will be provided by the sequence instead of
the original ones.
- batch_transform_kwargs: Additional keyword arguments passed to `batch_transform`.
# Returns:
a DataSequence instance
"""
return DataSequence(self, np.arange(len(self.X_test)), self.y_test,
train=False, augment=augment,
batch_size=batch_size, shuffle=shuffle, batch_transform=batch_transform, batch_transform_kwargs=batch_transform_kwargs)
def compose_batch(self, indices, train, augment = False):
""" Composes a batch of augmented images given by their indices.
# Arguments:
- indices: List with image indices to be contained in the batch.
- train: If True, images will be taken from the training data matrix, otherwise from the test data.
- augment: Whether data augmentation should be applied or not.
# Returns:
a batch of images as 4-dimensional numpy array.
"""
X = self.X_train if train else self.X_test
image_generator = self.image_generator if augment else self.test_image_generator
batch = np.zeros((len(indices),) + tuple(X.shape[1:]), dtype=K.floatx())
for i, j in enumerate(indices):
x = X[j]
x = image_generator.random_transform(x.astype(K.floatx()))
x = image_generator.standardize(x)
batch[i] = x
return batch
@property
def labels_train(self):
""" List with labels corresponding to the training files in `self.X_train`.
This is an alias for `self.y_train` for compatibility with other data generators.
"""
return self.y_train
@property
def labels_test(self):
""" List with labels corresponding to the test files in `self.X_test`.
This is an alias for `self.y_test` for compatibility with other data generators.
"""
return self.y_test
@property
def num_classes(self):
""" Number of unique classes in the dataset. """
return max(self.y_train) + 1
@property
def num_train(self):
""" Number of training images in the dataset. """
return len(self.X_train)
@property
def num_test(self):
""" Number of test images in the dataset. """
return len(self.X_test)
@property
def num_channels(self):
""" Number of channels (e.g., 3 for RGB, 1 for grayscale). """
return self.X_train.shape[-1]
def distort_color(img, fast_mode=True,
brightness_delta=32./255., hue_delta=0.2, saturation_range=(0.5, 1.5), contrast_range=(0.5, 1.5),
data_format='channels_last'):
nonnormalized = (img.max() > 2.0)
if nonnormalized:
img = img.astype(np.float32) / 255.
if data_format == 'channels_first':
img = np.transpose(img, (1, 2, 0))
if (not nonnormalized) and (data_format == 'channels_last'):
img = img.copy()
noop = lambda x: x
brightness_hsv = (lambda x: random_brightness_hsv(x, max_delta=brightness_delta)) if brightness_delta > 0 else noop
saturation = (lambda x: random_saturation(x, *saturation_range)) if (saturation_range[0] <= saturation_range[1]) and ((saturation_range[0] != 1) or (saturation_range[1] != 1)) else noop
if fast_mode:
ordering = np.random.choice(2)
if ordering == 0:
img = hsv_to_rgb(saturation(brightness_hsv(rgb_to_hsv(img))))
else:
img = hsv_to_rgb(brightness_hsv(saturation(rgb_to_hsv(img))))
else:
brightness = (lambda x: random_brightness(x, max_delta=brightness_delta)) if brightness_delta > 0 else noop
hue = (lambda x: random_hue(x, max_delta=hue_delta)) if hue_delta > 0 else noop
contrast = (lambda x: random_contrast(x, *contrast_range)) if (contrast_range[0] <= contrast_range[1]) and ((contrast_range[0] != 1) or (contrast_range[1] != 1)) else noop
ordering = np.random.choice(4)
if ordering == 0:
img = contrast(hsv_to_rgb(hue(saturation(rgb_to_hsv(brightness(img))))))
elif ordering == 1:
img = hsv_to_rgb(hue(rgb_to_hsv(contrast(brightness(hsv_to_rgb(saturation(rgb_to_hsv(img))))))))
elif ordering == 2:
img = hsv_to_rgb(saturation(brightness_hsv(hue(rgb_to_hsv(contrast(img))))))
elif ordering == 3:
img = brightness(contrast(hsv_to_rgb(saturation(hue(rgb_to_hsv(img))))))
if data_format == 'channels_first':
img = np.transpose(img, (2, 0, 1))
if nonnormalized:
img = img * 255.
return img
def random_brightness(img, max_delta=32./255.):
""" Randomly adjusts the brightness of a given RGB image. """
img += np.random.uniform(-max_delta, max_delta)
img[img > 1] = 1
img[img < 0] = 0
return img
def random_brightness_hsv(img, max_delta=32./255.):
""" Randomly adjusts the brightness of a given HSV image. """
val = img[:,:,2]
val += np.random.uniform(-max_delta, max_delta)
val[val > 1] = 1
val[val < 0] = 0
return img
def random_hue(img, max_delta=0.2):
""" Randomly shifts the hue of a given HSV image. """
delta = np.random.uniform(-max_delta, max_delta)
hue = img[:,:,0]
hue += delta
hue[hue > 1.0] -= 1.0
hue[hue < 0.0] += 1.0
return img
def random_saturation(img, low=0.5, high=1.5):
""" Randomly scales the saturation of a given HSV image. """
sat = img[:,:,1]
sat *= np.random.uniform(low, high)
sat[sat > 1] = 1
sat[sat < 0] = 0
return img
def random_contrast(img, low=0.5, high=1.5):
""" Randomly scales the contrast of a given RGB image. """
mean = img.mean(axis=(0,1), keepdims=True)
cf = np.random.uniform(low, high, mean.shape)
img -= mean
img *= cf
img += mean
img[img > 1] = 1
img[img < 0] = 0
return img