Daankular commited on
Commit
7d6d7f7
·
verified ·
1 Parent(s): 3543950

Upload patches/MDM_rotation2xyz.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. patches/MDM_rotation2xyz.py +103 -0
patches/MDM_rotation2xyz.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This code is based on https://github.com/Mathux/ACTOR.git
2
+ # Patched: SMPL load wrapped in try/except — falls back to DummySMPL when
3
+ # gated SMPL weights are unavailable (Vast.ai / open deployments).
4
+ import torch
5
+ import utils.rotation_conversions as geometry
6
+
7
+
8
+ from model.smpl import SMPL, JOINTSTYPE_ROOT
9
+ # from .get_model import JOINTSTYPES
10
+ JOINTSTYPES = ["a2m", "a2mpl", "smpl", "vibe", "vertices"]
11
+
12
+
13
+ class Rotation2xyz:
14
+ def __init__(self, device, dataset='amass'):
15
+ self.device = device
16
+ self.dataset = dataset
17
+ try:
18
+ self.smpl_model = SMPL().eval().to(device)
19
+ except Exception as _e:
20
+ print(f'[MDM] SMPL unavailable ({_e}) -- rotation2xyz disabled')
21
+ class _DummySMPL:
22
+ num_betas = 10
23
+ def train(self, *a, **k): return self
24
+ def eval(self): return self
25
+ def to(self, *a, **k): return self
26
+ def __call__(self, *a, **k): raise RuntimeError('SMPL not loaded')
27
+ self.smpl_model = _DummySMPL()
28
+
29
+ def __call__(self, x, mask, pose_rep, translation, glob,
30
+ jointstype, vertstrans, betas=None, beta=0,
31
+ glob_rot=None, get_rotations_back=False, **kwargs):
32
+ if pose_rep == "xyz":
33
+ return x
34
+
35
+ if mask is None:
36
+ mask = torch.ones((x.shape[0], x.shape[-1]), dtype=bool, device=x.device)
37
+
38
+ if not glob and glob_rot is None:
39
+ raise TypeError("You must specify global rotation if glob is False")
40
+
41
+ if jointstype not in JOINTSTYPES:
42
+ raise NotImplementedError("This jointstype is not implemented.")
43
+
44
+ if translation:
45
+ x_translations = x[:, -1, :3]
46
+ x_rotations = x[:, :-1]
47
+ else:
48
+ x_rotations = x
49
+
50
+ x_rotations = x_rotations.permute(0, 3, 1, 2)
51
+ nsamples, time, njoints, feats = x_rotations.shape
52
+
53
+ # Compute rotations (convert only masked sequences output)
54
+ if pose_rep == "rotvec":
55
+ rotations = geometry.axis_angle_to_matrix(x_rotations[mask])
56
+ elif pose_rep == "rotmat":
57
+ rotations = x_rotations[mask].view(-1, njoints, 3, 3)
58
+ elif pose_rep == "rotquat":
59
+ rotations = geometry.quaternion_to_matrix(x_rotations[mask])
60
+ elif pose_rep == "rot6d":
61
+ rotations = geometry.rotation_6d_to_matrix(x_rotations[mask])
62
+ else:
63
+ raise NotImplementedError("No geometry for this one.")
64
+
65
+ if not glob:
66
+ global_orient = torch.tensor(glob_rot, device=x.device)
67
+ global_orient = geometry.axis_angle_to_matrix(global_orient).view(1, 1, 3, 3)
68
+ global_orient = global_orient.repeat(len(rotations), 1, 1, 1)
69
+ else:
70
+ global_orient = rotations[:, 0]
71
+ rotations = rotations[:, 1:]
72
+
73
+ if betas is None:
74
+ betas = torch.zeros([rotations.shape[0], self.smpl_model.num_betas],
75
+ dtype=rotations.dtype, device=rotations.device)
76
+ betas[:, 1] = beta
77
+ out = self.smpl_model(body_pose=rotations, global_orient=global_orient, betas=betas)
78
+
79
+ # get the desirable joints
80
+ joints = out[jointstype]
81
+
82
+ x_xyz = torch.empty(nsamples, time, joints.shape[1], 3, device=x.device, dtype=x.dtype)
83
+ x_xyz[~mask] = 0
84
+ x_xyz[mask] = joints
85
+
86
+ x_xyz = x_xyz.permute(0, 2, 3, 1).contiguous()
87
+
88
+ # the first translation root at the origin on the prediction
89
+ if jointstype != "vertices":
90
+ rootindex = JOINTSTYPE_ROOT[jointstype]
91
+ x_xyz = x_xyz - x_xyz[:, [rootindex], :, :]
92
+
93
+ if translation and vertstrans:
94
+ # the first translation root at the origin
95
+ x_translations = x_translations - x_translations[:, :, [0]]
96
+
97
+ # add the translation to all the joints
98
+ x_xyz = x_xyz + x_translations[:, None, :, :]
99
+
100
+ if get_rotations_back:
101
+ return x_xyz, rotations, global_orient
102
+ else:
103
+ return x_xyz