-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasets.py
380 lines (328 loc) · 12.1 KB
/
datasets.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import torch
import torch.nn.functional as F
import torchaudio.transforms as AT
from torch.utils.data import Dataset
import numpy as np
import random
import pandas as pd
import csv
import argparse
from tqdm import tqdm
import librosa
import json
import os
def make_index_dict(label_csv):
index_lookup = {}
with open(label_csv, 'r') as f:
csv_reader = csv.DictReader(f)
for row in csv_reader:
index_lookup[row['mids']] = row['index']
return index_lookup
class FSD50K(Dataset):
def __init__(self, cfg, split='train', transform=None, norm_stats=None, crop_frames=None):
super().__init__()
# initializations
self.cfg = cfg
self.split = split
self.transform = transform
self.norm_stats = norm_stats
self.crop_frames = self.cfg.crop_frames if crop_frames is None else crop_frames
self.unit_length = int(cfg.unit_sec * cfg.sample_rate)
self.to_melspecgram = AT.MelSpectrogram(
sample_rate=cfg.sample_rate,
n_fft=cfg.n_fft,
win_length=cfg.win_length,
hop_length=cfg.hop_length,
n_mels=cfg.n_mels,
f_min=cfg.f_min,
f_max=cfg.f_max,
power=2,
)
# load in csv files
if split != 'test':
self.df = pd.read_csv("data/FSD50K/FSD50K.ground_truth/dev.csv", header=None)
if split == 'train_val':
pass
elif split == 'train':
self.df = self.df[self.df.iloc[:, 3] == 'train']
elif split == 'val':
self.df = self.df[self.df.iloc[:, 3] == 'val']
else:
self.df = pd.read_csv("data/FSD50K/FSD50K.ground_truth/eval.csv", header=None)
self.files = np.asarray(self.df.iloc[:, 0], dtype=str)
self.labels = np.asarray(self.df.iloc[:, 2], dtype=str) # mids (separated by ,)
self.index_dict = make_index_dict("data/FSD50K/FSD50K.ground_truth/vocabulary.csv")
self.label_num = len(self.index_dict)
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
fname = self.files[idx]
labels = self.labels[idx]
# initialize the label
label_indices = np.zeros(self.label_num)
# add sample labels
for label_str in labels.split(','):
label_indices[int(self.index_dict[label_str])] = 1.0
label_indices = torch.FloatTensor(label_indices)
if self.cfg.load_lms:
# load lms
if self.split != 'test':
audio_path = "data/FSD50K_lms/FSD50K.dev_audio/" + fname + ".npy"
else:
audio_path = "data/FSD50K_lms/FSD50K.eval_audio/" + fname + ".npy"
lms = torch.tensor(np.load(audio_path)).unsqueeze(0)
# Trim or pad
l = lms.shape[-1]
if l > self.crop_frames:
start = np.random.randint(l - self.crop_frames)
lms = lms[..., start:start + self.crop_frames]
elif l < self.crop_frames:
pad_param = []
for i in range(len(lms.shape)):
pad_param += [0, self.crop_frames - l] if i == 0 else [0, 0]
lms = F.pad(lms, pad_param, mode='constant', value=0)
lms = lms.to(torch.float)
else:
# load raw audio
if self.split != 'test':
audio_path = "data/FSD50K/FSD50K.dev_audio/" + fname + ".wav"
else:
audio_path = "data/FSD50K/FSD50K.eval_audio/" + fname + ".wav"
wav, org_sr = librosa.load(audio_path, sr=self.cfg.sample_rate)
wav = torch.tensor(wav) # (length,)
# zero padding to both ends
length_adj = self.unit_length - len(wav)
if length_adj > 0:
half_adj = length_adj // 2
wav = F.pad(wav, (half_adj, length_adj - half_adj))
# random crop unit length wave
length_adj = len(wav) - self.unit_length
start = random.randint(0, length_adj) if length_adj > 0 else 0
wav = wav[start:start + self.unit_length]
# to log mel spectogram -> (1, n_mels, time)
lms = (self.to_melspecgram(wav) + torch.finfo().eps).log()
lms = lms.unsqueeze(0)
# normalise lms with pre-computed dataset statistics
if self.norm_stats is not None:
lms = (lms - self.norm_stats[0]) / self.norm_stats[1]
# transforms to lms
if self.transform is not None:
lms = self.transform(lms)
return lms, label_indices
class LibriSpeech(Dataset):
def __init__(self, cfg, train=True, transform=None, norm_stats=None, n_dummy=200):
super().__init__()
# initializations
self.cfg = cfg
self.train = train
self.transform = transform
self.norm_stats = norm_stats
self.n_dummy = n_dummy
if self.cfg.load_lms:
self.base_path= "data/LibriSpeech_lms/"
else:
self.base_path = "data/LibriSpeech/"
self.unit_length = int(cfg.unit_sec * cfg.sample_rate)
self.to_melspecgram = AT.MelSpectrogram(
sample_rate=cfg.sample_rate,
n_fft=cfg.n_fft,
win_length=cfg.win_length,
hop_length=cfg.hop_length,
n_mels=cfg.n_mels,
f_min=cfg.f_min,
f_max=cfg.f_max,
power=2,
)
# load in json file
self.datapath = self.base_path + "librispeech_tr960_cut.json"
with open(self.datapath, 'r') as fp:
data_json = json.load(fp)
self.data = data_json.get('data')
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
datum = self.data[idx]
fname = datum.get('wav')
dummy_label = torch.zeros(self.n_dummy)
if self.cfg.load_lms:
# load lms
audio_path = self.base_path + fname[:-len(".flac")] + ".npy"
lms = torch.tensor(np.load(audio_path)).unsqueeze(0)
# Trim or pad
l = lms.shape[-1]
if l > self.cfg.crop_frames:
start = np.random.randint(l - self.cfg.crop_frames)
lms = lms[..., start:start + self.cfg.crop_frames]
elif l < self.cfg.crop_frames:
pad_param = []
for i in range(len(lms.shape)):
pad_param += [0, self.cfg.crop_frames - l] if i == 0 else [0, 0]
lms = F.pad(lms, pad_param, mode='constant', value=0)
lms = lms.to(torch.float)
else:
# load raw audio
audio_path = self.base_path + fname
wav, org_sr = librosa.load(audio_path, sr=self.cfg.sample_rate)
wav = torch.tensor(wav) # (length,)
# zero padding to both ends
length_adj = self.unit_length - len(wav)
if length_adj > 0:
half_adj = length_adj // 2
wav = F.pad(wav, (half_adj, length_adj - half_adj))
# random crop unit length wave
length_adj = len(wav) - self.unit_length
start = random.randint(0, length_adj) if length_adj > 0 else 0
wav = wav[start:start + self.unit_length]
# to log mel spectogram -> (1, n_mels, time)
lms = (self.to_melspecgram(wav) + torch.finfo().eps).log()
lms = lms.unsqueeze(0)
# normalise lms with pre-computed dataset statistics
if self.norm_stats is not None:
lms = (lms - self.norm_stats[0]) / self.norm_stats[1]
# transforms to lms
if self.transform is not None:
lms = self.transform(lms)
return lms, dummy_label
class NSynth_HEAR(Dataset):
def __init__(self, cfg, split='train', transform=None, norm_stats=None):
super().__init__()
# initializations
self.cfg = cfg
self.split = split
self.transform = transform
self.norm_stats = norm_stats
self.base_path = "hear/tasks/nsynth_pitch-v2.2.3-50h/"
self.data_path = self.base_path + f"16000/{split}/"
self.jsonpath = self.base_path + f"{split}.json"
with open(self.jsonpath, 'r') as fp:
data_json = json.load(fp)
self.data = [(name, label[0]) for name, label in data_json.items()]
self.unit_length = int(cfg.unit_sec * cfg.sample_rate)
self.to_melspecgram = AT.MelSpectrogram(
sample_rate=cfg.sample_rate,
n_fft=cfg.n_fft,
win_length=cfg.win_length,
hop_length=cfg.hop_length,
n_mels=cfg.n_mels,
f_min=cfg.f_min,
f_max=cfg.f_max,
power=2,
)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
fname, label = self.data[idx]
label = int(label - 21) # convert pitch to index
if self.cfg.load_lms:
# load lms
audio_path = f"data/nsynth_lms/nsynth-{self.split}/audio/{fname[:-len('.wav')]}.npy"
lms = torch.tensor(np.load(audio_path)).unsqueeze(0)
# Trim or pad
l = lms.shape[-1]
if l > self.cfg.crop_frames:
start = np.random.randint(l - self.cfg.crop_frames)
lms = lms[..., start:start + self.cfg.crop_frames]
elif l < self.cfg.crop_frames:
pad_param = []
for i in range(len(lms.shape)):
pad_param += [0, self.cfg.crop_frames - l] if i == 0 else [0, 0]
lms = F.pad(lms, pad_param, mode='constant', value=0)
lms = lms.to(torch.float)
else:
# load raw audio
audio_path = self.data_path + fname
wav, org_sr = librosa.load(audio_path, sr=self.cfg.sample_rate)
wav = torch.tensor(wav) # (length,)
# zero padding to both ends
length_adj = self.unit_length - len(wav)
if length_adj > 0:
half_adj = length_adj // 2
wav = F.pad(wav, (half_adj, length_adj - half_adj))
# random crop unit length wave
length_adj = len(wav) - self.unit_length
start = random.randint(0, length_adj) if length_adj > 0 else 0
wav = wav[start:start + self.unit_length]
# to log mel spectogram -> (1, n_mels, time)
lms = (self.to_melspecgram(wav) + torch.finfo().eps).log()
lms = lms.unsqueeze(0)
# normalise lms with pre-computed dataset statistics
if self.norm_stats is not None:
lms = (lms - self.norm_stats[0]) / self.norm_stats[1]
# transforms to lms
if self.transform is not None:
lms = self.transform(lms)
return lms, label
class AudioSet(Dataset):
def __init__(self, cfg, transform=None, norm_stats=None):
super().__init__()
self.cfg = cfg
self.transform = transform
self.norm_stats = norm_stats
self.base_dir = "data/audioset_lms/"
# load in csv file
df = pd.read_csv(os.path.join(self.base_dir, "unbalanced_train_segments-downloaded.csv"), header=None)
# first column contains the audio fnames
self.audio_fnames = np.asarray(df.iloc[:, 0])
# second column contains the labels (separated by # for multi-label)
self.labels = np.asarray(df.iloc[:, 1])
# third column contains the identifier (balanced_train_segments or unbalanced_train_segments)
self.ident = np.asarray(df.iloc[:, 2])
# load in class labels and create label -> index look-up dict
self.index_dict = make_index_dict(os.path.join(self.base_dir, "class_labels_indices.csv"))
self.label_num = len(self.index_dict)
# also read in FSD50K csv files (in case of ValueErrors for incorrectly downloaded AS samples)
df_fsd50k = pd.read_csv("data/FSD50K/FSD50K.ground_truth/dev.csv", header=None)
self.files_fsd50k = np.asarray(df_fsd50k.iloc[:, 0], dtype=str)
def __len__(self):
return len(self.audio_fnames)
def __getitem__(self, idx):
audio_fname = self.audio_fnames[idx]
labels = self.labels[idx]
ident = self.ident[idx]
# initialize the label
label_indices = np.zeros(self.label_num)
# add sample labels
for label_str in labels.split('#'):
label_indices[int(self.index_dict[label_str])] = 1.0
label_indices = torch.FloatTensor(label_indices)
# load .npy spectrograms
audio_fpath = os.path.join(os.path.join(*[self.base_dir, "unbalanced_train_segments", f"{audio_fname}.npy"]))
try:
lms = torch.tensor(np.load(audio_fpath)).unsqueeze(0)
except ValueError:
fname = np.random.choice(self.files_fsd50k)
audio_fpath = "data/FSD50K_lms/FSD50K.dev_audio/" + fname + ".npy"
lms = torch.tensor(np.load(audio_fpath)).unsqueeze(0)
# Trim or pad
l = lms.shape[-1]
if l > self.cfg.crop_frames:
start = np.random.randint(l - self.cfg.crop_frames)
lms = lms[..., start:start + self.cfg.crop_frames]
elif l < self.cfg.crop_frames:
pad_param = []
for i in range(len(lms.shape)):
pad_param += [0, self.cfg.crop_frames - l] if i == 0 else [0, 0]
lms = F.pad(lms, pad_param, mode='constant', value=0)
lms = lms.to(torch.float)
# normalize
if self.norm_stats is not None:
lms = (lms - self.norm_stats[0]) / self.norm_stats[1]
# transforms
if self.transform is not None:
lms = self.transform(lms)
return lms, label_indices
def calculate_norm_stats(dataset, n_norm_calc=10000):
# calculate norm stats (randomly sample n_norm_calc points from dataset)
idxs = np.random.randint(0, len(dataset), size=n_norm_calc)
lms_vectors = []
for i in tqdm(idxs):
lms_vectors.append(dataset[i][0])
lms_vectors = torch.stack(lms_vectors)
norm_stats = float(lms_vectors.mean()), float(lms_vectors.std() + torch.finfo().eps)
print(f'Dataset contains {len(dataset)} files with normalizing stats\n'
f'mean: {norm_stats[0]}\t std: {norm_stats[1]}')
norm_stats_dict = {'mean': norm_stats[0], 'std': norm_stats[1]}
with open('norm_stats.json', mode='w') as jsonfile:
json.dump(norm_stats_dict, jsonfile, indent=2)
if __name__ == "__main__":
pass