-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdataloader.py
298 lines (236 loc) · 9.63 KB
/
dataloader.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import pickle
import random
from multiprocessing import Pool
from os.path import basename, join
import librosa
import numpy as np
import soundfile as sf
import torch
import torch.nn as nn
import torch.nn.functional as F
from boltons.fileutils import iter_find_files
from loguru import logger
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
torch.multiprocessing.set_sharing_strategy('file_system')
def collate_fn_padd(batch):
"""collate_fn_padd
Padds batch of variable length
:param batch:
"""
# get sequence lengths
spects = [t[0] for t in batch]
segs = [t[1] for t in batch]
labels = [t[2] for t in batch]
lengths = [t[3] for t in batch]
# pad and stack
padded_spects = torch.nn.utils.rnn.pad_sequence(spects, batch_first=True)
lengths = torch.LongTensor(lengths)
return padded_spects, segs, labels, lengths
def mfcc_dist(mfcc):
"""mfcc_dist
calc 4-dimensional dist features like in HTK
:param mfcc:
"""
d = []
for i in range(2, 9, 2):
pad = int(i/2)
d_i = np.concatenate([np.zeros(pad), ((mfcc[:, i:] - mfcc[:, :-i]) ** 2).sum(0) ** 0.5, np.zeros(pad)], axis=0)
d.append(d_i)
return np.stack(d)
def phoneme_lebels_to_frame_labels(segmentation, phonemes):
"""
replicates phonemes to frame-wise labels
example:
segmentation - [0, 3, 4]
phonemes - [a, b]
returns - [a, a, a, b]
:param segmentation:
:param phonemes:
"""
segmentation = torch.LongTensor(segmentation)
return torch.cat([torch.LongTensor([l]).repeat(t) for (l, t) in zip(phonemes, segmentation[1:] - segmentation[:-1])])
def segmentation_to_binary_mask(segmentation):
"""
replicates boundaries to frame-wise labels
example:
segmentation - [0, 3, 5]
returns - [1, 0, 0, 1, 0, 1]
:param segmentation:
:param phonemes:
"""
mask = torch.zeros(segmentation[-1] + 1).long()
for boundary in segmentation[1:-1]:
mask[boundary] = 1
return mask
def extract_features(wav_file, hparams):
wav, sr = sf.read(wav_file)
# extract mel-spectrogram
if hparams.feats == 'mel':
spect = librosa.feature.melspectrogram(wav,
sr=sr,
n_fft=hparams.n_fft,
hop_length=hparams.hop_length,
n_mels=hparams.rnn_input_size)
# extract mfcc
elif hparams.feats == 'mfcc':
spect = librosa.feature.mfcc(wav,
sr=sr,
n_fft=hparams.n_fft,
hop_length=hparams.hop_length,
n_mels=hparams.n_mels,
n_mfcc=hparams.n_mfcc)
if hparams.normalize:
spect = (spect - spect.mean(0)) / spect.std(0)
if hparams.delta_feats:
delta = librosa.feature.delta(spect, order=1)
delta2 = librosa.feature.delta(spect, order=2)
spect = np.concatenate([spect, delta, delta2], axis=0)
if hparams.dist_feats:
dist = mfcc_dist(spect)
spect = np.concatenate([spect, dist], axis=0)
else:
raise Exception("no features specified!")
spect = torch.transpose(torch.FloatTensor(spect), 0, 1)
return spect
def random_trim(spect, seg):
start_trim = random.randint(1, seg[1].item() - 1)
end_trim = random.randint(1, (seg[-1] - seg[-2]).item() - 1)
spect = spect[start_trim: -end_trim]
seg[1:] -= start_trim
seg[-1] = seg[-1] - end_trim
return spect, seg
def get_onset_offset(segmentations):
search_start, search_end = float("inf"), 0
for seg in segmentations:
start, end = seg[0], seg[-1]
if start < search_start:
search_start = start
if end > search_end:
search_end = end
return search_start, search_end
def is_valid_lens(spect, seg, hparams):
sizes = seg[1:] - seg[:-1]
if len(spect) <= hparams.max_len and \
sizes.min().item() >= hparams.min_seg_size and \
sizes.max().item() <= hparams.max_seg_size:
return True
return False
class TimitDictionary():
def __init__(self):
dict_file = "dicts/phoneme_39_dict.txt"
with open(dict_file, 'r') as f:
self.phn_reduction_dict = {line.split(' ')[0].strip(): line.split(' ')[1].strip() for line in f.readlines()}
self.words = set(self.phn_reduction_dict.values())
self.n_words = len(self.words)
self._words2idx = {'ae': 29, 'ah': 25, 'ao': 8, 'aw': 1, 'ay': 12, 'b': 9,
'ch': 38, 'd': 6, 'dh': 19, 'dx': 32, 'eh': 0, 'el': 11,
'en': 31, 'er': 16, 'ey': 5, 'f': 18, 'g': 34, 'hh': 28,
'ih': 26, 'iy': 20, 'jh': 23, 'k': 7, 'm': 3, 'ng': 27,
'ow': 24, 'oy': 2, 'p': 17, 'r': 4, 's': 36, 'sh': 33,
'sil': 10, 't': 35, 'th': 14, 'uh': 37, 'uw': 30,
'v': 15, 'w': 13, 'y': 21, 'z': 22}
self._idx2words = {v:k for k,v in self._words2idx.items()}
def word2idx(self, word):
word = self.phn_reduction_dict[word]
return self._words2idx[word]
def words2idx(self, words):
return [self.word2idx(word) for word in words]
class BuckeyeDictionary():
def __init__(self):
dict_file = "dicts/phoneme_39_dict.txt"
with open(dict_file, 'r') as f:
self.phn_reduction_dict = {line.split(' ')[0].strip(): line.split(' ')[1].strip() for line in f.readlines()}
self.n_words = 0
self._words2idx = {}
def word2idx(self, word):
if word in self.phn_reduction_dict:
word = self.phn_reduction_dict[word]
elif word in ['SIL', 'VOCNOISE', 'NOISE']:
word = 'sil'
else:
word = 'vn'
if word in self._words2idx:
return self._words2idx[word]
else:
self._words2idx[word] = self.n_words
self.n_words += 1
return self._words2idx[word]
def words2idx(self, words):
return [self.word2idx(word) for word in words]
class WavPhnDataset(Dataset):
def __init__(self, path, hparams):
self.hparams = hparams
self.wav_path = path
super(WavPhnDataset, self).__init__()
@staticmethod
def get_datasets(hparams):
raise NotImplementedError
def process_file(self, wav_path):
phn_path = wav_path.replace("wav", "phn")
# load audio
spect = extract_features(wav_path, self.hparams)
# load labels -- segmentation and phonemes
with open(phn_path, "r") as f:
lines = f.readlines()
lines = list(map(lambda line: line.split(" "), lines))
# get segment times
times = torch.FloatTensor([0] + list(map(lambda line: int(line[1]), lines)))
wav_len = times[-1]
times = (times / wav_len * (len(spect) - 1)).long()
# get phonemes in each segment (for K times there should be K+1 phonemes)
phonemes = list(map(lambda line: line[2].strip(), lines))
phonemes = self.vocab.words2idx(phonemes)
# check if audio len and segment sizes are ok
if is_valid_lens(spect, times, self.hparams):
return spect, times.tolist(), phonemes
return None
def _make_dataset(self):
files = []
wavs = list(iter_find_files(self.wav_path, "*.wav"))
if self.hparams.devrun:
wavs = wavs[:self.hparams.devrun_size]
for wav in tqdm(wavs, desc="loading data into memory"):
res = self.process_file(wav)
if res is not None:
files.append(res)
return files
def __getitem__(self, idx):
spect, seg, phonemes = self.data[idx]
if self.hparams.random_trim:
spect, seg = random_trim(spect, seg)
return spect, seg, phonemes, spect.shape[0]
def __len__(self):
return len(self.data)
class TimitDataset(WavPhnDataset):
def __init__(self, path, hparams):
super(TimitDataset, self).__init__(path, hparams)
self.vocab = TimitDictionary()
self.data = self._make_dataset()
@staticmethod
def get_datasets(hparams):
train_dataset = TimitDataset(join(hparams.wav_path, 'train'),
hparams)
test_dataset = TimitDataset(join(hparams.wav_path, 'test'),
hparams)
train_len = len(train_dataset)
train_split = int(train_len * (1 - hparams.val_ratio))
val_split = train_len - train_split
train_dataset, val_dataset = torch.utils.data.random_split(train_dataset, [train_split, val_split])
logger.info(f"split timit from {train_len} to train {train_split}, valid {val_split}")
return train_dataset, val_dataset, test_dataset
class BuckeyeDataset(WavPhnDataset):
def __init__(self, path, hparams):
super(BuckeyeDataset, self).__init__(path, hparams)
self.vocab = BuckeyeDictionary()
self.data = self._make_dataset()
@staticmethod
def get_datasets(hparams):
train_dataset = BuckeyeDataset(join(hparams.wav_path, 'train'),
hparams)
val_dataset = BuckeyeDataset(join(hparams.wav_path, 'val'),
hparams)
test_dataset = BuckeyeDataset(join(hparams.wav_path, 'test'),
hparams)
return train_dataset, val_dataset, test_dataset