rec_ctc_loss.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # copyright (c) 2019 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. class CTCLoss(nn.Layer):
  20. def __init__(self, use_focal_loss=False, **kwargs):
  21. super(CTCLoss, self).__init__()
  22. self.loss_func = nn.CTCLoss(blank=0, reduction='none')
  23. self.use_focal_loss = use_focal_loss
  24. def forward(self, predicts, batch):
  25. if isinstance(predicts, (list, tuple)):
  26. predicts = predicts[-1]
  27. predicts = predicts.transpose((1, 0, 2))
  28. N, B, _ = predicts.shape
  29. preds_lengths = paddle.to_tensor(
  30. [N] * B, dtype='int64', place=paddle.CPUPlace())
  31. labels = batch[1].astype("int32")
  32. label_lengths = batch[2].astype('int64')
  33. loss = self.loss_func(predicts, labels, preds_lengths, label_lengths)
  34. if self.use_focal_loss:
  35. weight = paddle.exp(-loss)
  36. weight = paddle.subtract(paddle.to_tensor([1.0]), weight)
  37. weight = paddle.square(weight)
  38. loss = paddle.multiply(loss, weight)
  39. loss = loss.mean()
  40. return {'loss': loss}