You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

losses.py 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """OhemLoss."""
  16. import mindspore.nn as nn
  17. import mindspore.common.dtype as mstype
  18. from mindspore.ops import operations as P
  19. from mindspore.ops import functional as F
  20. class OhemLoss(nn.Cell):
  21. """Ohem loss cell."""
  22. def __init__(self, num, ignore_label):
  23. super(OhemLoss, self).__init__()
  24. self.mul = P.Mul()
  25. self.shape = P.Shape()
  26. self.one_hot = nn.OneHot(-1, num, 1.0, 0.0)
  27. self.squeeze = P.Squeeze()
  28. self.num = num
  29. self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
  30. self.mean = P.ReduceMean()
  31. self.select = P.Select()
  32. self.reshape = P.Reshape()
  33. self.cast = P.Cast()
  34. self.not_equal = P.NotEqual()
  35. self.equal = P.Equal()
  36. self.reduce_sum = P.ReduceSum(keep_dims=False)
  37. self.fill = P.Fill()
  38. self.transpose = P.Transpose()
  39. self.ignore_label = ignore_label
  40. self.loss_weight = 1.0
  41. def construct(self, logits, labels):
  42. logits = self.transpose(logits, (0, 2, 3, 1))
  43. logits = self.reshape(logits, (-1, self.num))
  44. labels = F.cast(labels, mstype.int32)
  45. labels = self.reshape(labels, (-1,))
  46. one_hot_labels = self.one_hot(labels)
  47. losses = self.cross_entropy(logits, one_hot_labels)[0]
  48. weights = self.cast(self.not_equal(labels, self.ignore_label), mstype.float32) * self.loss_weight
  49. weighted_losses = self.mul(losses, weights)
  50. loss = self.reduce_sum(weighted_losses, (0,))
  51. zeros = self.fill(mstype.float32, self.shape(weights), 0.0)
  52. ones = self.fill(mstype.float32, self.shape(weights), 1.0)
  53. present = self.select(self.equal(weights, zeros), zeros, ones)
  54. present = self.reduce_sum(present, (0,))
  55. zeros = self.fill(mstype.float32, self.shape(present), 0.0)
  56. min_control = self.fill(mstype.float32, self.shape(present), 1.0)
  57. present = self.select(self.equal(present, zeros), min_control, present)
  58. loss = loss / present
  59. return loss