bootstrapped_cross_entropy.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import paddle
  15. from paddle import nn
  16. import paddle.nn.functional as F
  17. from paddlex.paddleseg.cvlibs import manager
  18. @manager.LOSSES.add_component
  19. class BootstrappedCrossEntropyLoss(nn.Layer):
  20. """
  21. Implements the cross entropy loss function.
  22. Args:
  23. min_K (int): the minimum number of pixels to be counted in loss computation.
  24. loss_th (float): the loss threshold. Only loss that is larger than the threshold
  25. would be calculated.
  26. weight (tuple|list, optional): The weight for different classes. Default: None.
  27. ignore_index (int, optional): Specifies a target value that is ignored
  28. and does not contribute to the input gradient. Default: 255.
  29. """
  30. def __init__(self, min_K, loss_th, weight=None, ignore_index=255):
  31. super().__init__()
  32. self.ignore_index = ignore_index
  33. self.K = min_K
  34. self.threshold = loss_th
  35. if weight is not None:
  36. weight = paddle.to_tensor(weight, dtype='float32')
  37. self.weight = weight
  38. def forward(self, logit, label):
  39. n, c, h, w = logit.shape
  40. total_loss = 0.0
  41. if len(label.shape) != len(logit.shape):
  42. label = paddle.unsqueeze(label, 1)
  43. for i in range(n):
  44. x = paddle.unsqueeze(logit[i], 0)
  45. y = paddle.unsqueeze(label[i], 0)
  46. x = paddle.transpose(x, (0, 2, 3, 1))
  47. y = paddle.transpose(y, (0, 2, 3, 1))
  48. x = paddle.reshape(x, shape=(-1, c))
  49. y = paddle.reshape(y, shape=(-1, ))
  50. loss = F.cross_entropy(
  51. x,
  52. y,
  53. weight=self.weight,
  54. ignore_index=self.ignore_index,
  55. reduction="none")
  56. sorted_loss = paddle.sort(loss, descending=True)
  57. if sorted_loss[self.K] > self.threshold:
  58. new_indices = paddle.nonzero(sorted_loss > self.threshold)
  59. loss = paddle.gather(sorted_loss, new_indices)
  60. else:
  61. loss = sorted_loss[:self.K]
  62. total_loss += paddle.mean(loss)
  63. return total_loss / float(n)