gfocal_loss.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Copyright (c) 2021 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. import paddle
  19. import paddle.nn as nn
  20. import paddle.nn.functional as F
  21. from paddlex.ppdet.core.workspace import register, serializable
  22. from paddlex.ppdet.modeling import ops
  23. __all__ = ['QualityFocalLoss', 'DistributionFocalLoss']
  24. def quality_focal_loss(pred, target, beta=2.0, use_sigmoid=True):
  25. """
  26. Quality Focal Loss (QFL) is from `Generalized Focal Loss: Learning
  27. Qualified and Distributed Bounding Boxes for Dense Object Detection
  28. <https://arxiv.org/abs/2006.04388>`_.
  29. Args:
  30. pred (Tensor): Predicted joint representation of classification
  31. and quality (IoU) estimation with shape (N, C), C is the number of
  32. classes.
  33. target (tuple([Tensor])): Target category label with shape (N,)
  34. and target quality label with shape (N,).
  35. beta (float): The beta parameter for calculating the modulating factor.
  36. Defaults to 2.0.
  37. Returns:
  38. Tensor: Loss tensor with shape (N,).
  39. """
  40. assert len(target) == 2, """target for QFL must be a tuple of two elements,
  41. including category label and quality label, respectively"""
  42. # label denotes the category id, score denotes the quality score
  43. label, score = target
  44. if use_sigmoid:
  45. func = F.binary_cross_entropy_with_logits
  46. else:
  47. func = F.binary_cross_entropy
  48. # negatives are supervised by 0 quality score
  49. pred_sigmoid = F.sigmoid(pred) if use_sigmoid else pred
  50. scale_factor = pred_sigmoid
  51. zerolabel = paddle.zeros(pred.shape, dtype='float32')
  52. loss = func(pred, zerolabel, reduction='none') * scale_factor.pow(beta)
  53. # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
  54. bg_class_ind = pred.shape[1]
  55. pos = paddle.logical_and((label >= 0),
  56. (label < bg_class_ind)).nonzero().squeeze(1)
  57. if pos.shape[0] == 0:
  58. return loss.sum(axis=1)
  59. pos_label = paddle.gather(label, pos, axis=0)
  60. pos_mask = np.zeros(pred.shape, dtype=np.int32)
  61. pos_mask[pos.numpy(), pos_label.numpy()] = 1
  62. pos_mask = paddle.to_tensor(pos_mask, dtype='bool')
  63. score = score.unsqueeze(-1).expand([-1, pred.shape[1]]).cast('float32')
  64. # positives are supervised by bbox quality (IoU) score
  65. scale_factor_new = score - pred_sigmoid
  66. loss_pos = func(
  67. pred, score, reduction='none') * scale_factor_new.abs().pow(beta)
  68. loss = loss * paddle.logical_not(pos_mask) + loss_pos * pos_mask
  69. loss = loss.sum(axis=1)
  70. return loss
  71. def distribution_focal_loss(pred, label):
  72. """Distribution Focal Loss (DFL) is from `Generalized Focal Loss: Learning
  73. Qualified and Distributed Bounding Boxes for Dense Object Detection
  74. <https://arxiv.org/abs/2006.04388>`_.
  75. Args:
  76. pred (Tensor): Predicted general distribution of bounding boxes
  77. (before softmax) with shape (N, n+1), n is the max value of the
  78. integral set `{0, ..., n}` in paper.
  79. label (Tensor): Target distance label for bounding boxes with
  80. shape (N,).
  81. Returns:
  82. Tensor: Loss tensor with shape (N,).
  83. """
  84. dis_left = label.cast('int64')
  85. dis_right = dis_left + 1
  86. weight_left = dis_right.cast('float32') - label
  87. weight_right = label - dis_left.cast('float32')
  88. loss = F.cross_entropy(pred, dis_left, reduction='none') * weight_left \
  89. + F.cross_entropy(pred, dis_right, reduction='none') * weight_right
  90. return loss
  91. @register
  92. @serializable
  93. class QualityFocalLoss(nn.Layer):
  94. r"""Quality Focal Loss (QFL) is a variant of `Generalized Focal Loss:
  95. Learning Qualified and Distributed Bounding Boxes for Dense Object
  96. Detection <https://arxiv.org/abs/2006.04388>`_.
  97. Args:
  98. use_sigmoid (bool): Whether sigmoid operation is conducted in QFL.
  99. Defaults to True.
  100. beta (float): The beta parameter for calculating the modulating factor.
  101. Defaults to 2.0.
  102. reduction (str): Options are "none", "mean" and "sum".
  103. loss_weight (float): Loss weight of current loss.
  104. """
  105. def __init__(self,
  106. use_sigmoid=True,
  107. beta=2.0,
  108. reduction='mean',
  109. loss_weight=1.0):
  110. super(QualityFocalLoss, self).__init__()
  111. self.use_sigmoid = use_sigmoid
  112. self.beta = beta
  113. assert reduction in ('none', 'mean', 'sum')
  114. self.reduction = reduction
  115. self.loss_weight = loss_weight
  116. def forward(self, pred, target, weight=None, avg_factor=None):
  117. """Forward function.
  118. Args:
  119. pred (Tensor): Predicted joint representation of
  120. classification and quality (IoU) estimation with shape (N, C),
  121. C is the number of classes.
  122. target (tuple([Tensor])): Target category label with shape
  123. (N,) and target quality label with shape (N,).
  124. weight (Tensor, optional): The weight of loss for each
  125. prediction. Defaults to None.
  126. avg_factor (int, optional): Average factor that is used to average
  127. the loss. Defaults to None.
  128. """
  129. loss = self.loss_weight * quality_focal_loss(
  130. pred, target, beta=self.beta, use_sigmoid=self.use_sigmoid)
  131. if weight is not None:
  132. loss = loss * weight
  133. if avg_factor is None:
  134. if self.reduction == 'none':
  135. return loss
  136. elif self.reduction == 'mean':
  137. return loss.mean()
  138. elif self.reduction == 'sum':
  139. return loss.sum()
  140. else:
  141. # if reduction is mean, then average the loss by avg_factor
  142. if self.reduction == 'mean':
  143. loss = loss.sum() / avg_factor
  144. # if reduction is 'none', then do nothing, otherwise raise an error
  145. elif self.reduction != 'none':
  146. raise ValueError(
  147. 'avg_factor can not be used with reduction="sum"')
  148. return loss
  149. @register
  150. @serializable
  151. class DistributionFocalLoss(nn.Layer):
  152. """Distribution Focal Loss (DFL) is a variant of `Generalized Focal Loss:
  153. Learning Qualified and Distributed Bounding Boxes for Dense Object
  154. Detection <https://arxiv.org/abs/2006.04388>`_.
  155. Args:
  156. reduction (str): Options are `'none'`, `'mean'` and `'sum'`.
  157. loss_weight (float): Loss weight of current loss.
  158. """
  159. def __init__(self, reduction='mean', loss_weight=1.0):
  160. super(DistributionFocalLoss, self).__init__()
  161. assert reduction in ('none', 'mean', 'sum')
  162. self.reduction = reduction
  163. self.loss_weight = loss_weight
  164. def forward(self, pred, target, weight=None, avg_factor=None):
  165. """Forward function.
  166. Args:
  167. pred (Tensor): Predicted general distribution of bounding
  168. boxes (before softmax) with shape (N, n+1), n is the max value
  169. of the integral set `{0, ..., n}` in paper.
  170. target (Tensor): Target distance label for bounding boxes
  171. with shape (N,).
  172. weight (Tensor, optional): The weight of loss for each
  173. prediction. Defaults to None.
  174. avg_factor (int, optional): Average factor that is used to average
  175. the loss. Defaults to None.
  176. """
  177. loss = self.loss_weight * distribution_focal_loss(pred, target)
  178. if weight is not None:
  179. loss = loss * weight
  180. if avg_factor is None:
  181. if self.reduction == 'none':
  182. return loss
  183. elif self.reduction == 'mean':
  184. return loss.mean()
  185. elif self.reduction == 'sum':
  186. return loss.sum()
  187. else:
  188. # if reduction is mean, then average the loss by avg_factor
  189. if self.reduction == 'mean':
  190. loss = loss.sum() / avg_factor
  191. # if reduction is 'none', then do nothing, otherwise raise an error
  192. elif self.reduction != 'none':
  193. raise ValueError(
  194. 'avg_factor can not be used with reduction="sum"')
  195. return loss