Forward function of SmoothJointLoss. Args: body_pose (torch.Tensor): The body pose parameters loss_weight_override (float, optional): The weight of loss used to override the original weight of loss reduction_override (str, optional): The r
(self,
body_pose,
loss_weight_override=None,
reduction_override=None)
| 373 | self.loss_func = loss_func |
| 374 | |
| 375 | def forward(self, |
| 376 | body_pose, |
| 377 | loss_weight_override=None, |
| 378 | reduction_override=None): |
| 379 | """Forward function of SmoothJointLoss. |
| 380 | |
| 381 | Args: |
| 382 | body_pose (torch.Tensor): The body pose parameters |
| 383 | loss_weight_override (float, optional): The weight of loss used to |
| 384 | override the original weight of loss |
| 385 | reduction_override (str, optional): The reduction method used to |
| 386 | override the original reduction method of the loss. |
| 387 | Defaults to None |
| 388 | Returns: |
| 389 | torch.Tensor: The calculated loss |
| 390 | """ |
| 391 | assert reduction_override in (None, 'none', 'mean', 'sum') |
| 392 | reduction = (reduction_override |
| 393 | if reduction_override else self.reduction) |
| 394 | loss_weight = (loss_weight_override if loss_weight_override is not None |
| 395 | else self.loss_weight) |
| 396 | |
| 397 | theta = body_pose.reshape(body_pose.shape[0], -1, 3) |
| 398 | if self.degree: |
| 399 | theta = torch.deg2rad(theta) |
| 400 | rot_6d = aa_to_rot6d(theta) |
| 401 | rot_6d_diff = rot_6d[1:] - rot_6d[:-1] |
| 402 | |
| 403 | if self.loss_func == 'L2': |
| 404 | smooth_joint_loss = (rot_6d_diff**2).sum(dim=[1, 2]) |
| 405 | elif self.loss_func == 'L1': |
| 406 | smooth_joint_loss = rot_6d_diff.abs().sum(dim=[1, 2]) |
| 407 | else: |
| 408 | raise TypeError(f'{self.func} is not defined') |
| 409 | |
| 410 | # add zero padding to retain original batch_size |
| 411 | smooth_joint_loss = torch.cat( |
| 412 | [torch.zeros_like(smooth_joint_loss)[:1], smooth_joint_loss]) |
| 413 | |
| 414 | if reduction == 'mean': |
| 415 | smooth_joint_loss = smooth_joint_loss.mean() |
| 416 | elif reduction == 'sum': |
| 417 | smooth_joint_loss = smooth_joint_loss.sum() |
| 418 | |
| 419 | smooth_joint_loss *= loss_weight |
| 420 | |
| 421 | return smooth_joint_loss |
| 422 | |
| 423 | |
| 424 | class SmoothPelvisLoss(nn.Module): |
nothing calls this directly
no test coverage detected