-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_augmentation.py
324 lines (264 loc) · 12.2 KB
/
data_augmentation.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
import math
import tensorflow as tf
class RandomSpeed(tf.keras.layers.Layer):
def __init__(self, min_frames=96, max_frames=128, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_frames = min_frames
self.max_frames = max_frames
self.seed = seed
self.debug = debug
@tf.function
def call(self, images):
height = tf.shape(images)[1]
width = tf.shape(images)[2]
x_min = tf.cond(height < self.min_frames,
lambda: height, lambda: self.min_frames)
x_max = self.max_frames + 1
x = tf.random.uniform(shape=[], minval=x_min, maxval=x_max,
dtype=tf.int32, seed=self.seed)
resized_images = tf.image.resize(images, [x, width])
if self.debug:
tf.print("speed", x)
return resized_images
class RandomShift(tf.keras.layers.Layer):
def __init__(self, min_value=0.0, max_value=255.0, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
@tf.function
def call(self, image):
[red, green, blue] = tf.unstack(image, axis=-1)
left_offset = tf.abs(tf.reduce_min(red) - self.min_value)
right_offset = tf.abs(self.max_value - tf.reduce_max(red))
red_shift = tf.random.uniform(shape=[],
minval=tf.math.negative(left_offset),
maxval=right_offset,
seed=self.seed)
if self.debug:
tf.print("red shift", red_shift)
bottom_offset = tf.abs(tf.reduce_min(green) - self.min_value)
top_offset = tf.abs(self.max_value - tf.reduce_max(green))
green_shift = tf.random.uniform(shape=[],
minval=tf.math.negative(bottom_offset),
maxval=top_offset,
seed=self.seed)
new_red = tf.add(red, red_shift)
new_green = tf.add(green, green_shift)
if self.debug:
tf.print("green shift", green_shift)
return tf.stack([new_red, new_green, blue], axis=-1)
class RandomFlip(tf.keras.layers.Layer):
def __init__(self, mode, min_value=0.0, max_value=255.0, around_zero=False, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.mode = mode
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
self.around_zero = tf.constant(around_zero)
@tf.function
def add_factor(self, channel):
# channel.shape => (examples, frames, joints)
# channel_maxs.shape => (examples, 1, 1)
# channel max per example
channel_max = tf.reduce_max(channel, axis=[-1, -2], keepdims=True)
channel_min = tf.reduce_min(channel, axis=[-1, -2], keepdims=True)
channel_mid = (channel_max + channel_min) / 2
return channel_mid * 2
@tf.function
def call(self, image):
rand = tf.random.uniform(shape=[],
minval=0.,
maxval=1.,
seed=self.seed)
[red, green, blue] = tf.unstack(image, axis=-1)
flip_horizontal = tf.logical_and(
rand > 0.5, tf.equal(self.mode, 'horizontal'))
flip_vertical = tf.logical_and(
rand > 0.5, tf.equal(self.mode, 'vertical'))
zeros = tf.zeros(tf.shape(red))
flip_horizontal_around_mid = tf.logical_and(
flip_horizontal, tf.math.logical_not(self.around_zero))
flip_vertical_around_mid = tf.logical_and(
flip_vertical, tf.math.logical_not(self.around_zero))
red_add_factor = tf.cond(
flip_horizontal_around_mid, lambda: self.add_factor(red), lambda: zeros)
green_add_factor = tf.cond(
flip_vertical_around_mid, lambda: self.add_factor(green), lambda: zeros)
new_red = tf.cond(
flip_horizontal, lambda: tf.add(-red, red_add_factor), lambda: red)
new_green = tf.cond(
flip_vertical, lambda: tf.add(-green, green_add_factor), lambda: green)
if self.debug:
tf.print("flip", rand)
return tf.stack([new_red, new_green, blue], axis=-1)
class RandomRotation(tf.keras.layers.Layer):
def __init__(self, factor=15.0, min_value=0.0, max_value=255.0, around_zero=False, clip=True, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_degree = tf.math.negative(factor)
self.max_degree = factor
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
self.around_zero = tf.constant(around_zero)
self.clip = tf.constant(clip)
@tf.function
def red_origin(self, red):
red_maxs = tf.reduce_max(red, axis=-1, keepdims=True)
red_mins = tf.reduce_min(red, axis=-1, keepdims=True)
return (red_maxs + red_mins) / 2
@tf.function
def green_origin(self, green):
# option #1 middle of green
# green_maxs = tf.reduce_max(green, axis=-1, keepdims=True)
# green_mins = tf.reduce_min(green, axis=-1, keepdims=True)
# return (green_maxs + green_mins) / 2
# option #2 max of green because lower body is closer to 1 than to 0
green_maxs = tf.reduce_max(green, axis=-1, keepdims=True)
return green_maxs
@tf.function
def call(self, image):
degree = tf.random.uniform(shape=[],
minval=self.min_degree,
maxval=self.max_degree,
seed=self.seed)
if self.debug:
tf.print("degree", degree)
angle = degree * math.pi / 180.0
[red, green, blue] = tf.unstack(image, axis=-1)
red_origin = tf.cond(self.around_zero,
lambda: tf.zeros(tf.shape(red)),
lambda: self.red_origin(red))
green_origin = tf.cond(self.around_zero,
lambda: tf.zeros(tf.shape(green)),
lambda: self.green_origin(green))
new_red = red_origin + \
tf.math.cos(angle) * (red - red_origin) - \
tf.math.sin(angle) * (green - green_origin)
new_green = green_origin + \
tf.math.sin(angle) * (red - red_origin) + \
tf.math.cos(angle) * (green - green_origin)
new_red = tf.cond(
self.clip,
lambda: tf.clip_by_value(new_red, self.min_value, self.max_value),
lambda: new_red)
new_green = tf.cond(
self.clip,
lambda: tf.clip_by_value(
new_green, self.min_value, self.max_value),
lambda: new_green)
return tf.stack([new_red, new_green, blue], axis=-1)
class RandomVerticalStretch(tf.keras.layers.Layer):
def __init__(self, min_value=0.0, max_value=255.0, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
@tf.function
def round_down_float_to_1_decimal(self, num):
return tf.math.floor(num * 10.0) / 10.0
@tf.function
def call(self, image):
[red, green, blue] = tf.unstack(image, axis=-1)
green_maxs = tf.reduce_max(green, axis=-1, keepdims=True)
green_mins = tf.reduce_min(green, axis=-1, keepdims=True)
green_mids = (green_maxs + green_mins) / 2
green_alphas_1 = tf.abs(
(green_mids - self.min_value) / (green_mids - green_mins))
green_alphas_2 = tf.abs(
(self.max_value - green_mids) / (green_maxs - green_mids))
green_alpha = self.round_down_float_to_1_decimal(
tf.reduce_min([green_alphas_1, green_alphas_2]))
max_alpha = tf.maximum(tf.reduce_min([green_alpha]), 0.5)
alpha = tf.random.uniform(
shape=[], minval=0.5, maxval=max_alpha, seed=self.seed)
new_green = alpha * (green - green_mids) + green_mids
if self.debug:
tf.print("alpha", alpha, "max_alpha", max_alpha)
return tf.stack([red, new_green, blue], axis=-1)
class RandomHorizontalStretch(tf.keras.layers.Layer):
def __init__(self, min_value=0.0, max_value=255.0, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
@tf.function
def round_down_float_to_1_decimal(self, num):
return tf.math.floor(num * 10.0) / 10.0
@tf.function
def call(self, image):
[red, green, blue] = tf.unstack(image, axis=-1)
red_maxs = tf.reduce_max(red, axis=-1, keepdims=True)
red_mins = tf.reduce_min(red, axis=-1, keepdims=True)
red_mids = (red_maxs + red_mins) / 2
red_alphas_1 = tf.abs(
(red_mids - self.min_value) / (red_mids - red_mins))
red_alphas_2 = tf.abs(
(self.max_value - red_mids) / (red_maxs - red_mids))
red_alpha = self.round_down_float_to_1_decimal(
tf.reduce_min([red_alphas_1, red_alphas_2]))
max_alpha = tf.maximum(tf.reduce_min([red_alpha]), 0.5)
alpha = tf.random.uniform(
shape=[], minval=0.5, maxval=max_alpha, seed=self.seed)
new_red = alpha * (red - red_mids) + red_mids
if self.debug:
tf.print("alpha", alpha, "max_alpha", max_alpha)
return tf.stack([new_red, green, blue], axis=-1)
class RandomScale(tf.keras.layers.Layer):
def __init__(self, min_value=0.0, max_value=255.0, seed=None, debug=False, **kwargs):
super().__init__(**kwargs)
self.min_value = min_value
self.max_value = max_value
self.seed = seed
self.debug = debug
@tf.function
def round_down_float_to_1_decimal(self, num):
return tf.math.floor(num * 10.0) / 10.0
@tf.function
def call(self, batch):
# batch.shape => [examples, frames, joints, coordinates]
# [red, green, blue].shape => [examples, frames, joints]
[red, green, blue] = tf.unstack(batch, axis=-1)
# [color]_max/min/mid.shape => [examples]
red_max = tf.reduce_max(tf.reduce_max(red, axis=-1), axis=-1)
red_min = tf.reduce_min(tf.reduce_min(red, axis=-1), axis=-1)
red_mid = (red_max + red_min) / 2
green_max = tf.reduce_max(tf.reduce_max(green, axis=-1), axis=-1)
green_min = tf.reduce_min(tf.reduce_min(green, axis=-1), axis=-1)
green_mid = (green_max + green_min) / 2
# [color]_centered.shape => [examples, frames, joints]
red_centered = red - red_mid
green_centered = green - green_mid
# [color]_max_allowed_value.shape => [examples]
red_max_allowed_value = tf.minimum(
tf.abs(self.max_value - red_mid),
tf.abs(red_mid - self.min_value))
green_max_allowed_value = tf.minimum(
tf.abs(self.max_value - green_mid),
tf.abs(green_mid - self.min_value))
# [color]_scale.shape => [examples]
red_centered_max = tf.reduce_max(tf.reduce_max(
tf.abs(red_centered), axis=-1), axis=-1)
red_max_scale = self.round_down_float_to_1_decimal(
red_max_allowed_value / red_centered_max)
green_centered_max = tf.reduce_max(tf.reduce_max(
tf.abs(green_centered), axis=-1), axis=-1)
green_max_scale = self.round_down_float_to_1_decimal(
green_max_allowed_value / green_centered_max)
# max_alpha.shape => [examples]
max_alpha = tf.maximum(tf.reduce_min(
[red_max_scale, green_max_scale], axis=0), 0.5)
# alpha.shape => [examples]
alpha = tf.random.uniform(shape=tf.shape(
max_alpha), minval=0.5, maxval=max_alpha, seed=self.seed)
if self.debug:
tf.print("alpha", alpha)
# new_[color].shape: (examples, frames, joints)
new_red = alpha * (red - red_mid) + red_mid
new_green = alpha * (green - green_mid) + green_mid
return tf.stack([new_red, new_green, blue], axis=-1)