-
Notifications
You must be signed in to change notification settings - Fork 77
/
preprocess.py
183 lines (153 loc) · 8.17 KB
/
preprocess.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
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
from config import paths, amass_data
import numpy as np
from tqdm import tqdm
import glob
def process_amass(smooth_n=4):
def _syn_acc(v):
r"""
Synthesize accelerations from vertex positions.
"""
mid = smooth_n // 2
acc = torch.stack([(v[i] + v[i + 2] - 2 * v[i + 1]) * 3600 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]) * 3600 / smooth_n ** 2
for i in range(0, v.shape[0] - smooth_n * 2)])
return acc
vi_mask = torch.tensor([1961, 5424, 1176, 4662, 411, 3021])
ji_mask = torch.tensor([18, 19, 4, 5, 15, 0])
body_model = art.ParametricModel(paths.smpl_file)
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, 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 = [], [], [], [], [], []
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(pose[b:b + l].clone()) # N, 24, 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
b += l
print('Saving')
os.makedirs(paths.amass_dir, exist_ok=True)
torch.save(out_pose, os.path.join(paths.amass_dir, 'pose.pt'))
torch.save(out_shape, os.path.join(paths.amass_dir, 'shape.pt'))
torch.save(out_tran, os.path.join(paths.amass_dir, 'tran.pt'))
torch.save(out_joint, os.path.join(paths.amass_dir, 'joint.pt'))
torch.save(out_vrot, os.path.join(paths.amass_dir, 'vrot.pt'))
torch.save(out_vacc, os.path.join(paths.amass_dir, 'vacc.pt'))
print('Synthetic AMASS dataset is saved at', paths.amass_dir)
def process_dipimu():
imu_mask = [7, 8, 11, 12, 0, 2]
test_split = ['s_09', 's_10']
accs, oris, poses, trans = [], [], [], []
for subject_name in test_split:
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]
if torch.isnan(acc).sum() == 0 and torch.isnan(ori).sum() == 0 and torch.isnan(pose).sum() == 0:
accs.append(acc.clone())
oris.append(ori.clone())
poses.append(pose.clone())
trans.append(torch.zeros(pose.shape[0], 3)) # dip-imu does not contain translations
else:
print('DIP-IMU: %s/%s has too much nan! Discard!' % (subject_name, motion_name))
os.makedirs(paths.dipimu_dir, exist_ok=True)
torch.save({'acc': accs, 'ori': oris, 'pose': poses, 'tran': trans}, os.path.join(paths.dipimu_dir, 'test.pt'))
print('Preprocessed DIP-IMU 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)
if __name__ == '__main__':
# process_amass()
process_dipimu()
process_totalcapture()