forked from CalciferZh/SMPL
-
Notifications
You must be signed in to change notification settings - Fork 8
/
smpl_torch.py
208 lines (175 loc) · 6.53 KB
/
smpl_torch.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
import numpy as np
import pickle
import torch
from torch.nn import Module
import os
class SMPLModel(Module):
def __init__(self, device=None, model_path='./model.pkl'):
super(SMPLModel, self).__init__()
with open(model_path, 'rb') as f:
params = pickle.load(f)
self.J_regressor = torch.from_numpy(
np.array(params['J_regressor'].todense())
).type(torch.float64)
self.weights = torch.from_numpy(params['weights']).type(torch.float64)
self.posedirs = torch.from_numpy(params['posedirs']).type(torch.float64)
self.v_template = torch.from_numpy(params['v_template']).type(torch.float64)
self.shapedirs = torch.from_numpy(params['shapedirs']).type(torch.float64)
self.kintree_table = params['kintree_table']
self.faces = params['f']
self.device = device if device is not None else torch.device('cpu')
for name in ['J_regressor', 'weights', 'posedirs', 'v_template', 'shapedirs']:
_tensor = getattr(self, name)
setattr(self, name, _tensor.to(device))
@staticmethod
def rodrigues(r):
"""
Rodrigues' rotation formula that turns axis-angle tensor into rotation
matrix in a batch-ed manner.
Parameter:
----------
r: Axis-angle rotation tensor of shape [batch_size, 1, 3].
Return:
-------
Rotation matrix of shape [batch_size, 3, 3].
"""
#r = r.to(self.device)
eps = r.clone().normal_(std=1e-8)
theta = torch.norm(r + eps, dim=(1, 2), keepdim=True) # dim cannot be tuple
theta_dim = theta.shape[0]
r_hat = r / theta
cos = torch.cos(theta)
z_stick = torch.zeros(theta_dim, dtype=torch.float64).to(r.device)
m = torch.stack(
(z_stick, -r_hat[:, 0, 2], r_hat[:, 0, 1], r_hat[:, 0, 2], z_stick,
-r_hat[:, 0, 0], -r_hat[:, 0, 1], r_hat[:, 0, 0], z_stick), dim=1)
m = torch.reshape(m, (-1, 3, 3))
i_cube = (torch.eye(3, dtype=torch.float64).unsqueeze(dim=0) \
+ torch.zeros((theta_dim, 3, 3), dtype=torch.float64)).to(r.device)
A = r_hat.permute(0, 2, 1)
dot = torch.matmul(A, r_hat)
R = cos * i_cube + (1 - cos) * dot + torch.sin(theta) * m
return R
@staticmethod
def with_zeros(x):
"""
Append a [0, 0, 0, 1] tensor to a [3, 4] tensor.
Parameter:
---------
x: Tensor to be appended.
Return:
------
Tensor after appending of shape [4,4]
"""
ones = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float64).to(x.device)
ret = torch.cat((x, ones), dim=0)
return ret
@staticmethod
def pack(x):
"""
Append zero tensors of shape [4, 3] to a batch of [4, 1] shape tensor.
Parameter:
----------
x: A tensor of shape [batch_size, 4, 1]
Return:
------
A tensor of shape [batch_size, 4, 4] after appending.
"""
zeros43 = torch.zeros((x.shape[0], 4, 3), dtype=torch.float64).to(x.device)
ret = torch.cat((zeros43, x), dim=2)
return ret
def write_obj(self, verts, file_name):
with open(file_name, 'w') as fp:
for v in verts:
fp.write('v %f %f %f\n' % (v[0], v[1], v[2]))
for f in self.faces + 1:
fp.write('f %d %d %d\n' % (f[0], f[1], f[2]))
def forward(self, betas, pose, trans, simplify=False):
"""
Construct a compute graph that takes in parameters and outputs a tensor as
model vertices. Face indices are also returned as a numpy ndarray.
Prameters:
---------
pose: Also known as 'theta', a [24,3] tensor indicating child joint rotation
relative to parent joint. For root joint it's global orientation.
Represented in a axis-angle format.
betas: Parameter for model shape. A tensor of shape [10] as coefficients of
PCA components. Only 10 components were released by SMPL author.
trans: Global translation tensor of shape [3].
Return:
------
A tensor for vertices, and a numpy ndarray as face indices.
"""
id_to_col = {self.kintree_table[1, i]: i
for i in range(self.kintree_table.shape[1])}
parent = {
i: id_to_col[self.kintree_table[0, i]]
for i in range(1, self.kintree_table.shape[1])
}
v_shaped = torch.tensordot(self.shapedirs, betas, dims=([2], [0])) + self.v_template
J = torch.matmul(self.J_regressor, v_shaped)
R_cube_big = self.rodrigues(pose.view(-1, 1, 3))
if simplify:
v_posed = v_shaped
else:
R_cube = R_cube_big[1:]
I_cube = (torch.eye(3, dtype=torch.float64).unsqueeze(dim=0) + \
torch.zeros((R_cube.shape[0], 3, 3), dtype=torch.float64)).to(self.device)
lrotmin = torch.reshape(R_cube - I_cube, (-1, 1)).squeeze()
v_posed = v_shaped + torch.tensordot(self.posedirs, lrotmin, dims=([2], [0]))
results = []
results.append(
self.with_zeros(torch.cat((R_cube_big[0], torch.reshape(J[0, :], (3, 1))), dim=1))
)
for i in range(1, self.kintree_table.shape[1]):
results.append(
torch.matmul(
results[parent[i]],
self.with_zeros(
torch.cat(
(R_cube_big[i], torch.reshape(J[i, :] - J[parent[i], :], (3, 1))),
dim=1
)
)
)
)
stacked = torch.stack(results, dim=0)
results = stacked - \
self.pack(
torch.matmul(
stacked,
torch.reshape(
torch.cat((J, torch.zeros((24, 1), dtype=torch.float64).to(self.device)), dim=1),
(24, 4, 1)
)
)
)
T = torch.tensordot(self.weights, results, dims=([1], [0]))
rest_shape_h = torch.cat(
(v_posed, torch.ones((v_posed.shape[0], 1), dtype=torch.float64).to(self.device)), dim=1
)
v = torch.matmul(T, torch.reshape(rest_shape_h, (-1, 4, 1)))
v = torch.reshape(v, (-1, 4))[:, :3]
result = v + torch.reshape(trans, (1, 3))
return result
def test_gpu(gpu_id=[0]):
if len(gpu_id) > 0 and torch.cuda.is_available():
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id[0])
device = torch.device('cuda')
else:
device = torch.device('cpu')
print(device)
pose_size = 72
beta_size = 10
np.random.seed(9608)
pose = torch.from_numpy((np.random.rand(pose_size) - 0.5) * 0.4)\
.type(torch.float64).to(device)
betas = torch.from_numpy((np.random.rand(beta_size) - 0.5) * 0.06) \
.type(torch.float64).to(device)
trans = torch.from_numpy(np.zeros(3)).type(torch.float64).to(device)
outmesh_path = './smpl_torch.obj'
model = SMPLModel(device=device)
result = model(betas, pose, trans)
model.write_obj(result, outmesh_path)
if __name__ == '__main__':
test_gpu([1])