-
Notifications
You must be signed in to change notification settings - Fork 4
/
mocap_dataset.py
executable file
·198 lines (162 loc) · 8.47 KB
/
mocap_dataset.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
import numpy as np
import torch
from utils import qeuler_np, qfix, qexp_np
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class MocapDataset:
def __init__(self, path, skeleton, fps):
self._data = self._load(path)
self._fps = fps
self._use_gpu = False
self._skeleton = skeleton
def cuda(self):
self._use_gpu = True
self._skeleton.cuda()
return self
def _load(self, path):
result = {}
data = np.load(path, 'r', allow_pickle=True)
for i, (trajectory, rotations, subject, action) in enumerate(zip(data['trajectories'],
data['rotations'],
data['subjects'],
data['actions'])):
if subject not in result:
result[subject] = {}
result[subject][action] = {
'rotations': rotations,
'trajectory': trajectory
}
return result
def downsample(self, factor, keep_strides=True):
"""
Downsample this dataset by an integer factor, keeping all strides of the data
if keep_strides is True.
The frame rate must be divisible by the given factor.
The sequences will be replaced by their downsampled versions, whose actions
will have '_d0', ... '_dn' appended to their names.
"""
assert self._fps % factor == 0
for subject in self._data.keys():
new_actions = {}
for action in list(self._data[subject].keys()):
for idx in range(factor):
tup = {}
for k in self._data[subject][action].keys():
tup[k] = self._data[subject][action][k][idx::factor]
new_actions[action + '_d' + str(idx)] = tup
if not keep_strides:
break
self._data[subject] = new_actions
self._fps //= factor
def _mirror_sequence(self, sequence):
mirrored_rotations = sequence['rotations'].copy()
mirrored_trajectory = sequence['trajectory'].copy()
joints_left = self._skeleton.joints_left()
joints_right = self._skeleton.joints_right()
# Flip left/right joints
mirrored_rotations[:, joints_left] = sequence['rotations'][:, joints_right]
mirrored_rotations[:, joints_right] = sequence['rotations'][:, joints_left]
mirrored_rotations[:, :, [2, 3]] *= -1
mirrored_trajectory[:, 0] *= -1
return {
'rotations': qfix(mirrored_rotations),
'trajectory': mirrored_trajectory
}
def mirror(self):
"""
Perform data augmentation by mirroring every sequence in the dataset.
The mirrored sequences will have '_m' appended to the action name.
"""
for subject in self._data.keys():
for action in list(self._data[subject].keys()):
if '_m' in action:
continue
self._data[subject][action + '_m'] = self._mirror_sequence(self._data[subject][action])
def compute_euler_angles(self, order):
for subject in self._data.values():
for action in subject.values():
action['rotations_euler'] = qeuler_np(action['rotations'], order, use_gpu=self._use_gpu)
def compute_exp_angles(self):
for subject in self._data.values():
for action in subject.values():
action['rotations_exp'] = qexp_np(action['rotations'], use_gpu=self._use_gpu)
def compute_positions_quat(self): # joint angle (quaternion) to positions
for subject in self._data.values():
for action in subject.values():
rotations = torch.from_numpy(action['rotations'].astype('float32')).unsqueeze(0).to(device)
trajectory = torch.from_numpy(action['trajectory'].astype('float32')).unsqueeze(0).to(device)
action['positions_world'] = self._skeleton.forward_kinematics_quat(rotations, trajectory).squeeze(0).cpu().numpy()
# Absolute translations across the XY plane are removed here -> only Y axis (height)
trajectory[:, :, [0, 2]] = 0
action['positions_local'] = self._skeleton.forward_kinematics_quat(rotations, trajectory).squeeze(0).cpu().numpy()
def compute_positions_exp(self): # joint angle (exponential coordiante) to positions
for subject in self._data.values():
for action in subject.values():
rotations = torch.from_numpy(action['rotations_exp'].astype('float32')).unsqueeze(0).to(device)
trajectory = torch.from_numpy(action['trajectory'].astype('float32')).unsqueeze(0).to(device)
action['positions_world'] = self._skeleton.forward_kinematics_exp(rotations, trajectory).squeeze(
0).cpu().numpy()
# Absolute translations across the XY plane are removed here
trajectory[:, :, [0, 2]] = 0
action['positions_local'] = self._skeleton.forward_kinematics_exp(rotations, trajectory).squeeze(
0).cpu().numpy()
def compute_transformation_quat(self):
for subject in self._data.values():
for action in subject.values():
rotations = torch.from_numpy(action['rotations'].astype('float32')).unsqueeze(0).to(device)
trajectory = torch.from_numpy(action['trajectory'].astype('float32')).unsqueeze(0).to(device)
action['transformations_world'] = self._skeleton.compute_trasformation_matrix(rotations, trajectory).squeeze(0).cpu().numpy()
# Absolute translations across the XY plane are removed here
trajectory[:, :, [0, 2]] = 0
action['transformations_local'] = self._skeleton.compute_trasformation_matrix(rotations, trajectory).squeeze(0).cpu().numpy()
def normalized_stats(self, sequences, representation):
"""
Normalize the datasets.
Args
sequences: list of motion sequence's name
representation: 'rotations', 'rotations_exp', 'positions_world', 'positions_local', 'transformations'
Returns
data_mean:
data_std:
dim_to_use: vector with dimensions not used by the model
dim_to_ignore: vector with dimensions used by the model
"""
for i, (subject, action) in enumerate(sequences):
if i == 0:
data = self._data[subject][action][representation]
else:
data = np.append(data, self._data[subject][action][representation], axis=0)
data = np.reshape(data, (data.shape[0], -1))
data_mean = np.mean(data, axis=0).astype(np.float32)
data_std = np.std(data, axis=0).astype(np.float32)
dim_to_ignore = []
dim_to_use = []
dim_to_ignore.extend(list(np.where(data_std < 1e-4)[0]))
dim_to_use.extend(list(np.where(data_std >= 1e-4)[0]))
data_std[dim_to_ignore] = 1.0
return data_mean, data_std, dim_to_use, dim_to_ignore
def normalize_data(self, representation, data_mean, data_std, dim_to_use=None):
eps = 1e-15
for subject in self._data.values():
for action in subject.values():
data = np.reshape(action[representation], (action[representation].shape[0], -1))
norm_data = (np.divide((data - data_mean), data_std+eps)).astype(np.float32)
if dim_to_use:
norm_data = norm_data[:, dim_to_use]
action[representation] = np.reshape(norm_data, (action[representation].shape[0], -1,
action[representation].shape[-1]))
def __getitem__(self, key):
return self._data[key]
def subjects(self):
return self._data.keys()
def subject_actions(self, subject):
return self._data[subject].keys()
def all_actions(self):
result = []
for subject, actions in self._data.items():
for action in actions.keys():
result.append((subject, action))
return result
def fps(self):
return self._fps
def skeleton(self):
return self._skeleton