rec_enhanced_ctc_loss.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # copyright (c) 2021 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import paddle
  18. from paddle import nn
  19. from .ace_loss import ACELoss
  20. from .center_loss import CenterLoss
  21. from .rec_ctc_loss import CTCLoss
  22. class EnhancedCTCLoss(nn.Layer):
  23. def __init__(self,
  24. use_focal_loss=False,
  25. use_ace_loss=False,
  26. ace_loss_weight=0.1,
  27. use_center_loss=False,
  28. center_loss_weight=0.05,
  29. num_classes=6625,
  30. feat_dim=96,
  31. init_center=False,
  32. center_file_path=None,
  33. **kwargs):
  34. super(EnhancedCTCLoss, self).__init__()
  35. self.ctc_loss_func = CTCLoss(use_focal_loss=use_focal_loss)
  36. self.use_ace_loss = False
  37. if use_ace_loss:
  38. self.use_ace_loss = use_ace_loss
  39. self.ace_loss_func = ACELoss()
  40. self.ace_loss_weight = ace_loss_weight
  41. self.use_center_loss = False
  42. if use_center_loss:
  43. self.use_center_loss = use_center_loss
  44. self.center_loss_func = CenterLoss(
  45. num_classes=num_classes,
  46. feat_dim=feat_dim,
  47. init_center=init_center,
  48. center_file_path=center_file_path)
  49. self.center_loss_weight = center_loss_weight
  50. def __call__(self, predicts, batch):
  51. loss = self.ctc_loss_func(predicts, batch)["loss"]
  52. if self.use_center_loss:
  53. center_loss = self.center_loss_func(
  54. predicts, batch)["loss_center"] * self.center_loss_weight
  55. loss = loss + center_loss
  56. if self.use_ace_loss:
  57. ace_loss = self.ace_loss_func(
  58. predicts, batch)["loss_ace"] * self.ace_loss_weight
  59. loss = loss + ace_loss
  60. return {'enhanced_ctc_loss': loss}