-
Notifications
You must be signed in to change notification settings - Fork 31
/
losses.py
432 lines (368 loc) · 14.9 KB
/
losses.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
# -*- 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. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de
import torch
import torch.nn as nn
from loguru import logger
from pare.losses.keypoints import JointsMSELoss
from pare.losses.segmentation import CrossEntropy
from pare.utils.geometry import batch_rodrigues, rotmat_to_rot6d
class HMRLoss(nn.Module):
def __init__(
self,
shape_loss_weight=0,
keypoint_loss_weight=5.,
pose_loss_weight=1.,
smpl_part_loss_weight=1.,
beta_loss_weight=0.001,
openpose_train_weight=0.,
gt_train_weight=1.,
loss_weight=60.,
estimate_var=False,
uncertainty_loss='MultivariateGaussianNegativeLogLikelihood',
):
super(HMRLoss, self).__init__()
self.criterion_shape = nn.L1Loss()
self.criterion_keypoints = nn.MSELoss(reduction='none')
self.estimate_var = estimate_var
if self.estimate_var:
self.criterion_regr = eval(uncertainty_loss)() # AleatoricLoss
else:
self.criterion_regr = nn.MSELoss()
self.loss_weight = loss_weight
self.gt_train_weight = gt_train_weight
self.pose_loss_weight = pose_loss_weight
self.beta_loss_weight = beta_loss_weight
self.shape_loss_weight = shape_loss_weight
self.keypoint_loss_weight = keypoint_loss_weight
self.openpose_train_weight = openpose_train_weight
self.smpl_part_loss_weight = smpl_part_loss_weight
def forward(self, pred, gt):
pred_cam = pred['pred_cam']
pred_betas = pred['pred_shape_var'] if self.estimate_var else pred['pred_shape']
pred_rotmat = pred['pred_pose_var'] if self.estimate_var else pred['pred_pose']
pred_joints = pred['smpl_joints3d']
pred_vertices = pred['smpl_vertices']
pred_projected_keypoints_2d = pred['smpl_joints2d']
gt_pose = gt['pose']
gt_pose_conf = gt['pose_conf']
gt_betas = gt['betas']
gt_joints = gt['pose_3d']
gt_vertices = gt['vertices']
gt_keypoints_2d = gt['keypoints']
has_smpl = gt['has_smpl'].bool()
has_pose_3d = gt['has_pose_3d'].bool()
smpl_loss_f = smpl_losses_uncertainty if self.estimate_var else smpl_losses
# Compute loss on SMPL parameters
loss_regr_pose, loss_regr_betas = smpl_loss_f(
pred_rotmat,
pred_betas,
gt_pose,
gt_betas,
has_smpl,
gt_pose_conf,
criterion=self.criterion_regr,
)
# Compute 2D reprojection loss for the keypoints
loss_keypoints = projected_keypoint_loss(
pred_projected_keypoints_2d,
gt_keypoints_2d,
self.openpose_train_weight,
self.gt_train_weight,
criterion=self.criterion_keypoints,
)
# Compute 3D keypoint loss
loss_keypoints_3d = keypoint_3d_loss(
pred_joints,
gt_joints,
has_pose_3d,
criterion=self.criterion_keypoints,
)
# Per-vertex loss for the shape
loss_shape = shape_loss(
pred_vertices,
gt_vertices,
has_smpl,
criterion=self.criterion_shape,
)
loss_shape *= self.shape_loss_weight
loss_keypoints *= self.keypoint_loss_weight
loss_keypoints_3d *= self.keypoint_loss_weight
loss_regr_pose *= self.pose_loss_weight
loss_regr_betas *= self.beta_loss_weight
loss_cam = ((torch.exp(-pred_cam[:, 0] * 10)) ** 2).mean()
loss_dict = {
'loss/loss_keypoints': loss_keypoints,
'loss/loss_keypoints_3d': loss_keypoints_3d,
'loss/loss_regr_pose': loss_regr_pose,
'loss/loss_regr_betas': loss_regr_betas,
'loss/loss_shape': loss_shape,
'loss/loss_cam': loss_cam,
}
if 'pred_segm_rgb' in pred.keys():
loss_part_segm = self.criterion_part(pred['pred_segm_rgb'], gt['gt_segm_rgb'])
loss_part_segm *= self.smpl_part_loss_weight
loss_dict['loss/loss_part_segm'] = loss_part_segm
loss = sum(loss for loss in loss_dict.values())
loss *= self.loss_weight
loss_dict['loss/total_loss'] = loss
return loss, loss_dict
class HMRCamLoss(nn.Module):
def __init__(
self,
shape_loss_weight=0,
keypoint_loss_weight=5.,
pose_loss_weight=1.,
smpl_part_loss_weight=1.,
beta_loss_weight=0.001,
openpose_train_weight=0.,
gt_train_weight=1.,
loss_weight=60.,
):
super(HMRCamLoss, self).__init__()
self.criterion_shape = nn.L1Loss()
self.criterion_keypoints = nn.MSELoss(reduction='none')
self.criterion_regr = nn.MSELoss()
self.loss_weight = loss_weight
self.gt_train_weight = gt_train_weight
self.pose_loss_weight = pose_loss_weight
self.beta_loss_weight = beta_loss_weight
self.shape_loss_weight = shape_loss_weight
self.keypoint_loss_weight = keypoint_loss_weight
self.openpose_train_weight = openpose_train_weight
self.smpl_part_loss_weight = smpl_part_loss_weight
def forward(self, pred, gt):
pred_cam = pred['pred_cam']
pred_betas = pred['pred_shape']
pred_rotmat = pred['pred_pose']
pred_joints = pred['smpl_joints3d']
pred_vertices = pred['smpl_vertices']
pred_projected_keypoints_2d = pred['smpl_joints2d']
gt_pose = gt['pose']
gt_pose_conf = gt['pose_conf']
gt_betas = gt['betas']
gt_joints = gt['pose_3d']
gt_vertices = gt['vertices']
has_smpl = gt['has_smpl'].bool()
has_pose_3d = gt['has_pose_3d'].bool()
img_size = gt['orig_shape'].rot90().T.unsqueeze(1) # image size (H,W) -> (W,H)
# normalize predicted keypoints between -1 and 1 to compute the loss
pred_projected_keypoints_2d[:, :, :2] = 2 * (pred_projected_keypoints_2d[:, :, :2] / img_size) - 1
# normalize gt keypoints between -1 and 1 to compute the loss
gt_keypoints_2d_full_img = gt['keypoints_orig'].clone()
gt_keypoints_2d_full_img[:, :, :2] = 2 * (gt_keypoints_2d_full_img[:, :, :2] / img_size) - 1
smpl_loss_f = smpl_losses
# Compute loss on SMPL parameters
loss_regr_pose, loss_regr_betas = smpl_loss_f(
pred_rotmat,
pred_betas,
gt_pose,
gt_betas,
has_smpl,
gt_pose_conf,
criterion=self.criterion_regr,
)
# Compute 2D reprojection loss for the keypoints
loss_keypoints = projected_keypoint_loss(
pred_projected_keypoints_2d,
gt_keypoints_2d_full_img,
self.openpose_train_weight,
self.gt_train_weight,
criterion=self.criterion_keypoints,
reduce='none',
)
# scale keypoints with img_H/bbox_h and img_W/bbox_w to have
# loss magnitude identical to HMR
loss_keypoints_scale = img_size.squeeze(1) / (gt['scale'] * 200.).unsqueeze(-1)
loss_keypoints = loss_keypoints * loss_keypoints_scale.unsqueeze(1)
loss_keypoints = loss_keypoints.mean()
# Compute 3D keypoint loss
loss_keypoints_3d = keypoint_3d_loss(
pred_joints,
gt_joints,
has_pose_3d,
criterion=self.criterion_keypoints,
)
# Per-vertex loss for the shape
loss_shape = shape_loss(
pred_vertices,
gt_vertices,
has_smpl,
criterion=self.criterion_shape,
)
loss_shape *= self.shape_loss_weight
loss_keypoints *= self.keypoint_loss_weight
loss_keypoints_3d *= self.keypoint_loss_weight
loss_regr_pose *= self.pose_loss_weight
loss_regr_betas *= self.beta_loss_weight
loss_cam = ((torch.exp(-pred_cam[:, 0] * 10)) ** 2).mean()
loss_dict = {
'loss/loss_keypoints': loss_keypoints,
'loss/loss_keypoints_3d': loss_keypoints_3d,
'loss/loss_regr_pose': loss_regr_pose,
'loss/loss_regr_betas': loss_regr_betas,
'loss/loss_shape': loss_shape,
'loss/loss_cam': loss_cam,
}
if 'pred_segm_rgb' in pred.keys():
loss_part_segm = self.criterion_part(pred['pred_segm_rgb'], gt['gt_segm_rgb'])
loss_part_segm *= self.smpl_part_loss_weight
loss_dict['loss/loss_part_segm'] = loss_part_segm
loss = sum(loss for loss in loss_dict.values())
loss *= self.loss_weight
loss_dict['loss/total_loss'] = loss
# import IPython; IPython.embed(); exit()
return loss, loss_dict
def projected_keypoint_loss(
pred_keypoints_2d,
gt_keypoints_2d,
openpose_weight,
gt_weight,
criterion,
reduce='mean',
):
""" Compute 2D reprojection loss on the keypoints.
The loss is weighted by the confidence.
The available keypoints are different for each dataset.
"""
conf = gt_keypoints_2d[:, :, -1].unsqueeze(-1).clone()
conf[:, :25] *= openpose_weight
conf[:, 25:] *= gt_weight
if reduce == 'mean':
loss = (conf * criterion(pred_keypoints_2d, gt_keypoints_2d[:, :, :-1])).mean()
elif reduce == 'none':
loss = (conf * criterion(pred_keypoints_2d, gt_keypoints_2d[:, :, :-1]))
else:
raise ValueError(f'{reduce} value is not defined!')
return loss
def keypoint_loss(
pred_keypoints_2d,
gt_keypoints_2d,
criterion,
):
""" Compute 2D reprojection loss on the keypoints.
The loss is weighted by the confidence.
The available keypoints are different for each dataset.
"""
loss = criterion(pred_keypoints_2d, gt_keypoints_2d).mean()
return loss
def heatmap_2d_loss(
pred_heatmaps_2d,
gt_heatmaps_2d,
joint_vis,
criterion,
):
""" Compute 2D reprojection loss on the keypoints.
The loss is weighted by the confidence.
The available keypoints are different for each dataset.
"""
loss = criterion(pred_heatmaps_2d, gt_heatmaps_2d, joint_vis)
return loss
def keypoint_3d_loss(
pred_keypoints_3d,
gt_keypoints_3d,
has_pose_3d,
criterion,
):
"""Compute 3D keypoint loss for the examples that 3D keypoint annotations are available.
The loss is weighted by the confidence.
"""
pred_keypoints_3d = pred_keypoints_3d[:, 25:, :]
conf = gt_keypoints_3d[:, :, -1].unsqueeze(-1).clone()
gt_keypoints_3d = gt_keypoints_3d[:, :, :-1].clone()
gt_keypoints_3d = gt_keypoints_3d[has_pose_3d == 1]
conf = conf[has_pose_3d == 1]
pred_keypoints_3d = pred_keypoints_3d[has_pose_3d == 1]
if len(gt_keypoints_3d) > 0:
gt_pelvis = (gt_keypoints_3d[:, 2, :] + gt_keypoints_3d[:, 3, :]) / 2
gt_keypoints_3d = gt_keypoints_3d - gt_pelvis[:, None, :]
pred_pelvis = (pred_keypoints_3d[:, 2, :] + pred_keypoints_3d[:, 3, :]) / 2
pred_keypoints_3d = pred_keypoints_3d - pred_pelvis[:, None, :]
return (conf * criterion(pred_keypoints_3d, gt_keypoints_3d)).mean()
else:
return torch.FloatTensor(1).fill_(0.).to(pred_keypoints_3d.device)
def np_keypoint_3d_loss(
pred_joints,
gt_joints,
has_pose_3d,
criterion,
):
"""Compute 3D keypoint loss for the examples that 3D keypoint annotations are available.
The loss is weighted by the confidence.
"""
# pred_keypoints_3d = pred_keypoints_3d[:, 25:, :]
# conf = gt_keypoints_3d[:, :, -1].unsqueeze(-1).clone()
# gt_keypoints_3d = gt_keypoints_3d[:, :, :-1].clone()
# gt_keypoints_3d = gt_keypoints_3d[has_pose_3d == 1]
# conf = conf[has_pose_3d == 1]
# pred_keypoints_3d = pred_keypoints_3d[has_pose_3d == 1]
# gt_pelvis = (gt_joints[:, 2, :] + gt_joints[:, 3, :]) / 2
# gt_keypoints_3d = gt_joints - gt_pelvis[:, None, :]
# pred_pelvis = (pred_joints[:, 2, :] + pred_joints[:, 3, :]) / 2
# pred_keypoints_3d = pred_joints - pred_pelvis[:, None, :]
return criterion(pred_joints, gt_joints).mean()
def shape_loss(
pred_vertices,
gt_vertices,
has_smpl,
criterion,
):
"""Compute per-vertex loss on the shape for the examples that SMPL annotations are available."""
pred_vertices_with_shape = pred_vertices[has_smpl == 1]
gt_vertices_with_shape = gt_vertices[has_smpl == 1]
if len(gt_vertices_with_shape) > 0:
return criterion(pred_vertices_with_shape, gt_vertices_with_shape)
else:
return torch.FloatTensor(1).fill_(0.).to(pred_vertices.device)
def smpl_losses_uncertainty(
pred_rot6d,
pred_betas,
gt_pose,
gt_betas,
has_smpl,
criterion,
):
pred_rot6d_valid = pred_rot6d[has_smpl == 1]
gt_rotmat_valid = batch_rodrigues(gt_pose.view(-1, 3)).view(-1, 24, 3, 3)[has_smpl == 1]
gt_rot6d_valid = rotmat_to_rot6d(gt_rotmat_valid)
pred_betas_valid = pred_betas[has_smpl == 1]
gt_betas_valid = gt_betas[has_smpl == 1]
if len(pred_rot6d_valid) > 0:
loss_regr_pose = criterion(pred_rot6d_valid, gt_rot6d_valid)
loss_regr_betas = criterion(pred_betas_valid, gt_betas_valid)
else:
loss_regr_pose = torch.FloatTensor(1).fill_(0.).to(pred_rot6d.device)
loss_regr_betas = torch.FloatTensor(1).fill_(0.).to(pred_rot6d.device)
return loss_regr_pose, loss_regr_betas
def smpl_losses(
pred_rotmat,
pred_betas,
gt_pose,
gt_betas,
has_smpl,
pose_conf,
criterion,
):
pred_rotmat_valid = pred_rotmat[has_smpl == 1]
gt_rotmat_valid = batch_rodrigues(gt_pose.view(-1, 3)).view(-1, 24, 3, 3)[has_smpl == 1]
pred_betas_valid = pred_betas[has_smpl == 1]
gt_betas_valid = gt_betas[has_smpl == 1]
pose_conf = pose_conf[has_smpl == 1].unsqueeze(-1).unsqueeze(-1)
if len(pred_rotmat_valid) > 0:
loss_regr_pose = (pose_conf * criterion(pred_rotmat_valid, gt_rotmat_valid)).mean()
loss_regr_betas = criterion(pred_betas_valid, gt_betas_valid).mean()
else:
loss_regr_pose = torch.FloatTensor(1).fill_(0.).to(pred_rotmat.device)
loss_regr_betas = torch.FloatTensor(1).fill_(0.).to(pred_rotmat.device)
return loss_regr_pose, loss_regr_betas