-
Notifications
You must be signed in to change notification settings - Fork 2
/
augment.py
144 lines (119 loc) · 3.94 KB
/
augment.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
import torch
import logging
import torch.nn as nn
import numpy as np
class RandomPad(nn.Module):
"""docstring for RandomPad"""
def __init__(self, value=0., padding=0):
super().__init__()
self.value = value
self.padding = padding
def forward(self, x):
if self.training and self.padding > 0:
left_right = torch.empty(2).random_(self.padding).int().numpy()
topad = (0, 0, *left_right)
x = nn.functional.pad(x, topad, value=self.value)
return x
class Roll(nn.Module):
"""docstring for Roll"""
def __init__(self, mean, std):
super().__init__()
self.mean = mean
self.std = std
def forward(self, x):
if self.training:
shift = torch.empty(1).normal_(self.mean, self.std).int().item()
x = torch.roll(x, shift, dims=0)
return x
class RandomCrop(nn.Module):
"""docstring for RandomPad"""
def __init__(self, size: int = 100):
super().__init__()
self.size = int(size)
def forward(self, x):
if self.training:
time, freq = x.shape
if time < self.size:
return x
hi = time - self.size
start_ind = torch.empty(1, dtype=torch.long).random_(0, hi).item()
x = x[start_ind:start_ind + self.size, :]
return x
class TimeMask(nn.Module):
def __init__(self, n=1, p=50):
super().__init__()
self.p = p
self.n = 1
def forward(self, x):
time, freq = x.shape
if self.training:
for i in range(self.n):
t = torch.empty(1, dtype=int).random_(self.p).item()
to_sample = max(time - t, 1)
t0 = torch.empty(1, dtype=int).random_(to_sample).item()
x[t0:t0 + t, :] = 0
return x
class FreqMask(nn.Module):
def __init__(self, n=1, p=12):
super().__init__()
self.p = p
self.n = 1
def forward(self, x):
time, freq = x.shape
if self.training:
for i in range(self.n):
f = torch.empty(1, dtype=int).random_(self.p).item()
f0 = torch.empty(1, dtype=int).random_(freq - f).item()
x[:, f0:f0 + f] = 0.
return x
class GaussianNoise(nn.Module):
"""docstring for Gaussian"""
def __init__(self, snr=30, mean=0):
super().__init__()
self._mean = mean
self._snr = snr
def forward(self, x):
if self.training:
E_x = (x**2).sum()/x.shape[0]
noise = torch.empty_like(x).normal_(self._mean, std=1)
E_noise = (noise**2).sum()/noise.shape[0]
alpha = np.sqrt(E_x / (E_noise * pow(10, self._snr / 10)))
x = x + alpha * noise
return x
class Shift(nn.Module):
"""
Randomly shift audio in time by up to `shift` samples.
"""
def __init__(self, shift=4000):
super().__init__()
self.shift = shift
def forward(self, wav):
time, channels = wav.size()
length = time - self.shift
if self.shift > 0:
if not self.training:
wav = wav[..., :length]
else:
offset = torch.randint(self.shift, [channels, 1],
device=wav.device)
indexes = torch.arange(length, device=wav.device)
offset = indexes + offset
wav = wav.gather(0, offset.transpose(0, 1))
return wav
class FlipSign(nn.Module):
"""
Random sign flip.
"""
def forward(self, wav):
time, channels = wav.size()
if self.training:
signs = torch.randint(2, (1, channels),
device=wav.device,
dtype=torch.float32)
wav = wav * (2 * signs - 1)
return wav
if __name__ == "__main__":
x = torch.randn(1, 10)
y = GaussianNoise(10)(x)
print(x)
print(y)