-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyDataLoader.py
126 lines (101 loc) · 3.72 KB
/
myDataLoader.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
import os
import numpy as np
from torch.utils.data import Dataset, DataLoader
from Hyper_parameters import HyperParams
import torch
from CNN_Train_Test_Plot import *
class GTZANDataset(Dataset):
'''
Custom torch dataloader
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, index):
return self.x[index], self.y[index]
def __len__(self):
return self.x.shape[0]
def get_label(filename):
'''
filename example: classical.00000.wav
'''
genre = filename.split(".")[0]
label = HyperParams.genres.index(genre)
return label
def load_dataset(name):
x, y = [], []
path = os.path.join(HyperParams.feature_path, name)
for root, _, files in os.walk(path):
for file in files:
data = np.load(os.path.join(root, file))
label = get_label(file)
x.append(data)
y.append(label)
x, y = np.stack(x), np.stack(y)
return x, y
def get_CNN_output(x):
x = torch.tensor(x).cuda()
ex = torch.load("CNN.pth", map_location='cuda').model.extractor
x = torch.unsqueeze(x, dim=1)
ll = list()
with torch.no_grad():
for st, en in zip(range(0, x.shape[0], HyperParams.batch_size), range(HyperParams.batch_size, x.shape[0]+HyperParams.batch_size, HyperParams.batch_size)):
tmp = ex(x[st:en]).cpu().numpy()
ll.append(tmp)
x = np.concatenate(ll, axis=0)
x = x.reshape((x.shape[0], -1))
return x
def get_ndarrays(test=False):
if not test:
x_train, y_train = load_dataset("train")
x_valid, y_valid = load_dataset("valid")
x_test, y_test = load_dataset("test")
# normalize
mean = np.mean(x_train)
std = np.std(x_train)
x_train = (x_train-mean)/std
x_valid = (x_valid-mean)/std
x_test = (x_test-mean)/std
x_train, x_test, x_valid = get_CNN_output(x_train), get_CNN_output(x_test), get_CNN_output(x_valid)
return x_train, y_train, x_test, y_test, x_valid, y_valid
else:
x_valid, y_valid = load_dataset("valid")
x_test, y_test = load_dataset("test")
# normalize
mean = np.mean(x_valid)
std = np.std(x_valid)
x_valid = (x_valid-mean)/std
x_test = (x_test-mean)/std
x_test, x_valid = get_CNN_output(x_test), get_CNN_output(x_valid)
return x_valid, y_valid, x_test, y_test, x_test, y_test
def get_loaders(test=False):
if not test:
x_train, y_train = load_dataset("train")
x_valid, y_valid = load_dataset("valid")
x_test, y_test = load_dataset("test")
# normalize
mean = np.mean(x_train)
std = np.std(x_train)
x_train = (x_train-mean)/std
x_valid = (x_valid-mean)/std
x_test = (x_test-mean)/std
train = GTZANDataset(x_train, y_train)
valid = GTZANDataset(x_valid, y_valid)
test = GTZANDataset(x_test, y_test)
train_loader = DataLoader(
train, batch_size=HyperParams.batch_size, shuffle=True, drop_last=False)
valid_loader = DataLoader(
valid, batch_size=HyperParams.batch_size, shuffle=False, drop_last=False)
test_loader = DataLoader(
test, batch_size=HyperParams.batch_size, shuffle=False, drop_last=False)
return train_loader, valid_loader, test_loader
else:
x_valid, y_valid = load_dataset("valid")
# normalize
mean = np.mean(x_valid)
std = np.std(x_valid)
x_valid = (x_valid-mean)/std
valid = GTZANDataset(x_valid, y_valid)
valid_loader = DataLoader(
valid, batch_size=HyperParams.batch_size, shuffle=False, drop_last=False)
return valid_loader, valid_loader, valid_loader