-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
48 lines (43 loc) · 2.55 KB
/
dataset.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
import torch
from torch.utils import data
import numpy as np
import pickle
class UrbanSound8KDataset(data.Dataset):
def __init__(self, dataset_path, mode):
self.dataset = pickle.load(open(dataset_path, 'rb'))
self.mode = mode
def __getitem__(self, index):
if self.mode == 'LMC':
feature = np.vstack((self.dataset[index]['features']['logmelspec'],
self.dataset[index]['features']['chroma'],
self.dataset[index]['features']['tonnetz'],
self.dataset[index]['features']['spectral_contrast']))
feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0)
elif self.mode == 'MC':
feature = np.vstack((self.dataset[index]['features']['mfcc'],
self.dataset[index]['features']['chroma'],
self.dataset[index]['features']['tonnetz'],
self.dataset[index]['features']['spectral_contrast']))
feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0)
elif self.mode == 'MLMC':
feature = np.vstack((self.dataset[index]['features']['logmelspec'],
self.dataset[index]['features']['mfcc'],
self.dataset[index]['features']['chroma'],
self.dataset[index]['features']['tonnetz'],
self.dataset[index]['features']['spectral_contrast']))
feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0)
elif self.mode == 'LMC+MC':
lmc = np.vstack((self.dataset[index]['features']['logmelspec'],
self.dataset[index]['features']['chroma'],
self.dataset[index]['features']['tonnetz'],
self.dataset[index]['features']['spectral_contrast']))
mc = np.vstack((self.dataset[index]['features']['mfcc'],
self.dataset[index]['features']['chroma'],
self.dataset[index]['features']['tonnetz'],
self.dataset[index]['features']['spectral_contrast']))
feature = (torch.from_numpy(lmc.astype(np.float32)).unsqueeze(0), torch.from_numpy(mc.astype(np.float32)).unsqueeze(0))
label = self.dataset[index]['classID']
fname = self.dataset[index]['filename']
return feature, label, fname
def __len__(self):
return len(self.dataset)