rec_enhanced_ctc_loss.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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__(
  24. self,
  25. use_focal_loss=False,
  26. use_ace_loss=False,
  27. ace_loss_weight=0.1,
  28. use_center_loss=False,
  29. center_loss_weight=0.05,
  30. num_classes=6625,
  31. feat_dim=96,
  32. init_center=False,
  33. center_file_path=None,
  34. **kwargs,
  35. ):
  36. super(EnhancedCTCLoss, self).__init__()
  37. self.ctc_loss_func = CTCLoss(use_focal_loss=use_focal_loss)
  38. self.use_ace_loss = False
  39. if use_ace_loss:
  40. self.use_ace_loss = use_ace_loss
  41. self.ace_loss_func = ACELoss()
  42. self.ace_loss_weight = ace_loss_weight
  43. self.use_center_loss = False
  44. if use_center_loss:
  45. self.use_center_loss = use_center_loss
  46. self.center_loss_func = CenterLoss(
  47. num_classes=num_classes,
  48. feat_dim=feat_dim,
  49. init_center=init_center,
  50. center_file_path=center_file_path,
  51. )
  52. self.center_loss_weight = center_loss_weight
  53. def __call__(self, predicts, batch):
  54. loss = self.ctc_loss_func(predicts, batch)["loss"]
  55. if self.use_center_loss:
  56. center_loss = (
  57. self.center_loss_func(predicts, batch)["loss_center"]
  58. * self.center_loss_weight
  59. )
  60. loss = loss + center_loss
  61. if self.use_ace_loss:
  62. ace_loss = (
  63. self.ace_loss_func(predicts, batch)["loss_ace"] * self.ace_loss_weight
  64. )
  65. loss = loss + ace_loss
  66. return {"enhanced_ctc_loss": loss}