-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtools.py
More file actions
283 lines (232 loc) · 9.01 KB
/
Copy pathtools.py
File metadata and controls
283 lines (232 loc) · 9.01 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
import numpy as np
import torch
import torch.nn.functional as F
import random
"""
NTU Mix
"""
trunk_ori_index = [4, 3, 21, 2, 1]
left_hand_ori_index = [9, 10, 11, 12, 24, 25]
right_hand_ori_index = [5, 6, 7, 8, 22, 23]
left_leg_ori_index = [17, 18, 19, 20]
right_leg_ori_index = [13, 14, 15, 16]
trunk = [i - 1 for i in trunk_ori_index]
left_hand = [i - 1 for i in left_hand_ori_index]
right_hand = [i - 1 for i in right_hand_ori_index]
left_leg = [i - 1 for i in left_leg_ori_index]
right_leg = [i - 1 for i in right_leg_ori_index]
body_parts = [trunk, left_hand, right_hand, left_leg, right_leg]
@torch.no_grad()
def ske_swap_randscale(x, spa_l, spa_u, tem_l, tem_u, p=None):
'''
swap a batch skeleton
T 100 --> 50 --> 25
S 25 --> 25 --> 25 (5 parts)
'''
# N M T V C -> N C T V M
x = x.permute(0, 4, 2, 3, 1).contiguous()
N, C, T, V, M = x.size()
tem_downsample_ratio = 4
# generate swap idx
idx = torch.arange(N)
if N > 2:
n = torch.randint(1, N - 1, (1,))
randidx = (idx + n) % N
else:
randidx = idx
# ------ Spatial ------ #
Cs = random.randint(spa_l, spa_u)
# sample the parts index
parts_idx = random.sample(body_parts, Cs)
# generate spa_idx
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
# ------ Temporal ------ #
Ct = random.randint(tem_l, tem_u)
tem_idx = random.randint(0, T // tem_downsample_ratio - Ct)
rt = Ct * tem_downsample_ratio
xst = x.clone()
if p==None:
p = random.random()
if p > 0.25:
N, C, T, V, M = xst.size()
Ct_2 = random.randint(Ct, 25)
tem_idx_2 = random.randint(0, T // tem_downsample_ratio - Ct_2)
rt_2 = Ct_2 * tem_downsample_ratio
xst_temp = xst[:, :, tem_idx_2 * tem_downsample_ratio: tem_idx_2 * tem_downsample_ratio + rt_2]
xst_temp = xst_temp.permute(0, 4, 3, 1, 2).contiguous()
xst_temp = xst_temp.view(N * M, V * C, -1)
xst_temp = torch.nn.functional.interpolate(xst_temp, size=rt)
xst_temp = xst_temp.view(N, M, V, C, rt)
xst_temp = xst_temp.permute(0, 3, 4, 2, 1).contiguous()
xst[:, :, tem_idx * tem_downsample_ratio: tem_idx * tem_downsample_ratio + rt, spa_idx, :] = \
xst_temp[randidx][:, :, :, spa_idx, :]
mask = torch.zeros(T // tem_downsample_ratio, V)
mask[tem_idx:tem_idx + Ct, spa_idx] = 1
else:
lamb = random.random()
xst = xst * (1 - lamb) + xst[randidx] * lamb
mask = torch.zeros(T // tem_downsample_ratio, V) + lamb
# N C T V M -> N M T V C
xst = xst.permute(0, 4, 2, 3, 1).contiguous()
return randidx, xst, mask.mean()
"""
K400 Mix
"""
trunk_ori_index_k400 = [1, 2, 3, 4, 5, 18, 19, 20]
left_hand_ori_index_k400 = [6, 8, 10]
right_hand_ori_index_k400 = [7, 9, 11]
left_leg_ori_index_k400 = [12, 14, 16]
right_leg_ori_index_k400 = [13, 15, 17]
trunk_k400 = [i - 1 for i in trunk_ori_index_k400]
left_hand_k400 = [i - 1 for i in left_hand_ori_index_k400]
right_hand_k400 = [i - 1 for i in right_hand_ori_index_k400]
left_leg_k400 = [i - 1 for i in left_leg_ori_index_k400]
right_leg_k400 = [i - 1 for i in right_leg_ori_index_k400]
body_parts_k400 = [trunk_k400, left_hand_k400, right_hand_k400, left_leg_k400, right_leg_k400]
@torch.no_grad()
def ske_swap_randscale_k400(x, spa_l, spa_u, tem_l, tem_u, p=None):
'''
swap a batch skeleton
T 100 --> 50 --> 25
S 25 --> 25 --> 25 (5 parts)
'''
# N M T V C -> N C T V M
x = x.permute(0, 4, 2, 3, 1).contiguous()
N, C, T, V, M = x.size()
tem_downsample_ratio = 4
# generate swap idx
idx = torch.arange(N)
if N > 2:
n = torch.randint(1, N - 1, (1,))
randidx = (idx + n) % N
else:
randidx = idx
# ------ Spatial ------ #
Cs = random.randint(spa_l, spa_u)
# sample the parts index
parts_idx = random.sample(body_parts_k400, Cs)
# generate spa_idx
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
# ------ Temporal ------ #
Ct = random.randint(tem_l, tem_u)
tem_idx = random.randint(0, T // tem_downsample_ratio - Ct)
rt = Ct * tem_downsample_ratio
xst = x.clone()
if p==None:
p = random.random()
if p > 0.25:
N, C, T, V, M = xst.size()
Ct_2 = random.randint(Ct, 25)
tem_idx_2 = random.randint(0, T // tem_downsample_ratio - Ct_2)
rt_2 = Ct_2 * tem_downsample_ratio
xst_temp = xst[:, :, tem_idx_2 * tem_downsample_ratio: tem_idx_2 * tem_downsample_ratio + rt_2]
xst_temp = xst_temp.permute(0, 4, 3, 1, 2).contiguous()
xst_temp = xst_temp.view(N * M, V * C, -1)
xst_temp = torch.nn.functional.interpolate(xst_temp, size=rt)
xst_temp = xst_temp.view(N, M, V, C, rt)
xst_temp = xst_temp.permute(0, 3, 4, 2, 1).contiguous()
xst[:, :, tem_idx * tem_downsample_ratio: tem_idx * tem_downsample_ratio + rt, spa_idx, :] = \
xst_temp[randidx][:, :, :, spa_idx, :]
mask = torch.zeros(T // tem_downsample_ratio, V)
mask[tem_idx:tem_idx + Ct, spa_idx] = 1
else:
lamb = random.random()
xst = xst * (1 - lamb) + xst[randidx] * lamb
mask = torch.zeros(T // tem_downsample_ratio, V) + lamb
# N C T V M -> N M T V C
xst = xst.permute(0, 4, 2, 3, 1).contiguous()
return randidx, xst, mask.mean()
"""
UAV Mix
"""
trunk_ori_index_uav = [1, 14, 15, 16, 17]
left_hand_ori_index_uav = [2, 3, 4]
right_hand_ori_index_uav = [5, 6, 7]
left_leg_ori_index_uav = [8, 9, 10]
right_leg_ori_index_uav = [11, 12, 13]
trunk_uav = [i - 1 for i in trunk_ori_index_uav]
left_hand_uav = [i - 1 for i in left_hand_ori_index_uav]
right_hand_uav = [i - 1 for i in right_hand_ori_index_uav]
left_leg_uav = [i - 1 for i in left_leg_ori_index_uav]
right_leg_uav = [i - 1 for i in right_leg_ori_index_uav]
body_parts_uav = [trunk_uav, left_hand_uav, right_hand_uav, left_leg_uav, right_leg_uav]
@torch.no_grad()
def ske_swap_randscale_uav(x, spa_l, spa_u, tem_l, tem_u, p=None):
'''
swap a batch skeleton
T 100 --> 50 --> 25
S 25 --> 25 --> 25 (5 parts)
'''
# N M T V C -> N C T V M
x = x.permute(0, 4, 2, 3, 1).contiguous()
N, C, T, V, M = x.size()
tem_downsample_ratio = 4
# generate swap idx
idx = torch.arange(N)
if N > 2:
n = torch.randint(1, N - 1, (1,))
randidx = (idx + n) % N
else:
randidx = idx
# ------ Spatial ------ #
Cs = random.randint(spa_l, spa_u)
# sample the parts index
parts_idx = random.sample(body_parts_uav, Cs)
# generate spa_idx
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
# ------ Temporal ------ #
Ct = random.randint(tem_l, tem_u)
tem_idx = random.randint(0, T // tem_downsample_ratio - Ct)
rt = Ct * tem_downsample_ratio
xst = x.clone()
if p==None:
p = random.random()
if p > 0.25:
N, C, T, V, M = xst.size()
Ct_2 = random.randint(Ct, 25)
tem_idx_2 = random.randint(0, T // tem_downsample_ratio - Ct_2)
rt_2 = Ct_2 * tem_downsample_ratio
xst_temp = xst[:, :, tem_idx_2 * tem_downsample_ratio: tem_idx_2 * tem_downsample_ratio + rt_2]
xst_temp = xst_temp.permute(0, 4, 3, 1, 2).contiguous()
xst_temp = xst_temp.view(N * M, V * C, -1)
xst_temp = torch.nn.functional.interpolate(xst_temp, size=rt)
xst_temp = xst_temp.view(N, M, V, C, rt)
xst_temp = xst_temp.permute(0, 3, 4, 2, 1).contiguous()
xst[:, :, tem_idx * tem_downsample_ratio: tem_idx * tem_downsample_ratio + rt, spa_idx, :] = \
xst_temp[randidx][:, :, :, spa_idx, :]
mask = torch.zeros(T // tem_downsample_ratio, V)
mask[tem_idx:tem_idx + Ct, spa_idx] = 1
else:
lamb = random.random()
xst = xst * (1 - lamb) + xst[randidx] * lamb
mask = torch.zeros(T // tem_downsample_ratio, V) + lamb
# N C T V M -> N M T V C
xst = xst.permute(0, 4, 2, 3, 1).contiguous()
return randidx, xst, mask.mean()
def skeleton_mix(keypoint, joint_cfg, num_classes, label):
if joint_cfg == 'nturgb+d':
randidx, mix_data, lamb = ske_swap_randscale(keypoint, 2, 3, 13, 20)
elif joint_cfg == 'coco_new':
randidx, mix_data, lamb = ske_swap_randscale_k400(keypoint, 2, 3, 13, 20)
elif joint_cfg == 'uav':
randidx, mix_data, lamb = ske_swap_randscale_uav(keypoint, 2, 3, 13, 20)
label_onehot = torch.zeros((keypoint.shape[0], num_classes)).cuda()
label_onehot.scatter_(1, label.unsqueeze(-1), 1)
mix_label = label_onehot * (1 - lamb) + label_onehot[randidx] * lamb
return mix_data, mix_label
def mix_chunk(cls_score, mix_label, x_1, x_2):
cls_score, mix_output = torch.chunk(cls_score, 2, dim=0)
mix_output = F.log_softmax(mix_output, dim=1)
cls_mix = -torch.mean(torch.sum(mix_output * mix_label, dim=1))
x_1, _ = torch.chunk(x_1, 2, dim=0)
x_2, _ = torch.chunk(x_2, 2, dim=0)
return cls_score, cls_mix, x_1, x_2