-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocessing.py
330 lines (263 loc) · 11.2 KB
/
preprocessing.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
import math
import tensorflow as tf
from mediapipe.python.solutions.pose import PoseLandmark
RANGE_DICT = {
'face': range(0, 468),
'leftHand': range(468, 468+21),
'pose': range(468+21, 468+21+33),
'rightHand': range(468+21+33, 468+21+33+21),
'root': range(468+21+33+21, 468+21+33+21+1)
}
SLICE_DICT = {
'face': slice(0, 468),
'leftHand': slice(468, 468+21),
'pose': slice(468+21, 468+21+33),
'rightHand': slice(468+21+33, 468+21+33+21),
'root': slice(468+21+33+21, 468+21+33+21+1)
}
class PadIfLessThan(tf.keras.layers.Layer):
def __init__(self, frames=128, **kwargs):
super().__init__(**kwargs)
self.frames = frames
@tf.function
def call(self, images):
height = tf.shape(images)[1]
height_pad = tf.math.maximum(0, self.frames - height)
paddings = [[0, 0], [0, height_pad], [0, 0], [0, 0]]
padded_images = tf.pad(images, paddings, "CONSTANT")
return padded_images
class ResizeIfMoreThan(tf.keras.layers.Layer):
def __init__(self, frames=128, **kwargs):
super().__init__(**kwargs)
self.frames = frames
@tf.function
def call(self, images):
height = tf.shape(images)[1]
width = tf.shape(images)[2]
new_size = [self.frames, width]
resized = tf.cond(height > self.frames,
lambda: tf.image.resize(images, new_size),
lambda: images)
return resized
class Center(tf.keras.layers.Layer):
def __init__(self, around_index=0, **kwargs):
super().__init__(**kwargs)
self.around_index = around_index
@tf.function
def call(self, batch):
# batch.shape => (examples, frames, joints, coordinates)
# [color].shape => (examples, frames, joints)
[red, green, blue] = tf.unstack(batch, axis=-1)
# [color]_around_joint.shape => (examples, frames, 1)
red_around_joint = tf.expand_dims(
red[:, :, self.around_index], axis=-1)
green_around_joint = tf.expand_dims(
green[:, :, self.around_index], axis=-1)
# new_[color].shape => (examples, frames, joints)
new_red = red - red_around_joint
new_green = green - green_around_joint
return tf.stack([new_red, new_green, blue], axis=-1)
class CenterAtFirstFrame2D(tf.keras.layers.Layer):
def __init__(self, around_index=0, **kwargs):
super().__init__(**kwargs)
self.around_index = around_index
@tf.function
def call(self, batch):
# batch.shape => (examples, frames, joints, coordinates)
# [color].shape => (examples, frames, joints)
red, green = tf.unstack(batch, axis=-1)
# [color]_around_joint.shape => (examples)
red_around_joint_at_0 = tf.expand_dims(
tf.expand_dims(
red[:, 0, self.around_index], axis=-1),
axis=-1)
green_around_joint_at_0 = tf.expand_dims(
tf.expand_dims(
green[:, 0, self.around_index], axis=-1),
axis=-1)
# new_[color].shape => (examples, frames, joints)
new_red = red - red_around_joint_at_0
new_green = green - green_around_joint_at_0
return tf.stack([new_red, new_green], axis=-1)
class TranslationScaleInvariant(tf.keras.layers.Layer):
def __init__(self, level='frame', **kwargs):
super().__init__(**kwargs)
self.level_dict = {
'frame': tf.constant(0),
'joint': tf.constant(1)
}
self.level = self.level_dict[level]
@tf.function
def frame_level(self, batch):
# batch.shape => (examples, frames, joints, coordinates)
# [color].shape => (examples, frames, joints)
[red, green, blue] = tf.unstack(batch, axis=-1)
# [color]_min.shape => (examples, frames, 1)
# min at each frame per channel
red_min = tf.reduce_min(red, axis=-1, keepdims=True)
green_min = tf.reduce_min(green, axis=-1, keepdims=True)
blue_min = tf.reduce_min(blue, axis=-1, keepdims=True)
# [color]_max.shape => (examples, frames, 1)
# max at each frame per channel
red_max = tf.reduce_max(red, axis=-2, keepdims=True)
green_max = tf.reduce_max(green, axis=-2, keepdims=True)
blue_max = tf.reduce_max(blue, axis=-2, keepdims=True)
# [color]_dist.shape => (examples, frames, 1)
# distance between max and min at each frame per channel
red_dist = red_max - red_min
green_dist = green_max - green_min
blue_dist = blue_max - blue_min
# [color]_dist_max.shape => ()
# max_distance of all frames per channel
red_dist_max = tf.reduce_max(red_dist)
green_dist_max = tf.reduce_max(green_dist)
blue_dist_max = tf.reduce_max(blue_dist)
# new_[color].shape => (examples, frames, joints)
new_red = tf.math.divide_no_nan((red - red_min), red_dist_max)
new_green = tf.math.divide_no_nan((green - green_min), green_dist_max)
new_blue = tf.math.divide_no_nan((blue - blue_min), blue_dist_max)
return tf.stack([new_red, new_green, new_blue], axis=-1)
@tf.function
def joint_level(self, batch):
# batch.shape => (examples, frames, joints, coordinates)
# [color].shape => (examples, frames, joints)
[red, green, blue] = tf.unstack(batch, axis=-1)
# [color]_min.shape => (examples, 1, joints)
# min at each joint per channel
red_min = tf.reduce_min(red, axis=-2, keepdims=True)
green_min = tf.reduce_min(green, axis=-2, keepdims=True)
blue_min = tf.reduce_min(blue, axis=-2, keepdims=True)
# [color]_max.shape => (examples, 1, joints)
# max at each joint per channel
red_max = tf.reduce_max(red, axis=-2, keepdims=True)
green_max = tf.reduce_max(green, axis=-2, keepdims=True)
blue_max = tf.reduce_max(blue, axis=-2, keepdims=True)
# [color]_dist.shape => (examples, 1, joint)
# distance between max and min at each joint per channel
red_dist = red_max - red_min
green_dist = green_max - green_min
blue_dist = blue_max - blue_min
# [color]_dist_max.shape => ()
# max_distance of all joints per channel
red_dist_max = tf.reduce_max(red_dist)
green_dist_max = tf.reduce_max(green_dist)
blue_dist_max = tf.reduce_max(blue_dist)
# new_[color].shape => (examples, frames, joints)
new_red = tf.math.divide_no_nan((red - red_min), red_dist_max)
new_green = tf.math.divide_no_nan((green - green_min), green_dist_max)
new_blue = tf.math.divide_no_nan((blue - blue_min), blue_dist_max)
return tf.stack([new_red, new_green, new_blue], axis=-1)
@tf.function
def call(self, batch):
batch = tf.cond(
self.level == self.level_dict['frame'],
lambda: self.frame_level(batch),
lambda: self.joint_level(batch))
return batch
class FillBlueWithAngle(tf.keras.layers.Layer):
def __init__(self, x_channel=0, y_channel=1, scale_to=[0, 1], **kwargs):
super().__init__(**kwargs)
self.x_channel = x_channel
self.y_channel = y_channel
self.scale_to = scale_to
@tf.function
def call(self, batch):
batch = tf.cast(batch, tf.float32)
unstacked = tf.unstack(batch, axis=-1)
x, y = unstacked[self.x_channel], unstacked[self.y_channel]
angles = tf.math.multiply(tf.math.atan2(y, x), (180 / math.pi)) % 360
data_min, data_max = 0, 359
range_min, range_max = self.scale_to
std = (angles - data_min) / (data_max - data_min)
scaled = std * (range_max - range_min) + range_min
return tf.stack([x, y, scaled], axis=-1)
class FillZWithZeros(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@tf.function
def call(self, batch):
x, y, _ = tf.unstack(batch, axis=-1)
zeros = tf.zeros(tf.shape(x), dtype=x.dtype)
return tf.stack([x, y, zeros], axis=-1)
class RemoveZ(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@tf.function
def call(self, batch):
x, y, _ = tf.unstack(batch, axis=-1)
return tf.stack([x, y], axis=-1)
class AddRoot(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.left_shoulder = RANGE_DICT["pose"][PoseLandmark.LEFT_SHOULDER]
self.right_shoulder = RANGE_DICT["pose"][PoseLandmark.RIGHT_SHOULDER]
@tf.function
def call(self, batch):
left = batch[:, :, self.left_shoulder, :]
right = batch[:, :, self.right_shoulder, :]
root = (left + right) / 2
root = tf.expand_dims(root, axis=2)
batch = tf.concat([batch, root], axis=2)
return batch
class SortColumns(tf.keras.layers.Layer):
def __init__(self, tssi_order, **kwargs):
super().__init__(**kwargs)
joints_idxs = []
for joint in tssi_order:
joint_type = joint.split("_")[0]
if joint_type == "root":
landmark_id = 0
else:
landmark_id = int(joint.split("_")[1])
idx = RANGE_DICT[joint_type][landmark_id]
joints_idxs.append(idx)
self.joints_idxs = joints_idxs
@tf.function
def call(self, keypoints):
keypoints = tf.gather(keypoints, indices=self.joints_idxs, axis=2)
return keypoints
class FillNaNValues(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.nose_idx = RANGE_DICT["pose"][PoseLandmark.NOSE]
self.left_wrist_idx = RANGE_DICT["pose"][PoseLandmark.LEFT_WRIST]
self.right_wrist_idx = RANGE_DICT["pose"][PoseLandmark.RIGHT_WRIST]
@tf.function
def call(self, keypoints):
face = keypoints[:, :, SLICE_DICT["face"], :]
left_hand = keypoints[:, :, SLICE_DICT["leftHand"], :]
pose = keypoints[:, :, SLICE_DICT["pose"], :]
right_hand = keypoints[:, :, SLICE_DICT["rightHand"], :]
nose = keypoints[:, :, self.nose_idx, :]
nose = tf.expand_dims(nose, axis=2)
left_wrist = keypoints[:, :, self.left_wrist_idx, :]
left_wrist = tf.expand_dims(left_wrist, axis=2)
right_wrist = keypoints[:, :, self.right_wrist_idx, :]
right_wrist = tf.expand_dims(right_wrist, axis=2)
left_hand = tf.where(
tf.math.is_nan(left_hand),
tf.repeat(left_wrist, 21, axis=2),
left_hand)
right_hand = tf.where(
tf.math.is_nan(right_hand),
tf.repeat(right_wrist, 21, axis=2),
right_hand)
face = tf.where(
tf.math.is_nan(face),
tf.repeat(nose, 468, axis=2),
face)
keypoints = tf.concat([face, left_hand, pose, right_hand], axis=2)
return keypoints
class OneItemBatch(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@tf.function
def call(self, keypoints):
keypoints = tf.expand_dims(keypoints, 0)
return keypoints
class OneItemUnbatch(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@tf.function
def call(self, keypoints):
return keypoints[0]