-
Notifications
You must be signed in to change notification settings - Fork 5
/
surfaceem.py
347 lines (277 loc) · 14.8 KB
/
surfaceem.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
import os
import pickle
import torch
import smplx
import numpy as np
from src.customloss import body_fitting_loss_em
from src.prior import MaxMixturePrior
# surface EM
class surface_EM_pt():
"""Implementation of SMPLify, use surface."""
def __init__(self,
smplxmodel,
step_size=1e-1,
batch_size=1,
num_iters=100,
selected_index=np.arange(6890),
use_collision=False,
device=torch.device('cuda:0'),
GMM_MODEL_DIR="./smpl_models/",
mu = 0.02
):
# Store options
self.batch_size = batch_size
self.device = device
self.step_size = step_size
self.num_iters = num_iters
# GMM pose prior
self.pose_prior = MaxMixturePrior(prior_folder=GMM_MODEL_DIR,
num_gaussians=8,
dtype=torch.float32).to(device)
# Load SMPL-X model
self.smpl = smplxmodel
self.modelfaces = torch.from_numpy(np.int32(smplxmodel.faces)).to(device)
self.selected_index = selected_index
# mesh intersection
self.model_faces = smplxmodel.faces_tensor.view(-1)
self.use_collision = use_collision
# outlier prob
self.mu = mu
@torch.no_grad()
def prob_cal(self, modelVerts_in, meshVerts_in, sigma=0.05**2, mu = 0.02):
modelVerts_sq = torch.squeeze(modelVerts_in)
meshVerts_sq = torch.squeeze(meshVerts_in)
modelVerts = modelVerts_sq
meshVerts = meshVerts_sq
model_x, model_y, model_z = torch.split(modelVerts, [1,1,1], dim=1)
mesh_x, mesh_y, mesh_z = torch.split(meshVerts, [1,1,1], dim=1)
M = model_x.shape[0]
N = mesh_x.shape[0]
delta_x = torch.repeat_interleave(torch.transpose(mesh_x, 0, 1), M, dim=0) - torch.repeat_interleave(model_x, N, dim=1)
delta_y = torch.repeat_interleave(torch.transpose(mesh_y, 0, 1), M, dim=0) - torch.repeat_interleave(model_y, N, dim=1)
delta_z = torch.repeat_interleave(torch.transpose(mesh_z, 0, 1), M, dim=0) - torch.repeat_interleave(model_z, N, dim=1)
deltaVerts= delta_x * delta_x + delta_y * delta_y + delta_z * delta_z
sigmaInit = sigma #1e-3 # 1e-4
d = 3.0 # three dimension
mu_c = ((2.0 * torch.asin(torch.tensor(1.)) * sigmaInit)**(d/2.0) * mu * M)/((1-mu)*N)
deltaExp = torch.exp(-deltaVerts / (2*sigmaInit))
deltaExpN = torch.repeat_interleave(torch.reshape(torch.sum(deltaExp, dim=0),(1, N)), M, dim=0)
probArray = deltaExp / (deltaExpN + mu_c)
Ind = torch.where(probArray > 1e-6) #2e-7
modelInd, meshInd = Ind[0], Ind[1]
probInput = probArray[Ind]
#print(deltaVerts.shape)
#print(probArray.shape)
#P_sum = torch.sum(probArray)
#P_sep = torch.sum(probArray * deltaVerts)
#sigma2 = P_sep/(P_sum*3)
return probInput, modelInd, meshInd
# ---- get the man function hrere
def __call__(self, init_pose, init_betas, init_cam_t, meshVerts):
"""Perform body fitting.
Input:
init_pose: SMPL pose
init_betas: SMPL betas
init_cam_t: Camera translation
meshVerts: point3d from mesh
Returns:
vertices: Vertices of optimized shape
joints: 3D joints of optimized shape
pose: SMPL pose parameters of optimized shape
betas: SMPL beta parameters of optimized shape
camera_translation: Camera translation
"""
### add the mesh inter-section to avoid
search_tree = None
pen_distance = None
filter_faces = None
# Make camera translation a learnable parameter
# Split SMPL pose to body pose and global orientation
body_pose = init_pose[:, 3:].detach().clone()
global_orient = init_pose[:, :3].detach().clone()
camera_translation = init_cam_t.clone()
betas = init_betas.detach().clone()
preserve_betas = init_betas.detach().clone()
preserve_pose = init_pose[:, 3:].detach().clone()
# -------- Step : Optimize use surface points ---------
betas.requires_grad = True
body_pose.requires_grad = True
global_orient.requires_grad = True
camera_translation.requires_grad = True
body_opt_params = [body_pose, global_orient, betas, camera_translation] #
# optimize the body_pose
body_optimizer = torch.optim.LBFGS(body_opt_params, max_iter=20,
lr=self.step_size, line_search_fn='strong_wolfe') #
for i in range(self.num_iters):
def closure():
body_optimizer.zero_grad()
smpl_output = self.smpl(global_orient=global_orient,
body_pose=body_pose,
betas=betas,
transl=camera_translation,
return_verts=True)
modelVerts = smpl_output.vertices[:, self.selected_index]
# calculate the probInput
probInput, modelInd, meshInd = self.prob_cal(modelVerts, meshVerts, sigma=(0.15**2)*(self.num_iters-i+1)/self.num_iters, mu=self.mu)
#sigma=(0.1**2)*(self.num_iters-i+1)/self.num_iters
loss = body_fitting_loss_em(body_pose, preserve_pose, betas, preserve_betas,
camera_translation,
modelVerts, meshVerts, modelInd, meshInd, probInput,
self.pose_prior,
smpl_output, self.modelfaces,
pose_prior_weight=4.78*2.0,
pose_preserve_weight=1.0,
correspond_weight=1500.0,
chamfer_weight=200.0,
point2mesh_weight=0.0,
use_collision=self.use_collision,
model_vertices=smpl_output.vertices, model_faces=self.model_faces,
search_tree=search_tree, pen_distance=pen_distance, filter_faces=filter_faces)
loss.backward()
return loss
body_optimizer.step(closure)
# Get final loss value
with torch.no_grad():
smpl_output = self.smpl(global_orient=global_orient,
body_pose=body_pose,
betas=betas,
transl=camera_translation,
return_full_pose=True)
vertices = smpl_output.vertices.detach()
joints = smpl_output.joints.detach()
pose = torch.cat([global_orient, body_pose], dim=-1).detach()
betas = betas.detach()
return vertices, joints, pose, betas, camera_translation
# surface EM
class surface_EM_depth():
"""Implementation of SMPLify, use surface."""
def __init__(self,
smplxmodel,
step_size=1e-1,
batch_size=1,
num_iters=100,
selected_index=np.arange(6890),
use_collision=False,
device=torch.device('cuda:0'),
GMM_MODEL_DIR="./smpl_models/",
mu=0.05
):
# Store options
self.batch_size = batch_size
self.device = device
self.step_size = step_size
self.num_iters = num_iters
# GMM pose prior
self.pose_prior = MaxMixturePrior(prior_folder=GMM_MODEL_DIR,
num_gaussians=8,
dtype=torch.float32).to(device)
# Load SMPL-X model
self.smpl = smplxmodel
self.modelfaces = torch.from_numpy(np.int32(smplxmodel.faces)).to(device)
self.selected_index = selected_index
# mesh intersection
self.model_faces = smplxmodel.faces_tensor.view(-1)
self.use_collision = use_collision
# mu prob
self.mu = mu
@torch.no_grad()
def prob_cal(self, modelVerts_in, meshVerts_in, sigma=0.05**2, mu = 0.02):
modelVerts_sq = torch.squeeze(modelVerts_in)
meshVerts_sq = torch.squeeze(meshVerts_in)
modelVerts = modelVerts_sq
meshVerts = meshVerts_sq
model_x, model_y, model_z = torch.split(modelVerts, [1,1,1], dim=1)
mesh_x, mesh_y, mesh_z = torch.split(meshVerts, [1,1,1], dim=1)
M = model_x.shape[0]
N = mesh_x.shape[0]
delta_x = torch.repeat_interleave(torch.transpose(mesh_x, 0, 1), M, dim=0) - torch.repeat_interleave(model_x, N, dim=1)
delta_y = torch.repeat_interleave(torch.transpose(mesh_y, 0, 1), M, dim=0) - torch.repeat_interleave(model_y, N, dim=1)
delta_z = torch.repeat_interleave(torch.transpose(mesh_z, 0, 1), M, dim=0) - torch.repeat_interleave(model_z, N, dim=1)
deltaVerts= delta_x * delta_x + delta_y * delta_y + delta_z * delta_z
sigmaInit = sigma
d = 3.0 # three dimension
mu_c = ((2.0 * torch.asin(torch.tensor(1.)) * sigmaInit)**(d/2.0) * mu * M)/((1-mu)*N)
deltaExp = torch.exp(-deltaVerts / (2*sigmaInit))
deltaExpN = torch.repeat_interleave(torch.reshape(torch.sum(deltaExp, dim=0),(1, N)), M, dim=0)
probArray = deltaExp / (deltaExpN + mu_c)
Ind = torch.where(probArray > 1e-6) #2e-7
modelInd, meshInd = Ind[0], Ind[1]
probInput = probArray[Ind]
return probInput, modelInd, meshInd
# ---- get the man function hrere
def __call__(self, init_pose, init_betas, init_cam_t, meshVerts):
"""Perform body fitting.
Input:
init_pose: SMPL pose
init_betas: SMPL betas
init_cam_t: Camera translation
meshVerts: point3d from mesh
Returns:
vertices: Vertices of optimized shape
joints: 3D joints of optimized shape
pose: SMPL pose parameters of optimized shape
betas: SMPL beta parameters of optimized shape
camera_translation: Camera translation
"""
### add the mesh inter-section to avoid
search_tree = None
pen_distance = None
filter_faces = None
# Make camera translation a learnable parameter
# Split SMPL pose to body pose and global orientation
body_pose = init_pose[:, 3:].detach().clone()
global_orient = init_pose[:, :3].detach().clone()
camera_translation = init_cam_t.clone()
betas = init_betas.detach().clone()
preserve_betas = init_betas.detach().clone()
preserve_pose = init_pose[:, 3:].detach().clone()
# -------- Step : Optimize use surface points ---------
betas.requires_grad = True
body_pose.requires_grad = True
global_orient.requires_grad = True
camera_translation.requires_grad = True
body_opt_params = [body_pose, global_orient, betas, camera_translation] #
# optimize the body_pose
body_optimizer = torch.optim.LBFGS(body_opt_params, max_iter=20,
lr=self.step_size, line_search_fn='strong_wolfe') #
for i in range(self.num_iters):
def closure():
body_optimizer.zero_grad()
smpl_output = self.smpl(global_orient=global_orient,
body_pose=body_pose,
betas=betas,
transl=camera_translation,
return_verts=True)
modelVerts = smpl_output.vertices[:, self.selected_index]
# calculate the probInput
probInput, modelInd, meshInd = self.prob_cal(modelVerts, meshVerts, sigma=(0.1**2)*(self.num_iters-i+1)/self.num_iters, mu=self.mu)
#sigma=(0.1**2)*(self.num_iters-i+1)/self.num_iters
loss = body_fitting_loss_em(body_pose, preserve_pose, betas, preserve_betas,
camera_translation,
modelVerts, meshVerts, modelInd, meshInd, probInput,
self.pose_prior,
smpl_output, self.modelfaces,
pose_prior_weight=4.78*3.0,
pose_preserve_weight=3.0,
correspond_weight=1000.0,
chamfer_weight=100.0,
point2mesh_weight=200.0,
shape_prior_weight=2.0,
use_collision=self.use_collision,
model_vertices=smpl_output.vertices, model_faces=self.model_faces,
search_tree=search_tree, pen_distance=pen_distance, filter_faces=filter_faces)
loss.backward()
return loss
body_optimizer.step(closure)
# Get final loss value
with torch.no_grad():
smpl_output = self.smpl(global_orient=global_orient,
body_pose=body_pose,
betas=betas,
transl=camera_translation,
return_full_pose=True)
vertices = smpl_output.vertices.detach()
joints = smpl_output.joints.detach()
pose = torch.cat([global_orient, body_pose], dim=-1).detach()
betas = betas.detach()
return vertices, joints, pose, betas, camera_translation