forked from JHang2020/PCM3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
259 lines (236 loc) · 9.88 KB
/
utils.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
import torch
import torch.nn as nn
import numpy as np
import math
import random
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]
#temoral-frame mask
class TemporalMask:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
self.mask_ratio = mask_ratio
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
def __call__(self, data, frame):
'''
given a data: N,C,T,V,M
frame: the number of the valid frames in data
return Mask: N,C,T,V,M
randomly mask the frame*ratio frames
'''
#data N,C,T,V,M
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
#data = data.reshape(N,T,C*V*M)
size, max_frame, feature_dim = N,T,V*C*M
#data = data.view(size, max_frame, self.person_num, self.joint_num, self.channel_num)#N,T,M,V,C
mask_idx = torch.tensor((frame * (1 - self.mask_ratio))).reshape((1, 1, 1, 1, 1)).repeat(size, max_frame, self.person_num, self.joint_num, self.channel_num)
frame_idx = torch.arange(max_frame).reshape((1, max_frame, 1, 1, 1)).repeat(size, 1, self.person_num, self.joint_num, self.channel_num)
mask = (frame_idx < mask_idx).float()
randper = np.random.permutation(range(frame))
rand = torch.arange(max_frame)
rand[0:frame] = torch.from_numpy(randper)
mask = mask[:,rand,...]
#N,T,M,V,C -> N C T V M
#mask = mask.permute((0,4,1,3,2))
#trans_data = data * mask
#trans_data = trans_data.view(size, max_frame, feature_dim)
return mask #N,T,M,V,C
#random joint mask different for frames
class Jointmask:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
self.mask_ratio = mask_ratio
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
def __call__(self, data, frame=None):
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((T,V))
mask_joint_num = int(V*self.mask_ratio)
for i in range(T):
rand = random.sample(range(0,V),mask_joint_num)
mask[i][rand] = 0.0
mask = mask.reshape(1,T,1,V,1).repeat(N,1,M,1,C)
mask[:,:,1,:,:] = 1.0
return mask
#random topology joints mask same clip frames
class Jointmask3:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
#self.mask_ratio = mask_ratio
self.mask_part_num = int(5*mask_ratio)
if self.mask_part_num == 0:
print('WARNING: Too small mask ratio in the implenmentation!')
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
self.chunk_num = 8
self.clip_length = 64 // self.chunk_num
def __call__(self, data, frame=None):
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
for i in range(self.chunk_num):
parts_idx = random.sample(body_parts, self.mask_part_num)
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
mask[:,i*self.clip_length:(i+1)*self.clip_length,spa_idx] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
mask[:,:,1,:,:] = 1.0#set the second people visable
return mask#NTMVC
#random joint mask same for frames
class Jointmask2:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
self.mask_ratio = mask_ratio
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
def __call__(self, data, frame=None):
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
mask_joint_num = int(V*self.mask_ratio)
rand = random.sample(range(0,V),mask_joint_num)
mask[:,:,rand] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
return mask
#random joint mask same for clip frames
class Jointmask4:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
self.mask_ratio = mask_ratio
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
def __call__(self, data, frame=None):
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
mask_joint_num = int(V*self.mask_ratio)
for i in range(8):
rand = random.sample(range(0,V),mask_joint_num)
mask[:,i*8:(i+1)*8,rand] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
return mask
#random topology joints mask different frames
class Jointmask5:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
#self.mask_ratio = mask_ratio
self.mask_part_num = int(5*mask_ratio)
if self.mask_part_num == 0:
print('WARNING: Too small mask ratio in the implenmentation!')
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
self.chunk_num = 64
self.clip_length = 64 // self.chunk_num
def __call__(self, data, frame=None):
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
for i in range(self.chunk_num):
parts_idx = random.sample(body_parts, self.mask_part_num)
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
mask[:,i*self.clip_length:(i+1)*self.clip_length,spa_idx] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
mask[:,:,1,:,:] = 1.0#set the second people visable
return mask#NTMVC
#random topology joints mask random clip frames
class Jointmask6:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
#self.mask_ratio = mask_ratio
self.mask_part_num = int(5*mask_ratio)
if self.mask_part_num == 0:
print('WARNING: Too small mask ratio in the implenmentation!')
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
self.chunk_num = 8
self.clip_length = 64 // self.chunk_num
def __call__(self, data, frame=None):
self.clip_length = random.randint(1,10)
self.chunk_num = 64//self.clip_length
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
for i in range(self.chunk_num):
parts_idx = random.sample(body_parts, self.mask_part_num)
spa_idx = []
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
mask[:,i*self.clip_length:(i+1)*self.clip_length,spa_idx] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
mask[:,:,1,:,:] = 1.0#set the second people visable
return mask#NTMVC
#temporal random clip based
#spatial combine random and topology
class Jointmask7:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
#self.mask_ratio = mask_ratio
self.mask_part_num = int(5*mask_ratio)
if self.mask_part_num == 0:
print('WARNING: Too small mask ratio in the implenmentation!')
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
self.chunk_num = 8
self.clip_length = 64 // self.chunk_num
def __call__(self, data, frame=None):
self.clip_length = random.randint(1,10)
self.chunk_num = 64//self.clip_length
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((N,T,V))
for i in range(self.chunk_num):
parts_idx = random.sample(body_parts, self.mask_part_num)
spa_idx = []
noise1 = random.sample(range(0,V),2)
noise2 = random.sample(range(0,V),3)
for part_idx in parts_idx:
spa_idx += part_idx
spa_idx.sort()
mask[:,i*self.clip_length:(i+1)*self.clip_length,spa_idx] = 0.0
mask[:,i*self.clip_length:(i+1)*self.clip_length,noise1] = 1.0
mask[:,i*self.clip_length:(i+1)*self.clip_length,noise2] = 0.0
mask = mask.reshape(N,T,1,V,1).repeat(1,1,M,1,C)
mask[:,:,1,:,:] = 1.0#set the second people visable
return mask#NTMVC
#random joint mask totally random
class Jointmask8:
def __init__(self, mask_ratio, person_num, joint_num, channel_num):
self.mask_ratio = mask_ratio
self.person_num = person_num
self.joint_num = joint_num
self.channel_num = channel_num
def __call__(self, data, ratio=None):
if ratio!=None:
self.mask_ratio = ratio
N,C,T,V,M = data.shape
data = data.permute((0,2,4,3,1))##N,T,M,V,C
mask = torch.ones((T*V))
mask_joint_num = int(T*V*self.mask_ratio)
rand = random.sample(range(0,T*V),mask_joint_num)
mask[rand] = 0.0
mask = mask.reshape(1,T,1,V,1).repeat(N,1,M,1,C)
mask[:,:,1,:,:] = 1.0
return mask
if __name__ =='__main__':
J = Jointmask3(0.4,1,25,1)
T = TemporalMask(0.5,1,10,1)
mask = J(torch.rand((1,1,64,25,2)))
print(mask[0,45,0,:,0])