rec_vl_loss.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """
  15. This code is refer from:
  16. https://github.com/wangyuxin87/VisionLAN
  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. class VLLoss(nn.Layer):
  24. def __init__(self, mode='LF_1', weight_res=0.5, weight_mas=0.5, **kwargs):
  25. super(VLLoss, self).__init__()
  26. self.loss_func = paddle.nn.loss.CrossEntropyLoss(reduction="mean")
  27. assert mode in ['LF_1', 'LF_2', 'LA']
  28. self.mode = mode
  29. self.weight_res = weight_res
  30. self.weight_mas = weight_mas
  31. def flatten_label(self, target):
  32. label_flatten = []
  33. label_length = []
  34. for i in range(0, target.shape[0]):
  35. cur_label = target[i].tolist()
  36. label_flatten += cur_label[:cur_label.index(0) + 1]
  37. label_length.append(cur_label.index(0) + 1)
  38. label_flatten = paddle.to_tensor(label_flatten, dtype='int64')
  39. label_length = paddle.to_tensor(label_length, dtype='int32')
  40. return (label_flatten, label_length)
  41. def _flatten(self, sources, lengths):
  42. return paddle.concat([t[:l] for t, l in zip(sources, lengths)])
  43. def forward(self, predicts, batch):
  44. text_pre = predicts[0]
  45. target = batch[1].astype('int64')
  46. label_flatten, length = self.flatten_label(target)
  47. text_pre = self._flatten(text_pre, length)
  48. if self.mode == 'LF_1':
  49. loss = self.loss_func(text_pre, label_flatten)
  50. else:
  51. text_rem = predicts[1]
  52. text_mas = predicts[2]
  53. target_res = batch[2].astype('int64')
  54. target_sub = batch[3].astype('int64')
  55. label_flatten_res, length_res = self.flatten_label(target_res)
  56. label_flatten_sub, length_sub = self.flatten_label(target_sub)
  57. text_rem = self._flatten(text_rem, length_res)
  58. text_mas = self._flatten(text_mas, length_sub)
  59. loss_ori = self.loss_func(text_pre, label_flatten)
  60. loss_res = self.loss_func(text_rem, label_flatten_res)
  61. loss_mas = self.loss_func(text_mas, label_flatten_sub)
  62. loss = loss_ori + loss_res * self.weight_res + loss_mas * self.weight_mas
  63. return {'loss': loss}