rec_multi_loss.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # copyright (c) 2022 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 .rec_ctc_loss import CTCLoss
  20. from .rec_sar_loss import SARLoss
  21. from .rec_nrtr_loss import NRTRLoss
  22. class MultiLoss(nn.Layer):
  23. def __init__(self, **kwargs):
  24. super().__init__()
  25. self.loss_funcs = {}
  26. self.loss_list = kwargs.pop("loss_config_list")
  27. self.weight_1 = kwargs.get("weight_1", 1.0)
  28. self.weight_2 = kwargs.get("weight_2", 1.0)
  29. for loss_info in self.loss_list:
  30. for name, param in loss_info.items():
  31. if param is not None:
  32. kwargs.update(param)
  33. loss = eval(name)(**kwargs)
  34. self.loss_funcs[name] = loss
  35. def forward(self, predicts, batch):
  36. self.total_loss = {}
  37. total_loss = 0.0
  38. # batch [image, label_ctc, label_sar, length, valid_ratio]
  39. for name, loss_func in self.loss_funcs.items():
  40. if name == "CTCLoss":
  41. loss = (
  42. loss_func(predicts["ctc"], batch[:2] + batch[3:])["loss"]
  43. * self.weight_1
  44. )
  45. elif name == "SARLoss":
  46. loss = (
  47. loss_func(predicts["sar"], batch[:1] + batch[2:])["loss"]
  48. * self.weight_2
  49. )
  50. elif name == "NRTRLoss":
  51. loss = (
  52. loss_func(predicts["gtc"], batch[:1] + batch[2:])["loss"]
  53. * self.weight_2
  54. )
  55. else:
  56. raise NotImplementedError(
  57. "{} is not supported in MultiLoss yet".format(name)
  58. )
  59. self.total_loss[name] = loss
  60. total_loss += loss
  61. self.total_loss["loss"] = total_loss
  62. return self.total_loss