forked from NVIDIA/tacotron2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_utils.py
executable file
·154 lines (135 loc) · 5.88 KB
/
data_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import random,os
import numpy as np
import torch
import torch.utils.data
import layers
from utils import load_wav_to_torch, load_fbs_and_fb_text_dict
#from text import text_to_sequence
from hparams import create_hparams
from torch.utils.data import DataLoader
from symbols import phone2id, tone2id
class TextMelLoader(torch.utils.data.Dataset):
"""
1) loads audio,text pairs
2) normalizes text and converts them to sequences of one-hot vectors
3) computes mel-spectrograms from audio files.
"""
def __init__(self, lstfile, hparams):
self.fbs, self.fb_text_dict = load_fbs_and_fb_text_dict(
lstfile, hparams.lab_path)
self.max_wav_value = hparams.max_wav_value
self.sampling_rate = hparams.sampling_rate
self.load_mel_from_disk = hparams.load_mel_from_disk
self.audio_path = hparams.audio_path
self.mel_path = hparams.mel_path
self.MelStd_mel = hparams.MelStd_mel
self.stft = layers.TacotronSTFT(
hparams.filter_length, hparams.hop_length, hparams.win_length,
hparams.n_mel_channels, hparams.sampling_rate, hparams.mel_fmin,
hparams.mel_fmax)
random.seed(1234)
random.shuffle(self.fbs)
def get_mel_text_pair(self, fb):
# separate filename and text
text = self.get_text(fb)
mel = self.get_mel(fb)
return text, mel
def get_mel(self, fb):
if not self.load_mel_from_disk:
cur_audio_path = os.path.join(self.audio_path, fb+'.wav')
audio = load_wav_to_torch(cur_audio_path, self.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)
melspec = self.stft.mel_spectrogram(audio_norm)
melspec = torch.squeeze(melspec, 0) # [mel_bin, T]
else:
cur_mel_path = os.path.join(self.mel_path, fb+'.npy')
melspec = np.load(cur_mel_path)
mean, std = np.load(self.MelStd_mel)
melspec = (melspec - mean) / std
melspec = np.transpose(melspec)
melspec = torch.from_numpy(melspec)
assert melspec.size(0) == self.stft.n_mel_channels, (
'Mel dimension mismatch: given {}, expected {}'.format(
melspec.size(0), self.stft.n_mel_channels))
return melspec
def get_text(self, fb):
text_norm = self.fb_text_dict[fb]
text_norm = torch.IntTensor([[phone2id[ph], tone2id[tn]] for ph, tn in text_norm])
return text_norm
def __getitem__(self, index):
return self.get_mel_text_pair(self.fbs[index])
def __len__(self):
return len(self.fbs)
class TextMelCollate():
""" Zero-pads model inputs and targets based on number of frames per setep
"""
def __init__(self, n_frames_per_step):
self.n_frames_per_step = n_frames_per_step
def __call__(self, batch):
"""Collate's training batch from normalized text and mel-spectrogram
PARAMS
------
batch: [text_normalized, mel_normalized]
"""
# Right zero-pad all one-hot text sequences to max input length
input_lengths, ids_sorted_decreasing = torch.sort(
torch.LongTensor([len(x[0]) for x in batch]),
dim=0, descending=True)
max_input_len = input_lengths[0]
text_padded = torch.LongTensor(len(batch), max_input_len, 2) ## phone, tone
text_padded.zero_()
for i in range(len(ids_sorted_decreasing)):
text = batch[ids_sorted_decreasing[i]][0]
text_padded[i, :text.size(0), :] = text
# Right zero-pad mel-spec
num_mels = batch[0][1].size(0)
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
# include mel padded and gate padded
mel_padded = torch.FloatTensor(len(batch), num_mels, max_target_len)
mel_padded.zero_()
gate_padded = torch.FloatTensor(len(batch), max_target_len)
gate_padded.zero_()
output_lengths = torch.LongTensor(len(batch))
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:] = 1
output_lengths[i] = mel.size(1)
return text_padded, input_lengths, mel_padded, gate_padded, \
output_lengths
if __name__ == "__main__":
from hparams import create_hparams
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from model import Tacotron2
from loss_function import Tacotron2Loss
hparams = create_hparams()
text_loader = TextMelLoader(hparams.training_lst, hparams)
collate_fn = TextMelCollate(hparams.n_frames_per_step)
text, mel = text_loader[0] # mel.shape (80 * frame_num)
plt.matshow(mel, origin='lower')
plt.colorbar()
plt.savefig('mel_demo.png')
train_loader = torch.utils.data.DataLoader(
text_loader,
num_workers=1, shuffle=False,
batch_size=32, pin_memory=False,
drop_last=True, collate_fn=collate_fn)
print(len(train_loader))
tacotron = Tacotron2(hparams)
criterion = Tacotron2Loss()
for batch in train_loader:
text_padded, input_lengths, mel_padded, gate_padded, \
output_lengths = batch
max_len = torch.max(input_lengths.data).item()
x = (text_padded, input_lengths, mel_padded, max_len, output_lengths)
y = (mel_padded, gate_padded)
y_pred = tacotron(x)
print(criterion(y_pred, y))
break