loss.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. import paddle
  15. import paddle.nn.functional as F
  16. __all__ = [
  17. 'CELoss', 'MixCELoss', 'GoogLeNetLoss', 'JSDivLoss', 'MultiLabelLoss'
  18. ]
  19. class Loss(object):
  20. """
  21. Loss
  22. """
  23. def __init__(self, class_dim=1000, epsilon=None):
  24. assert class_dim > 1, "class_dim=%d is not larger than 1" % (class_dim)
  25. self._class_dim = class_dim
  26. if epsilon is not None and epsilon >= 0.0 and epsilon <= 1.0:
  27. self._epsilon = epsilon
  28. self._label_smoothing = True
  29. else:
  30. self._epsilon = None
  31. self._label_smoothing = False
  32. def _labelsmoothing(self, target):
  33. if target.shape[-1] != self._class_dim:
  34. one_hot_target = F.one_hot(target, self._class_dim)
  35. else:
  36. one_hot_target = target
  37. soft_target = F.label_smooth(one_hot_target, epsilon=self._epsilon)
  38. soft_target = paddle.reshape(soft_target, shape=[-1, self._class_dim])
  39. return soft_target
  40. def _binary_crossentropy(self, input, target):
  41. if self._label_smoothing:
  42. target = self._labelsmoothing(target)
  43. cost = F.binary_cross_entropy_with_logits(
  44. logit=input, label=target)
  45. else:
  46. cost = F.binary_cross_entropy_with_logits(
  47. logit=input, label=target)
  48. avg_cost = paddle.mean(cost)
  49. return avg_cost
  50. def _crossentropy(self, input, target):
  51. if self._label_smoothing:
  52. target = self._labelsmoothing(target)
  53. input = -F.log_softmax(input, axis=-1)
  54. cost = paddle.sum(target * input, axis=-1)
  55. else:
  56. cost = F.cross_entropy(input=input, label=target)
  57. avg_cost = paddle.mean(cost)
  58. return avg_cost
  59. def _kldiv(self, input, target, name=None):
  60. eps = 1.0e-10
  61. cost = target * paddle.log(
  62. (target + eps) / (input + eps)) * self._class_dim
  63. return cost
  64. def _jsdiv(self, input, target):
  65. input = F.softmax(input)
  66. target = F.softmax(target)
  67. cost = self._kldiv(input, target) + self._kldiv(target, input)
  68. cost = cost / 2
  69. avg_cost = paddle.mean(cost)
  70. return avg_cost
  71. def __call__(self, input, target):
  72. pass
  73. class MultiLabelLoss(Loss):
  74. """
  75. Multilabel loss based binary cross entropy
  76. """
  77. def __init__(self, class_dim=1000, epsilon=None):
  78. super(MultiLabelLoss, self).__init__(class_dim, epsilon)
  79. def __call__(self, input, target):
  80. cost = self._binary_crossentropy(input, target)
  81. return cost
  82. class CELoss(Loss):
  83. """
  84. Cross entropy loss
  85. """
  86. def __init__(self, class_dim=1000, epsilon=None):
  87. super(CELoss, self).__init__(class_dim, epsilon)
  88. def __call__(self, input, target):
  89. cost = self._crossentropy(input, target)
  90. return cost
  91. class MixCELoss(Loss):
  92. """
  93. Cross entropy loss with mix(mixup, cutmix, fixmix)
  94. """
  95. def __init__(self, class_dim=1000, epsilon=None):
  96. super(MixCELoss, self).__init__(class_dim, epsilon)
  97. def __call__(self, input, target0, target1, lam):
  98. cost0 = self._crossentropy(input, target0)
  99. cost1 = self._crossentropy(input, target1)
  100. cost = lam * cost0 + (1.0 - lam) * cost1
  101. avg_cost = paddle.mean(cost)
  102. return avg_cost
  103. class GoogLeNetLoss(Loss):
  104. """
  105. Cross entropy loss used after googlenet
  106. """
  107. def __init__(self, class_dim=1000, epsilon=None):
  108. super(GoogLeNetLoss, self).__init__(class_dim, epsilon)
  109. def __call__(self, input0, input1, input2, target):
  110. cost0 = self._crossentropy(input0, target)
  111. cost1 = self._crossentropy(input1, target)
  112. cost2 = self._crossentropy(input2, target)
  113. cost = cost0 + 0.3 * cost1 + 0.3 * cost2
  114. avg_cost = paddle.mean(cost)
  115. return avg_cost
  116. class JSDivLoss(Loss):
  117. """
  118. JSDiv loss
  119. """
  120. def __init__(self, class_dim=1000, epsilon=None):
  121. super(JSDivLoss, self).__init__(class_dim, epsilon)
  122. def __call__(self, input, target):
  123. cost = self._jsdiv(input, target)
  124. return cost