-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_loader.py
287 lines (257 loc) · 8.83 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import os
import csv
from collections import defaultdict
import random
import json
from glob import glob
import torch
import torch.nn.functional as F
import librosa
import numpy as np
from scipy import signal
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm
from tinytag import TinyTag
class FeatureExtractor(object):
def __init__(
self,
sample_rate,
win_length,
hop_length,
n_frames,
n_fft,
n_filterbanks,
feat_type,
musan_path,
rir_path,
augmet_prob
):
self.sample_rate = sample_rate
self.n_win_length = int(win_length * sample_rate)
self.n_hop_length = int(hop_length * sample_rate)
self.n_frames = n_frames
self.n_fft = n_fft
self.n_filterbanks = n_filterbanks
self.feat_type = feat_type
self.add_musan = musan_path
self.add_rir = rir_path
self.augmet_prob = augmet_prob
self.clip_length = (n_frames-3) * self.n_hop_length + self.n_win_length
# init augmentation stuff
if musan_path:
self.noise_snr_range = {
'noise': (0, 15),
'speech': (15, 20),
'music': (5, 15)
}
self.num_noise = {
'noise': (1, 1),
'speech': (3, 7),
'music': (1, 1)
}
self.musan_files = defaultdict(list)
for filepath in glob(os.path.join(musan_path, '*/*/*.wav')):
noise_cat = filepath.split('/')[-3]
self.musan_files[noise_cat].append(filepath)
if rir_path:
self.rir_files = glob(os.path.join(rir_path, '*/*/*.wav'))
def load_audio_4train(self, filepath, filesize):
if filesize >= self.clip_length:
# random crop
offset = np.random.randint(0, max(0, filesize - self.clip_length))
x, _ = librosa.load(
filepath,
offset=offset / self.sample_rate,
duration=self.clip_length / self.sample_rate,
sr=None
)
else:
# load whole audio and fix length
x, _ = librosa.load(filepath, sr=None)
x = self.fix_length(x)
# augmentation
if random.random() < self.augmet_prob:
noise_type = random.randint(0, 3)
if noise_type == 0:
rir_file = random.choice(self.rir_files)
rir, _ = librosa.load(rir_file, sr=self.sample_rate)
rir /= np.sqrt(np.sum(rir ** 2))
x = signal.convolve(x, rir, mode='same')
elif noise_type == 1:
x = self.add_noise(x, 'music')
elif noise_type == 2:
x = self.add_noise(x, 'speech')
elif noise_type == 3:
x = self.add_noise(x, 'noise')
return self.audio_to_feat(x)
def add_noise(self, x, noise_cat):
x_db = 10 * np.log10(np.mean(x ** 2) + 1e-4)
num_noise = self.num_noise[noise_cat]
file_list = random.sample(
self.musan_files[noise_cat],
random.randint(*num_noise)
)
for filepath in file_list:
# load noise
noise, _ = librosa.load(filepath, sr=self.sample_rate)
filesize = noise.shape[-1]
if filesize >= self.clip_length:
offset = np.random.randint(
0,
max(0, filesize - self.clip_length)
)
noise = noise[offset:offset + self.clip_length]
else:
noise = self.fix_length(noise)
# scale and add to original signal
noise_snr = random.uniform(*self.noise_snr_range[noise_cat])
noise_db = 10 * np.log10(np.mean(noise ** 2) + 1e-4)
noise *= np.sqrt(10 ** ((x_db - noise_db - noise_snr) / 10))
x += noise
return x
def load_audio_4test(self, filepath):
x, _ = librosa.load(filepath, sr=None)
return self.audio_to_feat(x)
def audio_to_feat(self, x):
# feature extraction
if self.feat_type == 'mel':
feature = librosa.feature.melspectrogram(
y=x,
n_fft=self.n_fft,
hop_length=self.n_hop_length,
win_length=self.n_win_length,
n_mels=self.n_filterbanks
)
elif self.feat_type == 'spect':
D = librosa.stft(
x,
n_fft=self.n_fft,
hop_length=self.n_hop_length,
win_length=self.n_win_length,
)
feature, _ = librosa.magphase(D)
# normalize by log
feature = np.log1p(feature)
# CMVN normalization
feature = torch.FloatTensor(feature)
with torch.no_grad():
mean = feature.mean()
std = feature.std()
feature.add_(-mean)
feature.div_(std)
return feature
def fix_length(self, signal):
lack = self.clip_length - signal.shape[0]
pad_left = pad_right = lack // 2
pad_left += 1 if lack % 2 else 0
return np.concatenate(
[np.zeros(pad_left), signal, np.zeros(pad_right)]
)
class VoxCelebDataset(Dataset):
def __init__(
self,
sample_rate,
win_length,
hop_length,
n_frames,
n_fft,
n_filterbanks,
feat_type,
musan_path,
rir_path,
augmet_prob,
mode,
csv_path,
samples_per_speaker
):
self.feature_extractor = FeatureExtractor(
sample_rate,
win_length,
hop_length,
n_frames,
n_fft,
n_filterbanks,
feat_type,
musan_path,
rir_path,
augmet_prob
)
self.mode = mode
if mode == 'dev':
self.developing_mode_init(csv_path, samples_per_speaker)
elif mode == 'eval':
self.evaluation_mode_init(csv_path)
else:
raise ValueError('dataset mode must be "dev" or "eval".')
def developing_mode_init(self, csv_path, samples_per_speaker):
self.samples_per_speaker = samples_per_speaker
self.data = defaultdict(list)
with open(csv_path) as f:
for speaker_id, filepath in csv.reader(f, delimiter=' '):
self.data[speaker_id].append(filepath)
speaker_list = sorted(list(self.data.keys()))
self.index2speaker = {ind: spk for ind, spk in enumerate(speaker_list)}
self.speaker2index = {spk: ind for ind, spk in enumerate(speaker_list)}
# read file sizes
if os.path.exists('filesize.json'):
with open('filesize.json') as f:
self.filesize = json.load(f)
else:
print('read file tags...')
self.filesize = {}
for file_list in tqdm(self.data.values()):
for filepath in file_list:
tag = TinyTag.get(filepath)
filesize = int(tag.duration * tag.samplerate)
self.filesize[filepath] = filesize
with open('filesize.json', 'w') as f:
json.dump(self.filesize, f)
def evaluation_mode_init(self, csv_path):
self.data = []
print(csv_path)
with open(csv_path) as f:
for label, filepath0, filepath1 in csv.reader(f, delimiter=' '):
self.data.append((label, filepath0, filepath1))
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if self.mode == 'dev':
return self.dev_getitem(idx)
else:
return self.test_getitem(idx)
def dev_getitem(self, speaker_index):
speaker = self.index2speaker[speaker_index]
filenames = random.choices(
self.data[speaker],
k=self.samples_per_speaker
)
audio_segments = [
self.feature_extractor.load_audio_4train(i, self.filesize[i])
for i in filenames
]
label = torch.LongTensor([speaker_index] * self.samples_per_speaker)
return torch.stack(audio_segments), label
def test_getitem(self, line_ind):
label, filepath0, filepath1 = self.data[line_ind]
return label, filepath0, filepath1
if __name__ == '__main__':
ds = VoxCelebDataset(
16000,
.025,
.01,
200,
512,
40,
'spect',
'/data/musan',
'/data/RIRS_NOISES/simulated_rirs',
.8,
'dev',
'/data/voxceleb2_dev.csv',
3
)
dl = DataLoader(ds, batch_size=4, shuffle=True, num_workers=8, drop_last=True)
for x, y in tqdm(dl):
pass
print(x)
print(y)