From 1683a57ae5a37104d162d9cd9de596df9fb9d04e Mon Sep 17 00:00:00 2001 From: rafaelvalle Date: Sun, 25 Nov 2018 22:32:47 -0800 Subject: [PATCH] data_utils.py: rewrite --- data_utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/data_utils.py b/data_utils.py index 09f42ac..fdfd287 100644 --- a/data_utils.py +++ b/data_utils.py @@ -14,9 +14,8 @@ class TextMelLoader(torch.utils.data.Dataset): 2) normalizes text and converts them to sequences of one-hot vectors 3) computes mel-spectrograms from audio files. """ - def __init__(self, audiopaths_and_text, hparams, shuffle=True): - self.audiopaths_and_text = load_filepaths_and_text( - audiopaths_and_text, hparams.sort_by_length) + def __init__(self, audiopaths_and_text, hparams): + self.audiopaths_and_text = load_filepaths_and_text(audiopaths_and_text) self.text_cleaners = hparams.text_cleaners self.max_wav_value = hparams.max_wav_value self.sampling_rate = hparams.sampling_rate @@ -26,8 +25,7 @@ class TextMelLoader(torch.utils.data.Dataset): hparams.n_mel_channels, hparams.sampling_rate, hparams.mel_fmin, hparams.mel_fmax) random.seed(1234) - if shuffle: - random.shuffle(self.audiopaths_and_text) + random.shuffle(self.audiopaths_and_text) def get_mel_text_pair(self, audiopath_and_text): # separate filename and text @@ -38,7 +36,10 @@ class TextMelLoader(torch.utils.data.Dataset): def get_mel(self, filename): if not self.load_mel_from_disk: - audio = load_wav_to_torch(filename, self.sampling_rate) + audio, sampling_rate = load_wav_to_torch(filename) + if sampling_rate != self.stft.sampling_rate: + raise ValueError("{} {} SR doesn't match target {} SR".format( + sampling_rate, self.stft.sampling_rate)) audio_norm = audio / self.max_wav_value audio_norm = audio_norm.unsqueeze(0) audio_norm = torch.autograd.Variable(audio_norm, requires_grad=False) @@ -87,9 +88,9 @@ class TextMelCollate(): text = batch[ids_sorted_decreasing[i]][0] text_padded[i, :text.size(0)] = text - # Right zero-pad mel-spec with extra single zero vector to mark the end + # Right zero-pad mel-spec num_mels = batch[0][1].size(0) - max_target_len = max([x[1].size(1) for x in batch]) + 1 + max_target_len = max([x[1].size(1) for x in batch]) if max_target_len % self.n_frames_per_step != 0: max_target_len += self.n_frames_per_step - max_target_len % self.n_frames_per_step assert max_target_len % self.n_frames_per_step == 0 @@ -103,7 +104,7 @@ class TextMelCollate(): for i in range(len(ids_sorted_decreasing)): mel = batch[ids_sorted_decreasing[i]][1] mel_padded[i, :, :mel.size(1)] = mel - gate_padded[i, mel.size(1):] = 1 + gate_padded[i, mel.size(1)-1:] = 1 output_lengths[i] = mel.size(1) return text_padded, input_lengths, mel_padded, gate_padded, \