-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
277 lines (214 loc) · 8.91 KB
/
Copy pathdatasets.py
File metadata and controls
277 lines (214 loc) · 8.91 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
import torch
import os
import librosa
import tqdm
import numpy as np
import glob
import time_frequency as tf
def fmconst(n_points, fnorm=0.25):
ts = torch.arange(n_points)
random_phase = torch.rand(1) * (2*torch.pi)
y = torch.sin(2.0 * torch.pi * fnorm * ts + random_phase)
y = y / torch.max(y)
return y
def gauss_pulse(t_loc, f_loc, sigma, n_points):
gauss_window = tf.gauss_whole(sigma, t_loc, n_points)
fm_signal = fmconst(n_points, f_loc)
gp = gauss_window * fm_signal
return gp - torch.mean(gp)
def torch_random_uniform(limits):
r1, r2 = limits
x = (r1 - r2) * torch.rand(1) + r2
return x
class GaussPulseDatasetTimeFrequency(torch.utils.data.Dataset):
def __init__(self, sigma, n_points, noise_std, n_samples=10000, f_center_max_offset=0, t_center_max_offset=0, demo=False):
self.xs = torch.empty((n_samples, n_points), dtype=torch.float64)
self.ys = torch.empty((n_samples), dtype=torch.long)
self.locs = torch.zeros((n_samples, 4), dtype=torch.float64)
# maximum displacement limits for time-offset for pulses from center
image_displacement = 5
t_max = n_points / image_displacement
f_max = 0.5 / image_displacement
# lower displacement limits
t_min = sigma
f_min = 0.5 * (t_min / n_points) # = sigma / (2N) = sigma / K = 1/(2*pi*sigma)
sigma_scale_max = (2*t_max)/(6*sigma) + 1
# minimum duration scaling for pulses on center
sigma_scale_min = 1 / sigma_scale_max
# generate samples
for idx in range(n_samples):
if demo:
f_center_offset = 0
t_center_offset = 0
else:
f_center_offset = torch_random_uniform([-f_center_max_offset, f_center_max_offset])
t_center_offset = torch_random_uniform([-t_center_max_offset, t_center_max_offset])
t_center = t_center_offset + torch.tensor(n_points/2, dtype=torch.float)
f_center = f_center_offset + 0.25
if demo:
f_offset = 0.5 * f_max
t_offset = 0.5 * t_max
else:
f_offset = torch_random_uniform([f_min, f_max])
t_offset = torch_random_uniform([t_min, t_max])
y = np.random.choice([0, 1, 2])
if y == 0:
# spread randomly along frequency or time axis
r = np.random.choice([True, False])
if r:
sigma_scale = torch_random_uniform([1.0, sigma_scale_max])
else:
sigma_scale = torch_random_uniform([sigma_scale_min, 1.0])
if demo:
sigma_scale = 1.0
x = gauss_pulse(t_center, f_center, sigma*sigma_scale, n_points)
# used to sanity check
self.locs[idx, 0] = t_center
self.locs[idx, 1] = f_center
self.locs[idx, 2] = int(r)
self.locs[idx, 3] = sigma_scale
elif y == 1:
f_loc = f_center
t_loc_1 = t_center - t_offset
t_loc_2 = t_center + t_offset
x1 = gauss_pulse(t_loc_1, f_loc, sigma, n_points)
x2 = gauss_pulse(t_loc_2, f_loc, sigma, n_points)
x = x1 + x2
# used to sanity check
self.locs[idx, 0] = t_loc_1
self.locs[idx, 1] = f_loc
self.locs[idx, 2] = t_loc_2
self.locs[idx, 3] = f_loc
else:
t_loc = t_center
f_loc_1 = f_center - f_offset
f_loc_2 = f_center + f_offset
x1 = gauss_pulse(t_loc, f_loc_1, sigma, n_points)
x2 = gauss_pulse(t_loc, f_loc_2, sigma, n_points)
x = x1 + x2
# used to sanity check
self.locs[idx, 0] = t_loc
self.locs[idx, 1] = f_loc_1
self.locs[idx, 2] = t_loc
self.locs[idx, 3] = f_loc_2
# variability
noise = noise_std * torch.rand(n_points)
if demo:
amplitude_scale = 1.0
else:
amplitude_scale = torch_random_uniform([0.5, 1])
x = (x * amplitude_scale) + noise
self.ys[idx] = torch.tensor(y, dtype=torch.long)
self.xs[idx] = x - torch.mean(x)
def __len__(self):
return len(self.xs)
def __getitem__(self, idx):
return self.xs[idx], self.ys[idx]
def parse_row(row):
filename = row[0]
fold = int(row[1])
target = int(row[2])
category = row[3]
return filename, fold, target, category
def parse_csv(csv_file):
with open(csv_file, 'r') as f:
lines = f.readlines()
meta = []
for line in lines[1:]:
row = line.rstrip().split(',')
filename, fold, target, category = parse_row(row)
meta.append((filename, fold, target, category))
return meta
def load_meta_data(source_dir):
csv_file = os.path.join(source_dir, 'meta', 'esc50.csv')
meta_data = parse_csv(csv_file)
return meta_data
class AudioMNISTBigDataset(torch.utils.data.Dataset):
def __init__(self, wav_paths):
self.xs = []
self.ys = []
sample_rates = []
for wav_path in wav_paths:
audio, sr = librosa.load(wav_path, sr=None) # already 8000 Hz
sample_rates.append(sr)
target = int(os.path.basename(wav_path).split('_')[0])
x = audio.copy()
# zero pad signal on both sides
x = np.pad(x, 1 + (8000-len(x)) // 2)[:8000]
self.xs.append(x)
self.ys.append(target)
assert len(list(set(self.ys))) == 10 # assert 10 classes
self.xs = np.array(self.xs)
self.ys = np.array(self.ys)
# assert all files have the same sample rates
assert len(list(set(sample_rates))) == 1
# assert proper sample rate
assert sample_rates[0] == 8000
self.sample_rate = 8000
def __len__(self):
return len(self.xs)
def __getitem__(self, idx):
return self.xs[idx], self.ys[idx]
class AudioMNISTDataset(torch.utils.data.Dataset):
# VERSION: https://doi.org/10.5281/zenodo.1342401
def __init__(self, source_dir):
# load data
wav_paths = glob.glob(os.path.join(source_dir, 'recordings', '*.wav'))
self.xs = []
self.ys = []
sample_rates = []
for wav_path in wav_paths:
audio, sr = librosa.load(wav_path, sr=None) # already 8000 Hz
sample_rates.append(sr)
target = int(os.path.basename(wav_path).split('_')[0])
if len(audio) >= 1500 and len(audio) <= 5500:
x = audio.copy()
x.resize(5500) # pad end with zeros up to max length
self.xs.append(x)
self.ys.append(target)
assert len(list(set(self.ys))) == 10 # assert 10 classes
self.xs = np.array(self.xs)
self.ys = np.array(self.ys)
# assert all files have the same sample rates
assert len(list(set(sample_rates))) == 1
# assert proper sample rate
assert sample_rates[0] == 8000
self.sample_rate = 8000
def __len__(self):
return len(self.xs)
def __getitem__(self, idx):
return self.xs[idx], self.ys[idx]
class ESC50Dataset(torch.utils.data.Dataset):
def __init__(self, source_dir, resample_rate=8000):
meta_data = load_meta_data(source_dir)
self.xs = []
self.ys = []
self.sample_rate = None
xs_path = os.path.join(source_dir, "{}_xs.npy".format(resample_rate))
ys_path = os.path.join(source_dir, "{}_ys.npy".format(resample_rate))
if os.path.exists(xs_path) and os.path.exists(ys_path):
self.xs = np.load(xs_path)
self.ys = np.load(ys_path)
self.sample_rate = resample_rate
else:
sample_rates = []
for (filename, fold, target, category) in tqdm.tqdm(meta_data):
# load audio
audio_file = os.path.join(source_dir, 'audio', filename)
audio, sr = librosa.load(audio_file, sr=resample_rate, res_type='kaiser_fast')
sample_rates.append(sr)
self.xs.append(audio)
self.ys.append(target)
self.xs = np.array(self.xs)
self.ys = np.array(self.ys)
np.save(xs_path, self.xs)
np.save(ys_path, self.ys)
# assert all files have the same sample rates
assert len(list(set(sample_rates))) == 1
# assert proper resampling
assert sample_rates[0] == resample_rate
self.sample_rate = resample_rate
def __len__(self):
return len(self.xs)
def __getitem__(self, idx):
return self.xs[idx], self.ys[idx]