ciou_loss.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import torch
  2. from ..utils import _log_api_usage_once
  3. from ._utils import _upcast_non_float
  4. from .diou_loss import _diou_iou_loss
  5. def complete_box_iou_loss(
  6. boxes1: torch.Tensor,
  7. boxes2: torch.Tensor,
  8. reduction: str = "none",
  9. eps: float = 1e-7,
  10. ) -> torch.Tensor:
  11. """
  12. Gradient-friendly IoU loss with an additional penalty that is non-zero when the
  13. boxes do not overlap. This loss function considers important geometrical
  14. factors such as overlap area, normalized central point distance and aspect ratio.
  15. This loss is symmetric, so the boxes1 and boxes2 arguments are interchangeable.
  16. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
  17. ``0 <= x1 < x2`` and ``0 <= y1 < y2``, and The two boxes should have the
  18. same dimensions.
  19. Args:
  20. boxes1 : (Tensor[N, 4] or Tensor[4]) first set of boxes
  21. boxes2 : (Tensor[N, 4] or Tensor[4]) second set of boxes
  22. reduction : (string, optional) Specifies the reduction to apply to the output:
  23. ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: No reduction will be
  24. applied to the output. ``'mean'``: The output will be averaged.
  25. ``'sum'``: The output will be summed. Default: ``'none'``
  26. eps : (float): small number to prevent division by zero. Default: 1e-7
  27. Returns:
  28. Tensor: Loss tensor with the reduction option applied.
  29. Reference:
  30. Zhaohui Zheng et al.: Complete Intersection over Union Loss:
  31. https://arxiv.org/abs/1911.08287
  32. """
  33. # Original Implementation from https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/losses.py
  34. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  35. _log_api_usage_once(complete_box_iou_loss)
  36. boxes1 = _upcast_non_float(boxes1)
  37. boxes2 = _upcast_non_float(boxes2)
  38. diou_loss, iou = _diou_iou_loss(boxes1, boxes2)
  39. x1, y1, x2, y2 = boxes1.unbind(dim=-1)
  40. x1g, y1g, x2g, y2g = boxes2.unbind(dim=-1)
  41. # width and height of boxes
  42. w_pred = x2 - x1
  43. h_pred = y2 - y1
  44. w_gt = x2g - x1g
  45. h_gt = y2g - y1g
  46. v = (4 / (torch.pi**2)) * torch.pow((torch.atan(w_gt / h_gt) - torch.atan(w_pred / h_pred)), 2)
  47. with torch.no_grad():
  48. alpha = v / (1 - iou + v + eps)
  49. loss = diou_loss + alpha * v
  50. # Check reduction option and return loss accordingly
  51. if reduction == "none":
  52. pass
  53. elif reduction == "mean":
  54. loss = loss.mean() if loss.numel() > 0 else 0.0 * loss.sum()
  55. elif reduction == "sum":
  56. loss = loss.sum()
  57. else:
  58. raise ValueError(
  59. f"Invalid Value for arg 'reduction': '{reduction} \n Supported reduction modes: 'none', 'mean', 'sum'"
  60. )
  61. return loss