-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmotion_quality.py
More file actions
446 lines (372 loc) · 17.3 KB
/
Copy pathmotion_quality.py
File metadata and controls
446 lines (372 loc) · 17.3 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import torch
import numpy as np
from tqdm import tqdm
from mbench.utils import load_dimension_info
from mbench.third_party.visualize.rotation_conversions import axis_angle_to_rotation_6d
from scipy.signal import find_peaks
FOOT_IDX = [10, 11]
def load_joints(evaluation_file: str, device: str = 'cuda') -> torch.Tensor:
"""
Load joints from either .npy or .pt file format.
Args:
evaluation_file: Path to the evaluation file (.npy or .pt)
device: Target device for the tensor
Returns:
Tensor of shape (T, num_joints, 3)
"""
if evaluation_file.endswith('.npy'):
# Direct numpy array of joints (T, 22, 3)
joints = np.load(evaluation_file)
return torch.from_numpy(joints).float().to(device)
else:
# PyTorch dict with 'joints' key
data = torch.load(evaluation_file, map_location=device)
if isinstance(data, dict) and 'joints' in data:
joints = data['joints']
if isinstance(joints, np.ndarray):
return torch.from_numpy(joints).float().to(device)
return joints.float().to(device)
else:
raise ValueError(f"Unexpected data format in {evaluation_file}")
def find_common_intervals(intervals1, intervals2):
"""Find common intervals between two sets of intervals."""
intervals1 = torch.tensor(intervals1, dtype=torch.float32)
intervals2 = torch.tensor(intervals2, dtype=torch.float32)
start_max = torch.max(intervals1[:, 0].unsqueeze(1), intervals2[:, 0])
end_min = torch.min(intervals1[:, 1].unsqueeze(1), intervals2[:, 1])
overlap_mask = start_max < end_min
common_intervals = torch.stack((start_max, end_min), dim=2)[overlap_mask]
return common_intervals
def calculate_angle(vector1, vector2):
"""Calculate angle between two vectors."""
vector1 = vector1 / (torch.norm(vector1, dim=-1).unsqueeze(-1) + 1e-6)
vector2 = vector2 / (torch.norm(vector2, dim=-1).unsqueeze(-1) + 1e-6)
dot_product = torch.sum(vector1 * vector2, dim=-1)
dot_product = torch.clamp(dot_product, -1.0, 1.0)
angle = torch.acos(dot_product)
return angle
def get_contact(joints: torch.Tensor, foot_idx: list = FOOT_IDX, vel_ts: float = 0.01, height_ts: float = 0.02, device: str = 'cuda') -> torch.Tensor:
"""Detect contact from foot velocities and height."""
foot_pos = joints[:, foot_idx] # (frames, 2, 3)
# Calculate foot velocity
foot_vel = foot_pos[1:] - foot_pos[:-1]
foot_vel = torch.cat([foot_vel, foot_vel[-1:]], dim=0)
vel_ts = torch.tensor(vel_ts, dtype=torch.float32).to(device)
height_ts = torch.tensor(height_ts, dtype=torch.float32).to(device)
# Calculate the velocity magnitudes (norm)
delta = torch.norm(foot_vel, dim=-1)
# Detect foot contact based on velocity and height threshold
contact = (delta < vel_ts) | (foot_pos[:, :, 2] < height_ts).to(device)
return contact.int()
def get_range(contact: torch.Tensor, contact_state: bool = True) -> list:
"""Get contact range from contact info.
Args:
contact (torch.Tensor): contact information (frames, 2).
contact_state (bool, optional): contact state. Defaults to True.
Returns:
list: contact ranges.
"""
contact_state = int(contact_state)
frames = contact.shape[0]
# Get contact range
contact_range = []
for i in range(contact.shape[1]):
rge = []
start = -1
end = -1
for idx in range(frames):
if contact[idx, i] != contact_state:
continue
if start == -1:
start = idx
end = idx
else:
if idx - end == 1:
end += 1
else:
rge.append([start, end])
start = idx
end = idx
if end != -1:
rge.append([start, end])
contact_range.append(rge)
return contact_range
def remove_global_translation(joints):
"""Remove global translation by subtracting the root joint's position."""
root_joint = joints[:, 0:1, :] # Root joint is joint 0 (shape: T, 1, 3)
local_joints = joints - root_joint # Subtract root joint position
return local_joints
def summarize_scores(values):
"""Return mean/std/count summary for a list of scalar values."""
if not values:
return {"mean": 0.0, "std": 0.0, "num_samples": 0}
tensor = torch.tensor(values, dtype=torch.float32)
std = tensor.std(unbiased=False).item() if tensor.numel() > 1 else 0.0
return {
"mean": tensor.mean().item(),
"std": std,
"num_samples": tensor.numel(),
}
def compute_jitter_degree(full_info_path: str, device: str, **kwargs):
"""
Compute the jitter degree of the motion based on the acceleration of the joints.
"""
prompt_dict_ls = load_dimension_info(full_info_path, dimension='Jitter_Degree')
jitter_degree_list = []
per_motion_metrics = []
for prompt_dict in tqdm(prompt_dict_ls):
evaluation_file = prompt_dict["evaluation_file"]
pred_joints = load_joints(evaluation_file, device)
# Global jitter degree
velocity = pred_joints[1:] - pred_joints[:-1] # Shape: (T-1, 24, 3)
acceleration = velocity[1:] - velocity[:-1] # Shape: (T-2, 24, 3)
acceleration_magnitude = torch.norm(acceleration, dim=2) # Shape: (T-2, 24)
global_jitter = acceleration_magnitude.mean()
# Local jitter degree (remove global translation)
local_joints = remove_global_translation(pred_joints)
local_velocity = local_joints[1:] - local_joints[:-1] # Shape: (T-1, 24, 3)
local_acceleration = local_velocity[1:] - local_velocity[:-1] # Shape: (T-2, 24, 3)
local_acceleration_magnitude = torch.norm(local_acceleration, dim=2) # Shape: (T-2, 24)
local_jitter = local_acceleration_magnitude.mean()
# Combined jitter degree
combined_jitter = global_jitter + local_jitter
combined_value = combined_jitter.item()
jitter_degree_list.append(combined_value)
per_motion_metrics.append(
{
"id": prompt_dict.get("id"),
"prompt": prompt_dict.get("prompt"),
"value": combined_value,
"evaluation_file": evaluation_file,
"motion_duration": prompt_dict.get("motion_duration"),
}
)
return {
"aggregate": summarize_scores(jitter_degree_list),
"per_motion": per_motion_metrics,
}
def compute_ground_penetration(full_info_path: str, device: str, **kwargs):
"""
Compute foot-floor penetration based on the foot joints.
"""
prompt_dict_ls = load_dimension_info(full_info_path, dimension='Ground_Penetration')
penetration_list = []
per_motion_metrics = []
for prompt_dict in tqdm(prompt_dict_ls):
evaluation_file = prompt_dict["evaluation_file"]
pred_joints = load_joints(evaluation_file, device)
delta_ts = 0.005 # 5mm tolerance
floor_height = 0.0
foot_pos = pred_joints[:, FOOT_IDX] # (frames, 2, 3)
foot_ground_height = foot_pos[:, :, 2] - floor_height
# Compute penetration distance (below the ground)
penetration_dist = torch.abs(foot_ground_height[foot_ground_height < -delta_ts])
penetration_score = penetration_dist.mean() if penetration_dist.numel() > 0 else torch.tensor(0.0)
penetration_value = penetration_score.item()
penetration_list.append(penetration_value)
per_motion_metrics.append(
{
"id": prompt_dict.get("id"),
"prompt": prompt_dict.get("prompt"),
"value": penetration_value,
"evaluation_file": evaluation_file,
"motion_duration": prompt_dict.get("motion_duration"),
}
)
return {
"aggregate": summarize_scores(penetration_list),
"per_motion": per_motion_metrics,
}
def compute_foot_floating(full_info_path: str, device: str, **kwargs):
"""
Check for foot floating in the motion data.
"""
prompt_dict_ls = load_dimension_info(full_info_path, dimension='Foot_Floating')
floating_list = []
per_motion_metrics = []
for prompt_dict in tqdm(prompt_dict_ls):
evaluation_file = prompt_dict["evaluation_file"]
pred_joints = load_joints(evaluation_file, device)
contact = get_contact(pred_joints, device=device)
frames = pred_joints.shape[0]
delta_ts = 0.001
rate_ts = 0.6
rate_high_ts = 1.75
# Root position and velocity
root_pos = pred_joints[:, 0]
root_vel = root_pos[1:] - root_pos[:-1]
root_vel = torch.cat([root_vel, root_vel[-1:]], dim=0)
# Foot positions and velocities
foot_pos = pred_joints[:, FOOT_IDX] # (frames, 2, 3)
foot_vel = foot_pos[1:] - foot_pos[:-1]
foot_vel = torch.cat([foot_vel, foot_vel[-1:]], dim=0)
# Relative foot positions and velocities
rel_foot_pos = foot_pos - root_pos.unsqueeze(1)
rel_foot_vel = rel_foot_pos[1:] - rel_foot_pos[:-1]
rel_foot_vel = torch.cat([rel_foot_vel, rel_foot_vel[-1:]], dim=0)
# Check frame floating
left_foot_fl_rate = torch.zeros((frames, 1)).to(device)
right_foot_fl_rate = torch.zeros((frames, 1)).to(device)
invalid_flag = torch.ones((frames, 2)).to(device)
for f in range(frames):
root_dis = torch.norm(root_vel[f], p=2, dim=-1)
left_parent_dis = torch.norm(rel_foot_vel[f, 0], p=2, dim=-1)
right_parent_dis = torch.norm(rel_foot_vel[f, 1], p=2, dim=-1)
rate_left = left_parent_dis / (root_dis + 1e-6)
rate_right = right_parent_dis / (root_dis + 1e-6)
left_foot_fl_rate[f] = rate_left
right_foot_fl_rate[f] = rate_right
left_foot_dis = torch.norm(foot_vel[f, 0], p=2, dim=-1)
right_foot_dis = torch.norm(foot_vel[f, 1], p=2, dim=-1)
if root_dis < delta_ts:
continue
lf_l_invalid = rate_left < rate_ts and left_foot_dis > 1.2e-4
lf_h_invalid = rate_left > rate_high_ts and left_foot_dis > 1.2e-4
lf_invalid = lf_l_invalid or (lf_h_invalid and root_dis > 1.2e-4)
rf_l_invalid = rate_right < rate_ts and right_foot_dis > 1.2e-4
rf_h_invalid = rate_right > rate_high_ts and right_foot_dis > 1.2e-4
rf_invalid = rf_l_invalid or (rf_h_invalid and root_dis > 1.2e-4)
if torch.sum(contact[f]) == 2 and lf_invalid and rf_invalid:
invalid_flag[f, 0] = 0
invalid_flag[f, 1] = 0
elif contact[f, 0] == 1 and contact[f, 1] == 0 and lf_invalid:
invalid_flag[f, 0] = 0
elif contact[f, 1] == 1 and contact[f, 0] == 0 and rf_invalid:
invalid_flag[f, 1] = 0
# Get not contact range
all_rates = torch.cat([left_foot_fl_rate, right_foot_fl_rate], dim=-1)
no_contact_range = get_range(contact, False)
# Check sequence floating
floating_range_lens = [0]
for i in range(len(no_contact_range)):
for j, rge in enumerate(no_contact_range[i]):
rates = all_rates[rge[0]:rge[1]+1, i]
if len(rates) < 4:
continue
skip_n = 0
for f in range(rge[0], rge[1]+1):
if torch.norm(root_vel[f], p=2, dim=-1) < delta_ts:
skip_n += 1
if skip_n / (rge[1] - rge[0] + 1) > 0.5:
continue
cur_invalid_flag = rates < (rate_ts - 0.2)
diff = torch.diff(torch.cat(
[torch.tensor([0]).to(device),
cur_invalid_flag.float(),
torch.tensor([0]).to(device)]))
start_indices = torch.where(diff == 1)[0]
end_indices = torch.where(diff == -1)[0]
if len(start_indices) != 0:
lengths = end_indices - start_indices
floating_range_lens.extend(lengths.tolist())
# Check mass floating
mass_floating_len_list = []
if len(no_contact_range[0]) != 0 and len(no_contact_range[1]) != 0:
merged_no_contact_range = find_common_intervals(
no_contact_range[0], no_contact_range[1])
for i, rge in enumerate(merged_no_contact_range):
start, end = rge
start, end = int(start), int(end)
if (end - start + 1) < 4:
continue
l_start_end_vec = foot_pos[end, 0] - foot_pos[start, 0]
agl_list = []
for f in range(start + 1, end + 1):
l_start_cur_vec = foot_pos[f, 0] - foot_pos[start, 0]
vec_angle = torch.rad2deg(torch.abs(
calculate_angle(l_start_cur_vec, l_start_end_vec)))
agl_list.append(vec_angle.detach().cpu().numpy())
peaks, _ = find_peaks(agl_list)
if len(peaks) > 2:
mass_floating_len_list.append(end - start + 1)
# Check valid
merge_invalid_flag = invalid_flag[:, 0] + invalid_flag[:, 1]
merge_invalid_flag = merge_invalid_flag <= 1
invalid_n = len(merge_invalid_flag[merge_invalid_flag])
invalid_n += sum(floating_range_lens) / 2
invalid_n += sum(mass_floating_len_list)
floating_score = invalid_n / frames
floating_value = float(floating_score)
floating_list.append(floating_value)
per_motion_metrics.append(
{
"id": prompt_dict.get("id"),
"prompt": prompt_dict.get("prompt"),
"value": floating_value,
"evaluation_file": evaluation_file,
"motion_duration": prompt_dict.get("motion_duration"),
}
)
return {
"aggregate": summarize_scores(floating_list),
"per_motion": per_motion_metrics,
}
def compute_foot_sliding(full_info_path: str, device: str, **kwargs):
"""
Check for foot sliding in the motion data.
"""
prompt_dict_ls = load_dimension_info(full_info_path, dimension='Foot_Sliding')
sliding_list = []
per_motion_metrics = []
for prompt_dict in tqdm(prompt_dict_ls):
evaluation_file = prompt_dict["evaluation_file"]
pred_joints = load_joints(evaluation_file, device)
contact = get_contact(pred_joints, device=device)
foot_pos = pred_joints[:, FOOT_IDX] # (frames, 2, 3)
# Compute foot velocity and delta joints
foot_vel = foot_pos[1:] - foot_pos[:-1]
foot_vel = torch.cat([foot_vel, foot_vel[-1:]], dim=0)
foot_delta = torch.norm(foot_vel[:, :, :2], dim=-1)
# Calculate sliding distance for left and right foot
left_sliding_dis = (foot_delta[:, 0] * contact[:, 0]).sum(dim=0) / ((contact[:, 0]).sum(dim=0) + 1e-6)
right_sliding_dis = (foot_delta[:, 1] * contact[:, 1]).sum(dim=0) / ((contact[:, 1]).sum(dim=0) + 1e-6)
sliding_score = (left_sliding_dis + right_sliding_dis) / 2
sliding_value = sliding_score.item()
sliding_list.append(sliding_value)
per_motion_metrics.append(
{
"id": prompt_dict.get("id"),
"prompt": prompt_dict.get("prompt"),
"value": sliding_value,
"evaluation_file": evaluation_file,
"motion_duration": prompt_dict.get("motion_duration"),
}
)
return {
"aggregate": summarize_scores(sliding_list),
"per_motion": per_motion_metrics,
}
def compute_dynamic_degree(full_info_path: str, device: str, **kwargs):
"""
Compute the dynamic degree of the motion based on the average velocity of the joints.
"""
prompt_dict_ls = load_dimension_info(full_info_path, dimension='Dynamic_Degree')
dynamic_degree_list = []
per_motion_metrics = []
for prompt_dict in tqdm(prompt_dict_ls):
evaluation_file = prompt_dict["evaluation_file"]
pred_joints = load_joints(evaluation_file, device)
# Global dynamic degree
velocity = torch.norm(pred_joints[1:] - pred_joints[:-1], dim=2) # Shape: (T-1, 24)
global_dynamic = velocity.mean()
# Local dynamic degree (remove global translation)
local_joints = remove_global_translation(pred_joints)
local_velocity = torch.norm(local_joints[1:] - local_joints[:-1], dim=2) # Shape: (T-1, 24)
local_dynamic = local_velocity.mean()
# Combined dynamic degree
combined_dynamic = global_dynamic + local_dynamic
dynamic_value = combined_dynamic.item()
dynamic_degree_list.append(dynamic_value)
per_motion_metrics.append(
{
"id": prompt_dict.get("id"),
"prompt": prompt_dict.get("prompt"),
"value": dynamic_value,
"evaluation_file": evaluation_file,
"motion_duration": prompt_dict.get("motion_duration"),
}
)
return {
"aggregate": summarize_scores(dynamic_degree_list),
"per_motion": per_motion_metrics,
}