-
Notifications
You must be signed in to change notification settings - Fork 44
/
motion_lib_base.py
573 lines (448 loc) · 23.8 KB
/
motion_lib_base.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import glob
import os
import sys
import pdb
import os.path as osp
sys.path.append(os.getcwd())
import numpy as np
import os
import yaml
from tqdm import tqdm
from phc.utils import torch_utils
import joblib
import torch
from poselib.poselib.skeleton.skeleton3d import SkeletonMotion, SkeletonState
import torch.multiprocessing as mp
import gc
from scipy.spatial.transform import Rotation as sRot
import random
from phc.utils.flags import flags
from enum import Enum
import resource
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, rlimit[1]))
USE_CACHE = False
print("MOVING MOTION DATA TO GPU, USING CACHE:", USE_CACHE)
class FixHeightMode(Enum):
no_fix = 0
full_fix = 1
ankle_fix = 2
if not USE_CACHE:
old_numpy = torch.Tensor.numpy
class Patch:
def numpy(self):
if self.is_cuda:
return self.to("cpu").numpy()
else:
return old_numpy(self)
torch.Tensor.numpy = Patch.numpy
def local_rotation_to_dof_vel(local_rot0, local_rot1, dt):
# Assume each joint is 3dof
diff_quat_data = torch_utils.quat_mul(torch_utils.quat_conjugate(local_rot0), local_rot1)
diff_angle, diff_axis = torch_utils.quat_to_angle_axis(diff_quat_data)
dof_vel = diff_axis * diff_angle.unsqueeze(-1) / dt
return dof_vel[1:, :].flatten()
def compute_motion_dof_vels(motion):
num_frames = motion.tensor.shape[0]
dt = 1.0 / motion.fps
dof_vels = []
for f in range(num_frames - 1):
local_rot0 = motion.local_rotation[f]
local_rot1 = motion.local_rotation[f + 1]
frame_dof_vel = local_rotation_to_dof_vel(local_rot0, local_rot1, dt)
dof_vels.append(frame_dof_vel)
dof_vels.append(dof_vels[-1])
dof_vels = torch.stack(dof_vels, dim=0).view(num_frames, -1, 3)
return dof_vels
class DeviceCache:
def __init__(self, obj, device):
self.obj = obj
self.device = device
keys = dir(obj)
num_added = 0
for k in keys:
try:
out = getattr(obj, k)
except:
# print("Error for key=", k)
continue
if isinstance(out, torch.Tensor):
if out.is_floating_point():
out = out.to(self.device, dtype=torch.float32)
else:
out.to(self.device)
setattr(self, k, out)
num_added += 1
elif isinstance(out, np.ndarray):
out = torch.tensor(out)
if out.is_floating_point():
out = out.to(self.device, dtype=torch.float32)
else:
out.to(self.device)
setattr(self, k, out)
num_added += 1
# print("Total added", num_added)
def __getattr__(self, string):
out = getattr(self.obj, string)
return out
class MotionlibMode(Enum):
file = 1
directory = 2
class MotionLibBase():
def __init__(self, motion_file, device, fix_height=FixHeightMode.full_fix, masterfoot_conifg=None, min_length=-1, im_eval=False, multi_thread=True):
self._device = device
self.mesh_parsers = None
self.load_data(motion_file, min_length = min_length, im_eval = im_eval)
self.setup_constants(fix_height = fix_height, masterfoot_conifg = masterfoot_conifg, multi_thread = multi_thread)
if flags.real_traj:
if self._masterfoot_conifg is None:
self.track_idx = self._motion_data_load[next(iter(self._motion_data_load))].get("track_idx", [13, 18, 23])
else:
self.track_idx = self._motion_data_load[next(iter(self._motion_data_load))].get("track_idx", [19, 24, 29])
return
def load_data(self, motion_file, min_length=-1, im_eval = False):
if osp.isfile(motion_file):
self.mode = MotionlibMode.file
self._motion_data_load = joblib.load(motion_file)
else:
self.mode = MotionlibMode.directory
self._motion_data_load = glob.glob(osp.join(motion_file, "*.pkl"))
data_list = self._motion_data_load
if self.mode == MotionlibMode.file:
if min_length != -1:
data_list = {k: v for k, v in list(self._motion_data_load.items()) if len(v['pose_quat_global']) >= min_length}
elif im_eval:
data_list = {item[0]: item[1] for item in sorted(self._motion_data_load.items(), key=lambda entry: len(entry[1]['pose_quat_global']), reverse=True)}
# data_list = self._motion_data
else:
data_list = self._motion_data_load
self._motion_data_list = np.array(list(data_list.values()))
self._motion_data_keys = np.array(list(data_list.keys()))
else:
self._motion_data_list = np.array(self._motion_data_load)
self._motion_data_keys = np.array(self._motion_data_load)
self._num_unique_motions = len(self._motion_data_list)
if self.mode == MotionlibMode.directory:
self._motion_data_load = joblib.load(self._motion_data_load[0]) # set self._motion_data_load to a sample of the data
def setup_constants(self, fix_height = FixHeightMode.full_fix, masterfoot_conifg=None, multi_thread = True):
self._masterfoot_conifg = masterfoot_conifg
self.fix_height = fix_height
self.multi_thread = multi_thread
#### Termination history
self._curr_motion_ids = None
self._termination_history = torch.zeros(self._num_unique_motions).to(self._device)
self._success_rate = torch.zeros(self._num_unique_motions).to(self._device)
self._sampling_history = torch.zeros(self._num_unique_motions).to(self._device)
self._sampling_prob = torch.ones(self._num_unique_motions).to(self._device) / self._num_unique_motions # For use in sampling batches
self._sampling_batch_prob = None # For use in sampling within batches
@staticmethod
def load_motion_with_skeleton(ids, motion_data_list, skeleton_trees, gender_betas, fix_height, mesh_parsers, masterfoot_config, max_len, queue, pid):
raise NotImplementedError
@staticmethod
def fix_trans_height(pose_aa, trans, curr_gender_betas, mesh_parsers, fix_height_mode):
raise NotImplementedError
def load_motions(self, skeleton_trees, gender_betas, limb_weights, random_sample=True, start_idx=0, max_len=-1):
# load motion load the same number of motions as there are skeletons (humanoids)
if "gts" in self.__dict__:
del self.gts, self.grs, self.lrs, self.grvs, self.gravs, self.gavs, self.gvs, self.dvs,
del self._motion_lengths, self._motion_fps, self._motion_dt, self._motion_num_frames, self._motion_bodies, self._motion_aa
if flags.real_traj:
del self.q_gts, self.q_grs, self.q_gavs, self.q_gvs
motions = []
self._motion_lengths = []
self._motion_fps = []
self._motion_dt = []
self._motion_num_frames = []
self._motion_bodies = []
self._motion_aa = []
if flags.real_traj:
self.q_gts, self.q_grs, self.q_gavs, self.q_gvs = [], [], [], []
torch.cuda.empty_cache()
gc.collect()
total_len = 0.0
self.num_joints = len(skeleton_trees[0].node_names)
num_motion_to_load = len(skeleton_trees)
if random_sample:
sample_idxes = torch.multinomial(self._sampling_prob, num_samples=num_motion_to_load, replacement=True).to(self._device)
else:
sample_idxes = torch.remainder(torch.arange(len(skeleton_trees)) + start_idx, self._num_unique_motions ).to(self._device)
# import ipdb; ipdb.set_trace()
self._curr_motion_ids = sample_idxes
self.one_hot_motions = torch.nn.functional.one_hot(self._curr_motion_ids, num_classes = self._num_unique_motions).to(self._device) # Testing for obs_v5
self.curr_motion_keys = self._motion_data_keys[sample_idxes]
self._sampling_batch_prob = self._sampling_prob[self._curr_motion_ids] / self._sampling_prob[self._curr_motion_ids].sum()
print("\n****************************** Current motion keys ******************************")
print("Sampling motion:", sample_idxes[:30])
if len(self.curr_motion_keys) < 100:
print(self.curr_motion_keys)
else:
print(self.curr_motion_keys[:30], ".....")
print("*********************************************************************************\n")
motion_data_list = self._motion_data_list[sample_idxes.cpu().numpy()]
mp.set_sharing_strategy('file_descriptor')
# mp.set_sharing_strategy('file_system')
manager = mp.Manager()
queue = manager.Queue(maxsize=1)
num_jobs = min(mp.cpu_count(), 64)
if num_jobs <= 8 or not self.multi_thread:
num_jobs = 1
if flags.debug:
num_jobs = 1
res_acc = {} # using dictionary ensures order of the results.
jobs = motion_data_list
chunk = np.ceil(len(jobs) / num_jobs).astype(int)
ids = np.arange(len(jobs))
jobs = [(ids[i:i + chunk], jobs[i:i + chunk], skeleton_trees[i:i + chunk], gender_betas[i:i + chunk], self.fix_height, self.mesh_parsers, self._masterfoot_conifg, max_len) for i in range(0, len(jobs), chunk)]
job_args = [jobs[i] for i in range(len(jobs))]
for i in range(1, len(jobs)):
worker_args = (*job_args[i], queue, i)
worker = mp.Process(target=self.load_motion_with_skeleton, args=worker_args)
worker.start()
res_acc.update(self.load_motion_with_skeleton(*jobs[0], None, 0))
for i in tqdm(range(len(jobs) - 1)):
res = queue.get()
res_acc.update(res)
for f in tqdm(range(len(res_acc))):
motion_file_data, curr_motion = res_acc[f]
if USE_CACHE:
curr_motion = DeviceCache(curr_motion, self._device)
motion_fps = curr_motion.fps
curr_dt = 1.0 / motion_fps
num_frames = curr_motion.tensor.shape[0]
curr_len = 1.0 / motion_fps * (num_frames - 1)
if "beta" in motion_file_data:
self._motion_aa.append(motion_file_data['pose_aa'].reshape(-1, self.num_joints * 3))
self._motion_bodies.append(curr_motion.gender_beta)
else:
self._motion_aa.append(np.zeros((num_frames, self.num_joints * 3)))
self._motion_bodies.append(torch.zeros(17))
self._motion_fps.append(motion_fps)
self._motion_dt.append(curr_dt)
self._motion_num_frames.append(num_frames)
motions.append(curr_motion)
self._motion_lengths.append(curr_len)
if flags.real_traj:
self.q_gts.append(curr_motion.quest_motion['quest_trans'])
self.q_grs.append(curr_motion.quest_motion['quest_rot'])
self.q_gavs.append(curr_motion.quest_motion['global_angular_vel'])
self.q_gvs.append(curr_motion.quest_motion['linear_vel'])
del curr_motion
self._motion_lengths = torch.tensor(self._motion_lengths, device=self._device, dtype=torch.float32)
self._motion_fps = torch.tensor(self._motion_fps, device=self._device, dtype=torch.float32)
self._motion_bodies = torch.stack(self._motion_bodies).to(self._device).type(torch.float32)
self._motion_aa = torch.tensor(np.concatenate(self._motion_aa), device=self._device, dtype=torch.float32)
self._motion_dt = torch.tensor(self._motion_dt, device=self._device, dtype=torch.float32)
self._motion_num_frames = torch.tensor(self._motion_num_frames, device=self._device)
self._motion_limb_weights = torch.tensor(np.array(limb_weights), device=self._device, dtype=torch.float32)
self._num_motions = len(motions)
self.gts = torch.cat([m.global_translation for m in motions], dim=0).float().to(self._device)
self.grs = torch.cat([m.global_rotation for m in motions], dim=0).float().to(self._device)
self.lrs = torch.cat([m.local_rotation for m in motions], dim=0).float().to(self._device)
self.grvs = torch.cat([m.global_root_velocity for m in motions], dim=0).float().to(self._device)
self.gravs = torch.cat([m.global_root_angular_velocity for m in motions], dim=0).float().to(self._device)
self.gavs = torch.cat([m.global_angular_velocity for m in motions], dim=0).float().to(self._device)
self.gvs = torch.cat([m.global_velocity for m in motions], dim=0).float().to(self._device)
self.dvs = torch.cat([m.dof_vels for m in motions], dim=0).float().to(self._device)
if flags.real_traj:
self.q_gts = torch.cat(self.q_gts, dim=0).float().to(self._device)
self.q_grs = torch.cat(self.q_grs, dim=0).float().to(self._device)
self.q_gavs = torch.cat(self.q_gavs, dim=0).float().to(self._device)
self.q_gvs = torch.cat(self.q_gvs, dim=0).float().to(self._device)
lengths = self._motion_num_frames
lengths_shifted = lengths.roll(1)
lengths_shifted[0] = 0
self.length_starts = lengths_shifted.cumsum(0)
self.motion_ids = torch.arange(len(motions), dtype=torch.long, device=self._device)
motion = motions[0]
self.num_bodies = motion.num_joints
num_motions = self.num_motions()
total_len = self.get_total_length()
print(f"Loaded {num_motions:d} motions with a total length of {total_len:.3f}s and {self.gts.shape[0]} frames.")
return motions
def num_motions(self):
return self._num_motions
def get_total_length(self):
return sum(self._motion_lengths)
# def update_sampling_weight(self):
# ## sampling weight based on success rate.
# # sampling_temp = 0.2
# sampling_temp = 0.1
# curr_termination_prob = 0.5
# curr_succ_rate = 1 - self._termination_history[self._curr_motion_ids] / self._sampling_history[self._curr_motion_ids]
# self._success_rate[self._curr_motion_ids] = curr_succ_rate
# sample_prob = torch.exp(-self._success_rate / sampling_temp)
# self._sampling_prob = sample_prob / sample_prob.sum()
# self._termination_history[self._curr_motion_ids] = 0
# self._sampling_history[self._curr_motion_ids] = 0
# topk_sampled = self._sampling_prob.topk(50)
# print("Current most sampled", self._motion_data_keys[topk_sampled.indices.cpu().numpy()])
def update_hard_sampling_weight(self, failed_keys):
# sampling weight based on evaluation, only trained on "failed" sequences. Auto PMCP.
if len(failed_keys) > 0:
all_keys = self._motion_data_keys.tolist()
indexes = [all_keys.index(k) for k in failed_keys]
self._sampling_prob[:] = 0
self._sampling_prob[indexes] = 1/len(indexes)
print("############################################################ Auto PMCP ############################################################")
print(f"Training on only {len(failed_keys)} seqs")
print(failed_keys)
else:
all_keys = self._motion_data_keys.tolist()
self._sampling_prob = torch.ones(self._num_unique_motions).to(self._device) / self._num_unique_motions # For use in sampling batches
def update_soft_sampling_weight(self, failed_keys):
# sampling weight based on evaluation, only "mostly" trained on "failed" sequences. Auto PMCP.
if len(failed_keys) > 0:
all_keys = self._motion_data_keys.tolist()
indexes = [all_keys.index(k) for k in failed_keys]
self._termination_history[indexes] += 1
self.update_sampling_prob(self._termination_history)
print("############################################################ Auto PMCP ############################################################")
print(f"Training mostly on {len(self._sampling_prob.nonzero())} seqs ")
print(self._motion_data_keys[self._sampling_prob.nonzero()].flatten())
print(f"###############################################################################################################################")
else:
all_keys = self._motion_data_keys.tolist()
self._sampling_prob = torch.ones(self._num_unique_motions).to(self._device) / self._num_unique_motions # For use in sampling batches
def update_sampling_prob(self, termination_history):
if len(termination_history) == len(self._termination_history):
self._sampling_prob[:] = termination_history/termination_history.sum()
self._termination_history = termination_history
return True
else:
return False
# def update_sampling_history(self, env_ids):
# self._sampling_history[self._curr_motion_ids[env_ids]] += 1
# # print("sampling history: ", self._sampling_history[self._curr_motion_ids])
# def update_termination_history(self, termination):
# self._termination_history[self._curr_motion_ids] += termination
# # print("termination history: ", self._termination_history[self._curr_motion_ids])
def sample_motions(self, n):
motion_ids = torch.multinomial(self._sampling_batch_prob, num_samples=n, replacement=True).to(self._device)
return motion_ids
def sample_time(self, motion_ids, truncate_time=None):
n = len(motion_ids)
phase = torch.rand(motion_ids.shape, device=self._device)
motion_len = self._motion_lengths[motion_ids]
if (truncate_time is not None):
assert (truncate_time >= 0.0)
motion_len -= truncate_time
motion_time = phase * motion_len
return motion_time.to(self._device)
def sample_time_interval(self, motion_ids, truncate_time=None):
phase = torch.rand(motion_ids.shape, device=self._device)
motion_len = self._motion_lengths[motion_ids]
if (truncate_time is not None):
assert (truncate_time >= 0.0)
motion_len -= truncate_time
curr_fps = 1 / 30
motion_time = ((phase * motion_len) / curr_fps).long() * curr_fps
return motion_time
def get_motion_length(self, motion_ids=None):
if motion_ids is None:
return self._motion_lengths
else:
return self._motion_lengths[motion_ids]
def get_motion_num_steps(self, motion_ids=None):
if motion_ids is None:
return (self._motion_num_frames * 30 / self._motion_fps).int()
else:
return (self._motion_num_frames[motion_ids] * 30 / self._motion_fps).int()
def get_motion_state(self, motion_ids, motion_times, offset=None):
n = len(motion_ids)
num_bodies = self._get_num_bodies()
motion_len = self._motion_lengths[motion_ids]
num_frames = self._motion_num_frames[motion_ids]
dt = self._motion_dt[motion_ids]
frame_idx0, frame_idx1, blend = self._calc_frame_blend(motion_times, motion_len, num_frames, dt)
# print("non_interval", frame_idx0, frame_idx1)
f0l = frame_idx0 + self.length_starts[motion_ids]
f1l = frame_idx1 + self.length_starts[motion_ids]
local_rot0 = self.lrs[f0l]
local_rot1 = self.lrs[f1l]
body_vel0 = self.gvs[f0l]
body_vel1 = self.gvs[f1l]
body_ang_vel0 = self.gavs[f0l]
body_ang_vel1 = self.gavs[f1l]
rg_pos0 = self.gts[f0l, :]
rg_pos1 = self.gts[f1l, :]
dof_vel0 = self.dvs[f0l]
dof_vel1 = self.dvs[f1l]
vals = [local_rot0, local_rot1, body_vel0, body_vel1, body_ang_vel0, body_ang_vel1, rg_pos0, rg_pos1, dof_vel0, dof_vel1]
for v in vals:
assert v.dtype != torch.float64
blend = blend.unsqueeze(-1)
blend_exp = blend.unsqueeze(-1)
if offset is None:
rg_pos = (1.0 - blend_exp) * rg_pos0 + blend_exp * rg_pos1 # ZL: apply offset
else:
rg_pos = (1.0 - blend_exp) * rg_pos0 + blend_exp * rg_pos1 + offset[..., None, :] # ZL: apply offset
body_vel = (1.0 - blend_exp) * body_vel0 + blend_exp * body_vel1
body_ang_vel = (1.0 - blend_exp) * body_ang_vel0 + blend_exp * body_ang_vel1
dof_vel = (1.0 - blend_exp) * dof_vel0 + blend_exp * dof_vel1
local_rot = torch_utils.slerp(local_rot0, local_rot1, torch.unsqueeze(blend, axis=-1))
dof_pos = self._local_rotation_to_dof_smpl(local_rot)
rb_rot0 = self.grs[f0l]
rb_rot1 = self.grs[f1l]
rb_rot = torch_utils.slerp(rb_rot0, rb_rot1, blend_exp)
if flags.real_traj:
q_body_ang_vel0, q_body_ang_vel1 = self.q_gavs[f0l], self.q_gavs[f1l]
q_rb_rot0, q_rb_rot1 = self.q_grs[f0l], self.q_grs[f1l]
q_rg_pos0, q_rg_pos1 = self.q_gts[f0l, :], self.q_gts[f1l, :]
q_body_vel0, q_body_vel1 = self.q_gvs[f0l], self.q_gvs[f1l]
q_ang_vel = (1.0 - blend_exp) * q_body_ang_vel0 + blend_exp * q_body_ang_vel1
q_rb_rot = torch_utils.slerp(q_rb_rot0, q_rb_rot1, blend_exp)
q_rg_pos = (1.0 - blend_exp) * q_rg_pos0 + blend_exp * q_rg_pos1
q_body_vel = (1.0 - blend_exp) * q_body_vel0 + blend_exp * q_body_vel1
rg_pos[:, self.track_idx] = q_rg_pos
rb_rot[:, self.track_idx] = q_rb_rot
body_vel[:, self.track_idx] = q_body_vel
body_ang_vel[:, self.track_idx] = q_ang_vel
return {
"root_pos": rg_pos[..., 0, :].clone(),
"root_rot": rb_rot[..., 0, :].clone(),
"dof_pos": dof_pos.clone(),
"root_vel": body_vel[..., 0, :].clone(),
"root_ang_vel": body_ang_vel[..., 0, :].clone(),
"dof_vel": dof_vel.view(dof_vel.shape[0], -1),
"motion_aa": self._motion_aa[f0l],
"rg_pos": rg_pos,
"rb_rot": rb_rot,
"body_vel": body_vel,
"body_ang_vel": body_ang_vel,
"motion_bodies": self._motion_bodies[motion_ids],
"motion_limb_weights": self._motion_limb_weights[motion_ids],
}
def get_root_pos_smpl(self, motion_ids, motion_times):
n = len(motion_ids)
num_bodies = self._get_num_bodies()
motion_len = self._motion_lengths[motion_ids]
num_frames = self._motion_num_frames[motion_ids]
dt = self._motion_dt[motion_ids]
frame_idx0, frame_idx1, blend = self._calc_frame_blend(motion_times, motion_len, num_frames, dt)
# print("non_interval", frame_idx0, frame_idx1)
f0l = frame_idx0 + self.length_starts[motion_ids]
f1l = frame_idx1 + self.length_starts[motion_ids]
rg_pos0 = self.gts[f0l, :]
rg_pos1 = self.gts[f1l, :]
vals = [rg_pos0, rg_pos1]
for v in vals:
assert v.dtype != torch.float64
blend = blend.unsqueeze(-1)
blend_exp = blend.unsqueeze(-1)
rg_pos = (1.0 - blend_exp) * rg_pos0 + blend_exp * rg_pos1 # ZL: apply offset
return {"root_pos": rg_pos[..., 0, :].clone()}
def _calc_frame_blend(self, time, len, num_frames, dt):
time = time.clone()
phase = time / len
phase = torch.clip(phase, 0.0, 1.0) # clip time to be within motion length.
time[time < 0] = 0
frame_idx0 = (phase * (num_frames - 1)).long()
frame_idx1 = torch.min(frame_idx0 + 1, num_frames - 1)
blend = torch.clip((time - frame_idx0 * dt) / dt, 0.0, 1.0) # clip blend to be within 0 and 1
return frame_idx0, frame_idx1, blend
def _get_num_bodies(self):
return self.num_bodies
def _local_rotation_to_dof_smpl(self, local_rot):
B, J, _ = local_rot.shape
dof_pos = torch_utils.quat_to_exp_map(local_rot[:, 1:])
return dof_pos.reshape(B, -1)