-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.py
451 lines (379 loc) · 16.6 KB
/
ops.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
"""
Opearations
Fred Zhang <[email protected]>
The Australian National University
Australian Centre for Robotic Vision
"""
import torch
import torch.nn.functional as F
import torchvision.ops.boxes as box_ops
import cv2
import numpy as np
import time
from torch import Tensor
from typing import List, Tuple
def compute_spatial_encodings(
boxes_1: List[Tensor], boxes_2: List[Tensor],
shapes: List[Tuple[int, int]], eps: float = 1e-10
):
"""
Parameters:
-----------
boxes_1: List[Tensor]
First set of bounding boxes (M, 4)
boxes_1: List[Tensor]
Second set of bounding boxes (M, 4)
shapes: List[Tuple[int, int]]
Image shapes, heights followed by widths
eps: float
A small constant used for numerical stability
Returns:
--------
Tensor
Computed spatial encodings between the boxes (N, 36)
"""
features = []
for b1, b2, shape in zip(boxes_1, boxes_2, shapes):
h, w = shape
c1_x = (b1[:, 0] + b1[:, 2]) / 2; c1_y = (b1[:, 1] + b1[:, 3]) / 2
c2_x = (b2[:, 0] + b2[:, 2]) / 2; c2_y = (b2[:, 1] + b2[:, 3]) / 2
b1_w = b1[:, 2] - b1[:, 0]; b1_h = b1[:, 3] - b1[:, 1]
b2_w = b2[:, 2] - b2[:, 0]; b2_h = b2[:, 3] - b2[:, 1]
d_x = torch.abs(c2_x - c1_x) / (b1_w + eps)
d_y = torch.abs(c2_y - c1_y) / (b1_h + eps)
iou = torch.diag(box_ops.box_iou(b1, b2))
# Construct spatial encoding
f = torch.stack([
# Relative position of box centre
c1_x / w, c1_y / h, c2_x / w, c2_y / h,
# Relative box width and height
b1_w / w, b1_h / h, b2_w / w, b2_h / h,
# Relative box area
b1_w * b1_h / (h * w), b2_w * b2_h / (h * w),
b2_w * b2_h / (b1_w * b1_h + eps),
# Box aspect ratio
b1_w / (b1_h + eps), b2_w / (b2_h + eps),
# Intersection over union
iou,
# Relative distance and direction of the object w.r.t. the person
(c2_x > c1_x).float() * d_x,
(c2_x < c1_x).float() * d_x,
(c2_y > c1_y).float() * d_y,
(c2_y < c1_y).float() * d_y,
], 1)
features.append(
torch.cat([f, torch.log(f + eps)], 1)
)
return torch.cat(features)
def compute_spatial_encodings_with_pose(
boxes_1: List[Tensor], boxes_2: List[Tensor], human_joints: List[Tensor],
shapes: List[Tuple[int, int]], eps: float = 1e-10
):
"""
Parameters:
-----------
boxes_1: List[Tensor]
First set of bounding boxes (M, 4)
boxes_1: List[Tensor]
Second set of bounding boxes (M, 4)
shapes: List[Tuple[int, int]]
Image shapes, heights followed by widths
eps: float
A small constant used for numerical stability
Returns:
--------
Tensor
Computed spatial encodings between the boxes (N, 36)
"""
features = []
for b1, b2, shape, human_joint in zip(boxes_1, boxes_2, shapes, human_joints):
h, w = shape
if len(human_joint)!=0:
human_joint[..., 0] = human_joint[..., 0].clip(0, w)
human_joint[..., 1] = human_joint[..., 1].clip(0, h)
c1_x = (b1[:, 0] + b1[:, 2]) / 2; c1_y = (b1[:, 1] + b1[:, 3]) / 2
c2_x = (b2[:, 0] + b2[:, 2]) / 2; c2_y = (b2[:, 1] + b2[:, 3]) / 2
b1_w = b1[:, 2] - b1[:, 0]; b1_h = b1[:, 3] - b1[:, 1]
b2_w = b2[:, 2] - b2[:, 0]; b2_h = b2[:, 3] - b2[:, 1]
d_x = torch.abs(c2_x - c1_x) / (b1_w + eps)
d_y = torch.abs(c2_y - c1_y) / (b1_h + eps)
iou = torch.diag(box_ops.box_iou(b1, b2))
human_joint_x = human_joint[:, :, 0]
human_joint_y = human_joint[:, :, 1]
human_joint_d_x = torch.abs(c2_x.unsqueeze(-1) - human_joint_x) / (b1_w.unsqueeze(-1) + eps)
human_joint_d_y = torch.abs(c2_y.unsqueeze(-1) - human_joint_y) / (b1_h.unsqueeze(-1) + eps)
# Construct spatial encoding
f = torch.stack([
# Relative position of box centre
c1_x / w, c1_y / h, c2_x / w, c2_y / h,
# Relative box width and height
b1_w / w, b1_h / h, b2_w / w, b2_h / h,
# Relative box area
b1_w * b1_h / (h * w), b2_w * b2_h / (h * w),
b2_w * b2_h / (b1_w * b1_h + eps),
# Box aspect ratio
b1_w / (b1_h + eps), b2_w / (b2_h + eps),
# Intersection over union
iou,
# Relative distance and direction of the object w.r.t. the person
(c2_x > c1_x).float() * d_x,
(c2_x < c1_x).float() * d_x,
(c2_y > c1_y).float() * d_y,
(c2_y < c1_y).float() * d_y,
], 1)
g = torch.cat([human_joint_x / w, human_joint_y / h, (c2_x.unsqueeze(-1) > human_joint_x).float() * human_joint_d_x, (c2_x.unsqueeze(-1) < human_joint_x).float() * human_joint_d_x,
(c2_y.unsqueeze(-1) > human_joint_y).float() * human_joint_d_y, (c2_y.unsqueeze(-1) < human_joint_y).float() * human_joint_d_y], 1)
h = torch.cat([f, g], dim=1)
features.append(
torch.cat([h, torch.log(h + eps)], 1)
)
return torch.cat(features)
def compute_spatial_encodings_with_pose_to_attention(
boxes_1: List[Tensor], boxes_2: List[Tensor], human_joints: List[Tensor],
shapes: List[Tuple[int, int]], eps: float = 1e-10
):
"""
Parameters:
-----------
boxes_1: List[Tensor]
First set of bounding boxes (M, 4)
boxes_1: List[Tensor]
Second set of bounding boxes (M, 4)
shapes: List[Tuple[int, int]]
Image shapes, heights followed by widths
eps: float
A small constant used for numerical stability
Returns:
--------
Tensor
Computed spatial encodings between the boxes (N, 36)
"""
spatial_query = []
pose_key = []
for b1, b2, shape, human_joint in zip(boxes_1, boxes_2, shapes, human_joints):
h, w = shape
if len(human_joint)!=0:
human_joint[..., 0] = human_joint[..., 0].clip(0, w)
human_joint[..., 1] = human_joint[..., 1].clip(0, h)
c1_x = (b1[:, 0] + b1[:, 2]) / 2; c1_y = (b1[:, 1] + b1[:, 3]) / 2
c2_x = (b2[:, 0] + b2[:, 2]) / 2; c2_y = (b2[:, 1] + b2[:, 3]) / 2
b1_w = b1[:, 2] - b1[:, 0]; b1_h = b1[:, 3] - b1[:, 1]
b2_w = b2[:, 2] - b2[:, 0]; b2_h = b2[:, 3] - b2[:, 1]
d_x = torch.abs(c2_x - c1_x) / (b1_w + eps)
d_y = torch.abs(c2_y - c1_y) / (b1_h + eps)
iou = torch.diag(box_ops.box_iou(b1, b2))
human_joint_x = human_joint[:, :, 0]
human_joint_y = human_joint[:, :, 1]
human_joint_d_x = torch.abs(c2_x.unsqueeze(-1) - human_joint_x) / (b1_w.unsqueeze(-1) + eps)
human_joint_d_y = torch.abs(c2_y.unsqueeze(-1) - human_joint_y) / (b1_h.unsqueeze(-1) + eps)
# Construct spatial encoding
f = torch.stack([
# Relative position of box centre
c1_x / w, c1_y / h, c2_x / w, c2_y / h,
# Relative box width and height
b1_w / w, b1_h / h, b2_w / w, b2_h / h,
# Relative box area
b1_w * b1_h / (h * w), b2_w * b2_h / (h * w),
b2_w * b2_h / (b1_w * b1_h + eps),
# Box aspect ratio
b1_w / (b1_h + eps), b2_w / (b2_h + eps),
# Intersection over union
iou,
# Relative distance and direction of the object w.r.t. the person
(c2_x > c1_x).float() * d_x,
(c2_x < c1_x).float() * d_x,
(c2_y > c1_y).float() * d_y,
(c2_y < c1_y).float() * d_y,
], 1)
g = torch.cat([human_joint_x / w, human_joint_y / h, (c2_x.unsqueeze(-1) > human_joint_x).float() * human_joint_d_x, (c2_x.unsqueeze(-1) < human_joint_x).float() * human_joint_d_x,
(c2_y.unsqueeze(-1) > human_joint_y).float() * human_joint_d_y, (c2_y.unsqueeze(-1) < human_joint_y).float() * human_joint_d_y], 1)
g = g.reshape(-1, 6, 17).permute(0, 2, 1)
pose_key.append(torch.cat([g, torch.log(g+eps)], 2))
spatial_query.append(
torch.cat([f, torch.log(f + eps)], 1)
)
return torch.cat(spatial_query), torch.cat(pose_key)
def binary_focal_loss(
x: Tensor, y: Tensor,
alpha: float = 0.5,
gamma: float = 2.0,
reduction: str = 'mean',
eps: float = 1e-6
):
loss = (1 - y - alpha).abs() * ((y-x).abs() + eps) ** gamma * \
torch.nn.functional.binary_cross_entropy(
x, y, reduction='none'
)
if reduction == 'mean':
return loss.mean()
elif reduction == 'sum':
return loss.sum()
elif reduction == 'none':
return loss
else:
raise ValueError("Unsupported reduction method {}".format(reduction))
def generate_pose_heatmap(human_bbox: Tensor, human_joints : Tensor, human_joints_score: Tensor):
if len(human_bbox) == 0:
return torch.tensor([]).to(human_bbox.device)
EPS = 1e-3
SIGMA = 0.6
PADDING = 3
heatmap_wh = torch.tensor([28, 56]).to(human_bbox.device)
num_box = human_bbox.shape[0]
assert num_box == human_joints.shape[0]
assert num_box == human_joints_score.shape[0]
human_joints_score = torch.clip(human_joints_score, min=EPS)
min_joints = torch.min(human_joints, dim=1).values
max_joints = torch.max(human_joints, dim=1).values
pose_wh = max_joints - min_joints
scale_wh = (heatmap_wh - PADDING * 2) / pose_wh
resized_human_joints = (human_joints - min_joints.unsqueeze(1)) * scale_wh.unsqueeze(1) + PADDING
x = torch.arange(0, heatmap_wh[0]).to(torch.float32).to(human_bbox.device)
y = torch.arange(0, heatmap_wh[1]).to(torch.float32).to(human_bbox.device).unsqueeze(-1)
heatmap = torch.exp(-((x - resized_human_joints[..., 0].unsqueeze(-1).unsqueeze(-1))**2 + (y - resized_human_joints[..., 1].unsqueeze(-1).unsqueeze(-1))**2) / 2 / SIGMA**2)
heatmap = torch.where(heatmap>EPS, heatmap, torch.tensor(0, dtype=heatmap.dtype, device=heatmap.device))
heatmap = heatmap * human_joints_score.unsqueeze(-1).unsqueeze(-1)
return heatmap
#### human_joint clipping
def make_pose_box(box_coords, box_labels, human_joints, human_idx, image_hw, gamma=0.3):
height = image_hw[0]
width = image_hw[1]
result = []
batch_size = len(box_coords)
for b_i in range(batch_size):
box_coord = box_coords[b_i]
box_label = box_labels[b_i]
human_joint = human_joints[b_i]
human_box_coord = box_coord[box_label == human_idx]
if len(human_joint)!=0:
human_joint[..., 0] = human_joint[..., 0].clip(0, width)
human_joint[..., 1] = human_joint[..., 1].clip(0, height)
num_human_box = len(human_box_coord)
human_box_height = human_box_coord[:, 3] - human_box_coord[:, 1]
pose_box_size = (human_box_height * gamma).unsqueeze(-1).unsqueeze(-1)
human_pose_box_coord = torch.cat([human_joint - pose_box_size / 2, human_joint + pose_box_size / 2], dim=-1).reshape(num_human_box*17, 4)
result.append(human_pose_box_coord)
return result
def warpaffine_image(image, n_px, device):
width, height = image.size
#height, width = image.shape[1:]
#image_size = np.array([n_px, n_px])
image_size = torch.tensor([n_px, n_px]).to(device)
x,y = torch.tensor(0).to(device), torch.tensor(0).to(device)
#x,y = 0, 0
#w = width
#h = height
w = torch.tensor(width-1).to(device)
h = torch.tensor(height-1).to(device)
#x1 = max(0, x)
#y1 = max(0, y)
#x2 = min(width - 1, x1 + max(0, w - 1))
#y2 = min(height - 1, y1 + max(0, h - 1))
#x1 = torch.minimum(width-1, x+ )
#x,y,w,h = x1, y1, x2 - x1, y2 - y1
aspect_ratio = image_size[0] / image_size[1]
#center = np.array([x + w * 0.5, y + h * 0.5], dtype=np.float32)
center = torch.tensor([x + w * 0.5, y + h * 0.5], dtype=torch.float32).to(device)
if w > aspect_ratio * h:
h = w * 1.0 / aspect_ratio
elif w < aspect_ratio * h:
w = h * aspect_ratio
#scale = np.array([w / 200.0, h / 200.0], dtype=np.float32)
scale = torch.tensor([w / 200.0, h / 200.0], dtype=torch.float32).to(device)
trans = get_warp_matrix(center * 2.0, image_size - 1.0, scale * 200.0, device)
processed_img = cv2.warpAffine(
np.array(image),
np.array(trans.cpu()), (int(image_size[0]), int(image_size[1])),
flags=cv2.INTER_LINEAR)
img_meta = {'center': center, 'scale': scale, 'n_px': n_px}
return processed_img, trans, img_meta
def get_warp_matrix(size_input, size_dst, size_target, device):
"""Calculate the transformation matrix under the constraint of unbiased.
Paper ref: Huang et al. The Devil is in the Details: Delving into Unbiased
Data Processing for Human Pose Estimation (CVPR 2020).
Args:
theta (float): Rotation angle in degrees.
size_input (np.ndarray): Size of input image [w, h].
size_dst (np.ndarray): Size of output image [w, h].
size_target (np.ndarray): Size of ROI in input plane [w, h].
Returns:
np.ndarray: A matrix for transformation.
"""
# theta = np.deg2rad(theta) theta:0
#matrix = np.zeros((2, 3), dtype=np.float32)
matrix = torch.zeros((2,3), dtype=torch.float32).to(device)
scale_x = size_dst[0] / size_target[0]
scale_y = size_dst[1] / size_target[1]
#matrix[0, 0] = math.cos(theta) * scale_x
matrix[0, 0] = 1.0 * scale_x
#matrix[0, 1] = -math.sin(theta) * scale_x
matrix[0, 1] = -0.0 * scale_x
# matrix[0, 2] = scale_x * (-0.5 * size_input[0] * math.cos(theta) +
# 0.5 * size_input[1] * math.sin(theta) +
# 0.5 * size_target[0])
matrix[0, 2] = scale_x * (-0.5 * size_input[0] * 1.0 +
0.5 * size_input[1] * 0.0 +
0.5 * size_target[0])
# matrix[1, 0] = math.sin(theta) * scale_y
matrix[1, 0] = 0.0 * scale_y
# matrix[1, 1] = math.cos(theta) * scale_y
matrix[1, 1] = 1.0 * scale_y
# matrix[1, 2] = scale_y * (-0.5 * size_input[0] * math.sin(theta) -
# 0.5 * size_input[1] * math.cos(theta) +
# 0.5 * size_target[1])
matrix[1, 2] = scale_y * (-0.5 * size_input[0] * 0.0 -
0.5 * size_input[1] * 1.0 +
0.5 * size_target[1])
return matrix
def warp_affine_joints(joints, mat):
"""Apply affine transformation defined by the transform matrix on the
joints.
Args:
joints (np.ndarray[..., 2]): Origin coordinate of joints.
mat (np.ndarray[3, 2]): The affine matrix.
Returns:
np.ndarray[..., 2]: Result coordinate of joints.
"""
#joints = np.array(joints)
shape = joints.shape
joints = joints.reshape(-1, 2)
return torch.matmul(torch.cat((joints, joints[:, 0:1] * 0 +1), dim=1), mat.T).reshape(shape)
#return np.dot(
# np.concatenate((joints, joints[:, 0:1] * 0 + 1), axis=1),
# mat.T).reshape(shape)
def transform_preds(coords, center, scale, output_size, use_udp=False):
"""Get final keypoint predictions from heatmaps and apply scaling and
translation to map them back to the image.
Note:
num_keypoints: K
Args:
coords (np.ndarray[K, ndims]):
* If ndims=2, corrds are predicted keypoint location.
* If ndims=4, corrds are composed of (x, y, scores, tags)
* If ndims=5, corrds are composed of (x, y, scores, tags,
flipped_tags)
center (np.ndarray[2, ]): Center of the bounding box (x, y).
scale (np.ndarray[2, ]): Scale of the bounding box
wrt [width, height].
output_size (np.ndarray[2, ] | list(2,)): Size of the
destination heatmaps.
use_udp (bool): Use unbiased data processing
Returns:
np.ndarray: Predicted coordinates in the images.
"""
assert coords.shape[1] in (2, 4, 5)
assert len(center) == 2
assert len(scale) == 2
assert len(output_size) == 2
# Recover the scale which is normalized by a factor of 200.
scale = scale * 200.0
if use_udp:
scale_x = scale[0] / (output_size[0] - 1.0)
scale_y = scale[1] / (output_size[1] - 1.0)
else:
scale_x = scale[0] / output_size[0]
scale_y = scale[1] / output_size[1]
target_coords = torch.ones_like(coords).to(coords.device)
target_coords[:, 0] = coords[:, 0] * scale_x + center[0] - scale[0] * 0.5
target_coords[:, 1] = coords[:, 1] * scale_y + center[1] - scale[1] * 0.5
return target_coords