-
Notifications
You must be signed in to change notification settings - Fork 72
/
Data.py
248 lines (196 loc) · 7.3 KB
/
Data.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from Hyperparameters import Hyperparameters as hp
from torch.utils.data import Dataset, DataLoader
import torch
from utils import *
import os
import unicodedata
import re
class SpeechDataset(Dataset):
'''
text: [T_x]
mel: [T_y/r, n_mels*r]
mag: [T_y, 1+n_fft/2]
'''
def __init__(self, r=slice(0, None)):
print('Start loading data')
# fpaths, texts = get_data(hp.data, r) # thchs30
# fpaths, texts = get_keda_data(hp.data, r) # keda api
# fpaths, texts = get_thchs30_data(hp.data, r)
fpaths, texts = get_blizzard_data(hp.data, r)
print('Finish loading data')
self.fpaths = fpaths
self.texts = texts
def __len__(self):
return len(self.fpaths)
def __getitem__(self, idx):
_, mel, mag = load_spectrograms(self.fpaths[idx])
mel = torch.from_numpy(mel)
mag = torch.from_numpy(mag)
GO_mel = torch.zeros(1, mel.size(1)) # GO frame
mel = torch.cat([GO_mel, mel], dim=0)
text = self.texts[idx]
return {'text': text, 'mel': mel, 'mag': mag}
def collate_fn(batch):
'''
texts: [N, max_T_x]
mels: [N, max_T_y/r, n_mels*r]
mags: [N, max_T_y, 1+n_fft/2]
'''
texts = [d['text'] for d in batch]
mels = [d['mel'] for d in batch]
mags = [d['mag'] for d in batch]
texts = pad_sequence(texts)
mels = pad_sequence(mels)
mags = pad_sequence(mags)
return {'text': texts, 'mel': mels, 'mag': mags}
def text_normalize(text):
text = ''.join(char for char in unicodedata.normalize('NFD', text)
if unicodedata.category(char) != 'Mn') # Strip accents
text = text.lower()
text = re.sub("[^{}]".format(hp.vocab), " ", text)
text = re.sub("[ ]+", " ", text)
return text
def pad_sequence(sequences):
'''
pad sequence to same length (max length)
------------------
input:
sequences --- a list of tensor with variable length
out --- a tensor with max length
'''
lengths = [data.size(0) for data in sequences]
batch_size = len(sequences)
max_len = max(lengths)
trailing_dims = sequences[0].size()[1:]
out_dims = (batch_size, max_len) + trailing_dims
dtype = sequences[0].data.type()
out = torch.zeros(*out_dims).type(dtype)
for i, data in enumerate(sequences):
out[i, :lengths[i]] = data
return out
def get_keda_data(dataset_dir, r):
wav_paths = []
texts = []
wav_dirs = ['nannan', 'xiaofeng', 'donaldduck']
csv_paths = ['transcript-nannan.csv', 'transcript-xiaofeng.csv', 'transcript-donaldduck.csv']
for wav_dir, csv_path in zip(wav_dirs, csv_paths):
csv = open(os.path.join(dataset_dir, csv_path), 'r')
for line in csv.readlines():
items = line.strip().split('|')
wav_paths.append(os.path.join(dataset_dir, wav_dir, items[0] + '.wav'))
text = text_normalize(items[1]) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor)
texts.append(text)
csv.close()
for wav in wav_paths[-20:]:
print(wav)
return wav_paths[r], texts[r]
def get_thchs30_data(dataset_dir, r):
wav_paths = []
text_paths = []
data_dir = os.path.join(dataset_dir, 'data')
for file in os.listdir(data_dir):
file_path = os.path.join(data_dir, file)
fname, ext = os.path.splitext(file_path)
if ext == '.wav' and fname[-7:] != '_cutoff':
wav_paths.append(fname + '_cutoff' + ext)
text_paths.append(file_path + '.trn')
train_dir = os.path.join(dataset_dir, 'train')
test_dir = os.path.join(dataset_dir, 'test')
dev_dir = os.path.join(dataset_dir, 'dev')
for d in [train_dir, test_dir, dev_dir]:
for file in os.listdir(d):
file_path = os.path.join(d, file)
fname, ext = os.path.splitext(file_path)
if ext == '.wav' and fname[-7:] != '_cutoff':
text_path = os.path.join(data_dir, file + '.trn')
wav_paths.append(fname + '_cutoff' + ext)
text_paths.append(text_path)
for wav, txt in zip(wav_paths[-20:], text_paths[-20:]):
print(wav, txt)
texts = []
for file in text_paths[r]:
f = open(file, 'r', encoding='utf-8')
text = f.readlines()[1].strip()
text = text_normalize(text) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor)
texts.append(text)
print(wav_paths[r][0], text_paths[r][0])
return wav_paths[r], texts
def get_aishell_data(data_dir, r):
path = os.path.join(data_dir, 'transcript.txt')
data_dir = os.path.join(data_dir, 'wav', 'train')
wav_paths = []
texts = []
with open(path, 'r') as f:
for line in f.readlines():
items = line.strip().split('|')
wav_paths.append(os.path.join(data_dir, items[0] + '.wav'))
text = items[1]
text = text_normalize(text) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor)
texts.append(text)
for wav, txt in zip(wav_paths[-20:], texts[-20:]):
print(wav, txt)
return wav_paths[r], texts[r]
def get_LJ_data(data_dir, r):
path = os.path.join(data_dir, 'transcript.csv')
data_dir = os.path.join(data_dir, 'wavs')
wav_paths = []
texts = []
with open(path, 'r') as f:
for line in f.readlines():
items = line.strip().split('|')
wav_paths.append(os.path.join(data_dir, items[0] + '.wav'))
text = items[1]
text = text_normalize(text) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor)
texts.append(text)
for wav in wav_paths[-20:]:
print(wav)
return wav_paths[r], texts[r]
def get_blizzard_data(data_dir, r):
file_list = './filelists/bliz13_audio_text_train_filelist.txt'
texts = []
wav_paths = []
with open(file_list, 'r') as f:
for line in f.readlines():
wav_path, text = line.strip().split('|')
wav_paths.append(os.path.join(data_dir, wav_path))
text = text_normalize(text) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor)
texts.append(text)
for wav in wav_paths[-20:]:
print(wav)
return wav_paths[r], texts[r]
def get_eval_data(text, wav_path):
'''
get data for eval
--------------
input:
text --- pinyin format sequence
output:
text --- [1, T_x]
mel --- [1, 1, n_mels]
'''
text = text_normalize(text) + 'E'
text = [hp.char2idx[c] for c in text]
text = torch.Tensor(text).type(torch.LongTensor) # [T_x]
text = text.unsqueeze(0) # [1, T_x]
mel = torch.zeros(1, 1, hp.n_mels) # GO frame [1, 1, n_mels]
_, ref_mels, _ = load_spectrograms(wav_path)
ref_mels = torch.from_numpy(ref_mels).unsqueeze(0)
return text, mel, ref_mels
if __name__ == '__main__':
dataset = LJDataset()
loader = DataLoader(dataset=dataset, batch_size=8, collate_fn=collate_fn)
for batch in loader:
print(batch['text'][0])
print(batch['mel'].size())
print(batch['mag'].size())
break