-
Notifications
You must be signed in to change notification settings - Fork 1
/
mup_audio_unet.py
241 lines (169 loc) · 7.63 KB
/
mup_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
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
import torch
import torch.nn as nn
import mup
# 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'),
nn.ELU(inplace=True),
nn.Conv1d(num_channels, num_channels, kernel_size=(1,)),
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
self.skip = nn.Conv1d(num_channels, output_channels, kernel_size=(1,))
def forward(self, x):
return self.skip(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'))
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 = [
nn.ConvTranspose1d(channels, channels // 2, kernel_size=(2 * stride,), stride=(stride,), padding=stride),
nn.ReplicationPad1d(stride // 2), 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 MupAudioUNet(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'),
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.ELU(inplace=True),
nn.Conv1d(base_channels * 16, base_channels * 16, kernel_size=(7,), padding=3, padding_mode='replicate'),
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(
mup.MuOutConv1d(in_channels=base_channels, out_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)
# testing purposes, mup coord checks, less layers on the chart
class VeryShallowMupAudioUNet(nn.Module):
def __init__(self, base_channels=2, input_channels=1, output_channels=1, n_res_units=1, ismup=True):
super().__init__()
self.input_conv = nn.Sequential(
nn.Conv1d(input_channels, base_channels, kernel_size=(7,), padding=3, padding_mode='replicate'),
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),
])
self.middle_layers = nn.Sequential(
nn.Conv1d(base_channels * 2, base_channels * 2, kernel_size=(7,), padding=3, padding_mode='replicate'),
# nn.GroupNorm(16, base_channels * 16),
nn.ELU(inplace=True),
nn.Conv1d(base_channels * 2, base_channels * 2, kernel_size=(7,), padding=3, padding_mode='replicate'),
nn.ELU(inplace=True),
)
self.middle_skip = SkipConnection(base_channels * 2)
self.decoder_blocks = nn.ModuleList([
DecoderBlock(base_channels * 2, stride=2, n_res_units=n_res_units),
])
self.decoder_skips = nn.ModuleList([
SkipConnection(base_channels),
])
if ismup:
self.out_conv = nn.Sequential(
mup.MuReadout(in_features=base_channels, out_features=output_channels),
)
else:
self.out_conv = nn.Sequential(
nn.Linear(in_features=base_channels, out_features=output_channels),
)
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())
# shape of x is (batch_size, channels, time)
# out is linear layer so needs input shape of (batch_size, time, channels)
# return to (batch_size, channels, time) after out_conv
x = self.out_conv(x.transpose(1, 2)).transpose(1, 2)
return x + self.input_skip(input)
def remove_all_wn(self):
for m in self.modules():
if ('weight_g' in m._parameters) or ('weight_v' in m._parameters):
torch.nn.utils.remove_weight_norm(m)
def get_model(width=16, input_channels=1, n_res_units=3):
return MupAudioUNet(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)