iou_aware_loss.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 paddle
  18. import paddle.nn.functional as F
  19. from paddlex.ppdet.core.workspace import register, serializable
  20. from .iou_loss import IouLoss
  21. from ..bbox_utils import xywh2xyxy, bbox_iou
  22. @register
  23. @serializable
  24. class IouAwareLoss(IouLoss):
  25. """
  26. iou aware loss, see https://arxiv.org/abs/1912.05992
  27. Args:
  28. loss_weight (float): iou aware loss weight, default is 1.0
  29. max_height (int): max height of input to support random shape input
  30. max_width (int): max width of input to support random shape input
  31. """
  32. def __init__(self, loss_weight=1.0, giou=False, diou=False, ciou=False):
  33. super(IouAwareLoss, self).__init__(
  34. loss_weight=loss_weight, giou=giou, diou=diou, ciou=ciou)
  35. def __call__(self, ioup, pbox, gbox):
  36. iou = bbox_iou(
  37. pbox, gbox, giou=self.giou, diou=self.diou, ciou=self.ciou)
  38. iou.stop_gradient = True
  39. loss_iou_aware = F.binary_cross_entropy_with_logits(
  40. ioup, iou, reduction='none')
  41. loss_iou_aware = loss_iou_aware * self.loss_weight
  42. return loss_iou_aware