-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataMakerPlus.py
More file actions
434 lines (371 loc) · 14 KB
/
Copy pathDataMakerPlus.py
File metadata and controls
434 lines (371 loc) · 14 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
from __future__ import absolute_import
from typing import Tuple, Union, Optional
import h5py as h5
from tensorflow.keras.utils import Sequence
from tensorflow.keras.utils import to_categorical
import numpy as np
available_modes = {"train", "test"}
available_labels_encoding = {"hot", "smooth", False}
class HDF5DataGenerator(Sequence):
"""Just a simple custom Keras HDF5 ImageDataGenerator.
Custom Keras ImageDataGenerator that generates
batches of tensor images from HDF5 files with (optional) real-time
data augmentation.
Arguments
---------
src : str
Path of the hdf5 source file.
image_key : str
Key of the h5 file image tensors dataset.
Default is "images".
y_key : str
Key of the h5 file labels dataset.
Default is "labels".
numerical_keys: List[str]
classes_key : str
Key of the h5 file dataset containing
the raw classes.
Default is None.
batch_size : int
Size of each batch, must be a power of two.
(16, 32, 64, 128, 256, ...)
Default is 32.
shuffle : bool
Shuffle images at the end of each epoch.
Default is True.
scaler : "std", "norm" or False
"std" mode means standardization to range [-1, 1]
with 0 mean and unit variance.
"norm" mode means normalization to range [0, 1].
Default is "std".
num_classes : None or int
Specifies the total number of classes
for labels encoding.
Default is None.
labels_encoding : "hot", "smooth" or False
"hot" mode means classic one hot encoding.
"smooth" mode means smooth hot encoding.
Default is "hot".
smooth_factor : int or float
smooth factor used by smooth
labels encoding.
Default is 0.1.
augmenter : albumentations Compose([]) Pipeline or False
An albumentations transformations pipeline
to apply to each sample.
Default is False.
mode : str "train" or "test"
Model generator type. "train" is used for
fit_generator() and evaluate_generator.
"test" is used for predict_generator().
Default is "train".
Notes
-----
Turn off scaler (scaler=False) if using the
ToFloat(max_value=255) transformation from
albumentations.
Examples
--------
Example of usage:
```python
my_augmenter = Compose([
HorizontalFlip(p=0.5),
RandomContrast(limit=0.2, p=0.5),
RandomGamma(gamma_limit=(80, 120), p=0.5),
RandomBrightness(limit=0.2, p=0.5),
Resize(227, 227, cv2.INTER_AREA)
])
# Create the generator.
train_gen = HDF5ImageGenerator(
'path/to/my/file.h5',
augmenter=my_augmenter)
```
"""
def __init__(
self,
src,
image_key="images",
y_key="labels",
numerical_keys=None,
classes_key=None,
batch_size=32,
shuffle=True,
scaler=True,
num_classes=None,
labels_encoding="hot",
smooth_factor=0.1,
augmenter=False,
mode="train",
):
if mode not in available_modes:
raise ValueError('`mode` should be `train` '
'(fit_generator() and evaluate_generator()) or '
'`test` (predict_generator(). '
'Received: %s' % mode)
self.mode = mode
if labels_encoding not in available_labels_encoding:
raise ValueError('`labels_encoding` should be `hot` '
'(classic binary matrix) or '
'`smooth` (smooth encoding) or '
'False (no labels encoding). '
'Received: %s' % labels_encoding)
self.labels_encoding = labels_encoding
if (self.labels_encoding == "smooth") and not (0 < smooth_factor <= 1):
raise ValueError('`smooth` labels encoding '
'must use a `smooth_factor` '
'< 0 smooth_factor <= 1')
if augmenter and not isinstance(augmenter, Compose):
raise ValueError('`augmenter` argument '
'must be an instance of albumentations '
'`Compose` class. '
'Received type: %s' % type(augmenter))
self.augmenter = augmenter
self.src: str = src
self.image_key: str = image_key
self.y_key: str = y_key
self.numerical_keys = numerical_keys
self.classes_key: str = classes_key
self.batch_size: int = batch_size
self.shuffle: bool = shuffle
self.scaler: bool = scaler
self.num_classes: int = num_classes
self.smooth_factor: float = smooth_factor
self._indices = np.arange(self.__get_dataset_shape(self.image_key, 0))
def __repr__(self):
"""Representation of the class."""
return f"{self.__class__.__name__}({self.__dict__!r})"
def __get_dataset_shape(self, dataset: str, index: int) -> Tuple[int, ...]:
"""Get an h5py dataset shape.
Arguments
---------
dataset : str
The dataset key.
index : int
The dataset index.
Returns
-------
tuple of ints
A tuple of array dimensions.
"""
with h5.File(self.src, "r") as file:
return file[dataset].shape[index]
def __get_dataset_items(
self,
indices: np.ndarray,
dataset: Optional[str] = None
) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
"""Get an HDF5 dataset items.
Arguments
---------
indices : ndarray,
The list of current batch indices.
dataset : (optional) str
The dataset key. If None, returns
a batch of (image tensors, labels).
Defaults to None.
Returns
-------
np.ndarray or a tuple of ndarrays
A batch of samples.
"""
with h5.File(self.src, "r") as file:
if dataset is not None:
if dataset == self.numerical_keys:
return file[dataset][indices][:]
else:
return file[dataset][indices]
else:
return (file[self.image_key][indices], file[self.y_key][indices])
@property
def num_items(self) -> int:
"""Grab the total number of examples
from the dataset.
Returns
-------
int
The total number of examples.
"""
with h5.File(self.src, "r") as file:
return file[self.image_key].shape[0]
@property
def classes(self) -> list:
"""Grab "human" classes from the dataset.
Returns
-------
list
A list of the raw classes.
"""
if self.classes_key is None:
raise ValueError('Canceled. parameter `classes_key` '
'is set to None.')
with h5.File(self.src, "r") as file:
return file[self.classes_key][:]
def __len__(self):
"""Denotes the number of batches per epoch.
Returns
-------
int
The number of batches per epochs.
"""
return int(
np.ceil(
self.__get_dataset_shape(self.image_key, 0) /
float(self.batch_size)))
@staticmethod
def apply_labels_smoothing(batch_y: np.ndarray,
factor: float) -> np.ndarray:
"""Applies labels smoothing to the original
labels binary matrix.
Arguments
---------
batch_y : np.ndarray
Current batch integer labels.
factor : float
Smoothing factor.
Returns
-------
np.ndarray
A binary class matrix.
"""
batch_y *= 1 - factor
batch_y += factor / batch_y.shape[1]
return batch_y
def apply_labels_encoding(
self,
batch_y: np.ndarray,
smooth_factor: Optional[float] = None) -> np.ndarray:
"""Converts a class vector (integers) to binary class matrix.
See Keras to_categorical utils function.
Arguments
---------
batch_y : np.ndarray
Current batch integer labels.
smooth_factor : (optional) Float
Smooth factor.
Defaults to None.
Returns
-------
np.ndarray
A binary class matrix.
"""
batch_y = to_categorical(batch_y, num_classes=self.num_classes)
if smooth_factor is not None:
batch_y = self.apply_labels_smoothing(batch_y,
factor=smooth_factor)
return batch_y
@staticmethod
def apply_normalization(batch_images: np.ndarray) -> np.ndarray:
"""Normalize the pixel intensities.
Normalize the pixel intensities to the range [0, 1].
Arguments
---------
batch_images : np.ndarray
Batch of image tensors to be normalized.
Returns
-------
np.ndarray
A batch of normalized image tensors.
"""
return batch_images.astype("float32") / 255.0
def __next_batch_test(self, indices: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Generates a batch of test data for the given indices.
Arguments
---------
index : int
The index for the batch.
Returns
-------
tuple of ndarrays
A tuple containing a batch of image tensors
and their associated labels. Numerical data
tensor is optional for mutli-modal models.
"""
# Grab corresponding images from the HDF5 source file.
batch_images = self.__get_dataset_items(indices, self.image_key)
# Shall we rescale features?
if self.scaler:
batch_images = self.apply_normalization(batch_images)
if self.numerical_keys is not None:
batch_numerical = []
for key in self.numerical_keys:
numerical_data = self.__get_dataset_items(indices, key)
batch_numerical.append(numerical_data)
batch_numerical = np.stack(batch_numerical, axis=-1)
return ((batch_images, batch_numerical),)
else:
return batch_images
def __next_batch(self,
indices: np.ndarray) -> Tuple[Tuple[np.ndarray, np.ndarray], np.ndarray]:
"""Generates a batch of train/val data for the given indices.
Arguments
---------
index : int
The index for the batch.
Returns
-------
tuple of ndarrays
A tuple containing a batch of image tensors
and their associated labels. Numerical data
tensor is optional for mutli-modal models.
"""
# Grab samples (tensors, labels) HDF5 source file.
(batch_images, batch_y) = self.__get_dataset_items(indices)
# Shall we apply any data augmentation?
if self.augmenter:
batch_images = np.stack(
[self.augmenter(image=x)["image"] for x in batch_images], axis=0)
# Shall we rescale features?
if self.scaler:
batch_images = self.apply_normalization(batch_images)
# Shall we apply labels encoding?
if self.labels_encoding:
batch_y = self.apply_labels_encoding(
batch_y,
smooth_factor=self.smooth_factor
if self.labels_encoding == "smooth" else None,
)
if self.numerical_keys is not None:
batch_numerical = []
for key in self.numerical_keys:
numerical_data = self.__get_dataset_items(indices, key)
batch_numerical.append(numerical_data)
batch_numerical = np.stack(batch_numerical, axis=-1)
return ((batch_images, batch_numerical), batch_y)
else:
return (batch_images, batch_y)
def __getitem__(
self,
index: int):
"""Generates a batch of data for the given index.
Arguments
---------
index : int
The index for the current batch.
Returns
-------
tuple of ndarrays or ndarray
A tuple containing a batch of image tensors,
or a tuple of image and numerical data tensors,
and their associated labels (train) or
a tuple of image tensors, or a tuple of
image and numerical data tensors (predict).
"""
# Indices for the current batch.
indices = np.sort(self._indices[index * self.batch_size:(index + 1) *
self.batch_size])
if self.mode == "train":
return self.__next_batch(indices)
else:
return self.__next_batch_test(indices)
def __shuffle_indices(self):
"""If the shuffle parameter is set to True,
dataset will be shuffled (in-place).
(not available in test 'mode').
"""
if (self.mode == "train") and self.shuffle:
np.random.shuffle(self._indices)
def on_epoch_end(self):
"""Triggered once at the very beginning as well as
at the end of each epoch.
"""
self.__shuffle_indices()