Repository navigation
Expand file tree
/
Copy pathpreprocess.py
More file actions
292 lines (241 loc) · 12.1 KB
/
Copy pathpreprocess.py
File metadata and controls
292 lines (241 loc) · 12.1 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
284
285
286
287
288
289
290
291
292
r"""
Preprocess DIP-IMU and TotalCapture test dataset.
Synthesize AMASS dataset.
Please refer to the `paths` in `config.py` and set the path of each dataset correctly.
"""
import articulate as art
import torch
import os
import pickle
import numpy as np
from tqdm import tqdm
import glob
from config import paths, amass_data
# left wrist, right wrist, left thigh, right thigh, head, pelvis
vi_mask = torch.tensor([1961, 5424, 876, 4362, 411, 3021])
ji_mask = torch.tensor([18, 19, 1, 2, 15, 0])
body_model = art.ParametricModel(paths.smpl_file)
def _syn_acc(v, smooth_n=4):
r"""
Synthesize accelerations from vertex positions.
"""
fps = 30
mid = smooth_n // 2
acc = torch.stack([(v[i] + v[i + 2] - 2 * v[i + 1]) * (fps**2) for i in range(0, v.shape[0] - 2)])
acc = torch.cat((torch.zeros_like(acc[:1]), acc, torch.zeros_like(acc[:1])))
if mid != 0:
acc[smooth_n:-smooth_n] = torch.stack(
[(v[i] + v[i + smooth_n * 2] - 2 * v[i + smooth_n]) * (fps**2) / smooth_n ** 2
for i in range(0, v.shape[0] - smooth_n * 2)])
return acc
def _foot_ground_probs(joint):
r"""
Compute foot-ground contact probabilities.
"""
dist_lfeet = torch.norm(joint[1:, 10] - joint[:-1, 10], dim=1)
dist_rfeet = torch.norm(joint[1:, 11] - joint[:-1, 11], dim=1)
lfoot_contact = (dist_lfeet < 0.008).int()
rfoot_contact = (dist_rfeet < 0.008).int()
lfoot_contact = torch.cat((torch.zeros(1, dtype=torch.int), lfoot_contact))
rfoot_contact = torch.cat((torch.zeros(1, dtype=torch.int), rfoot_contact))
return torch.stack((lfoot_contact, rfoot_contact), dim=1)
def process_amass():
data_pose, data_trans, data_beta, length = [], [], [], []
for ds_name in amass_data:
print('\rReading', ds_name)
for npz_fname in tqdm(glob.glob(os.path.join(paths.raw_amass_dir, ds_name, '*/*_poses.npz'))):
try: cdata = np.load(npz_fname)
except: continue
framerate = int(cdata['mocap_framerate'])
if framerate == 120: step = 2
elif framerate == 60 or framerate == 59: step = 1
else: continue
data_pose.extend(cdata['poses'][::step].astype(np.float32))
data_trans.extend(cdata['trans'][::step].astype(np.float32))
data_beta.append(cdata['betas'][:10])
length.append(cdata['poses'][::step].shape[0])
assert len(data_pose) != 0, 'AMASS dataset not found. Check config.py or comment the function process_amass()'
length = torch.tensor(length, dtype=torch.int)
shape = torch.tensor(np.asarray(data_beta, np.float32))
tran = torch.tensor(np.asarray(data_trans, np.float32))
pose = torch.tensor(np.asarray(data_pose, np.float32)).view(-1, 52, 3)
pose[:, 23] = pose[:, 37] # right hand
pose = pose[:, :24].clone() # only use body
# align AMASS global fame with DIP
amass_rot = torch.tensor([[[1, 0, 0], [0, 0, 1], [0, -1, 0.]]])
tran = amass_rot.matmul(tran.unsqueeze(-1)).view_as(tran)
pose[:, 0] = art.math.rotation_matrix_to_axis_angle(
amass_rot.matmul(art.math.axis_angle_to_rotation_matrix(pose[:, 0])))
print('Synthesizing IMU accelerations and orientations')
b = 0
out_pose, out_shape, out_tran, out_joint, out_vrot, out_vacc, out_contact = [], [], [], [], [], [], []
for i, l in tqdm(list(enumerate(length))):
if l <= 12: b += l; print('\tdiscard one sequence with length', l); continue
p = art.math.axis_angle_to_rotation_matrix(pose[b:b + l]).view(-1, 24, 3, 3)
grot, joint, vert = body_model.forward_kinematics(p, shape[i], tran[b:b + l], calc_mesh=True)
out_pose.append(p.clone()) # N, 24, 3, 3
out_tran.append(tran[b:b + l].clone()) # N, 3
out_shape.append(shape[i].clone()) # 10
out_joint.append(joint[:, :24].contiguous().clone()) # N, 24, 3
out_vacc.append(_syn_acc(vert[:, vi_mask])) # N, 6, 3
out_vrot.append(grot[:, ji_mask]) # N, 6, 3, 3
out_contact.append(_foot_ground_probs(joint).clone()) # N, 2
b += l
print('Saving')
data = {
'joint': out_joint,
'pose': out_pose,
'shape': out_shape,
'tran': out_tran,
'acc': out_vacc,
'ori': out_vrot,
'contact': out_contact
}
torch.save(data, os.path.join(paths.amass_dir, 'AMASS.pt'))
print('Synthetic AMASS dataset is saved at', paths.amass_dir)
def process_dipimu(split='train'):
imu_mask = [7, 8, 9, 10, 0, 2]
train_split = ['s_01', 's_02', 's_03', 's_04', 's_05', 's_06', 's_07', 's_08']
test_split = ['s_09', 's_10']
subjects = train_split if split == "train" else test_split
accs, oris, poses, trans, joints, shapes = [], [], [], [], [], []
body_model = art.ParametricModel(paths.smpl_file)
for subject_name in subjects:
for motion_name in os.listdir(os.path.join(paths.raw_dipimu_dir, subject_name)):
path = os.path.join(paths.raw_dipimu_dir, subject_name, motion_name)
data = pickle.load(open(path, 'rb'), encoding='latin1')
acc = torch.from_numpy(data['imu_acc'][:, imu_mask]).float()
ori = torch.from_numpy(data['imu_ori'][:, imu_mask]).float()
pose = torch.from_numpy(data['gt']).float()
# fill nan with nearest neighbors
for _ in range(4):
acc[1:].masked_scatter_(torch.isnan(acc[1:]), acc[:-1][torch.isnan(acc[1:])])
ori[1:].masked_scatter_(torch.isnan(ori[1:]), ori[:-1][torch.isnan(ori[1:])])
acc[:-1].masked_scatter_(torch.isnan(acc[:-1]), acc[1:][torch.isnan(acc[:-1])])
ori[:-1].masked_scatter_(torch.isnan(ori[:-1]), ori[1:][torch.isnan(ori[:-1])])
acc, ori, pose = acc[6:-6], ori[6:-6], pose[6:-6]
shape = torch.zeros(10)
tran = torch.zeros(pose.shape[0], 3)
if torch.isnan(acc).sum() == 0 and torch.isnan(ori).sum() == 0 and torch.isnan(pose).sum() == 0:
# forward kinematics to get the joint position
pose = art.math.axis_angle_to_rotation_matrix(pose).reshape(-1, 24, 3, 3)
_, joint, _ = body_model.forward_kinematics(pose, shape, tran, calc_mesh=True)
accs.append(acc.clone())
oris.append(ori.clone())
poses.append(pose.clone())
trans.append(tran.clone()) # dip-imu does not contain translations
shapes.append(shape.clone())
joints.append(joint.clone())
else:
print('DIP-IMU: %s/%s has too much nan! Discard!' % (subject_name, motion_name))
print('Saving')
os.makedirs(paths.dipimu_dir, exist_ok=True)
data = {
'pose': poses,
'tran': trans,
'acc': accs,
'ori': oris,
'joint': joints,
'shape': shapes,
}
torch.save(data, os.path.join(paths.dipimu_dir, f'DIP_{split}.pt'))
print(f'Preprocessed DIP-IMU {split} dataset is saved at', paths.dipimu_dir)
def process_totalcapture():
inches_to_meters = 0.0254
file_name = 'gt_skel_gbl_pos.txt'
accs, oris, poses, trans = [], [], [], []
for file in sorted(os.listdir(paths.raw_totalcapture_dip_dir)):
data = pickle.load(open(os.path.join(paths.raw_totalcapture_dip_dir, file), 'rb'), encoding='latin1')
ori = torch.from_numpy(data['ori']).float()[:, torch.tensor([2, 3, 0, 1, 4, 5])]
acc = torch.from_numpy(data['acc']).float()[:, torch.tensor([2, 3, 0, 1, 4, 5])]
pose = torch.from_numpy(data['gt']).float().view(-1, 24, 3)
# acc/ori and gt pose do not match in the dataset
if acc.shape[0] < pose.shape[0]:
pose = pose[:acc.shape[0]]
elif acc.shape[0] > pose.shape[0]:
acc = acc[:pose.shape[0]]
ori = ori[:pose.shape[0]]
assert acc.shape[0] == ori.shape[0] and ori.shape[0] == pose.shape[0]
accs.append(acc) # N, 6, 3
oris.append(ori) # N, 6, 3, 3
poses.append(pose) # N, 24, 3
for subject_name in ['S1', 'S2', 'S3', 'S4', 'S5']:
for motion_name in sorted(os.listdir(os.path.join(paths.raw_totalcapture_official_dir, subject_name))):
if subject_name == 'S5' and motion_name == 'acting3':
continue # no SMPL poses
f = open(os.path.join(paths.raw_totalcapture_official_dir, subject_name, motion_name, file_name))
line = f.readline().split('\t')
index = torch.tensor([line.index(_) for _ in ['LeftFoot', 'RightFoot', 'Spine']])
pos = []
while line:
line = f.readline()
pos.append(torch.tensor([[float(_) for _ in p.split(' ')] for p in line.split('\t')[:-1]]))
pos = torch.stack(pos[:-1])[:, index] * inches_to_meters
pos[:, :, 0].neg_()
pos[:, :, 2].neg_()
trans.append(pos[:, 2] - pos[:1, 2]) # N, 3
# match trans with poses
for i in range(len(accs)):
if accs[i].shape[0] < trans[i].shape[0]:
trans[i] = trans[i][:accs[i].shape[0]]
assert trans[i].shape[0] == accs[i].shape[0]
os.makedirs(paths.totalcapture_dir, exist_ok=True)
torch.save({'acc': accs, 'ori': oris, 'pose': poses, 'tran': trans},
os.path.join(paths.totalcapture_dir, 'test.pt'))
print('Preprocessed TotalCapture dataset is saved at', paths.totalcapture_dir)
def process_imuposer(split: str="train"):
"""Preprocess the IMUPoser dataset"""
train_split = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8']
test_split = ['P9', 'P10']
subjects = train_split if split == "train" else test_split
accs, oris, poses, trans, contacts, joints = [], [], [], [], [], []
syn_acc, syn_ori = [], []
for subject_name in sorted(os.listdir(paths.raw_imuposer_dir)):
if subject_name not in subjects:
continue
print(f"Processing: {subject_name}")
for motion_name in sorted(os.listdir(os.path.join(paths.raw_imuposer_dir, subject_name))):
with open(os.path.join(paths.raw_imuposer_dir, subject_name, motion_name), "rb") as f:
fdata = pickle.load(f)
acc = fdata['imu'][:, :5*3].view(-1, 5, 3)
ori = fdata['imu'][:, 5*3:].view(-1, 5, 3, 3)
pose = art.math.axis_angle_to_rotation_matrix(fdata['pose']).view(-1, 24, 3, 3)
tran = fdata['trans'].to(torch.float32)
# align IMUPoser global fame with DIP
rot = torch.tensor([[[-1, 0, 0], [0, 0, 1], [0, 1, 0.]]])
pose[:, 0] = rot.matmul(pose[:, 0])
tran = tran.matmul(rot.squeeze())
# obtain joints positions and foot-ground contact probabilities
grot, joint, vert = body_model.forward_kinematics(pose, None, tran, calc_mesh=True)
contact = _foot_ground_probs(joint)
# ensure sizes are consistent
assert tran.shape[0] == pose.shape[0]
accs.append(acc.clone()) # N, 5, 3
oris.append(ori.clone()) # N, 5, 3, 3
contacts.append(contact.clone()) # N, 2
joints.append(joint.clone()) # N, 24, 3
poses.append(pose.clone()) # N, 24, 3, 3
trans.append(tran.clone()) # N, 3
# synthesize IMU accelerations and orientations
syn_acc.append(_syn_acc(vert[:, vi_mask]).clone()) # N, 6, 3
syn_ori.append(grot[:, ji_mask].clone()) # N, 6, 3, 3
print(f"# Data Processed: {len(accs)}")
data = {
'acc': accs,
'ori': oris,
'pose': poses,
'tran': trans,
'syn_acc': syn_acc,
'syn_ori': syn_ori,
'contact': contacts,
'joint': joints
}
os.makedirs(paths.imuposer_dir, exist_ok=True)
data_path = os.path.join(paths.imuposer_dir, f'{split}.pt')
torch.save(data, data_path)
print(f'Preprocessed IMUPoser {split} dataset is saved at', data_path)
if __name__ == '__main__':
# process_amass()
# process_dipimu(split='train')
# process_totalcapture()
process_imuposer(split='test')