example_loss.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from mmpose.models import KeypointMSELoss
  2. from mmpose.registry import MODELS
  3. # Register your loss to the `MODELS`.
  4. @MODELS.register_module()
  5. class ExampleLoss(KeypointMSELoss):
  6. """Implements an example loss.
  7. Implement the loss just like a normal pytorch module.
  8. """
  9. def __init__(self, **kwargs) -> None:
  10. print('Initializing ExampleLoss...')
  11. super().__init__(**kwargs)
  12. def forward(self, output, target, target_weights=None, mask=None):
  13. """Forward function of loss. The input arguments should match those
  14. given in `head.loss` function.
  15. Note:
  16. - batch_size: B
  17. - num_keypoints: K
  18. - heatmaps height: H
  19. - heatmaps weight: W
  20. Args:
  21. output (Tensor): The output heatmaps with shape [B, K, H, W]
  22. target (Tensor): The target heatmaps with shape [B, K, H, W]
  23. target_weights (Tensor, optional): The target weights of differet
  24. keypoints, with shape [B, K] (keypoint-wise) or
  25. [B, K, H, W] (pixel-wise).
  26. mask (Tensor, optional): The masks of valid heatmap pixels in
  27. shape [B, K, H, W] or [B, 1, H, W]. If ``None``, no mask will
  28. be applied. Defaults to ``None``
  29. Returns:
  30. Tensor: The calculated loss.
  31. """
  32. return super().forward(output, target, target_weights, mask)