-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtools.py
191 lines (157 loc) · 6.58 KB
/
tools.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
import random
import matplotlib.pyplot as plt
import numpy as np
import pdb
import torch
import torch.nn.functional as F
def valid_crop_resize(data_numpy,valid_frame_num,p_interval,window):
# input: C,T,V,M
C, T, V, M = data_numpy.shape
begin = 0
end = valid_frame_num
valid_size = end - begin
#crop
if len(p_interval) == 1:
p = p_interval[0]
bias = int((1-p) * valid_size/2)
data = data_numpy[:, begin+bias:end-bias, :, :]# center_crop
cropped_length = data.shape[1]
else:
p = np.random.rand(1)*(p_interval[1]-p_interval[0])+p_interval[0]
cropped_length = np.minimum(np.maximum(int(np.floor(valid_size*p)),64), valid_size)# constraint cropped_length lower bound as 64
bias = np.random.randint(0,valid_size-cropped_length+1)
data = data_numpy[:, begin+bias:begin+bias+cropped_length, :, :]
if data.shape[1] == 0:
print(cropped_length, bias, valid_size)
# resize
data = torch.tensor(data,dtype=torch.float)
data = data.permute(0, 2, 3, 1).contiguous().view(C * V * M, cropped_length)
data = data[None, None, :, :]
data = F.interpolate(data, size=(C * V * M, window), mode='bilinear',align_corners=False).squeeze() # could perform both up sample and down sample
data = data.contiguous().view(C, V, M, window).permute(0, 3, 1, 2).contiguous().numpy()
return data
def downsample(data_numpy, step, random_sample=True):
# input: C,T,V,M
begin = np.random.randint(step) if random_sample else 0
return data_numpy[:, begin::step, :, :]
def temporal_slice(data_numpy, step):
# input: C,T,V,M
C, T, V, M = data_numpy.shape
return data_numpy.reshape(C, T / step, step, V, M).transpose(
(0, 1, 3, 2, 4)).reshape(C, T / step, V, step * M)
def mean_subtractor(data_numpy, mean):
# input: C,T,V,M
# naive version
if mean == 0:
return
C, T, V, M = data_numpy.shape
valid_frame = (data_numpy != 0).sum(axis=3).sum(axis=2).sum(axis=0) > 0
begin = valid_frame.argmax()
end = len(valid_frame) - valid_frame[::-1].argmax()
data_numpy[:, :end, :, :] = data_numpy[:, :end, :, :] - mean
return data_numpy
def auto_pading(data_numpy, size, random_pad=False):
C, T, V, M = data_numpy.shape
if T < size:
begin = random.randint(0, size - T) if random_pad else 0
data_numpy_paded = np.zeros((C, size, V, M))
data_numpy_paded[:, begin:begin + T, :, :] = data_numpy
return data_numpy_paded
else:
return data_numpy
def random_choose(data_numpy, size, auto_pad=True):
# input: C,T,V,M 随机选择其中一段,不是很合理。因为有0
C, T, V, M = data_numpy.shape
if T == size:
return data_numpy
elif T < size:
if auto_pad:
return auto_pading(data_numpy, size, random_pad=True)
else:
return data_numpy
else:
begin = random.randint(0, T - size)
return data_numpy[:, begin:begin + size, :, :]
def random_move(data_numpy,
angle_candidate=[-10., -5., 0., 5., 10.],
scale_candidate=[0.9, 1.0, 1.1],
transform_candidate=[-0.2, -0.1, 0.0, 0.1, 0.2],
move_time_candidate=[1]):
# input: C,T,V,M
C, T, V, M = data_numpy.shape
move_time = random.choice(move_time_candidate)
node = np.arange(0, T, T * 1.0 / move_time).round().astype(int)
node = np.append(node, T)
num_node = len(node)
A = np.random.choice(angle_candidate, num_node)
S = np.random.choice(scale_candidate, num_node)
T_x = np.random.choice(transform_candidate, num_node)
T_y = np.random.choice(transform_candidate, num_node)
a = np.zeros(T)
s = np.zeros(T)
t_x = np.zeros(T)
t_y = np.zeros(T)
# linspace
for i in range(num_node - 1):
a[node[i]:node[i + 1]] = np.linspace(
A[i], A[i + 1], node[i + 1] - node[i]) * np.pi / 180
s[node[i]:node[i + 1]] = np.linspace(S[i], S[i + 1],
node[i + 1] - node[i])
t_x[node[i]:node[i + 1]] = np.linspace(T_x[i], T_x[i + 1],
node[i + 1] - node[i])
t_y[node[i]:node[i + 1]] = np.linspace(T_y[i], T_y[i + 1],
node[i + 1] - node[i])
theta = np.array([[np.cos(a) * s, -np.sin(a) * s],
[np.sin(a) * s, np.cos(a) * s]])
# perform transformation
for i_frame in range(T):
xy = data_numpy[0:2, i_frame, :, :]
new_xy = np.dot(theta[:, :, i_frame], xy.reshape(2, -1))
new_xy[0] += t_x[i_frame]
new_xy[1] += t_y[i_frame]
data_numpy[0:2, i_frame, :, :] = new_xy.reshape(2, V, M)
return data_numpy
def random_shift(data_numpy):
C, T, V, M = data_numpy.shape
data_shift = np.zeros(data_numpy.shape)
valid_frame = (data_numpy != 0).sum(axis=3).sum(axis=2).sum(axis=0) > 0
begin = valid_frame.argmax()
end = len(valid_frame) - valid_frame[::-1].argmax()
size = end - begin
bias = random.randint(0, T - size)
data_shift[:, bias:bias + size, :, :] = data_numpy[:, begin:end, :, :]
return data_shift
def _rot(rot):
"""
rot: T,3
"""
cos_r, sin_r = rot.cos(), rot.sin() # T,3
zeros = torch.zeros(rot.shape[0], 1) # T,1
ones = torch.ones(rot.shape[0], 1) # T,1
r1 = torch.stack((ones, zeros, zeros),dim=-1) # T,1,3
rx2 = torch.stack((zeros, cos_r[:,0:1], sin_r[:,0:1]), dim = -1) # T,1,3
rx3 = torch.stack((zeros, -sin_r[:,0:1], cos_r[:,0:1]), dim = -1) # T,1,3
rx = torch.cat((r1, rx2, rx3), dim = 1) # T,3,3
ry1 = torch.stack((cos_r[:,1:2], zeros, -sin_r[:,1:2]), dim =-1)
r2 = torch.stack((zeros, ones, zeros),dim=-1)
ry3 = torch.stack((sin_r[:,1:2], zeros, cos_r[:,1:2]), dim =-1)
ry = torch.cat((ry1, r2, ry3), dim = 1)
rz1 = torch.stack((cos_r[:,2:3], sin_r[:,2:3], zeros), dim =-1)
r3 = torch.stack((zeros, zeros, ones),dim=-1)
rz2 = torch.stack((-sin_r[:,2:3], cos_r[:,2:3],zeros), dim =-1)
rz = torch.cat((rz1, rz2, r3), dim = 1)
rot = rz.matmul(ry).matmul(rx)
return rot
def random_rot(data_numpy, theta=0.3):
"""
data_numpy: C,T,V,M
"""
data_torch = torch.from_numpy(data_numpy)
C, T, V, M = data_torch.shape
data_torch = data_torch.permute(1, 0, 2, 3).contiguous().view(T, C, V*M) # T,3,V*M
rot = torch.zeros(3).uniform_(-theta, theta)
rot = torch.stack([rot, ] * T, dim=0)
rot = _rot(rot) # T,3,3
data_torch = torch.matmul(rot, data_torch)
data_torch = data_torch.view(T, C, V, M).permute(1, 0, 2, 3).contiguous()
return data_torch