forked from kkoutini/cpjku_dcase19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
628 lines (502 loc) · 19.2 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import hashlib
import os
import time
from os.path import expanduser
import numpy as np
from attrdict import AttrDefault
import pandas as pd
import torch
from sklearn import preprocessing
from torch.utils.data import *
def parser_categorical_csv(csv_file, files_col, labels_col, header=None, delimiter="\t", label_encoder=None):
"""
csv metadata parser, reads file name and labels from CSV
:param csv_file: csv_file
:param files_col: the column with file names
:param labels_col: the column with the ile labels
:return: files, labels, label_encoder_decoder
"""
df = pd.read_csv(csv_file, header=header, delimiter=delimiter)
df = df[[df.columns[files_col], df.columns[labels_col]]]
df.columns = [0, 1]
le = label_encoder
if le is None:
le = preprocessing.LabelEncoder()
y_labels = le.fit_transform(df.iloc[:, 1])
else:
y_labels = le.fit_transform(df.iloc[:, 1])
return df.iloc[:, 0].values, y_labels, le
def get_meta_parser(parser):
return parser_categorical_csv
df_trset = None
loaded_dataset = None
label_encoder = None
class DatasetsManager:
def __init__(self, config):
self.config = AttrDefault(lambda: None, config)
def get_dataset(self, config):
return getattr(self, config.dataset.split(".")[1])()
def get_full_dataset(self):
name, normalize, audio_path, parser, parser_args, audio_processor, cache, cache_x_name, file_cache = \
self.config['name'], self.config['normalize'], self.config['audio_path'], self.config['parser'], \
self.config[
'parser_args'], self.config['audio_processor'], self.config['cache'], self.config['cache_x_name'], \
self.config['file_cache']
audio_path = os.path.expanduser(audio_path)
global loaded_dataset
global label_encoder
if loaded_dataset is not None:
return loaded_dataset
print("loading dataset from '{}'".format(name))
if normalize:
print("normalizing dataset")
def getdatset():
meta_parser = get_meta_parser(parser)
global label_encoder
files, labels, label_encoder = meta_parser(**parser_args)
return AudioPreprocessDataset(files, labels, label_encoder, audio_path, audio_processor)
if cache and file_cache:
loaded_dataset = FilesCachedDataset(getdatset, name, x_name=cache_x_name + "_ap_" + audio_processor)
else:
loaded_dataset = getdatset()
return loaded_dataset
def df_get_train_set(self):
train_files_csv, fold = self.config.train_files_csv, self.config.fold
global df_trset
if df_trset is not None:
return df_trset
df_trset = SelectionDataset(self.get_full_dataset(),
pd.read_csv(train_files_csv.format(fold), header=0, sep="\t")['filename'].values)
return df_trset
def df_get_test_set(self):
test_files_csv, fold = self.config.test_files_csv, self.config.fold
totalset = self.get_full_dataset()
return SelectionDataset(totalset,
pd.read_csv(test_files_csv.format(fold), header=0, sep="\t")['filename'].values)
def get_test_set(self):
normalize = self.config.normalize
if not normalize:
return df_get_test_set()
print("normalized test!")
name, fold, train_files_csv, audio_processor = self.config.name, \
self.config.fold, self.config.train_files_csv, \
self.config.audio_processor
fill_norms(name, fold, train_files_csv, audio_processor, self.df_get_train_set())
ds = self.df_get_test_set()
return PreprocessDataset(ds, norm_func)
def get_train_set(self):
normalize, subsample, roll, \
stereo_desync, stereo_flip, \
vertical_desync, spec_resize = self.config.normalize, \
self.config.subsample, self.config.roll, self.config.stereo_desync, \
self.config.stereo_flip, self.config.vertical_desync, self.config.spec_resize
name, fold, train_files_csv, audio_processor = self.config.name, \
self.config.fold, self.config.train_files_csv, \
self.config.audio_processor
ds = self.df_get_train_set()
if subsample == 1:
print("subsample train!")
ds = PreprocessDataset(ds, subsample_func)
elif subsample == 2:
print("subsample train to 10 secs!")
ds = PreprocessDataset(ds, subsample_func2)
if normalize:
print("normalized train!")
fill_norms(name, fold, train_files_csv, audio_processor, self.df_get_train_set())
ds = PreprocessDataset(ds, norm_func)
if roll:
ds = PreprocessDataset(ds, get_roll_func())
if stereo_flip:
ds = PreprocessDataset(ds, get_stereo_flip_func())
if stereo_desync:
ds = PreprocessDataset(ds, get_desync_stereo_roll_func())
if vertical_desync:
ds = PreprocessDataset(ds, get_vertical_desync_func())
return ds
def get_roll_func(axis=2, shift=None):
def roll_func(x):
sf = shift
if shift is None:
sf = int(np.random.random_integers(-50, 50))
global FirstTime
return x.roll(sf, axis)
return roll_func
def get_desync_stereo_roll_func(shift=None):
def roll_func(x):
sf = shift
if shift is None:
sf = int(np.random.random_integers(-1, 1))
global FirstTime
y = x[0]
y = y.roll(sf, 1)
x[0] = y
return x
return roll_func
def get_vertical_desync_func(shift=None):
def roll_func(x):
sf = shift
if shift is None:
sf = int(np.random.random_integers(-1, 1))
global FirstTime
x = x.roll(sf, 1)
return x
return roll_func
def get_stereo_flip_func(audio_processor):
if audio_processor == "k19_stereo":
def flip_func(x):
sf = int(np.random.random_integers(0, 1))
global FirstTime
if sf:
y = x[0]
x[0] = x[1]
x[1] = y
y = x[2]
x[2] = x[3]
x[3] = y
return x
return flip_func
def flip_func(x):
sf = int(np.random.random_integers(0, 1))
global FirstTime
if sf:
y = x[0]
x[0] = x[1]
x[1] = y
return x
return flip_func
class FilesCachedDataset(Dataset):
def __init__(self, get_dataset_func, dataset_name, x_name="", y_name="",
cache_path="datasets/cached_datasets/",
):
self.dataset = None
def getDataset():
if self.dataset == None:
self.dataset = get_dataset_func()
return self.dataset
self.get_dataset_func = getDataset
self.x_name = x_name + y_name
cache_path = expanduser(cache_path)
self.cache_path = os.path.join(cache_path, dataset_name, "files_cache", self.x_name)
try:
original_umask = os.umask(0)
os.makedirs(self.cache_path, exist_ok=True)
finally:
os.umask(original_umask)
def __getitem__(self, index):
cpath = os.path.join(self.cache_path, str(index) + ".pt")
try:
return torch.load(cpath)
except FileNotFoundError:
tup = self.get_dataset_func()[index]
torch.save(tup, cpath)
return tup
def get_ordered_labels(self):
return self.get_dataset_func().get_ordered_labels()
def get_ordered_ids(self):
return self.get_dataset_func().get_ordered_ids()
def get_xcache_path(self):
return os.path.join(self.cache_path, self.x_name + "_x.pt")
def get_ycache_path(self):
return os.path.join(self.cache_path, self.y_name + "_y.pt")
def get_sidcache_path(self):
return os.path.join(self.cache_path, self.y_name + "_sid.pt")
def __len__(self):
return len(self.get_dataset_func())
class PreprocessDataset(Dataset):
"""A bases preprocessing dataset representing a preprocessing step of a Dataset preprossessed on the fly.
supporting integer indexing in range from 0 to len(self) exclusive.
"""
def __init__(self, dataset, preprocessor):
self.dataset = dataset
if callable(preprocessor):
self.preprocessor = preprocessor
else:
print("preprocessor is not calllable ", preprocessor)
def __getitem__(self, index):
x, id, y = self.dataset[index]
return self.preprocessor(x), id, y
def get_ordered_ids(self):
return self.dataset.get_ordered_ids()
def get_ordered_labels(self):
return self.dataset.get_ordered_labels()
def __len__(self):
return len(self.dataset)
class SelectionDataset(Dataset):
"""A dataset that selects a subsample from a dataset based on a set of sample ids.
supporting integer indexing in range from 0 to len(self) exclusive.
"""
def __init__(self, dataset, sample_ids):
self.available_indexes = []
self.dataset = dataset
self.reselect(sample_ids)
def reselect(self, sample_ids):
reverse_dict = dict([(sid, i) for i, sid in enumerate(self.dataset.get_ordered_ids())])
self.available_indexes = [reverse_dict[sid] for sid in sample_ids]
def get_ordered_ids(self):
return self.sample_ids
def get_ordered_labels(self):
raise NotImplementedError("Maybe reconsider caching only a selection Dataset. why not select after cache?")
def __getitem__(self, index):
return self.dataset[self.available_indexes[index]]
def __len__(self):
return len(self.available_indexes)
class AudioPreprocessDataset(Dataset):
"""A bases preprocessing dataset representing a Dataset of files that are loaded and preprossessed on the fly.
Access elements via __getitem__ to return: preprocessor(x),sample_id,label
supporting integer indexing in range from 0 to len(self) exclusive.
"""
def __init__(self, files, labels, label_encoder, base_dir, preprocessor, return_tensor=True):
self.files = files
self.labels = labels
self.label_encoder = label_encoder
self.base_dir = base_dir
self.preprocessor = get_audio_processor(preprocessor)
self.return_tensor = return_tensor
def __getitem__(self, index):
x = self.preprocessor(self.base_dir + self.files[index])
if self.return_tensor and not isinstance(x, torch.Tensor):
x = torch.from_numpy(x)
return x, self.files[index], self.labels[index]
def get_ordered_ids(self):
return self.files
def get_ordered_labels(self):
return self.labels
def __len__(self):
return len(self.files)
import audioprocessors as ap
processors_names = sorted(name for name in ap.__dict__
if name.islower() and not name.startswith("__")
and name.startswith("processor_")
and callable(ap.__dict__[name]))
def get_audio_processor(name):
if 'processor_' + name not in processors_names:
print('processor_', name, " not in ", str(processors_names))
print("check for typos or is it implemented!")
assert 'processor_' + name in processors_names
return ap.__dict__['processor_' + name]
class ObjectCacher:
def __init__(self, get_obj_func, dataset_name, obj_name="",
cache_path="datasets/cached_datasets/", verbose=True):
self.dataset_name = dataset_name
self.obj_name = obj_name
cache_path = expanduser(cache_path)
self.cache_path = os.path.join(cache_path, dataset_name)
try:
startTime = time.time()
xpath = self.get_obj_cache_path()
if verbose:
print(
"attempting to load x from cache at " + xpath + "...")
self.obj = torch.load(xpath)
if verbose:
endTime = time.time()
print(
"loaded " + xpath + " from cache in %s " % (endTime - startTime))
except IOError:
if verbose:
print(
"Invalid cache " + xpath + " , recomputing")
self.obj = get_obj_func()
saveStartTime = time.time()
torch.save(self.obj, xpath)
if verbose:
endTime = time.time()
print(
"loaded " + obj_name + " in %s, and cached in %s, total %s seconds " % (
(saveStartTime - startTime),
(endTime - saveStartTime), (endTime - startTime)))
def get_obj_cache_path(self):
return os.path.join(self.cache_path, self.obj_name + "_obj.pt")
def get(self):
return self.obj
def norm_func(x):
return (x - tr_mean) / tr_std
def subsample_func(x):
k = torch.randint(x.size(2) - x.size(1) + 1, (1,))[0].item()
return x[:, :, k:k + x.size(1)]
def subsample_func2(x):
k = torch.randint(x.size(2) - 431 + 1, (1,))[0].item()
return x[:, :, k:k + 431]
def cal_mean_single_thread():
trainset = df_trainset
c = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c.append(x)
t = torch.stack(c).transpose(2, 3).contiguous()
print(t.size())
t = t.view(-1, t.size()[3])
print(t.size())
m = t.mean(0).float().reshape(1, 256, 1)
print("mean", m.size())
return m
def cal_mean():
trainset = df_trainset
c = []
print("cal_mean ")
lengsths_sum = 0
for i, (x, _, _) in enumerate(torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=False, num_workers=8)):
# = trainset[i]
if i == 0:
print(x.shape)
lengsths_sum += x.shape[3]
x = x[0]
x = x.transpose(1, 2).contiguous().view(-1, x.size(1))
c.append(x)
print("average length", lengsths_sum / len(trainset))
print("c [0,1]= ", c[0].size(), c[1].size())
t = torch.cat(c) # .transpose(2, 3).contiguous()
print(t.size())
m = t.mean(0).float().reshape(1, c[0].size(1), 1)
print("mean", m.size())
del t
return m
def cal_mean_with_deltas():
trainset = df_trainset
c1 = []
c2 = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c1.append(x[0:2])
c2.append(x[2:])
t1 = torch.stack(c1).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m0 = t1.mean(0).float().reshape(256, 1)
print("mean", m0.size())
t1 = torch.stack(c2).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m1 = t1.mean(0).float().reshape(256, 1)
print("mean", m1.size())
m = torch.stack([m0, m0, m1])
print("all mean", m.size())
return m
def cal_std_with_deltas():
trainset = df_trainset
c1 = []
c2 = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c1.append(x[0:2])
c2.append(x[2:])
t1 = torch.stack(c1).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m0 = t1.std(0).float().reshape(256, 1)
print("std", m0.size())
t1 = torch.stack(c2).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m1 = t1.std(0).float().reshape(256, 1)
print("std", m1.size())
m = torch.stack([m0, m0, m1])
print("all std", m.size())
return m
def cal_mean_per_2channel():
trainset = df_trainset
c1 = []
c2 = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c1.append(x[0:2])
c2.append(x[2:])
t1 = torch.stack(c1).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m0 = t1.mean(0).float().reshape(256, 1)
print("mean", m0.size())
t1 = torch.stack(c2).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m1 = t1.mean(0).float().reshape(256, 1)
print("mean", m1.size())
m = torch.stack([m0, m0, m1, m1])
print("all mean", m.size())
return m
def cal_std_per_2channel():
trainset = df_trainset
c1 = []
c2 = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c1.append(x[0:2])
c2.append(x[2:])
t1 = torch.stack(c1).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m0 = t1.std(0).float().reshape(256, 1)
print("std", m0.size())
t1 = torch.stack(c2).transpose(2, 3).contiguous()
print(t1.size())
t1 = t1.view(-1, t1.size()[3])
print(t1.size())
m1 = t1.std(0).float().reshape(256, 1)
print("std", m1.size())
m = torch.stack([m0, m0, m1, m1])
print("all std", m.size())
return m
def cal_std_single_thread():
trainset = df_trainset
c = []
for i in range(len(trainset)):
x, _, _ = trainset[i]
c.append(x)
t = torch.stack(c).transpose(2, 3).contiguous()
print(t.size())
t = t.view(-1, t.size()[3])
print(t.size())
sd = t.std(0).float().reshape(1, 256, 1)
print("sd", sd.size())
return sd
def cal_std():
trainset = df_trainset
c = []
for i, (x, _, _) in enumerate(torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=False, num_workers=10)):
# x, _, _ = trainset[i]
x = x[0]
x = x.transpose(1, 2).contiguous().view(-1, x.size(1))
c.append(x)
print("c [0,1]= ", c[0].size(), c[1].size())
t = torch.cat(c) # .transpose(2, 3).contiguous()
print(t.size())
sd = t.std(0).float().reshape(1, c[0].size(1), 1)
print("sd", sd.size())
return sd
df_trainset = None
tr_mean = None
tr_std = None
def fill_norms(name, fold, train_files_csv, audio_processor, training_set):
global tr_mean, tr_std, df_trainset
df_trainset = training_set
mnfunc = cal_mean
if audio_processor == "k19_stereo":
print("k19_stereo mean calc")
mnfunc = cal_mean_per_2channel
if audio_processor == "m18_stereo_deltas":
print("deltas mean calc")
mnfunc = cal_mean_with_deltas
if tr_mean is None:
tr_mean = ObjectCacher(mnfunc, name,
"tr_mean_{}_f{}_ap{}".format(h6(train_files_csv), fold, audio_processor)).get()
# print("train_val:", torch.all(torch.eq(tr_mean, tr_mean_val)))
stdfunc = cal_std
if audio_processor == "k19_stereo":
print("k19_stereo std calc")
stdfunc = cal_std_per_2channel
if audio_processor == "m18_stereo_deltas":
print("deltas std calc")
stdfunc = cal_std_with_deltas
if tr_std is None:
tr_std = ObjectCacher(stdfunc, name,
"tr_std_{}_f{}_ap{}".format(h6(train_files_csv), fold, audio_processor)).get()
# print("sd_val:", torch.all(torch.eq(tr_mean, tr_mean_val)))
def h6(w):
return hashlib.md5(w.encode('utf-8')).hexdigest()[:6]