iou_aware_loss.py 1.7 KB

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