Padding the data into training data Args: data: Iterable[List[{key, feat, label}]] Returns: Iterable[Tuple(keys, feats, labels, feats lengths, label lengths)]
(data, mode='train')
| 453 | |
| 454 | |
| 455 | def padding(data, mode='train'): |
| 456 | """ Padding the data into training data |
| 457 | |
| 458 | Args: |
| 459 | data: Iterable[List[{key, feat, label}]] |
| 460 | |
| 461 | Returns: |
| 462 | Iterable[Tuple(keys, feats, labels, feats lengths, label lengths)] |
| 463 | """ |
| 464 | if mode == "train": |
| 465 | for sample in data: |
| 466 | assert isinstance(sample, list) |
| 467 | if len(sample) != 0: |
| 468 | acoustic_feat_len = torch.tensor( |
| 469 | [x['acoustic_token'].size(0) for x in sample], |
| 470 | dtype=torch.int32) |
| 471 | order = torch.argsort(acoustic_feat_len, descending=True) |
| 472 | utts = [sample[i]['utt'] for i in order] |
| 473 | acoustic_token = [ |
| 474 | sample[i]['acoustic_token'].clone().to(torch.int32) for i in |
| 475 | order] |
| 476 | acoustic_token_len = torch.tensor( |
| 477 | [i.size(0) for i in acoustic_token], dtype=torch.int32) |
| 478 | |
| 479 | acoustic_token = pad_sequence(acoustic_token, |
| 480 | batch_first=True, |
| 481 | padding_value=0) |
| 482 | |
| 483 | text = [sample[i]['text'] for i in order] |
| 484 | text_token = [torch.tensor(sample[i]['text_token']).long() for i |
| 485 | in order] |
| 486 | text_token_len = torch.tensor([i.size(0) for i in text_token], |
| 487 | dtype=torch.int32) |
| 488 | text_token = pad_sequence(text_token, batch_first=True, |
| 489 | padding_value=0) |
| 490 | time_start = torch.tensor( |
| 491 | [sample[i]['time_start'] for i in order]) |
| 492 | time_end = torch.tensor([sample[i]['time_end'] for i in order]) |
| 493 | |
| 494 | if isinstance(sample[0]['chorus'], str): |
| 495 | chorus = torch.tensor( |
| 496 | [CHORUS[sample[i]['chorus']] for i in order]) |
| 497 | else: |
| 498 | chorus = [ |
| 499 | torch.tensor([CHORUS[t] for t in sample[i]['chorus']]) |
| 500 | for i in order] |
| 501 | chorus = pad_sequence(chorus, batch_first=True, |
| 502 | padding_value=-1) |
| 503 | |
| 504 | batch = { |
| 505 | "utts" : utts, |
| 506 | "acoustic_token" : acoustic_token, |
| 507 | "acoustic_token_len": acoustic_token_len, |
| 508 | "time_start" : time_start, |
| 509 | "time_end" : time_end, |
| 510 | "chorus" : chorus, |
| 511 | "text" : text, |
| 512 | "text_token" : text_token, |