diou_loss.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import numpy as np
  18. from paddle import fluid
  19. from ppdet.core.workspace import register, serializable
  20. from .giou_loss import GiouLoss
  21. __all__ = ['DiouLoss']
  22. @register
  23. @serializable
  24. class DiouLoss(GiouLoss):
  25. """
  26. Distance-IoU Loss, see https://arxiv.org/abs/1911.08287
  27. Args:
  28. loss_weight (float): diou loss weight, default as 10 in faster-rcnn
  29. is_cls_agnostic (bool): flag of class-agnostic
  30. num_classes (int): class num
  31. use_complete_iou_loss (bool): whether to use complete iou loss
  32. """
  33. def __init__(self,
  34. loss_weight=10.,
  35. is_cls_agnostic=False,
  36. num_classes=81,
  37. use_complete_iou_loss=True):
  38. super(DiouLoss, self).__init__(
  39. loss_weight=loss_weight,
  40. is_cls_agnostic=is_cls_agnostic,
  41. num_classes=num_classes)
  42. self.use_complete_iou_loss = use_complete_iou_loss
  43. def __call__(self,
  44. x,
  45. y,
  46. inside_weight=None,
  47. outside_weight=None,
  48. bbox_reg_weight=[0.1, 0.1, 0.2, 0.2]):
  49. eps = 1.e-10
  50. x1, y1, x2, y2 = self.bbox_transform(x, bbox_reg_weight)
  51. x1g, y1g, x2g, y2g = self.bbox_transform(y, bbox_reg_weight)
  52. cx = (x1 + x2) / 2
  53. cy = (y1 + y2) / 2
  54. w = x2 - x1
  55. h = y2 - y1
  56. cxg = (x1g + x2g) / 2
  57. cyg = (y1g + y2g) / 2
  58. wg = x2g - x1g
  59. hg = y2g - y1g
  60. x2 = fluid.layers.elementwise_max(x1, x2)
  61. y2 = fluid.layers.elementwise_max(y1, y2)
  62. # A and B
  63. xkis1 = fluid.layers.elementwise_max(x1, x1g)
  64. ykis1 = fluid.layers.elementwise_max(y1, y1g)
  65. xkis2 = fluid.layers.elementwise_min(x2, x2g)
  66. ykis2 = fluid.layers.elementwise_min(y2, y2g)
  67. # A or B
  68. xc1 = fluid.layers.elementwise_min(x1, x1g)
  69. yc1 = fluid.layers.elementwise_min(y1, y1g)
  70. xc2 = fluid.layers.elementwise_max(x2, x2g)
  71. yc2 = fluid.layers.elementwise_max(y2, y2g)
  72. intsctk = (xkis2 - xkis1) * (ykis2 - ykis1)
  73. intsctk = intsctk * fluid.layers.greater_than(
  74. xkis2, xkis1) * fluid.layers.greater_than(ykis2, ykis1)
  75. unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g
  76. ) - intsctk + eps
  77. iouk = intsctk / unionk
  78. # DIOU term
  79. dist_intersection = (cx - cxg) * (cx - cxg) + (cy - cyg) * (cy - cyg)
  80. dist_union = (xc2 - xc1) * (xc2 - xc1) + (yc2 - yc1) * (yc2 - yc1)
  81. diou_term = (dist_intersection + eps) / (dist_union + eps)
  82. # CIOU term
  83. ciou_term = 0
  84. if self.use_complete_iou_loss:
  85. ar_gt = wg / hg
  86. ar_pred = w / h
  87. arctan = fluid.layers.atan(ar_gt) - fluid.layers.atan(ar_pred)
  88. ar_loss = 4. / np.pi / np.pi * arctan * arctan
  89. alpha = ar_loss / (1 - iouk + ar_loss + eps)
  90. alpha.stop_gradient = True
  91. ciou_term = alpha * ar_loss
  92. iou_weights = 1
  93. if inside_weight is not None and outside_weight is not None:
  94. inside_weight = fluid.layers.reshape(inside_weight, shape=(-1, 4))
  95. outside_weight = fluid.layers.reshape(outside_weight, shape=(-1, 4))
  96. inside_weight = fluid.layers.reduce_mean(inside_weight, dim=1)
  97. outside_weight = fluid.layers.reduce_mean(outside_weight, dim=1)
  98. iou_weights = inside_weight * outside_weight
  99. class_weight = 2 if self.is_cls_agnostic else self.num_classes
  100. diou = fluid.layers.reduce_mean(
  101. (1 - iouk + ciou_term + diou_term) * iou_weights) * class_weight
  102. return diou * self.loss_weight