-
Notifications
You must be signed in to change notification settings - Fork 14
/
fitting.py
574 lines (476 loc) · 22.3 KB
/
fitting.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
574
# -*- coding: utf-8 -*-
# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems and the Max Planck Institute for Biological
# Cybernetics. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import sys
import os
import time
import numpy as np
import torch
import torch.nn as nn
from mesh_viewer import MeshViewer
import utils
@torch.no_grad()
def guess_init(model,
joints_2d,
edge_idxs,
focal_length=5000,
pose_embedding=None,
vposer=None,
use_vposer=True,
dtype=torch.float32,
model_type='smpl',
**kwargs):
''' Initializes the camera translation vector
Parameters
----------
model: nn.Module
The PyTorch module of the body
joints_2d: torch.tensor 1xJx2
The 2D tensor of the joints
edge_idxs: list of lists
A list of pairs, each of which represents a limb used to estimate
the camera translation
focal_length: float, optional (default = 5000)
The focal length of the camera
pose_embedding: torch.tensor 1x32
The tensor that contains the embedding of V-Poser that is used to
generate the pose of the model
dtype: torch.dtype, optional (torch.float32)
The floating point type used
vposer: nn.Module, optional (None)
The PyTorch module that implements the V-Poser decoder
Returns
-------
init_t: torch.tensor 1x3, dtype = torch.float32
The vector with the estimated camera location
'''
body_pose = vposer.decode(
pose_embedding, output_type='aa').view(1, -1) if use_vposer else None
if use_vposer and model_type == 'smpl':
wrist_pose = torch.zeros([body_pose.shape[0], 6],
dtype=body_pose.dtype,
device=body_pose.device)
body_pose = torch.cat([body_pose, wrist_pose], dim=1)
output = model(body_pose=body_pose, return_verts=False,
return_full_pose=False)
joints_3d = output.joints
joints_2d = joints_2d.to(device=joints_3d.device)
diff3d = []
diff2d = []
for edge in edge_idxs:
diff3d.append(joints_3d[:, edge[0]] - joints_3d[:, edge[1]])
diff2d.append(joints_2d[:, edge[0]] - joints_2d[:, edge[1]])
diff3d = torch.stack(diff3d, dim=1)
diff2d = torch.stack(diff2d, dim=1)
length_2d = diff2d.pow(2).sum(dim=-1).sqrt()
length_3d = diff3d.pow(2).sum(dim=-1).sqrt()
height2d = length_2d.mean(dim=1)
height3d = length_3d.mean(dim=1)
est_d = focal_length * (height3d / height2d)
# just set the z value
batch_size = joints_3d.shape[0]
x_coord = torch.zeros([batch_size], device=joints_3d.device,
dtype=dtype)
y_coord = x_coord.clone()
init_t = torch.stack([x_coord, y_coord, est_d], dim=1)
return init_t
class FittingMonitor(object):
def __init__(self, summary_steps=1, visualize=False,
maxiters=100, ftol=2e-09, gtol=1e-05,
body_color=(1.0, 1.0, 0.9, 1.0),
model_type='smpl',
**kwargs):
super(FittingMonitor, self).__init__()
self.maxiters = maxiters
self.ftol = ftol
self.gtol = gtol
self.visualize = visualize
self.summary_steps = summary_steps
self.body_color = body_color
self.model_type = model_type
def __enter__(self):
self.steps = 0
if self.visualize:
self.mv = MeshViewer(body_color=self.body_color)
return self
def __exit__(self, exception_type, exception_value, traceback):
if self.visualize:
self.mv.close_viewer()
def set_colors(self, vertex_color):
batch_size = self.colors.shape[0]
self.colors = np.tile(
np.array(vertex_color).reshape(1, 3),
[batch_size, 1])
def run_fitting(self, optimizer, closure, params, body_model,
use_vposer=True, pose_embedding=None, vposer=None,
**kwargs):
''' Helper function for running an optimization process
Parameters
----------
optimizer: torch.optim.Optimizer
The PyTorch optimizer object
closure: function
The function used to calculate the gradients
params: list
List containing the parameters that will be optimized
body_model: nn.Module
The body model PyTorch module
use_vposer: bool
Flag on whether to use VPoser (default=True).
pose_embedding: torch.tensor, BxN
The tensor that contains the latent pose variable.
vposer: nn.Module
The VPoser module
Returns
-------
loss: float
The final loss value
'''
append_wrists = self.model_type == 'smpl' and use_vposer
prev_loss = None
for n in range(self.maxiters):
loss = optimizer.step(closure)
if torch.isnan(loss).sum() > 0:
print('NaN loss value, stopping!')
break
if torch.isinf(loss).sum() > 0:
print('Infinite loss value, stopping!')
break
if n > 0 and prev_loss is not None and self.ftol > 0:
loss_rel_change = utils.rel_change(prev_loss, loss.item())
if loss_rel_change <= self.ftol:
break
if all([torch.abs(var.grad.view(-1).max()).item() < self.gtol
for var in params if var.grad is not None]):
break
if self.visualize and n % self.summary_steps == 0:
body_pose = vposer.decode(
pose_embedding, output_type='aa').view(
1, -1) if use_vposer else None
if append_wrists:
wrist_pose = torch.zeros([body_pose.shape[0], 6],
dtype=body_pose.dtype,
device=body_pose.device)
body_pose = torch.cat([body_pose, wrist_pose], dim=1)
model_output = body_model(
return_verts=True, body_pose=body_pose)
vertices = model_output.vertices.detach().cpu().numpy()
self.mv.update_mesh(vertices.squeeze(),
body_model.faces)
prev_loss = loss.item()
return prev_loss
def create_fitting_closure(self,
optimizer, body_model,
camera=None, global_body_translation=None,
body_model_scale=None,
gt_joints=None, loss=None,
joints_conf=None,
joint_weights=None,
return_verts=True, return_full_pose=False,
use_vposer=False, vposer=None,
pose_embedding=None,
create_graph=False,
**kwargs):
faces_tensor = body_model.faces_tensor.view(-1)
append_wrists = self.model_type == 'smpl' and use_vposer
def fitting_func(backward=True):
if backward:
optimizer.zero_grad()
body_pose = vposer.decode(
pose_embedding, output_type='aa').view(
1, -1) if use_vposer else None
if append_wrists:
wrist_pose = torch.zeros([body_pose.shape[0], 6],
dtype=body_pose.dtype,
device=body_pose.device)
body_pose = torch.cat([body_pose, wrist_pose], dim=1)
body_model_output = body_model(return_verts=return_verts,
body_pose=body_pose,
return_full_pose=return_full_pose)
total_loss = loss(body_model_output, camera=camera,
global_body_translation=global_body_translation,
body_model_scale=body_model_scale,
gt_joints=gt_joints,
body_model_faces=faces_tensor,
joints_conf=joints_conf,
joint_weights=joint_weights,
pose_embedding=pose_embedding,
use_vposer=use_vposer,
**kwargs)
if backward:
total_loss.backward(create_graph=create_graph)
self.steps += 1
if self.visualize and self.steps % self.summary_steps == 0:
model_output = body_model(return_verts=True,
body_pose=body_pose)
vertices = model_output.vertices.detach().cpu().numpy()
self.mv.update_mesh(vertices.squeeze(),
body_model.faces)
return total_loss
return fitting_func
def create_fitting_closure_multiview(self,
optimizer, body_model,
camera_list=None, global_body_translation=None,
body_model_scale=None,
gt_joints_list=None, loss_list=None,
joints_conf_list=None,
joint_weights=None,
return_verts=True, return_full_pose=False,
use_vposer=False, vposer=None,
pose_embedding=None,
create_graph=False,
**kwargs):
faces_tensor = body_model.faces_tensor.view(-1)
append_wrists = self.model_type == 'smpl' and use_vposer
def fitting_func(backward=True):
if backward:
optimizer.zero_grad()
body_pose = vposer.decode(
pose_embedding, output_type='aa').view(
1, -1) if use_vposer else None
if append_wrists:
wrist_pose = torch.zeros([body_pose.shape[0], 6],
dtype=body_pose.dtype,
device=body_pose.device)
body_pose = torch.cat([body_pose, wrist_pose], dim=1)
body_model_output = body_model(return_verts=return_verts,
body_pose=body_pose,
return_full_pose=return_full_pose)
total_loss = 0
for i in range(len(camera_list)):
loss = loss_list[i]
total_loss += loss(body_model_output, camera=camera_list[i],
global_body_translation=global_body_translation,
body_model_scale=body_model_scale,
gt_joints=gt_joints_list[i],
body_model_faces=faces_tensor,
joints_conf=joints_conf_list[i],
joint_weights=joint_weights,
pose_embedding=pose_embedding,
use_vposer=use_vposer,
**kwargs)
if backward:
total_loss.backward(create_graph=create_graph)
self.steps += 1
if self.visualize and self.steps % self.summary_steps == 0:
model_output = body_model(return_verts=True,
body_pose=body_pose)
vertices = model_output.vertices.detach().cpu().numpy()
self.mv.update_mesh(vertices.squeeze(),
body_model.faces)
return total_loss
return fitting_func
def create_loss(loss_type='smplify', **kwargs):
if loss_type == 'smplify':
return SMPLifyLoss(**kwargs)
elif loss_type == 'camera_init':
return SMPLifyCameraInitLoss(**kwargs)
else:
raise ValueError('Unknown loss type: {}'.format(loss_type))
class SMPLifyLoss(nn.Module):
def __init__(self, search_tree=None,
pen_distance=None, tri_filtering_module=None,
rho=100,
body_pose_prior=None,
shape_prior=None,
expr_prior=None,
angle_prior=None,
jaw_prior=None,
use_joints_conf=True,
use_face=True, use_hands=True,
left_hand_prior=None, right_hand_prior=None,
interpenetration=True, dtype=torch.float32,
data_weight=1.0,
body_pose_weight=0.0,
shape_weight=0.0,
bending_prior_weight=0.0,
hand_prior_weight=0.0,
expr_prior_weight=0.0, jaw_prior_weight=0.0,
coll_loss_weight=0.0,
body_model_scale=1.0,
reduction='sum',
**kwargs):
super(SMPLifyLoss, self).__init__()
self.use_joints_conf = use_joints_conf
self.angle_prior = angle_prior
self.robustifier = utils.GMoF(rho=rho)
self.rho = rho
self.body_pose_prior = body_pose_prior
self.shape_prior = shape_prior
self.interpenetration = interpenetration
if self.interpenetration:
self.search_tree = search_tree
self.tri_filtering_module = tri_filtering_module
self.pen_distance = pen_distance
self.use_hands = use_hands
if self.use_hands:
self.left_hand_prior = left_hand_prior
self.right_hand_prior = right_hand_prior
self.use_face = use_face
if self.use_face:
self.expr_prior = expr_prior
self.jaw_prior = jaw_prior
self.register_buffer('data_weight',
torch.tensor(data_weight, dtype=dtype))
self.register_buffer('body_pose_weight',
torch.tensor(body_pose_weight, dtype=dtype))
self.register_buffer('shape_weight',
torch.tensor(shape_weight, dtype=dtype))
self.register_buffer('bending_prior_weight',
torch.tensor(bending_prior_weight, dtype=dtype))
if self.use_hands:
self.register_buffer('hand_prior_weight',
torch.tensor(hand_prior_weight, dtype=dtype))
if self.use_face:
self.register_buffer('expr_prior_weight',
torch.tensor(expr_prior_weight, dtype=dtype))
self.register_buffer('jaw_prior_weight',
torch.tensor(jaw_prior_weight, dtype=dtype))
if self.interpenetration:
self.register_buffer('coll_loss_weight',
torch.tensor(coll_loss_weight, dtype=dtype))
def reset_loss_weights(self, loss_weight_dict):
for key in loss_weight_dict:
if hasattr(self, key):
weight_tensor = getattr(self, key)
if 'torch.Tensor' in str(type(loss_weight_dict[key])):
weight_tensor = loss_weight_dict[key].clone().detach()
else:
weight_tensor = torch.tensor(loss_weight_dict[key],
dtype=weight_tensor.dtype,
device=weight_tensor.device)
setattr(self, key, weight_tensor)
def forward(self, body_model_output, camera, global_body_translation,
body_model_scale,
gt_joints, joints_conf,
body_model_faces, joint_weights,
use_vposer=False, pose_embedding=None,
**kwargs):
projected_joints = camera(
body_model_scale * body_model_output.joints + global_body_translation)
# Calculate the weights for each joints
weights = (joint_weights * joints_conf
if self.use_joints_conf else
joint_weights).unsqueeze(dim=-1)
# Calculate the distance of the projected joints from
# the ground truth 2D detections
joint_diff = self.robustifier(gt_joints - projected_joints)
joint_loss = (torch.sum(weights ** 2 * joint_diff) *
self.data_weight ** 2)
# Calculate the loss from the Pose prior
if use_vposer:
pprior_loss = (pose_embedding.pow(2).sum() *
self.body_pose_weight ** 2)
else:
pprior_loss = torch.sum(self.body_pose_prior(
body_model_output.body_pose,
body_model_output.betas)) * self.body_pose_weight ** 2
shape_loss = torch.sum(self.shape_prior(
body_model_output.betas)) * self.shape_weight ** 2
# Calculate the prior over the joint rotations. This a heuristic used
# to prevent extreme rotation of the elbows and knees
body_pose = body_model_output.full_pose[:, 3:66]
angle_prior_loss = torch.sum(
self.angle_prior(body_pose)) * self.bending_prior_weight
# Apply the prior on the pose space of the hand
left_hand_prior_loss, right_hand_prior_loss = 0.0, 0.0
if self.use_hands and self.left_hand_prior is not None:
left_hand_prior_loss = torch.sum(
self.left_hand_prior(
body_model_output.left_hand_pose)) * \
self.hand_prior_weight ** 2
if self.use_hands and self.right_hand_prior is not None:
right_hand_prior_loss = torch.sum(
self.right_hand_prior(
body_model_output.right_hand_pose)) * \
self.hand_prior_weight ** 2
expression_loss = 0.0
jaw_prior_loss = 0.0
if self.use_face:
expression_loss = torch.sum(self.expr_prior(
body_model_output.expression)) * \
self.expr_prior_weight ** 2
if hasattr(self, 'jaw_prior'):
jaw_prior_loss = torch.sum(
self.jaw_prior(
body_model_output.jaw_pose.mul(
self.jaw_prior_weight)))
pen_loss = 0.0
# Calculate the loss due to interpenetration
if (self.interpenetration and self.coll_loss_weight.item() > 0):
batch_size = projected_joints.shape[0]
triangles = torch.index_select(
body_model_output.vertices, 1,
body_model_faces).view(batch_size, -1, 3, 3)
with torch.no_grad():
collision_idxs = self.search_tree(triangles)
# Remove unwanted collisions
if self.tri_filtering_module is not None:
collision_idxs = self.tri_filtering_module(collision_idxs)
if collision_idxs.ge(0).sum().item() > 0:
pen_loss = torch.sum(
self.coll_loss_weight *
self.pen_distance(triangles, collision_idxs))
total_loss = (joint_loss + pprior_loss + shape_loss +
angle_prior_loss + pen_loss +
jaw_prior_loss + expression_loss +
left_hand_prior_loss + right_hand_prior_loss)
return total_loss
class SMPLifyCameraInitLoss(nn.Module):
def __init__(self, init_joints_idxs, trans_estimation=None,
reduction='sum',
data_weight=1.0,
depth_loss_weight=1e2, dtype=torch.float32,
**kwargs):
super(SMPLifyCameraInitLoss, self).__init__()
self.dtype = dtype
if trans_estimation is not None:
self.register_buffer(
'trans_estimation',
utils.to_tensor(trans_estimation, dtype=dtype))
else:
self.trans_estimation = trans_estimation
self.register_buffer('data_weight',
torch.tensor(data_weight, dtype=dtype))
self.register_buffer(
'init_joints_idxs',
utils.to_tensor(init_joints_idxs, dtype=torch.long))
self.register_buffer('depth_loss_weight',
torch.tensor(depth_loss_weight, dtype=dtype))
def reset_loss_weights(self, loss_weight_dict):
for key in loss_weight_dict:
if hasattr(self, key):
weight_tensor = getattr(self, key)
weight_tensor = torch.tensor(loss_weight_dict[key],
dtype=weight_tensor.dtype,
device=weight_tensor.device)
setattr(self, key, weight_tensor)
def forward(self, body_model_output, camera, gt_joints,
**kwargs):
projected_joints = camera(body_model_output.joints)
joint_error = torch.pow(
torch.index_select(gt_joints, 1, self.init_joints_idxs) -
torch.index_select(projected_joints, 1, self.init_joints_idxs),
2)
joint_loss = torch.sum(joint_error) * self.data_weight ** 2
depth_loss = 0.0
if (self.depth_loss_weight.item() > 0 and self.trans_estimation is not
None):
depth_loss = self.depth_loss_weight ** 2 * torch.sum((
camera.translation[:, 2] - self.trans_estimation[:, 2]).pow(2))
return joint_loss + depth_loss