-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_augmentations.py
304 lines (259 loc) · 13.5 KB
/
new_augmentations.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
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
import numpy as np
import torch
import scipy
import random
def gen_new_aug(sample, args, DEVICE):
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
index = torch.randperm(sample.size(0))
mixing_coeff = (0.9 - 1) * torch.rand(1) + 1
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * mixing_coeff + (1 - mixing_coeff) * abs_fft[index]
z = torch.polar(mixed_abs, phase_fft) # Go back to fft
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time
def gen_new_aug_2(sample, args, inds, out, DEVICE, similarities):
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
mixing_coeff = mixing_coefficient_set_for_each(similarities, inds, args)
coeffs = mixing_coeff.squeeze()
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs[:, None, None] + (1 - coeffs[:, None, None]) * abs_fft[inds]
mixed_phase = phase_mix(phase_fft, inds, similarities)
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time
def gen_new_aug_3_ablation(sample, args, DEVICE, similarities): # Apply proposed mixup but use random coeffs instead of similarity based
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
coeffs = torch.ones(sample.shape[0])
coeffs = torch.nn.init.trunc_normal_(coeffs,1,0.1,0.9,1)
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs[:, None, None] + (1 - coeffs[:, None, None]) * abs_fft[inds]
dtheta, sign = phase_mix_2(phase_fft, inds)
mixed_phase = phase_fft + (1-coeffs[:, None, None]) * torch.abs(dtheta) * sign
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time
def gen_new_aug_4_comparison(sample, args, DEVICE): # Apply random phase changes but keep amplitude the same
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
coeffs = torch.ones(sample.shape[0])
coeffs = torch.nn.init.trunc_normal_(coeffs,1,0.1,0.9,1)
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs[:, None, None] + (1 - coeffs[:, None, None]) * abs_fft[inds]
dtheta, sign = phase_mix_2(phase_fft, inds)
mixed_phase = phase_fft + (1-coeffs[:, None, None]) * torch.abs(dtheta) * sign
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time
def opposite_phase(sample, args, DEVICE, similarities): # Show the importance of phase interpolations
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
coeffs = torch.ones(sample.shape[0])
coeffs = torch.nn.init.trunc_normal_(coeffs,1,0.1,0.9,1)
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs[:, None, None] + (1 - coeffs[:, None, None]) * abs_fft[inds]
dtheta, sign = phase_mix_2(phase_fft, inds)
mixed_phase = phase_fft + (1-coeffs[:, None, None]) * torch.abs(dtheta) * -sign
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time
def STAug(sample, args, DEVICE): # Comparison for Spectral and Time Augmentation
sample = sample.detach().cpu().numpy()
for i in range(sample.shape[0]): # For each sample in the batch
for k in range(sample.shape[2]): # If there is one more than one channel
current_imf = emd.sift.sift(sample[i,:,k])
w = np.random.uniform(0, 2, current_imf.shape[1])
weighted_imfs = current_imf * w[None,:]
s_prime = np.sum(weighted_imfs,axis=1)
sample[i,:,k] = s_prime
return torch.from_numpy(sample).float()
def vanilla_mix_up(sample):
mixing_coeff = (0.9 - 1) * torch.rand(1) + 1
#m = torch.distributions.beta.Beta(torch.tensor([0.5]), torch.tensor([0.5]))
#mixing_coeff = m.sample()
# Permute batch index for mixing
index = torch.randperm(sample.size(0))
# Mix the data
mixed_data = mixing_coeff * sample + (1 - mixing_coeff) * sample[index]
return mixed_data
def vanilla_mix_up_geo(sample):
mixing_coeff = (0.7 - 1) * torch.rand(1) + 1
# Permute batch index for mixing
index = torch.randperm(sample.size(0))
# Mix the data
mixed_data = sample**mixing_coeff * sample[index]**(1 - mixing_coeff)
return mixed_data
def vanilla_mix_up_binary(sample):
alpha=0.8
lam = torch.empty(sample.shape).uniform_(alpha, 1)
mask = torch.empty(sample.shape).bernoulli_(lam)
x_shuffle = sample[torch.randperm(sample.shape[0])]
x_mixup = sample * mask + x_shuffle * (1 - mask)
return x_mixup
def best_mix_up(sample, args, similarities, DEVICE): # Choose coeffs from best, but apply linear (Vanilla mixup) --- Ablation
index = torch.randperm(sample.size(0))
coeffs = mixing_coefficient_set_for_each(similarities, index, args)
coeffs = coeffs.squeeze()
# Mix the data
mixed_data = coeffs[:, None, None] * sample + (1 - coeffs[:, None, None]) * sample[index]
return mixed_data
def best_mix_up_geo(sample, args, inds, out):
mixed_samples = torch.empty(sample.shape, dtype=torch.float64)
for idx, ind in enumerate(inds):
mixing_coeff = (0.7 - 1) * torch.rand(1) + 1
mixed_samples[idx,:,:] = sample[idx,:,:]**mixing_coeff * sample[ind,:,:]**(1 - mixing_coeff)
return mixed_samples
def mixing_coefficient_set(out):
mixing_coefficient = torch.ones(out.shape).to(out.device)
for idx, ind in enumerate(out):
if ind > 0.7:
mixing_coefficient[idx] = (0.7 - 1) * torch.rand(1) + 1
else:
torch.nn.init.trunc_normal_(mixing_coefficient[idx],0.85,out[idx],0.7,1)
return mixing_coefficient
def mixing_coefficient_set_for_each(similarities, inds, args):
threshold = 0.8
mixing_coefficient = torch.ones(similarities.shape)
similarities = similarities.cpu()
distances = torch.gather(similarities,0,inds.unsqueeze(1)).cpu().numpy()
mixing_coefficient = torch.ones(similarities.shape)
distances[distances>threshold] = (0.7 - 1) * torch.rand(1) + 1
mixing_coefficient = torch.ones(distances.shape)
mixing_coefficient = torch.nn.init.trunc_normal_(mixing_coefficient,args.mean, args.std, args.low_limit,args.high_limit)
# mixing_coefficient = torch.nn.init.trunc_normal_(mixing_coefficient,0.9,0.2,0.7,1) --> Example
distances[distances<=threshold] = mixing_coefficient[distances<=threshold]
distances = torch.from_numpy(distances)
return distances
def spec_mix(samples):
batch_size, alpha = samples.size(0), 1
indices = torch.randperm(batch_size)
lam = (0.1 - 0.4) * torch.rand(1) + 0.4
for i in range(samples.size(2)):
current_channel = samples[:,:,i]
current_channel_stft = torch.stft(current_channel,samples.size(1),return_complex=True)
shuffled_data = current_channel_stft[indices, :, :]
cut_len = int(lam * current_channel_stft.size(1))
cut_start = np.random.randint(0, current_channel_stft.size(1) - cut_len + 1)
current_channel_stft[:, cut_start:cut_start+cut_len, :] = shuffled_data[:, cut_start:cut_start+cut_len, :]
samples[:,:,i] = torch.istft(current_channel_stft, n_fft=samples.size(1),length=samples.size(1))
return samples
def cut_mix(data,alpha=2):
batch_size = data.size(0)
indices = torch.randperm(batch_size)
shuffled_data = data[indices]
lam = (0.1 - 0.3) * torch.rand(1) + 0.3
cut_len = int(lam * data.size(1))
cut_start = np.random.randint(0, data.size(1) - cut_len + 1)
data[:, cut_start:cut_start+cut_len] = shuffled_data[:, cut_start:cut_start+cut_len]
return data
def phase_mix(phase_fft, inds, similarities):
phase_difference = phase_fft - phase_fft[inds]
dtheta = phase_difference % (2 * torch.pi)
dtheta[dtheta > torch.pi] -= 2 * torch.pi
clockwise = dtheta > 0
sign = torch.where(clockwise, -1, 1)
coeffs = torch.squeeze(mixing_coefficient_set_for_each_phase(similarities, inds))
mixed_phase = phase_fft
mixed_phase = phase_fft + (1-coeffs[:, None, None]) * torch.abs(dtheta) * sign
return mixed_phase
def phase_mix_2(phase_fft, inds):
phase_difference = phase_fft - phase_fft[inds]
dtheta = phase_difference % (2 * torch.pi)
dtheta[dtheta > torch.pi] -= 2 * torch.pi
clockwise = dtheta > 0
locs = torch.where(torch.abs(phase_difference) > torch.pi, -1, 1)
sign = torch.where(clockwise, -1, 1)
return dtheta, sign
def mixing_coefficient_set_for_each_phase(similarities, inds):
threshold = 0.8
mixing_coefficient = torch.ones(similarities.shape)
similarities = similarities.cpu()
distances = torch.gather(similarities,0,inds.unsqueeze(1)).cpu().numpy()
mixing_coefficient = torch.ones(similarities.shape)
distances[distances>threshold] = (0.9 - 1) * torch.rand(1) + 1
mixing_coefficient = torch.ones(distances.shape)
mixing_coefficient = torch.nn.init.trunc_normal_(mixing_coefficient,1,0.1,0.9,1)
# mixing_coefficient = torch.nn.init.trunc_normal_(mixing_coefficient,0.9,0.2,0.7,1)
distances[distances<=threshold] = mixing_coefficient[distances<=threshold]
distances = torch.from_numpy(distances)
return distances
def check_max_not_selected(max_indices, indices, abs_fft):
for i in range(len(max_indices)):
while indices[i] == max_indices[i].item():
#np.random.shuffle(indices)
indices = np.random.choice(np.ceil(abs_fft.size(1)/2).astype(int),abs_fft.size(2))
return indices
######################################### For Supervised Learning Paradigm #########################################
def vanilla_mixup_sup(sample, target, alpha=0.3):
size_of_batch = sample.size(0)
# Choose quarters of the batch to mix
indices = torch.randperm(size_of_batch)
m = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha]))
mixing_coeff = m.sample()
# Mix the data
mixed_data = mixing_coeff * sample + (1 - mixing_coeff) * sample[indices]
return mixed_data, target, mixing_coeff, target[indices]
def gen_new_aug_3_ablation_sup(sample, args, DEVICE, target, alpha=0.2):
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
m = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha]))
coeffs = m.sample()
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs + (1 - coeffs) * abs_fft[inds]
dtheta, sign = phase_mix_2(phase_fft, inds)
dtheta2, sign2 = phase_mix_2(phase_fft[inds], torch.linspace(0,63,64,dtype=inds.dtype))
#mixed_phase = phase_fft if coeffs > 0.5 else phase_fft[inds]
phase_coeff = (0.9 - 1) * torch.rand(1) + 1
mixed_phase = phase_fft + (1-phase_coeff) * torch.abs(dtheta) * sign if coeffs > 0.5 else phase_fft[inds] + (1-phase_coeff) * torch.abs(dtheta2) * sign2
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time, target, coeffs, target[inds]
def cutmix_sup(data, target, alpha=1.):
batch_size = data.size(0)
indices = torch.randperm(batch_size)
shuffled_data = data[indices]
m = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha]))
lam = m.sample()
cut_len = int(lam * data.size(1))
cut_start = np.random.randint(0, data.size(1) - cut_len + 1)
data[:, cut_start:cut_start+cut_len] = shuffled_data[:, cut_start:cut_start+cut_len]
return data, target, lam, target[indices]
def binary_mixup_sup(sample, target, alpha=0.2):
lam = torch.empty(sample.shape).uniform_(alpha, 1)
mask = torch.empty(sample.shape).bernoulli_(lam)
indices = torch.randperm(sample.shape[0])
x_shuffle = sample[indices]
x_mixup = sample * mask + x_shuffle * (1 - mask)
return x_mixup, target, lam, target[indices]
def gen_new_aug_2_sup(sample, args, inds, out, DEVICE, similarities, target):
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
inds = torch.randperm(sample.size(0))
mixing_coeff = mixing_coefficient_set_for_each(similarities, inds, args)
coeffs = mixing_coeff.squeeze()
abs_fft = torch.abs(fftsamples)
phase_fft = torch.angle(fftsamples)
mixed_abs = abs_fft * coeffs[:, None, None] + (1 - coeffs[:, None, None]) * abs_fft[inds]
mixed_phase = phase_mix(phase_fft, inds, similarities)
#z = torch.polar(mixed_abs, torch.angle(fftsamples)) # Go back to fft
z = torch.polar(mixed_abs, mixed_phase)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
return mixed_samples_time, target, coeffs, target[inds]
def mag_mixup_sup(sample, args, DEVICE, target, alpha=0.2):
fftsamples = torch.fft.rfft(sample, dim=1, norm='ortho')
index = torch.randperm(sample.size(0))
m = torch.distributions.beta.Beta(torch.tensor([alpha]), torch.tensor([alpha]))
coeffs = m.sample()
abs_fft = torch.abs(fftsamples)
phase_fft, phase_fft2 = torch.angle(fftsamples), torch.angle(fftsamples[index])
mixed_abs = abs_fft * coeffs + (1 - coeffs) * abs_fft[index]
z = torch.polar(mixed_abs, phase_fft) if coeffs > 0.5 else torch.polar(mixed_abs, phase_fft2)
mixed_samples_time = torch.fft.irfft(z, dim=1, norm='ortho')
#value = torch.roll(value,5,1)
return mixed_samples_time, target, coeffs, target[index]