You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.9 KiB

  1. import random
  2. import torch
  3. import torch.utils.data
  4. import layers
  5. from utils import load_wav_to_torch, load_filepaths_and_text
  6. from text import text_to_sequence
  7. class TextMelLoader(torch.utils.data.Dataset):
  8. """
  9. 1) loads audio,text pairs
  10. 2) normalizes text and converts them to sequences of one-hot vectors
  11. 3) computes mel-spectrograms from audio files.
  12. """
  13. def __init__(self, audiopaths_and_text, hparams, shuffle=True):
  14. self.audiopaths_and_text = load_filepaths_and_text(
  15. audiopaths_and_text, hparams.sort_by_length)
  16. self.text_cleaners = hparams.text_cleaners
  17. self.max_wav_value = hparams.max_wav_value
  18. self.sampling_rate = hparams.sampling_rate
  19. self.stft = layers.TacotronSTFT(
  20. hparams.filter_length, hparams.hop_length, hparams.win_length,
  21. hparams.n_mel_channels, hparams.sampling_rate, hparams.mel_fmin,
  22. hparams.mel_fmax)
  23. random.seed(1234)
  24. if shuffle:
  25. random.shuffle(self.audiopaths_and_text)
  26. def get_mel_text_pair(self, audiopath_and_text):
  27. # separate filename and text
  28. audiopath, text = audiopath_and_text[0], audiopath_and_text[1]
  29. text = self.get_text(text)
  30. mel = self.get_mel(audiopath)
  31. return (text, mel)
  32. def get_mel(self, filename):
  33. audio = load_wav_to_torch(filename, self.sampling_rate)
  34. audio_norm = audio / self.max_wav_value
  35. audio_norm = audio_norm.unsqueeze(0)
  36. audio_norm = torch.autograd.Variable(audio_norm, requires_grad=False)
  37. melspec = self.stft.mel_spectrogram(audio_norm)
  38. melspec = torch.squeeze(melspec, 0)
  39. return melspec
  40. def get_text(self, text):
  41. text_norm = torch.IntTensor(text_to_sequence(text, self.text_cleaners))
  42. return text_norm
  43. def __getitem__(self, index):
  44. return self.get_mel_text_pair(self.audiopaths_and_text[index])
  45. def __len__(self):
  46. return len(self.audiopaths_and_text)
  47. class TextMelCollate():
  48. """ Zero-pads model inputs and targets based on number of frames per setep
  49. """
  50. def __init__(self, n_frames_per_step):
  51. self.n_frames_per_step = n_frames_per_step
  52. def __call__(self, batch):
  53. """Collate's training batch from normalized text and mel-spectrogram
  54. PARAMS
  55. ------
  56. batch: [text_normalized, mel_normalized]
  57. """
  58. # Right zero-pad all one-hot text sequences to max input length
  59. input_lengths, ids_sorted_decreasing = torch.sort(
  60. torch.LongTensor([len(x[0]) for x in batch]),
  61. dim=0, descending=True)
  62. max_input_len = input_lengths[0]
  63. text_padded = torch.LongTensor(len(batch), max_input_len)
  64. text_padded.zero_()
  65. for i in range(len(ids_sorted_decreasing)):
  66. text = batch[ids_sorted_decreasing[i]][0]
  67. text_padded[i, :text.size(0)] = text
  68. # Right zero-pad mel-spec with extra single zero vector to mark the end
  69. num_mels = batch[0][1].size(0)
  70. max_target_len = max([x[1].size(1) for x in batch]) + 1
  71. if max_target_len % self.n_frames_per_step != 0:
  72. max_target_len += self.n_frames_per_step - max_target_len % self.n_frames_per_step
  73. assert max_target_len % self.n_frames_per_step == 0
  74. # include mel padded and gate padded
  75. mel_padded = torch.FloatTensor(len(batch), num_mels, max_target_len)
  76. mel_padded.zero_()
  77. gate_padded = torch.FloatTensor(len(batch), max_target_len)
  78. gate_padded.zero_()
  79. output_lengths = torch.LongTensor(len(batch))
  80. for i in range(len(ids_sorted_decreasing)):
  81. mel = batch[ids_sorted_decreasing[i]][1]
  82. mel_padded[i, :, :mel.size(1)] = mel
  83. gate_padded[i, mel.size(1):] = 1
  84. output_lengths[i] = mel.size(1)
  85. return text_padded, input_lengths, mel_padded, gate_padded, \
  86. output_lengths