ctc_loss.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. import torch
  2. from torch import nn
  3. class CTCLoss(nn.Module):
  4. def __init__(self, use_focal_loss=False, zero_infinity=False, **kwargs):
  5. super(CTCLoss, self).__init__()
  6. self.loss_func = nn.CTCLoss(blank=0,
  7. reduction='none',
  8. zero_infinity=zero_infinity)
  9. self.use_focal_loss = use_focal_loss
  10. def forward(self, predicts, batch):
  11. # predicts = predicts['res']
  12. batch_size = predicts.size(0)
  13. label, label_length = batch[1], batch[2]
  14. predicts = predicts.log_softmax(2)
  15. predicts = predicts.permute(1, 0, 2)
  16. preds_lengths = torch.tensor([predicts.size(0)] * batch_size,
  17. dtype=torch.long)
  18. loss = self.loss_func(predicts, label, preds_lengths, label_length)
  19. if self.use_focal_loss:
  20. # Use torch.clamp to limit the range of loss, avoiding overflow in exponential calculation
  21. clamped_loss = torch.clamp(loss, min=-20, max=20)
  22. weight = 1 - torch.exp(-clamped_loss)
  23. weight = torch.square(weight)
  24. # Use torch.where to avoid multiplying by zero weight
  25. loss = torch.where(weight > 0, loss * weight, loss)
  26. loss = loss.mean()
  27. return {'loss': loss}