diou_loss.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) 2020 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 .giou_loss import GiouLoss
  20. __all__ = ['DiouLoss']
  21. class DiouLoss(GiouLoss):
  22. """
  23. Distance-IoU Loss, see https://arxiv.org/abs/1911.08287
  24. Args:
  25. loss_weight (float): diou loss weight, default as 10 in faster-rcnn
  26. is_cls_agnostic (bool): flag of class-agnostic
  27. num_classes (int): class num
  28. use_complete_iou_loss (bool): whether to use complete iou loss
  29. """
  30. def __init__(self,
  31. loss_weight=10.,
  32. is_cls_agnostic=False,
  33. num_classes=81,
  34. use_complete_iou_loss=True):
  35. super(DiouLoss, self).__init__(
  36. loss_weight=loss_weight,
  37. is_cls_agnostic=is_cls_agnostic,
  38. num_classes=num_classes)
  39. self.use_complete_iou_loss = use_complete_iou_loss
  40. def __call__(self,
  41. x,
  42. y,
  43. inside_weight=None,
  44. outside_weight=None,
  45. bbox_reg_weight=[0.1, 0.1, 0.2, 0.2]):
  46. eps = 1.e-10
  47. x1, y1, x2, y2 = self.bbox_transform(x, bbox_reg_weight)
  48. x1g, y1g, x2g, y2g = self.bbox_transform(y, bbox_reg_weight)
  49. cx = (x1 + x2) / 2
  50. cy = (y1 + y2) / 2
  51. w = x2 - x1
  52. h = y2 - y1
  53. cxg = (x1g + x2g) / 2
  54. cyg = (y1g + y2g) / 2
  55. wg = x2g - x1g
  56. hg = y2g - y1g
  57. x2 = fluid.layers.elementwise_max(x1, x2)
  58. y2 = fluid.layers.elementwise_max(y1, y2)
  59. # A and B
  60. xkis1 = fluid.layers.elementwise_max(x1, x1g)
  61. ykis1 = fluid.layers.elementwise_max(y1, y1g)
  62. xkis2 = fluid.layers.elementwise_min(x2, x2g)
  63. ykis2 = fluid.layers.elementwise_min(y2, y2g)
  64. # A or B
  65. xc1 = fluid.layers.elementwise_min(x1, x1g)
  66. yc1 = fluid.layers.elementwise_min(y1, y1g)
  67. xc2 = fluid.layers.elementwise_max(x2, x2g)
  68. yc2 = fluid.layers.elementwise_max(y2, y2g)
  69. intsctk = (xkis2 - xkis1) * (ykis2 - ykis1)
  70. intsctk = intsctk * fluid.layers.greater_than(
  71. xkis2, xkis1) * fluid.layers.greater_than(ykis2, ykis1)
  72. unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g
  73. ) - intsctk + eps
  74. iouk = intsctk / unionk
  75. # DIOU term
  76. dist_intersection = (cx - cxg) * (cx - cxg) + (cy - cyg) * (cy - cyg)
  77. dist_union = (xc2 - xc1) * (xc2 - xc1) + (yc2 - yc1) * (yc2 - yc1)
  78. diou_term = (dist_intersection + eps) / (dist_union + eps)
  79. # CIOU term
  80. ciou_term = 0
  81. if self.use_complete_iou_loss:
  82. ar_gt = wg / hg
  83. ar_pred = w / h
  84. arctan = fluid.layers.atan(ar_gt) - fluid.layers.atan(ar_pred)
  85. ar_loss = 4. / np.pi / np.pi * arctan * arctan
  86. alpha = ar_loss / (1 - iouk + ar_loss + eps)
  87. alpha.stop_gradient = True
  88. ciou_term = alpha * ar_loss
  89. iou_weights = 1
  90. if inside_weight is not None and outside_weight is not None:
  91. inside_weight = fluid.layers.reshape(inside_weight, shape=(-1, 4))
  92. outside_weight = fluid.layers.reshape(
  93. outside_weight, shape=(-1, 4))
  94. inside_weight = fluid.layers.reduce_mean(inside_weight, dim=1)
  95. outside_weight = fluid.layers.reduce_mean(outside_weight, dim=1)
  96. iou_weights = inside_weight * outside_weight
  97. class_weight = 2 if self.is_cls_agnostic else self.num_classes
  98. diou = fluid.layers.reduce_mean(
  99. (1 - iouk + ciou_term + diou_term) * iou_weights) * class_weight
  100. return diou * self.loss_weight