-
Notifications
You must be signed in to change notification settings - Fork 14
/
Dataset.py
208 lines (163 loc) · 6.09 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from functools import reduce
from Utils import *
from Layers import *
class PipelineDataset(torch.utils.data.Dataset):
def __init__(self, X, Y, Y_id, n_src=2, objective='classification', stft_params=None, filter_params=None, shuffle=False):
super(PipelineDataset, self).__init__()
self.X = X
self.Y = Y
self.Y_id = Y_id
self.objective = objective
self.n_src = n_src
self.shuffle = shuffle
if objective == 'classification':
self.len = n_src*X.size(0)
self.X_classification = Y.view(-1, Y.size(-1)).unsqueeze(dim=1)
self.Y_classification = Y_id.view(-1)
if self.shuffle:
torch.manual_seed(42)
p = torch.randperm(self.X_classification.size(0))
self.X_classification = self.X_classification[p, :, :]
self.Y_classification = self.Y_classification[p]
elif self.objective == 'separation':
self.len = X.size(0)
if filter_params is not None:
self.filter = HighPassFilter(cutoff_freq=filter_params['cutoff_freq'],
sample_rate=filter_params['sample_rate'],
b=filter_params['b'])
else:
self.filter = lambda x: x
if stft_params is not None:
stft = STFT(stft_params['kernel_size'],
stft_params['stride'],
stft_params['dB'])
self.representation = lambda x: stft_transform(x, stft)
else:
self.representation = lambda x: x
self.transforms = lambda z: reduce((lambda x, fx: fx(x)), [n_src_channel_unroll(self.filter), torch.squeeze, self.representation], z)
def __len__(self):
return self.len
def __getitem__(self, idx):
if self.objective == 'classification':
return self.X_classification[idx], self.Y_classification[idx]
elif self.objective == 'separation':
return self.X[idx], self.transforms(self.Y[idx]), self.Y_id[idx]
class ClassifierDataset(torch.utils.data.Dataset):
def __init__(self, X, Y):
super(ClassifierDataset, self).__init__()
self.X = X
self.Y = Y
self.len = X.size(0)
def __len__(self):
return self.len
def __getitem__(self, idx):
return self.X[idx], self.Y[idx]
class SeparatorDataset(torch.utils.data.Dataset):
def __init__(self, X, Y, Y_id, stft_params=None, filter_params=None):
super(SeparatorDataset, self).__init__()
self.X = X
self.Y = Y
self.Y_id = Y_id
self.len = X.size(0)
if filter_params is not None:
self.filter = HighPassFilter(cutoff_freq=filter_params['cutoff_freq'],
sample_rate=filter_params['sample_rate'],
b=filter_params['b'])
else:
self.filter = lambda x: x
if stft_params is not None:
stft = STFT(stft_params['kernel_size'],
stft_params['stride'],
stft_params['dB'])
self.representation = lambda x: stft_transform(x, stft)
else:
self.representation = lambda x: x
self.transforms = lambda z: reduce((lambda x, fx: fx(x)), [n_src_channel_unroll(self.filter), torch.squeeze, self.representation], z)
def __len__(self):
return self.len
def __getitem__(self, idx):
return self.X[idx], self.transforms(self.Y[idx]), self.Y_id[idx]
class MixtureDataset(torch.utils.data.Dataset):
def __init__(self, X, Y, size=None, n_src=2, subset='train', shift_factor=0.1, shift_overlaps=True, pad='zero', side='front', seed=42):
self.Y = Y
self.categories = torch.unique(Y).tolist()
self.X_cat = {c:X[self.Y == c] for c in self.categories}
if size is None:
self.size = X.size(0) // n_src
else:
self.size = size
self.n_src = n_src
self.subset = subset
if shift_factor is not None:
self.overlapper = lambda s: self.mix_overlaps(s,
shift_factor=shift_factor,
shift_overlaps=shift_overlaps,
pad=pad,
side=side)
else:
self.overlapper = lambda s: s
self.seed = seed
random.seed(self.seed)
def __len__(self):
return self.size
def __getitem__(self, idx):
if self.subset == 'val':
random.seed(idx)
ids = random.sample(self.categories, self.n_src)
Xs = [random.choice(self.X_cat[i]) for i in ids]
Xs = self.overlapper(Xs)
Ys = [torch.LongTensor([i]) for i in ids]
X_mix = sum(Xs).unsqueeze(dim=0)
Y_mix = torch.vstack(Xs)
Y_mix_id = torch.vstack(Ys).squeeze(dim=-1)
return X_mix, Y_mix, Y_mix_id
@staticmethod
def signal_shifter(signal, shift_factor, pad='zero', side='front'):
if type(pad)==str:
assert pad in ['zero', 'noise', 'sub_noise'], print('Pad must be \'zero\', \'noise\', \'sub_noise\' or a float')
else:
assert type(pad) == float, print('Pad must be \'zero\', \'noise\', \'sub_noise\' or a float')
assert side in ['front', 'back']
frames = signal.shape[-1]
if pad == 'zero':
noise = 0
elif pad == 'noise':
noise = signal[-1]
elif pad == 'sub_noise':
noise = signal
signal = signal - noise
noise = 0
else:
noise = pad
frame_shift = random.randint(0, int(shift_factor * frames))
noise_frames = torch.ones(frame_shift) * noise
if side == 'front':
signal_frames = signal[0:frames - frame_shift]
elif side == 'back':
signal_frames = signal[frame_shift-1:-1]
signal = torch.cat([noise_frames, signal_frames], dim=-1)
return signal
def mix_overlaps(self, signals, shift_factor=0.1, shift_overlaps=True, pad='zero', side='front'):
frames = signals[0].shape[-1]
if shift_overlaps:
trimmed_signals = [s[torch.nonzero(s)].squeeze() for s in signals]
trimmed_lengths = [len(t) for t in trimmed_signals]
max_length = max(trimmed_lengths)
max_index = trimmed_lengths.index(max_length)
for i, e in enumerate(trimmed_signals):
if i != max_index:
frame_shift = min(random.randint(0, max_length), frames-trimmed_lengths[i])
shift = torch.zeros(frame_shift)
signals[i] = torch.cat([shift, trimmed_signals[i]])
else:
signals[i] = trimmed_signals[i]
max_length = max([len(s) for s in signals])
front_zeros = torch.zeros(random.randint(0, min(int(shift_factor * frames), frames-max_length)))
signals = [torch.cat([front_zeros, s], dim=-1) for s in signals]
signals = [torch.cat([s, torch.zeros(frames - len(s))], dim=-1) for s in signals]
else:
signals = [self.signal_shifter(s, shift_factor, pad=pad, side=side) for s in signals]
return signals