focal_loss.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. import paddle.nn as nn
  20. from ppdet.core.workspace import register
  21. __all__ = ['FocalLoss']
  22. @register
  23. class FocalLoss(nn.Layer):
  24. """A wrapper around paddle.nn.functional.sigmoid_focal_loss.
  25. Args:
  26. use_sigmoid (bool): currently only support use_sigmoid=True
  27. alpha (float): parameter alpha in Focal Loss
  28. gamma (float): parameter gamma in Focal Loss
  29. loss_weight (float): final loss will be multiplied by this
  30. """
  31. def __init__(self,
  32. use_sigmoid=True,
  33. alpha=0.25,
  34. gamma=2.0,
  35. loss_weight=1.0):
  36. super(FocalLoss, self).__init__()
  37. assert use_sigmoid == True, \
  38. 'Focal Loss only supports sigmoid at the moment'
  39. self.use_sigmoid = use_sigmoid
  40. self.alpha = alpha
  41. self.gamma = gamma
  42. self.loss_weight = loss_weight
  43. def forward(self, pred, target, reduction='none'):
  44. """forward function.
  45. Args:
  46. pred (Tensor): logits of class prediction, of shape (N, num_classes)
  47. target (Tensor): target class label, of shape (N, )
  48. reduction (str): the way to reduce loss, one of (none, sum, mean)
  49. """
  50. num_classes = pred.shape[1]
  51. target = F.one_hot(target, num_classes+1).cast(pred.dtype)
  52. target = target[:, :-1].detach()
  53. loss = F.sigmoid_focal_loss(
  54. pred, target, alpha=self.alpha, gamma=self.gamma,
  55. reduction=reduction)
  56. return loss * self.loss_weight