-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataLoader.py
More file actions
309 lines (258 loc) · 8.95 KB
/
dataLoader.py
File metadata and controls
309 lines (258 loc) · 8.95 KB
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
# @Author: bilotv
# @Date: 2019-03-13T09:59:33+01:00
# @Last modified by: bilotv
# @Last modified time: 2019-03-13T15:37:52+01:00
# -*- coding: utf-8 -*-
import torch
from random import randint
from torch.utils import data
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
import librosa
def F_plot2(data_m, title, col_v=np.zeros(0), row_v=np.zeros(0), labelCol='', labelRow=''):
#plt.imshow(data_m, origin='lower', aspect='auto', extent=[row_v[0], row_v[-1], col_v[0], col_v[-1]], interpolation='nearest')
plt.figure()
plt.imshow(data_m, origin='lower', aspect='auto', interpolation='nearest')
plt.colorbar()
plt.set_cmap('magma')
plt.xlabel("Time")
plt.ylabel("Mel Frequency")
plt.title(title)
#plt.grid(True)
#time.sleep(1)
plt.show()
class AllDrumSoundsSlicesDataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("../GAN/MelAll/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,101)
mel = mel[:,0:100]
out = np.zeros((20,1,240,5))
for i in range(20):
out[i,0]=mel[:,5*i:5*i+5]
X = torch.tensor(out, dtype=torch.float32)
X = X - X.min()
X = X/X.max()
#get labels
if ID[0:4]=="kick":
label=[1,0,0,0,0,0]
if ID[0:4]=="snar":
label=[0,1,0,0,0,0]
if ID[0:3]=="hit":
label=[0,0,1,0,0,0]
if ID[0:4]=="taik":
label=[0,0,0,1,0,0]
if ID[0:4]=="mara":
label=[0,0,0,0,1,0]
if ID[0:3]=="tam":
label=[0,0,0,0,0,1]
label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, label
class AnyDrumSoundsSlicesDataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("MelLoops/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,len(mel[0]))
lenSeq = len(mel[0])//5
mel = mel[:,0:5*lenSeq]
#print(mel.shape)
out = np.zeros((lenSeq,1,240,5))
for i in range(lenSeq):
out[i,0]=mel[:,5*i:5*i+5]
X = torch.tensor(out, dtype=torch.float32)
X = X - X.min()
X = X/X.max()
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, lenSeq
class AnyDrumSoundsSlices25Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("MelLoops/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,len(mel[0]))
lenSeq = len(mel[0])//25
mel = mel[:,0:25*lenSeq]
#print(mel.shape)
out = np.zeros((lenSeq,1,240,25))
for i in range(lenSeq):
out[i,0]=mel[:,25*i:25*i+25]
out = np.log(out+1e-8)
X = torch.tensor(out, dtype=torch.float32)
sig = torch.nn.Sigmoid()
#X = X - X.min()
X = sig(X)
X = X/X.max()
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, lenSeq
class MansDrumSoundsSlices25Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("DATA/AllMelLoops/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,len(mel[0]))
lenSeq = len(mel[0])//25
mel = mel[:,0:25*lenSeq]
#print(mel.shape)
out = np.zeros((lenSeq,1,240,25))
for i in range(lenSeq):
out[i,0]=mel[:,25*i:25*i+25]
out = np.log(out+1e-8)
X = torch.tensor(out, dtype=torch.float32)
sig = torch.nn.Sigmoid()
#X = X - X.min()
X = sig(X)
X = X/X.max()
X = X**0.5
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, lenSeq
class ReconstructionDrumSoundsSlices25Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("DATA/MelAll"+self.list_IDs[index][-9]+"/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,len(mel[0]))
lenSeq = 1
mel = mel[:,0:3+25*lenSeq]
#print(mel.shape)
out = np.zeros((lenSeq,1,240,25))
for i in range(lenSeq):
out[i,0]=mel[:,3+25*i:25*i+25+3]
out = np.log(out+1e-8)
X = torch.tensor(out, dtype=torch.float32)
sig = torch.nn.Sigmoid()
#X = X - X.min()
X = sig(X)
X = X/X.max()
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X
class Finder(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
mel = np.load("DATA/DoubleMelAll0/"+self.list_IDs[index])
ID = self.list_IDs[index]
mel = mel.reshape(240,len(mel[0]))
lenSeq = 1
mel = mel[:,0:3+25*lenSeq]
#print(mel.shape)
out = np.zeros((lenSeq,1,240,25))
for i in range(lenSeq):
out[i,0]=mel[:,3+25*i:25*i+25+3]
out = np.log(out+1e-8)
X = torch.tensor(out, dtype=torch.float32)
sig = torch.nn.Sigmoid()
#X = X - X.min()
X = sig(X)
X = X/X.max()
#get labels
if ID[0:4]=="kick":
label=[1,0,0,0,0,0]
if ID[0:4]=="snar":
label=[0,1,0,0,0,0]
if ID[0:3]=="hit":
label=[0,0,1,0,0,0]
if ID[0:4]=="taik":
label=[0,0,0,1,0,0]
if ID[0:4]=="mara":
label=[0,0,0,0,1,0]
if ID[0:3]=="tam":
label=[0,0,0,0,0,1]
label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, label
class MCNNDataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs):
'Initialization'
# .npy files are used as index
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
ID = self.list_IDs[index]
mel = np.load("MelMCNN/"+ID)
out = np.log(mel+1e-8)
X = torch.tensor(out, dtype=torch.float32)
sig = torch.nn.Sigmoid()
X = sig(X)
X = X/X.max()
wav, sr = librosa.load("wavMCNN/"+ID[:-3]+"wav", sr=48000)
wav = torch.tensor(wav, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.float32)
#label = torch.tensor(label, dtype=torch.string)
return X, wav
if __name__=="__main__":
import os
rootname = "MelAllSubSet1/"
filenames = os.listdir(rootname)
dataset = ReconstructionDrumSoundsSlices25Dataset(filenames)
# Create generators
params = {'batch_size': 1,
'shuffle': True,
'num_workers': 6}
training_generator = torch.utils.data.DataLoader(dataset, **params)
for local_batch in training_generator:
print(local_batch.shape)
spectrogram = local_batch[0,0,0]
F_plot2(spectrogram, "test")
lenSeq=lenSeq.item()
break