solov2_loss.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. from paddle import fluid
  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 = fluid.layers.reshape(
  40. input, shape=(fluid.layers.shape(input)[0], -1))
  41. target = fluid.layers.reshape(
  42. target, shape=(fluid.layers.shape(target)[0], -1))
  43. target = fluid.layers.cast(target, 'float32')
  44. a = fluid.layers.reduce_sum(paddle.multiply(input, target), dim=1)
  45. b = fluid.layers.reduce_sum(
  46. paddle.multiply(input, input), dim=1) + 0.001
  47. c = fluid.layers.reduce_sum(
  48. paddle.multiply(target, target), dim=1) + 0.001
  49. d = paddle.divide((2 * a), paddle.add(b, c))
  50. return 1 - d
  51. def __call__(self, ins_pred_list, ins_label_list, cate_preds, cate_labels,
  52. num_ins):
  53. """
  54. Get loss of network of SOLOv2.
  55. Args:
  56. ins_pred_list (list): Variable list of instance branch output.
  57. ins_label_list (list): List of instance labels pre batch.
  58. cate_preds (list): Concat Variable list of categroy branch output.
  59. cate_labels (list): Concat list of categroy labels pre batch.
  60. num_ins (int): Number of positive samples in a mini-batch.
  61. Returns:
  62. loss_ins (Variable): The instance loss Variable of SOLOv2 network.
  63. loss_cate (Variable): The category loss Variable of SOLOv2 network.
  64. """
  65. # Ues dice_loss to calculate instance loss
  66. loss_ins = []
  67. total_weights = fluid.layers.zeros(shape=[1], dtype='float32')
  68. for input, target in zip(ins_pred_list, ins_label_list):
  69. weights = fluid.layers.cast(
  70. fluid.layers.reduce_sum(
  71. target, dim=[1, 2]) > 0, 'float32')
  72. input = fluid.layers.sigmoid(input)
  73. dice_out = fluid.layers.elementwise_mul(
  74. self._dice_loss(input, target), weights)
  75. total_weights += fluid.layers.reduce_sum(weights)
  76. loss_ins.append(dice_out)
  77. loss_ins = fluid.layers.reduce_sum(fluid.layers.concat(
  78. loss_ins)) / total_weights
  79. loss_ins = loss_ins * self.ins_loss_weight
  80. # Ues sigmoid_focal_loss to calculate category loss
  81. loss_cate = fluid.layers.sigmoid_focal_loss(
  82. x=cate_preds,
  83. label=cate_labels,
  84. fg_num=num_ins + 1,
  85. gamma=self.focal_loss_gamma,
  86. alpha=self.focal_loss_alpha)
  87. loss_cate = fluid.layers.reduce_sum(loss_cate)
  88. return loss_ins, loss_cate