-
Notifications
You must be signed in to change notification settings - Fork 139
/
smpl_torch_batch.py
233 lines (199 loc) · 7.69 KB
/
smpl_torch_batch.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
import numpy as np
import pickle
import torch
from torch.nn import Module
import os
from time import time
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)
if 'joint_regressor' in params.keys():
self.joint_regressor = torch.from_numpy(
np.array(params['joint_regressor'].T.todense())
).type(torch.float64)
else:
self.joint_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', 'joint_regressor', 'weights', 'posedirs', 'v_template', 'shapedirs']:
_tensor = getattr(self, name)
print(' Tensor {} shape: '.format(name), _tensor.shape)
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 * angle_num, 1, 3].
Return:
-------
Rotation matrix of shape [batch_size * angle_num, 3, 3].
"""
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
).expand(x.shape[0],-1,-1).to(x.device)
ret = torch.cat((x, ones), dim=1)
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], x.shape[1], 4, 3), dtype=torch.float64).to(x.device)
ret = torch.cat((zeros43, x), dim=3)
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.
20190128: Add batch support.
Parameters:
---------
pose: Also known as 'theta', an [N, 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 [N, 10] as coefficients of
PCA components. Only 10 components were released by SMPL author.
trans: Global translation tensor of shape [N, 3].
Return:
------
A 3-D tensor of [N * 6890 * 3] for vertices,
and the corresponding [N * 19 * 3] joint positions.
"""
batch_num = betas.shape[0]
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(betas, self.shapedirs, dims=([1], [2])) + self.v_template
J = torch.matmul(self.J_regressor, v_shaped)
R_cube_big = self.rodrigues(pose.view(-1, 1, 3)).reshape(batch_num, -1, 3, 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((batch_num, R_cube.shape[1], 3, 3), dtype=torch.float64)).to(self.device)
lrotmin = (R_cube - I_cube).reshape(batch_num, -1, 1).squeeze(dim=2)
v_posed = v_shaped + torch.tensordot(lrotmin, self.posedirs, dims=([1], [2]))
results = []
results.append(
self.with_zeros(torch.cat((R_cube_big[:, 0], torch.reshape(J[:, 0, :], (-1, 3, 1))), dim=2))
)
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], :], (-1, 3, 1))),
dim=2
)
)
)
)
stacked = torch.stack(results, dim=1)
results = stacked - \
self.pack(
torch.matmul(
stacked,
torch.reshape(
torch.cat((J, torch.zeros((batch_num, 24, 1), dtype=torch.float64).to(self.device)), dim=2),
(batch_num, 24, 4, 1)
)
)
)
# Restart from here
T = torch.tensordot(results, self.weights, dims=([1], [1])).permute(0, 3, 1, 2)
rest_shape_h = torch.cat(
(v_posed, torch.ones((batch_num, v_posed.shape[1], 1), dtype=torch.float64).to(self.device)), dim=2
)
v = torch.matmul(T, torch.reshape(rest_shape_h, (batch_num, -1, 4, 1)))
v = torch.reshape(v, (batch_num, -1, 4))[:, :, :3]
result = v + torch.reshape(trans, (batch_num, 1, 3))
# estimate 3D joint locations
# print(result.shape)
# print(self.joint_regressor.shape)
joints = torch.tensordot(result, self.joint_regressor, dims=([1], [0])).transpose(1, 2)
return result, joints
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)
model = SMPLModel(device=device)
for i in range(10):
pose = torch.from_numpy((np.random.rand(32, pose_size) - 0.5) * 0.4)\
.type(torch.float64).to(device)
betas = torch.from_numpy((np.random.rand(32, beta_size) - 0.5) * 0.06) \
.type(torch.float64).to(device)
s = time()
trans = torch.from_numpy(np.zeros((32, 3))).type(torch.float64).to(device)
result, joints = model(betas, pose, trans)
print(time() - s)
# outmesh_path = './smpl_torch_{}.obj'
# for i in range(result.shape[0]):
# model.write_obj(result[i], outmesh_path.format(i))
if __name__ == '__main__':
test_gpu([1])