det_db_loss.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
  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. This code is refer from:
  16. https://github.com/WenmuZhou/DBNet.pytorch/blob/master/models/losses/DB_loss.py
  17. """
  18. from __future__ import absolute_import
  19. from __future__ import division
  20. from __future__ import print_function
  21. import paddle
  22. from paddle import nn
  23. from .det_basic_loss import BalanceLoss, MaskL1Loss, DiceLoss
  24. class DBLoss(nn.Layer):
  25. """
  26. Differentiable Binarization (DB) Loss Function
  27. args:
  28. param (dict): the super parameter for DB Loss
  29. """
  30. def __init__(
  31. self,
  32. balance_loss=True,
  33. main_loss_type="DiceLoss",
  34. alpha=5,
  35. beta=10,
  36. ohem_ratio=3,
  37. eps=1e-6,
  38. **kwargs,
  39. ):
  40. super(DBLoss, self).__init__()
  41. self.alpha = alpha
  42. self.beta = beta
  43. self.dice_loss = DiceLoss(eps=eps)
  44. self.l1_loss = MaskL1Loss(eps=eps)
  45. self.bce_loss = BalanceLoss(
  46. balance_loss=balance_loss,
  47. main_loss_type=main_loss_type,
  48. negative_ratio=ohem_ratio,
  49. )
  50. def forward(self, predicts, labels):
  51. predict_maps = predicts["maps"]
  52. (
  53. label_threshold_map,
  54. label_threshold_mask,
  55. label_shrink_map,
  56. label_shrink_mask,
  57. ) = labels[1:]
  58. shrink_maps = predict_maps[:, 0, :, :]
  59. threshold_maps = predict_maps[:, 1, :, :]
  60. binary_maps = predict_maps[:, 2, :, :]
  61. loss_shrink_maps = self.bce_loss(
  62. shrink_maps, label_shrink_map, label_shrink_mask
  63. )
  64. loss_threshold_maps = self.l1_loss(
  65. threshold_maps, label_threshold_map, label_threshold_mask
  66. )
  67. loss_binary_maps = self.dice_loss(
  68. binary_maps, label_shrink_map, label_shrink_mask
  69. )
  70. loss_shrink_maps = self.alpha * loss_shrink_maps
  71. loss_threshold_maps = self.beta * loss_threshold_maps
  72. # CBN loss
  73. if "distance_maps" in predicts.keys():
  74. distance_maps = predicts["distance_maps"]
  75. cbn_maps = predicts["cbn_maps"]
  76. cbn_loss = self.bce_loss(
  77. cbn_maps[:, 0, :, :], label_shrink_map, label_shrink_mask
  78. )
  79. else:
  80. dis_loss = paddle.to_tensor([0.0])
  81. cbn_loss = paddle.to_tensor([0.0])
  82. loss_all = loss_shrink_maps + loss_threshold_maps + loss_binary_maps
  83. losses = {
  84. "loss": loss_all + cbn_loss,
  85. "loss_shrink_maps": loss_shrink_maps,
  86. "loss_threshold_maps": loss_threshold_maps,
  87. "loss_binary_maps": loss_binary_maps,
  88. "loss_cbn": cbn_loss,
  89. }
  90. return losses