-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatasetLoader.py
335 lines (250 loc) · 12 KB
/
DatasetLoader.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#! /usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import numpy
import random
import pdb
import os
import threading
import time
import math
import glob
import soundfile
from scipy import signal
from scipy.io import wavfile
from torch.utils.data import Dataset, DataLoader, Sampler, BatchSampler
import torch.distributed as dist
def round_down(num, divisor):
return num - (num%divisor)
def worker_init_fn(worker_id):
numpy.random.seed(numpy.random.get_state()[1][0] + worker_id)
def loadWAV(filename, max_frames, evalmode=True, num_eval=10):
# Maximum audio length
max_audio = max_frames * 160 + 240
# Read wav file and convert to torch tensor
audio, sample_rate = soundfile.read(filename)
audiosize = audio.shape[0]
if audiosize <= max_audio:
shortage = max_audio - audiosize + 1
audio = numpy.pad(audio, (0, shortage), 'wrap')
audiosize = audio.shape[0]
if evalmode:
startframe = numpy.linspace(0,audiosize-max_audio,num=num_eval)
else:
startframe = numpy.array([numpy.int64(random.random()*(audiosize-max_audio))])
feats = []
if evalmode and max_frames == 0:
feats.append(audio)
else:
for asf in startframe:
feats.append(audio[int(asf):int(asf)+max_audio])
feat = numpy.stack(feats,axis=0).astype(numpy.float)
return feat
class AugmentWAV(object):
def __init__(self, musan_path, rir_path, max_frames):
self.max_frames = max_frames
self.max_audio = max_audio = max_frames * 160 + 240
self.noisetypes = ['noise','speech','music']
self.noisesnr = {'noise':[0,15],'speech':[13,20],'music':[5,15]}
self.numnoise = {'noise':[1,1], 'speech':[3,7], 'music':[1,1] }
self.noiselist = {}
augment_files = glob.glob(os.path.join(musan_path,'*/*/*/*.wav'));
for file in augment_files:
if not file.split('/')[-4] in self.noiselist:
self.noiselist[file.split('/')[-4]] = []
self.noiselist[file.split('/')[-4]].append(file)
self.rir_files = glob.glob(os.path.join(rir_path,'*/*/*.wav'));
def additive_noise(self, noisecat, audio):
clean_db = 10 * numpy.log10(numpy.mean(audio ** 2)+1e-4)
numnoise = self.numnoise[noisecat]
noiselist = random.sample(self.noiselist[noisecat], random.randint(numnoise[0],numnoise[1]))
noises = []
for noise in noiselist:
noiseaudio = loadWAV(noise, self.max_frames, evalmode=False)
noise_snr = random.uniform(self.noisesnr[noisecat][0],self.noisesnr[noisecat][1])
noise_db = 10 * numpy.log10(numpy.mean(noiseaudio[0] ** 2)+1e-4)
noises.append(numpy.sqrt(10 ** ((clean_db - noise_db - noise_snr) / 10)) * noiseaudio)
return numpy.sum(numpy.concatenate(noises,axis=0),axis=0,keepdims=True) + audio
def reverberate(self, audio):
rir_file = random.choice(self.rir_files)
rir, fs = soundfile.read(rir_file)
rir = numpy.expand_dims(rir.astype(numpy.float),0)
rir = rir / numpy.sqrt(numpy.sum(rir**2))
return signal.convolve(audio, rir, mode='full')[:,:self.max_audio]
class train_dataset_loader(Dataset):
def __init__(self, train_list, augment, musan_path, rir_path, max_frames, train_path, **kwargs):
self.augment_wav = AugmentWAV(musan_path=musan_path, rir_path=rir_path, max_frames = max_frames)
self.train_list = train_list
self.max_frames = max_frames
self.musan_path = musan_path
self.rir_path = rir_path
self.augment = augment
# Read training files
with open(train_list) as dataset_file:
lines = dataset_file.readlines()
# Make a dictionary of ID names and ID indices
dictkeys = list(set([x.split()[0] for x in lines]))
dictkeys.sort()
dictkeys = { key : ii for ii, key in enumerate(dictkeys) }
# Parse the training list into file names and ID indices
self.data_list = []
self.data_label = []
for lidx, line in enumerate(lines):
data = line.strip().split()
speaker_label = dictkeys[data[0]]
filename = os.path.join(train_path,data[1])
self.data_label.append(speaker_label)
self.data_list.append(filename)
def __getitem__(self, indices):
feat = []
for index in indices:
audio = loadWAV(self.data_list[index], self.max_frames, evalmode=False) # TODO: replace with h5 reading
if self.augment:
augtype = random.randint(0,4)
if augtype == 1:
audio = self.augment_wav.reverberate(audio)
elif augtype == 2:
audio = self.augment_wav.additive_noise('music',audio)
elif augtype == 3:
audio = self.augment_wav.additive_noise('speech',audio)
elif augtype == 4:
audio = self.augment_wav.additive_noise('noise',audio)
feat.append(audio)
feat = numpy.concatenate(feat, axis=0)
return torch.FloatTensor(feat), self.data_label[index]
def __len__(self):
return len(self.data_list)
class test_dataset_loader(Dataset):
def __init__(self, test_list, test_path, eval_frames, num_eval, **kwargs):
self.max_frames = eval_frames
self.num_eval = num_eval
self.test_path = test_path
self.test_list = test_list
def __getitem__(self, index):
audio = loadWAV(os.path.join(self.test_path,self.test_list[index]), self.max_frames, evalmode=True, num_eval=self.num_eval)
return torch.FloatTensor(audio), self.test_list[index]
def __len__(self):
return len(self.test_list)
class train_dataset_sampler(torch.utils.data.Sampler):
def __init__(self, data_source, nPerSpeaker, max_seg_per_spk, batch_size, distributed, seed, **kwargs):
self.data_label = data_source.data_label
self.nPerSpeaker = nPerSpeaker
self.max_seg_per_spk = max_seg_per_spk
self.batch_size = batch_size
self.epoch = 0
self.seed = seed
self.distributed = distributed
def __iter__(self):
g = torch.Generator()
g.manual_seed(self.seed + self.epoch)
indices = torch.randperm(len(self.data_label), generator=g).tolist()
data_dict = {}
# Sort into dictionary of file indices for each ID
for index in indices:
speaker_label = self.data_label[index]
if not (speaker_label in data_dict):
data_dict[speaker_label] = []
data_dict[speaker_label].append(index)
## Group file indices for each class
dictkeys = list(data_dict.keys())
dictkeys.sort()
lol = lambda lst, sz: [lst[i:i+sz] for i in range(0, len(lst), sz)]
flattened_list = []
flattened_label = []
for findex, key in enumerate(dictkeys):
data = data_dict[key]
numSeg = round_down(min(len(data),self.max_seg_per_spk),self.nPerSpeaker)
rp = lol(numpy.arange(numSeg),self.nPerSpeaker)
flattened_label.extend([findex] * (len(rp)))
for indices in rp:
flattened_list.append([data[i] for i in indices])
## Mix data in random order
mixid = torch.randperm(len(flattened_label), generator=g).tolist()
mixlabel = []
mixmap = []
resmixid = []
mixlabel_ins = 1 # for start while
# ## Prevent two pairs of the same speaker in the same batch
# for ii in mixid:
# startbatch = round_down(len(mixlabel), self.batch_size)
# if flattened_label[ii] not in mixlabel[startbatch:]:
# mixlabel.append(flattened_label[ii])
# mixmap.append(ii)
# mixlabel_ins += 1
## Prevent two pairs of the same speaker in the same batch (Reduce data waste with "resmixid")
while len(mixid)>0 and mixlabel_ins>0:
mixlabel_ins = 0
for ii in mixid:
startbatch = round_down(len(mixlabel), self.batch_size)
if flattened_label[ii] not in mixlabel[startbatch:]:
mixlabel.append(flattened_label[ii])
mixmap.append(ii)
mixlabel_ins += 1
else:
resmixid.append(ii)
mixid = resmixid
resmixid = []
mixed_list = [flattened_list[i] for i in mixmap]
## Divide data to each GPU
if self.distributed:
total_size = round_down(len(mixed_list), self.batch_size * dist.get_world_size())
start_index = int ( ( dist.get_rank() ) / dist.get_world_size() * total_size )
end_index = int ( ( dist.get_rank() + 1 ) / dist.get_world_size() * total_size )
self.num_samples = end_index - start_index
return iter(mixed_list[start_index:end_index])
else:
total_size = round_down(len(mixed_list), self.batch_size)
self.num_samples = total_size
return iter(mixed_list[:total_size])
def __len__(self) -> int:
return self.num_samples
def set_epoch(self, epoch: int) -> None:
self.epoch = epoch
class RandomBatchSampler(Sampler):
"""Sampling class to create random sequential batches from a given dataset
E.g. if data is [1,2,3,4] with bs=2. Then first batch, [[1,2], [3,4]] then shuffle batches -> [[3,4],[1,2]]
This is useful for cases when you are interested in 'weak shuffling'
:param dataset: dataset you want to batch
:type dataset: torch.utils.data.Dataset
:param batch_size: batch size
:type batch_size: int
:returns: generator object of shuffled batch indices
"""
def __init__(self, dataset, batch_size):
self.batch_size = batch_size
self.dataset_length = len(dataset)
self.n_batches = self.dataset_length / self.batch_size
self.batch_ids = torch.randperm(int(self.n_batches))
def __len__(self):
return self.batch_size
def __iter__(self):
for id in self.batch_ids:
idx = torch.arange(id * self.batch_size, (id + 1) * self.batch_size)
for index in idx:
yield int(index)
if int(self.n_batches) < self.n_batches:
idx = torch.arange(int(self.n_batches) * self.batch_size, self.dataset_length)
for index in idx:
yield int(index)
def fast_loader(dataset, batch_size=32, drop_last=False, transforms=None):
"""Implements fast loading by taking advantage of .h5 dataset
The .h5 dataset has a speed bottleneck that scales (roughly) linearly with the number
of calls made to it. This is because when queries are made to it, a search is made to find
the data item at that index. However, once the start index has been found, taking the next items
does not require any more significant computation. So indexing data[start_index: start_index+batch_size]
is almost the same as just data[start_index]. The fast loading scheme takes advantage of this. However,
because the goal is NOT to load the entirety of the data in memory at once, weak shuffling is used instead of
strong shuffling.
:param dataset: a dataset that loads data from .h5 files
:type dataset: torch.utils.data.Dataset
:param batch_size: size of data to batch
:type batch_size: int
:param drop_last: flag to indicate if last batch will be dropped (if size < batch_size)
:type drop_last: bool
:returns: dataloading that queries from data using shuffled batches
:rtype: torch.utils.data.DataLoader
"""
return DataLoader(
dataset, batch_size=None, # must be disabled when using samplers
sampler=BatchSampler(RandomBatchSampler(dataset, batch_size), batch_size=batch_size, drop_last=drop_last)
)