-
Notifications
You must be signed in to change notification settings - Fork 273
/
DatasetLoader.py
264 lines (186 loc) · 8.71 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
#! /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
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)
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 = []
## 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)
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