solov2_loss.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import paddle
  18. import paddle.nn.functional as F
  19. from ppdet.core.workspace import register, serializable
  20. __all__ = ['SOLOv2Loss']
  21. @register
  22. @serializable
  23. class SOLOv2Loss(object):
  24. """
  25. SOLOv2Loss
  26. Args:
  27. ins_loss_weight (float): Weight of instance loss.
  28. focal_loss_gamma (float): Gamma parameter for focal loss.
  29. focal_loss_alpha (float): Alpha parameter for focal loss.
  30. """
  31. def __init__(self,
  32. ins_loss_weight=3.0,
  33. focal_loss_gamma=2.0,
  34. focal_loss_alpha=0.25):
  35. self.ins_loss_weight = ins_loss_weight
  36. self.focal_loss_gamma = focal_loss_gamma
  37. self.focal_loss_alpha = focal_loss_alpha
  38. def _dice_loss(self, input, target):
  39. input = paddle.reshape(input, shape=(paddle.shape(input)[0], -1))
  40. target = paddle.reshape(target, shape=(paddle.shape(target)[0], -1))
  41. a = paddle.sum(input * target, axis=1)
  42. b = paddle.sum(input * input, axis=1) + 0.001
  43. c = paddle.sum(target * target, axis=1) + 0.001
  44. d = (2 * a) / (b + c)
  45. return 1 - d
  46. def __call__(self, ins_pred_list, ins_label_list, cate_preds, cate_labels,
  47. num_ins):
  48. """
  49. Get loss of network of SOLOv2.
  50. Args:
  51. ins_pred_list (list): Variable list of instance branch output.
  52. ins_label_list (list): List of instance labels pre batch.
  53. cate_preds (list): Concat Variable list of categroy branch output.
  54. cate_labels (list): Concat list of categroy labels pre batch.
  55. num_ins (int): Number of positive samples in a mini-batch.
  56. Returns:
  57. loss_ins (Variable): The instance loss Variable of SOLOv2 network.
  58. loss_cate (Variable): The category loss Variable of SOLOv2 network.
  59. """
  60. #1. Ues dice_loss to calculate instance loss
  61. loss_ins = []
  62. total_weights = paddle.zeros(shape=[1], dtype='float32')
  63. for input, target in zip(ins_pred_list, ins_label_list):
  64. if input is None:
  65. continue
  66. target = paddle.cast(target, 'float32')
  67. target = paddle.reshape(
  68. target,
  69. shape=[-1, paddle.shape(input)[-2], paddle.shape(input)[-1]])
  70. weights = paddle.cast(
  71. paddle.sum(target, axis=[1, 2]) > 0, 'float32')
  72. input = F.sigmoid(input)
  73. dice_out = paddle.multiply(self._dice_loss(input, target), weights)
  74. total_weights += paddle.sum(weights)
  75. loss_ins.append(dice_out)
  76. loss_ins = paddle.sum(paddle.concat(loss_ins)) / total_weights
  77. loss_ins = loss_ins * self.ins_loss_weight
  78. #2. Ues sigmoid_focal_loss to calculate category loss
  79. # expand onehot labels
  80. num_classes = cate_preds.shape[-1]
  81. cate_labels_bin = F.one_hot(cate_labels, num_classes=num_classes + 1)
  82. cate_labels_bin = cate_labels_bin[:, 1:]
  83. loss_cate = F.sigmoid_focal_loss(
  84. cate_preds,
  85. label=cate_labels_bin,
  86. normalizer=num_ins + 1.,
  87. gamma=self.focal_loss_gamma,
  88. alpha=self.focal_loss_alpha)
  89. return loss_ins, loss_cate