-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdata_loader.py
268 lines (227 loc) · 9.73 KB
/
data_loader.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os.path
import random
import numpy as np
import torch
import re
import torch.utils.data
import json
import kaldiio
from tqdm import tqdm
def intersperse(lst, item):
# Adds blank symbol
result = [item] * (len(lst) * 2 + 1)
result[1::2] = lst
return result
def check_frame_length(utt, dur, mel):
assert sum(dur) == mel.shape[1], f"Frame length mismatch: utt {utt}, dur: {sum(dur)}, mel: {mel.shape[1]}"
def check_phone_length(utt, dur, phn):
assert len(dur) == len(phn), f"Phone length mismatch: utt {utt}, phone length {len(phn)}, dur length {len(dur)}"
class BaseLoader(torch.utils.data.Dataset):
def __init__(self, utts: str, n_mel_channels: int, sampling_rate: int,
feats_scp: str, utt2num_frames: str, utt2phns: str, phn2id: str,
utt2phn_duration: str, add_blank=False, noise_scp: str = None):
"""
:param utts: file path. A list of utts for this loader. These are the only utts that this loader has access.
This loader only deals with text, duration and feats. Other files despite `utts` can be larger.
:param n_mel_channels: number of mel dimensions
:param sampling_rate: sampling rate
:param feats_scp: Kaldi-style feats.scp file path. Should contain contents like "utt1 /path/to/feats.ark:12345"
:param utt2num_frames: plain text file path, indicating every utterance's number of frames, like "utt1 300"
:param utt2phns: plain text file path, indicating every utterance's phone sequence, like "utt1 AH0 P AY1 L"
:param phn2id: plain text file path, indicating every phone's index.
:param utt2phn_duration: plain text file path, indicating every utterance's duration sequence, like "utt1 10 2 4 8"
:param add_blank: if True, then insert a <BLANK> token between every phoneme. Useful for MAS.
:param noise_scp: like feats.scp, but specifying the noise for ReFlow.
"""
self.n_mel_channels = n_mel_channels
self.sampling_rate = sampling_rate
self.utts = self.get_utts(utts)
self.add_blank = add_blank
self.utt2phn, self.phn2id = self.get_utt2phn(utt2phns, phn2id, add_blank=add_blank)
self.vocab_len = len(self.phn2id.keys())
self.utt2phn_dur = self.get_utt2phn_dur(utt2phn_duration)
self.utt2feat = self.get_utt2feat(feats_scp)
if noise_scp is not None:
self.utt2noise = self.get_utt2feat(noise_scp)
else:
self.utt2noise = None
self.utt2num_frames = self.get_utt2num_frames(utt2num_frames)
self.lengths = [self.utt2num_frames[utt] for utt in self.utts]
def get_utts(self, utts: str) -> list:
with open(utts, 'r') as f:
L = f.readlines()
L = list(map(lambda x: x.strip(), L))
random.seed(1234)
random.shuffle(L)
return L
def get_utt2phn(self, utt2phns: str, phn2id: str, add_blank: bool = False) -> (dict, dict):
res = dict()
with open(utt2phns, 'r') as f:
for l in f.readlines():
res[l.split()[0]] = l.strip().split()[1:]
res_phn2id = dict()
with open(phn2id, 'r') as f:
for l in f.readlines():
res_phn2id[l.split()[0]] = int(l.strip().split()[1])
if add_blank:
blank_id = max(res_phn2id.values()) + 1
res_phn2id['<BLANK>'] = blank_id
return res, res_phn2id
def get_utt2phn_dur(self, utt2phn_duration: str) -> dict:
res = dict()
with open(utt2phn_duration, 'r') as f:
for l in f.readlines():
uttid = l.split()[0]
# map to integer
durs = list(map(int, l.strip().split()[1:]))
res[uttid] = durs
return res
def get_utt2feat(self, feats_scp: str):
utt2feat = kaldiio.load_scp(feats_scp) # lazy load mode
print(f"Succeed reading feats from {feats_scp}")
return utt2feat
def get_mel_from_kaldi(self, utt):
feat = self.utt2feat[utt]
feat = torch.FloatTensor(np.copy(feat)).squeeze()
assert self.n_mel_channels in feat.shape
if feat.shape[0] == self.n_mel_channels:
return feat
else:
return feat.T
def get_noise_from_kaldi(self, utt):
feat = self.utt2noise[utt]
feat = torch.FloatTensor(np.copy(feat)).squeeze()
assert self.n_mel_channels in feat.shape
if feat.shape[0] == self.n_mel_channels:
return feat
else:
return feat.T
def get_text(self, utt):
phn_seq = self.utt2phn[utt]
phn_id_seq = list(map(lambda x: self.phn2id[x], phn_seq))
if self.add_blank:
phn_id_seq = intersperse(phn_id_seq, self.phn2id['<BLANK>'])
return torch.LongTensor(phn_id_seq)
def get_dur_from_kaldi(self, utt):
return torch.LongTensor(self.utt2phn_dur[utt])
def get_utt2num_frames(self, utt2num_frames: str):
res = dict()
with open(utt2num_frames, 'r') as fr:
for line in fr.readlines():
terms = line.strip().split()
utt, num = terms[0], int(terms[1])
res[utt] = num
return res
def __getitem__(self, index):
res = self.get_mel_text_pair(self.utts[index])
return res
def __len__(self):
return len(self.utts)
def sample_test_batch(self, size):
idx = np.random.choice(range(len(self)), size=size, replace=False)
test_batch = []
for index in idx:
test_batch.append(self.__getitem__(index))
return test_batch
class SpkIDLoader(BaseLoader):
def __init__(self, utts: str, n_mel_channels: int, sampling_rate: int,
feats_scp: str, utt2num_frames:str, utt2phns: str, phn2id: str,
utt2phn_duration: str, utt2spk: str, add_blank: bool = False, noise_scp: str = None):
"""
:param utt2spk: json file path (utt name -> spk id)
This loader loads speaker as a speaker ID for embedding table
"""
super(SpkIDLoader, self).__init__(utts, n_mel_channels, sampling_rate, feats_scp, utt2num_frames, utt2phns, phn2id, utt2phn_duration,
add_blank=add_blank, noise_scp=noise_scp)
self.utt2spk = self.get_utt2spk(utt2spk)
def get_utt2spk(self, utt2spk: str) -> dict:
with open(utt2spk, 'r') as f:
res = json.load(f)
return res
def get_mel_text_pair(self, utt):
spkid = self.utt2spk[utt]
phn_ids = self.get_text(utt)
mel = self.get_mel_from_kaldi(utt)
T_mel = mel.shape[1]
if self.utt2noise is not None:
noise = self.get_noise_from_kaldi(utt)
T_noise = noise.shape[1]
if abs(T_mel - T_noise) <= 3:
min_len = min(T_mel, T_noise)
mel = mel[:, :min_len]
noise = noise[:, :min_len]
else:
noise = None
dur = self.get_dur_from_kaldi(utt)
check_frame_length(utt, dur, mel)
if not self.add_blank:
check_phone_length(utt, dur, phn_ids)
res = {
"utt": utt,
"phn_ids": phn_ids,
"mel": mel,
"noise": noise,
"dur": dur,
"spk_ids": spkid
}
return res
def __getitem__(self, index):
res = self.get_mel_text_pair(self.utts[index])
return res
def __len__(self):
return len(self.utts)
class XvectorLoader(BaseLoader):
def __init__(self, utts: str, n_mel_channels: int, sampling_rate: int,
feats_scp: str, utt2num_frames: str, utt2phns: str, phn2id: str,
utt2phn_duration: str, utt2spk_name: str, spk_xvector_scp: str, add_blank: bool = False, noise_scp: str = None):
"""
:param utt2spk_name: like kaldi-style utt2spk
:param spk_xvector_scp: kaldi-style speaker-level xvector.scp
"""
super(XvectorLoader, self).__init__(utts, n_mel_channels, sampling_rate, feats_scp, utt2num_frames, utt2phns, phn2id, utt2phn_duration,
add_blank=add_blank, noise_scp=noise_scp)
self.utt2spk = self.get_utt2spk(utt2spk_name)
self.spk2xvector = self.get_spk2xvector(spk_xvector_scp)
def get_utt2spk(self, utt2spk):
res = dict()
with open(utt2spk, 'r') as f:
for l in f.readlines():
res[l.split()[0]] = l.split()[1]
return res
def get_spk2xvector(self, spk_xvector_scp: str) -> dict:
res = kaldiio.load_scp(spk_xvector_scp)
print(f"Succeed reading xvector from {spk_xvector_scp}")
return res
def get_xvector(self, utt):
xv = self.spk2xvector[self.utt2spk[utt]]
xv = torch.FloatTensor(xv).squeeze()
return xv
def get_mel_text_pair(self, utt):
phn_ids = self.get_text(utt)
if self.add_blank:
phn_ids = intersperse(phn_ids, self.phn2id['<BLANK>'])
mel = self.get_mel_from_kaldi(utt)
T_mel = mel.shape[1]
if self.utt2noise is not None:
noise = self.get_noise_from_kaldi(utt)
T_noise = noise.shape[1]
if abs(T_mel - T_noise) <= 3:
min_len = min(T_mel, T_noise)
mel = mel[:, :min_len]
noise = noise[:, :min_len]
else:
noise = None
dur = self.get_dur_from_kaldi(utt)
xvector = self.get_xvector(utt)
check_frame_length(utt, dur, mel)
if not self.add_blank:
check_phone_length(utt, dur, phn_ids)
res = {
"utt": utt,
"phn_ids": phn_ids,
"mel": mel,
"noise": noise,
"dur": dur,
"xvector": xvector,
}
return res