rec_satrn_loss.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/open-mmlab/mmocr/blob/1.x/mmocr/models/textrecog/module_losses/ce_module_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. class SATRNLoss(nn.Layer):
  24. def __init__(self, **kwargs):
  25. super(SATRNLoss, self).__init__()
  26. ignore_index = kwargs.get("ignore_index", 92) # 6626
  27. self.loss_func = paddle.nn.loss.CrossEntropyLoss(
  28. reduction="none", ignore_index=ignore_index
  29. )
  30. def forward(self, predicts, batch):
  31. predict = predicts[
  32. :, :-1, :
  33. ] # ignore last index of outputs to be in same seq_len with targets
  34. label = batch[1].astype("int64")[
  35. :, 1:
  36. ] # ignore first index of target in loss calculation
  37. batch_size, num_steps, num_classes = (
  38. predict.shape[0],
  39. predict.shape[1],
  40. predict.shape[2],
  41. )
  42. assert (
  43. len(label.shape) == len(list(predict.shape)) - 1
  44. ), "The target's shape and inputs's shape is [N, d] and [N, num_steps]"
  45. inputs = paddle.reshape(predict, [-1, num_classes])
  46. targets = paddle.reshape(label, [-1])
  47. loss = self.loss_func(inputs, targets)
  48. return {"loss": loss.mean()}