basic_loss.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #copyright (c) 2021 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 as nn
  16. import paddle.nn.functional as F
  17. from paddle.nn import L1Loss
  18. from paddle.nn import MSELoss as L2Loss
  19. from paddle.nn import SmoothL1Loss
  20. class CELoss(nn.Layer):
  21. def __init__(self, epsilon=None):
  22. super().__init__()
  23. if epsilon is not None and (epsilon <= 0 or epsilon >= 1):
  24. epsilon = None
  25. self.epsilon = epsilon
  26. def _labelsmoothing(self, target, class_num):
  27. if target.shape[-1] != class_num:
  28. one_hot_target = F.one_hot(target, class_num)
  29. else:
  30. one_hot_target = target
  31. soft_target = F.label_smooth(one_hot_target, epsilon=self.epsilon)
  32. soft_target = paddle.reshape(soft_target, shape=[-1, class_num])
  33. return soft_target
  34. def forward(self, x, label):
  35. loss_dict = {}
  36. if self.epsilon is not None:
  37. class_num = x.shape[-1]
  38. label = self._labelsmoothing(label, class_num)
  39. x = -F.log_softmax(x, axis=-1)
  40. loss = paddle.sum(x * label, axis=-1)
  41. else:
  42. if label.shape[-1] == x.shape[-1]:
  43. label = F.softmax(label, axis=-1)
  44. soft_label = True
  45. else:
  46. soft_label = False
  47. loss = F.cross_entropy(x, label=label, soft_label=soft_label)
  48. return loss
  49. class KLJSLoss(object):
  50. def __init__(self, mode='kl'):
  51. assert mode in ['kl', 'js', 'KL', 'JS'
  52. ], "mode can only be one of ['kl', 'KL', 'js', 'JS']"
  53. self.mode = mode
  54. def __call__(self, p1, p2, reduction="mean", eps=1e-5):
  55. if self.mode.lower() == 'kl':
  56. loss = paddle.multiply(p2,
  57. paddle.log((p2 + eps) / (p1 + eps) + eps))
  58. loss += paddle.multiply(p1,
  59. paddle.log((p1 + eps) / (p2 + eps) + eps))
  60. loss *= 0.5
  61. elif self.mode.lower() == "js":
  62. loss = paddle.multiply(
  63. p2, paddle.log((2 * p2 + eps) / (p1 + p2 + eps) + eps))
  64. loss += paddle.multiply(
  65. p1, paddle.log((2 * p1 + eps) / (p1 + p2 + eps) + eps))
  66. loss *= 0.5
  67. else:
  68. raise ValueError(
  69. "The mode.lower() if KLJSLoss should be one of ['kl', 'js']")
  70. if reduction == "mean":
  71. loss = paddle.mean(loss, axis=[1, 2])
  72. elif reduction == "none" or reduction is None:
  73. return loss
  74. else:
  75. loss = paddle.sum(loss, axis=[1, 2])
  76. return loss
  77. class DMLLoss(nn.Layer):
  78. """
  79. DMLLoss
  80. """
  81. def __init__(self, act=None, use_log=False):
  82. super().__init__()
  83. if act is not None:
  84. assert act in ["softmax", "sigmoid"]
  85. if act == "softmax":
  86. self.act = nn.Softmax(axis=-1)
  87. elif act == "sigmoid":
  88. self.act = nn.Sigmoid()
  89. else:
  90. self.act = None
  91. self.use_log = use_log
  92. self.jskl_loss = KLJSLoss(mode="kl")
  93. def _kldiv(self, x, target):
  94. eps = 1.0e-10
  95. loss = target * (paddle.log(target + eps) - x)
  96. # batch mean loss
  97. loss = paddle.sum(loss) / loss.shape[0]
  98. return loss
  99. def forward(self, out1, out2):
  100. if self.act is not None:
  101. out1 = self.act(out1) + 1e-10
  102. out2 = self.act(out2) + 1e-10
  103. if self.use_log:
  104. # for recognition distillation, log is needed for feature map
  105. log_out1 = paddle.log(out1)
  106. log_out2 = paddle.log(out2)
  107. loss = (
  108. self._kldiv(log_out1, out2) + self._kldiv(log_out2, out1)) / 2.0
  109. else:
  110. # for detection distillation log is not needed
  111. loss = self.jskl_loss(out1, out2)
  112. return loss
  113. class DistanceLoss(nn.Layer):
  114. """
  115. DistanceLoss:
  116. mode: loss mode
  117. """
  118. def __init__(self, mode="l2", **kargs):
  119. super().__init__()
  120. assert mode in ["l1", "l2", "smooth_l1"]
  121. if mode == "l1":
  122. self.loss_func = nn.L1Loss(**kargs)
  123. elif mode == "l2":
  124. self.loss_func = nn.MSELoss(**kargs)
  125. elif mode == "smooth_l1":
  126. self.loss_func = nn.SmoothL1Loss(**kargs)
  127. def forward(self, x, y):
  128. return self.loss_func(x, y)
  129. class LossFromOutput(nn.Layer):
  130. def __init__(self, key='loss', reduction='none'):
  131. super().__init__()
  132. self.key = key
  133. self.reduction = reduction
  134. def forward(self, predicts, batch):
  135. loss = predicts
  136. if self.key is not None and isinstance(predicts, dict):
  137. loss = loss[self.key]
  138. if self.reduction == 'mean':
  139. loss = paddle.mean(loss)
  140. elif self.reduction == 'sum':
  141. loss = paddle.sum(loss)
  142. return {'loss': loss}