-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio_unet.py
185 lines (130 loc) · 5.86 KB
/
audio_unet.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
import torch
import torch.nn as nn
# from this paper: https://arxiv.org/abs/2011.02421
# one-shot conditional audio filtering of arbitrary sounds
# skipping the FiLM layers, as we're just filtering everything but speech out
class ResidualUnit(nn.Module):
def __init__(self, num_channels, dilation):
super().__init__()
self.layers = nn.Sequential(
nn.Conv1d(num_channels, num_channels, kernel_size=(3,), dilation=dilation, padding=dilation,
padding_mode='replicate'),
# 16 channels per group
#nn.GroupNorm(num_channels // 16, num_channels),
nn.ELU(inplace=True),
nn.Conv1d(num_channels, num_channels, kernel_size=(1,)),
#nn.GroupNorm(num_channels // 16, num_channels),
nn.ELU(inplace=True),
)
self.skip = SkipConnection(num_channels)
def forward(self, x):
return self.layers(x) + self.skip(x)
class SkipConnection(nn.Module):
def __init__(self, num_channels, output_channels=None):
super().__init__()
if output_channels is None:
output_channels = num_channels
layers = [
nn.Conv1d(num_channels, output_channels, kernel_size=(1,)),
]
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
class EncoderBlock(nn.Module):
def __init__(self, channels, stride, n_res_units=3):
super().__init__()
layers = []
for i in range(n_res_units):
layers.append(ResidualUnit(channels, 3 ** i))
layers.append(
nn.Conv1d(channels, 2 * channels, kernel_size=(2 * stride,), stride=(stride,), padding=stride // 2,
padding_mode='replicate'))
# 16 channels per group
#layers.append(nn.GroupNorm(channels // 8, 2 * channels))
layers.append(nn.ELU(inplace=True))
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
class DecoderBlock(nn.Module):
def __init__(self, channels, stride, n_res_units=3):
super().__init__()
layers = []
layers.append(
nn.ConvTranspose1d(channels, channels // 2, kernel_size=(2 * stride,), stride=(stride,),
padding=stride)
)
layers.append(nn.ReplicationPad1d(stride // 2))
# 16 channels per group
#layers.append(nn.GroupNorm(channels // 32, channels // 2))
layers.append(nn.ELU(inplace=True))
for i in range(n_res_units):
layers.append(ResidualUnit(channels // 2, 3 ** i))
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
class AudioUNet(nn.Module):
def __init__(self, base_channels=32, input_channels=1, output_channels=1, n_res_units=3):
super().__init__()
self.input_conv = nn.Sequential(
nn.Conv1d(input_channels, base_channels, kernel_size=(7,), padding=3, padding_mode='replicate'),
# more conservative 8 channels per group
#nn.GroupNorm(base_channels//8, base_channels),
nn.ELU(inplace=True)
)
self.input_skip = SkipConnection(input_channels, output_channels)
self.encoder_blocks = nn.ModuleList([
EncoderBlock(base_channels, stride=2, n_res_units=n_res_units),
EncoderBlock(base_channels * 2, stride=2, n_res_units=n_res_units),
EncoderBlock(base_channels * 4, stride=8, n_res_units=n_res_units),
EncoderBlock(base_channels * 8, stride=8, n_res_units=n_res_units),
])
self.middle_layers = nn.Sequential(
nn.Conv1d(base_channels * 16, base_channels * 16, kernel_size=(7,), padding=3,
padding_mode='replicate'),
#nn.GroupNorm(base_channels, base_channels * 16),
nn.ELU(inplace=True),
nn.Conv1d(base_channels * 16, base_channels * 16, kernel_size=(7,), padding=3,
padding_mode='replicate'),
#nn.GroupNorm(base_channels, base_channels * 16),
nn.ELU(inplace=True),
)
self.middle_skip = SkipConnection(base_channels * 16)
self.decoder_blocks = nn.ModuleList([
DecoderBlock(base_channels * 16, stride=8, n_res_units=n_res_units),
DecoderBlock(base_channels * 8, stride=8, n_res_units=n_res_units),
DecoderBlock(base_channels * 4, stride=2, n_res_units=n_res_units),
DecoderBlock(base_channels * 2, stride=2, n_res_units=n_res_units),
])
self.decoder_skips = nn.ModuleList([
SkipConnection(base_channels * 8),
SkipConnection(base_channels * 4),
SkipConnection(base_channels * 2),
SkipConnection(base_channels),
])
self.out_conv = nn.Sequential(
nn.Conv1d(base_channels, output_channels, kernel_size=(7,), padding=3, padding_mode='replicate')
)
def forward(self, input):
# skip connections
xs = []
x = self.input_conv(input)
for block in self.encoder_blocks:
xs.append(x)
x = block(x)
xs.append(x)
x = self.middle_layers(x)
x = x + self.middle_skip(xs.pop())
for block, skip in zip(self.decoder_blocks, self.decoder_skips):
x = block(x)
x = x + skip(xs.pop())
x = self.out_conv(x)
return x + self.input_skip(input)
def get_model(width=16, input_channels=1, n_res_units=3):
return AudioUNet(width, input_channels=input_channels, output_channels=1, n_res_units=n_res_units)
if __name__ == '__main__':
model = get_model()
print(model)
print(sum(p.numel() for p in model.parameters() if p.requires_grad))
a = torch.zeros(1, 1, 16384)
with torch.no_grad():
y = model(a)