cross_entropy_loss.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 CrossEntropyLoss(nn.Layer):
  20. """
  21. Implements the cross entropy loss function.
  22. Args:
  23. weight (tuple|list|ndarray|Tensor, optional): A manual rescaling weight
  24. given to each class. Its length must be equal to the number of classes.
  25. Default ``None``.
  26. ignore_index (int64, optional): Specifies a target value that is ignored
  27. and does not contribute to the input gradient. Default ``255``.
  28. top_k_percent_pixels (float, optional): the value lies in [0.0, 1.0]. When its value < 1.0, only compute the loss for
  29. the top k percent pixels (e.g., the top 20% pixels). This is useful for hard pixel mining.
  30. """
  31. def __init__(self, weight=None, ignore_index=255,
  32. top_k_percent_pixels=1.0):
  33. super(CrossEntropyLoss, self).__init__()
  34. if weight is not None:
  35. weight = paddle.to_tensor(weight, dtype='float32')
  36. self.weight = weight
  37. self.ignore_index = ignore_index
  38. self.top_k_percent_pixels = top_k_percent_pixels
  39. self.EPS = 1e-8
  40. def forward(self, logit, label, semantic_weights=None):
  41. """
  42. Forward computation.
  43. Args:
  44. logit (Tensor): Logit tensor, the data type is float32, float64. Shape is
  45. (N, C), where C is number of classes, and if shape is more than 2D, this
  46. is (N, C, D1, D2,..., Dk), k >= 1.
  47. label (Tensor): Label tensor, the data type is int64. Shape is (N), where each
  48. value is 0 <= label[i] <= C-1, and if shape is more than 2D, this is
  49. (N, D1, D2,..., Dk), k >= 1.
  50. """
  51. if self.weight is not None and logit.shape[1] != len(self.weight):
  52. raise ValueError(
  53. 'The number of weights = {} must be the same as the number of classes = {}.'
  54. .format(len(self.weight), logit.shape[1]))
  55. logit = paddle.transpose(logit, [0, 2, 3, 1])
  56. if self.weight is None:
  57. loss = F.cross_entropy(
  58. logit, label, ignore_index=self.ignore_index, reduction='none')
  59. else:
  60. label_one_hot = F.one_hot(label, logit.shape[-1])
  61. loss = F.cross_entropy(
  62. logit,
  63. label_one_hot * self.weight,
  64. soft_label=True,
  65. ignore_index=self.ignore_index,
  66. reduction='none')
  67. loss = loss.squeeze(-1)
  68. mask = label != self.ignore_index
  69. mask = paddle.cast(mask, 'float32')
  70. loss = loss * mask
  71. if semantic_weights is not None:
  72. loss = loss * semantic_weights
  73. label.stop_gradient = True
  74. mask.stop_gradient = True
  75. if self.top_k_percent_pixels == 1.0:
  76. avg_loss = paddle.mean(loss) / (paddle.mean(mask) + self.EPS)
  77. return avg_loss
  78. loss = loss.reshape((-1, ))
  79. top_k_pixels = int(self.top_k_percent_pixels * loss.numel())
  80. loss, _ = paddle.topk(loss, top_k_pixels)
  81. return loss.mean()