giou_loss.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. __all__ = ['GiouLoss']
  20. class GiouLoss(object):
  21. '''
  22. Generalized Intersection over Union, see https://arxiv.org/abs/1902.09630
  23. Args:
  24. loss_weight (float): diou loss weight, default as 10 in faster-rcnn
  25. is_cls_agnostic (bool): flag of class-agnostic
  26. num_classes (int): class num
  27. do_average (bool): whether to average the loss
  28. use_class_weight(bool): whether to use class weight
  29. '''
  30. def __init__(self,
  31. loss_weight=10.,
  32. is_cls_agnostic=False,
  33. num_classes=81,
  34. do_average=True,
  35. use_class_weight=True):
  36. super(GiouLoss, self).__init__()
  37. self.loss_weight = loss_weight
  38. self.is_cls_agnostic = is_cls_agnostic
  39. self.num_classes = num_classes
  40. self.do_average = do_average
  41. self.class_weight = 2 if is_cls_agnostic else num_classes
  42. self.use_class_weight = use_class_weight
  43. # deltas: NxMx4
  44. def bbox_transform(self, deltas, weights):
  45. wx, wy, ww, wh = weights
  46. deltas = fluid.layers.reshape(deltas, shape=(0, -1, 4))
  47. dx = fluid.layers.slice(deltas, axes=[2], starts=[0], ends=[1]) * wx
  48. dy = fluid.layers.slice(deltas, axes=[2], starts=[1], ends=[2]) * wy
  49. dw = fluid.layers.slice(deltas, axes=[2], starts=[2], ends=[3]) * ww
  50. dh = fluid.layers.slice(deltas, axes=[2], starts=[3], ends=[4]) * wh
  51. dw = fluid.layers.clip(dw, -1.e10, np.log(1000. / 16))
  52. dh = fluid.layers.clip(dh, -1.e10, np.log(1000. / 16))
  53. pred_ctr_x = dx
  54. pred_ctr_y = dy
  55. pred_w = fluid.layers.exp(dw)
  56. pred_h = fluid.layers.exp(dh)
  57. x1 = pred_ctr_x - 0.5 * pred_w
  58. y1 = pred_ctr_y - 0.5 * pred_h
  59. x2 = pred_ctr_x + 0.5 * pred_w
  60. y2 = pred_ctr_y + 0.5 * pred_h
  61. x1 = fluid.layers.reshape(x1, shape=(-1, ))
  62. y1 = fluid.layers.reshape(y1, shape=(-1, ))
  63. x2 = fluid.layers.reshape(x2, shape=(-1, ))
  64. y2 = fluid.layers.reshape(y2, shape=(-1, ))
  65. return x1, y1, x2, y2
  66. def __call__(self,
  67. x,
  68. y,
  69. inside_weight=None,
  70. outside_weight=None,
  71. bbox_reg_weight=[0.1, 0.1, 0.2, 0.2],
  72. use_transform=True):
  73. eps = 1.e-10
  74. if use_transform:
  75. x1, y1, x2, y2 = self.bbox_transform(x, bbox_reg_weight)
  76. x1g, y1g, x2g, y2g = self.bbox_transform(y, bbox_reg_weight)
  77. else:
  78. x1, y1, x2, y2 = fluid.layers.split(x, num_or_sections=4, dim=1)
  79. x1g, y1g, x2g, y2g = fluid.layers.split(
  80. y, num_or_sections=4, dim=1)
  81. x2 = fluid.layers.elementwise_max(x1, x2)
  82. y2 = fluid.layers.elementwise_max(y1, y2)
  83. xkis1 = fluid.layers.elementwise_max(x1, x1g)
  84. ykis1 = fluid.layers.elementwise_max(y1, y1g)
  85. xkis2 = fluid.layers.elementwise_min(x2, x2g)
  86. ykis2 = fluid.layers.elementwise_min(y2, y2g)
  87. xc1 = fluid.layers.elementwise_min(x1, x1g)
  88. yc1 = fluid.layers.elementwise_min(y1, y1g)
  89. xc2 = fluid.layers.elementwise_max(x2, x2g)
  90. yc2 = fluid.layers.elementwise_max(y2, y2g)
  91. intsctk = (xkis2 - xkis1) * (ykis2 - ykis1)
  92. intsctk = intsctk * fluid.layers.greater_than(
  93. xkis2, xkis1) * fluid.layers.greater_than(ykis2, ykis1)
  94. unionk = (x2 - x1) * (y2 - y1) + (x2g - x1g) * (y2g - y1g
  95. ) - intsctk + eps
  96. iouk = intsctk / unionk
  97. area_c = (xc2 - xc1) * (yc2 - yc1) + eps
  98. miouk = iouk - ((area_c - unionk) / area_c)
  99. iou_weights = 1
  100. if inside_weight is not None and outside_weight is not None:
  101. inside_weight = fluid.layers.reshape(inside_weight, shape=(-1, 4))
  102. outside_weight = fluid.layers.reshape(
  103. outside_weight, shape=(-1, 4))
  104. inside_weight = fluid.layers.reduce_mean(inside_weight, dim=1)
  105. outside_weight = fluid.layers.reduce_mean(outside_weight, dim=1)
  106. iou_weights = inside_weight * outside_weight
  107. elif outside_weight is not None:
  108. iou_weights = outside_weight
  109. if self.do_average:
  110. miouk = fluid.layers.reduce_mean((1 - miouk) * iou_weights)
  111. else:
  112. iou_distance = fluid.layers.elementwise_mul(
  113. 1 - miouk, iou_weights, axis=0)
  114. miouk = fluid.layers.reduce_sum(iou_distance)
  115. if self.use_class_weight:
  116. miouk = miouk * self.class_weight
  117. return miouk * self.loss_weight