-
Notifications
You must be signed in to change notification settings - Fork 2
/
Encoders.py
440 lines (400 loc) · 13.3 KB
/
Encoders.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import torch
import torch.nn as nn
import torch.nn.functional as F
from Layers import *
from math import ceil
from Base import BaseModel
class ToyConvEncoder(BaseModel):
def __init__(self,
in_size=(None, 1, 16000),
nfft=int(25/1000 * 16000),
hop=int(10/1000 * 16000),
hidden_units=128,
n_res_blocks=1,
n_down=1,
latent_dim=64,
transform = 'conv1d',
double_channels = False,
clamp=True,
vae=False,
verbose=False,
**kwargs):
super(ToyConvEncoder, self).__init__()
self.in_size = in_size
self.nfft = nfft
if hop is None:
self.hop = self.nfft // 4
else:
self.hop = hop
self.hidden_units = hidden_units
self.n_res_blocks = n_res_blocks
self.n_down = n_down
self.latent_dim = latent_dim
self.clamp = clamp
self.vae = vae
self.verbose = verbose
self.transform = transform
self.double_channels = double_channels
if self.transform == 'conv1d':
_out_channels = self.nfft // 2 + 1
if self.double_channels: _out_channels *= 2
self.rep = nn.Conv1d(in_channels=1,
out_channels=_out_channels,
kernel_size=self.nfft,
stride=self.hop,
padding=self.nfft // 2)
if self.clamp:
self.rep_relu = ClampedReLU().apply
else:
self.rep_relu = nn.ReLU()
elif self.transform == 'stft':
self.post_process = lambda x: torch.cat(list(x), dim=1)
_out_channels = 2 * (self.nfft // 2 + 1)
self.coords = kwargs['stft_coords']
if self.coords == 'polar':
self.phase_unaware = kwargs['phase_unaware']
if self.phase_unaware:
_out_channels = self.nfft // 2 + 1
self.post_process = lambda x: list(x)
self.rep = STFT(kernel_size=self.nfft,
stride=self.hop,
coords=self.coords)
self._rep_size = (_out_channels, self._conv1d_out_size(in_size=self.in_size[-1],
k=self.nfft,
s=self.hop,
p=self.nfft // 2))
self._rep_padding = (ceil(self._rep_size[-1] / 2**n_down) * 2**n_down) - self._rep_size[-1]
self.rep_padding = Padding1D(pad=self._rep_padding)
self.conv1 = nn.Conv1d(in_channels=_out_channels,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn1 = nn.BatchNorm1d(self.hidden_units)
self.down_blocks = nn.ModuleList([self._build_down_blocks(self.n_res_blocks,
self.hidden_units) for _ in range(self.n_down)])
if self.vae:
_conv_linear_channels = self.latent_dim * 2
else:
_conv_linear_channels = self.latent_dim
self.conv_linear = nn.Conv1d(in_channels=self.hidden_units,
out_channels=_conv_linear_channels,
kernel_size=1)
self._latent_size = self._get_latent_size()
def forward(self, x):
self._verbose(self.verbose, 'Input:', x.size())
if self.transform == 'conv1d':
rep = self.rep_relu(self.rep(x))
elif self.transform == 'stft':
rep = self.post_process(self.rep(x))
if self.coords == 'polar' and self.phase_unaware:
phase, rep = rep
else:
phase = None
self._verbose(self.verbose, 'Representation:', rep.size())
padded_rep = self.rep_padding(rep)
self._verbose(self.verbose, 'Padded Representation:', padded_rep.size())
x = F.leaky_relu(self.bn1(self.conv1(padded_rep)))
self._verbose(self.verbose, 'First Conv:', x.size())
for db in self.down_blocks:
x = db(x)
self._verbose(self.verbose, 'Down Block:', x.size())
x = self.conv_linear(x)
self._verbose(self.verbose, 'Latent x:', x.size(), '\n')
if self.transform == 'stft' and phase is not None:
return phase, x
else:
return x
@staticmethod
def _conv1d_out_size(in_size, k, s, p):
return (in_size - (k-1) + 2*p) // s + 1
def _get_latent_size(self):
v = self.verbose
self.verbose = False
shape = (1, *self.in_size[-2:])
x = torch.randn(shape)
x = self.forward(x)
if type(x) == tuple:
x = x[-1]
x = x.squeeze(dim=0)
self.verbose = v
return x.size()
@staticmethod
def _build_down_blocks(n_res, hidden_units, batch_norm=True):
pre_res_blocks = [ResidualBlock1d(hidden_units, hidden_units) for _ in range(n_res)]
post_res_blocks = [ResidualBlock1d(hidden_units, hidden_units) for _ in range(n_res)]
layers = [*pre_res_blocks,
nn.Conv1d(in_channels=hidden_units,
out_channels=hidden_units,
kernel_size=4,
stride=2,
padding=1),
nn.LeakyReLU(inplace=True),
*post_res_blocks]
if batch_norm:
layers.insert(n_res+1, nn.BatchNorm1d(hidden_units))
down_block = nn.Sequential(*layers)
return down_block
class ConvEncoder(BaseModel):
def __init__(self,
in_size=(None, 1, 16000),
nfft=int(25 / 1000 * 16000),
hop=int(10 / 1000 * 16000),
hidden_units=768,
latent_dim=64,
res_blocks=4,
transform='conv1d',
clamp=True,
double_channels=False,
vae=False,
verbose=False,
**kwargs):
super(ConvEncoder, self).__init__()
self.in_size = in_size
self.nfft = nfft
if hop is None:
self.hop = nfft // 4
else:
self.hop = hop
self.hidden_units = hidden_units
self.latent_dim = latent_dim
self.res_blocks = res_blocks
self.vae = vae
self.clamp = clamp
self.verbose = verbose
self.transform = transform
self.double_channels = double_channels
if self.transform == 'conv1d':
_out_channels = self.nfft // 2 + 1
if self.double_channels: _out_channels *= 2
self.rep = nn.Conv1d(in_channels=1,
out_channels=_out_channels,
kernel_size=self.nfft,
stride=self.hop,
padding=self.nfft // 2)
if self.clamp:
self.rep_relu = ClampedReLU().apply
else:
self.rep_relu = nn.ReLU()
elif self.transform == 'stft':
self.post_process = lambda x: torch.cat(list(x), dim=1)
_out_channels = 2 * (self.nfft // 2 + 1)
self.coords = kwargs['stft_coords']
if self.coords == 'polar':
self.phase_unaware = kwargs['phase_unaware']
if self.phase_unaware:
_out_channels = self.nfft // 2 + 1
self.post_process = lambda x: list(x)
self.rep = STFT(kernel_size=self.nfft,
stride=self.hop,
coords=self.coords)
self._rep_size = (_out_channels, self._conv1d_out_size(in_size=self.in_size[-1],
k=self.nfft,
s=self.hop,
p=self.nfft // 2))
self.padding = Padding1D(pad=self._rep_size[-1] % 2)
self.conv1 = nn.Conv1d(in_channels=_out_channels,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn1 = nn.BatchNorm1d(self.hidden_units)
self.conv2 = nn.Conv1d(in_channels=self.hidden_units,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn2 = nn.BatchNorm1d(self.hidden_units)
self.conv3 = nn.Conv1d(in_channels=self.hidden_units,
out_channels=self.hidden_units,
kernel_size=4,
stride=2,
padding=1)
self.bn3 = nn.BatchNorm1d(self.hidden_units)
self.conv4 = nn.Conv1d(in_channels=self.hidden_units,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn4 = nn.BatchNorm1d(self.hidden_units)
self.conv5 = nn.Conv1d(in_channels=self.hidden_units,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn5 = nn.BatchNorm1d(self.hidden_units)
self.blocks = nn.ModuleList([ResidualBlock1d(self.hidden_units,
self.hidden_units)] * self.res_blocks)
if self.vae:
_conv_linear_channels = self.latent_dim * 2
else:
_conv_linear_channels = self.latent_dim
self.conv_final = nn.Conv1d(self.hidden_units, _conv_linear_channels, 1)
self._latent_size = self._get_latent_size()
def forward(self, x):
self._verbose(self.verbose, 'Input:', x.size())
if self.transform == 'conv1d':
x_rep = self.rep_relu(self.rep(x))
elif self.transform == 'stft':
x_rep = self.post_process(self.rep(x))
if self.coords == 'polar' and self.phase_unaware:
phase, x_rep = x_rep
else:
phase = None
self._verbose(self.verbose, 'Representation:', x_rep.size())
x_rep = self.padding(x_rep)
self._verbose(self.verbose, 'Padded Representation:', x_rep.size())
x_conv1 = F.leaky_relu(self.bn1(self.conv1(x_rep)))
self._verbose(self.verbose, 'Conv1:', x_conv1.size())
x_conv2 = F.leaky_relu(self.bn2(self.conv2(x_conv1)))
x = x_conv2 + x_conv1
self._verbose(self.verbose, 'X:', x.size())
x_conv3 = F.leaky_relu(self.bn3(self.conv3(x)))
self._verbose(self.verbose, 'x_conv3:', x_conv3.size())
x_conv4 = F.leaky_relu(self.bn4(self.conv4(x_conv3)))
x = x_conv4 + x_conv3
self._verbose(self.verbose, 'X:', x.size())
x_conv5 = F.leaky_relu(self.bn5(self.conv5(x)))
x = x_conv5 + x
self._verbose(self.verbose, 'X:', x.size())
for b in self.blocks:
x = b(x)
self._verbose(self.verbose, 'X:', x.size())
x = self.conv_final(x)
self._verbose(self.verbose, 'Latent:', x.size())
if self.transform == 'stft' and phase is not None:
return phase, x
else:
return x
@staticmethod
def _conv1d_out_size(in_size, k, s, p):
return (in_size - (k-1) + 2*p) // s + 1
def _get_latent_size(self):
v = self.verbose
self.verbose = False
shape = (1, *self.in_size[-2:])
x = torch.randn(shape)
x = self.forward(x)
if type(x) == tuple:
x = x[-1]
x = x.squeeze(dim=0)
self.verbose = v
return x.size()
class ToyConvEncoderV0(BaseModel):
def __init__(self,
in_size=(None, 1, 24414),
nfft=int(25/1000 * 24414),
hop=int(10/1000 * 24414),
hidden_units=128,
n_res_blocks=1,
n_down=1,
latent_dim=64,
transform='STFT',
clamp=True,
vae=True,
verbose=False,
**kwargs):
super(ToyConvEncoderV0, self).__init__()
self.in_size = in_size
self.nfft = nfft
if hop is None:
self.hop = self.nfft // 4
else:
self.hop = hop
self.hidden_units = hidden_units
self.n_res_blocks = n_res_blocks
self.n_down = n_down
self.latent_dim = latent_dim
self.clamp = clamp
self.vae = vae
self.verbose = verbose
self.transform = transform
if self.transform == 'Conv1d':
self.rep = nn.Conv1d(in_channels=1,
out_channels=self.nfft // 2 + 1,
kernel_size=self.nfft,
stride=self.hop,
padding=self.nfft // 2)
if self.clamp:
self.rep_relu = ClampedReLU().apply
else:
self.rep_relu = nn.ReLU()
self._rep_size = (nfft // 2 + 1, self._conv1d_out_size(in_size=self.in_size[-1],
k=self.nfft,
s=self.hop,
p=self.nfft // 2))
self._rep_padding = (ceil(self._rep_size[-1] / 2**n_down) * 2**n_down) - self._rep_size[-1]
self.rep_padding = Padding1D(pad=self._rep_padding)
conv1_channels = (nfft // 2 + 1)
elif self.transform == 'STFT':
self.rep = STFT(kernel_size=self.nfft,
stride=self.hop, coords='cartesian')
self._rep_size = (2*(nfft // 2 + 1), self._conv1d_out_size(in_size=self.in_size[-1],
k=self.nfft,
s=self.hop,
p=self.nfft // 2))
self._rep_padding = (ceil(self._rep_size[-1] / 2**n_down) * 2**n_down) - self._rep_size[-1]
self.rep_padding = Padding1D(pad=self._rep_padding)
conv1_channels = 2*(nfft // 2 + 1)
self.conv1 = nn.Conv1d(in_channels=conv1_channels,
out_channels=self.hidden_units,
kernel_size=3,
stride=1,
padding=1)
self.bn1 = nn.BatchNorm1d(self.hidden_units)
self.down_blocks = nn.ModuleList([self._build_down_blocks(self.n_res_blocks,
self.hidden_units) for _ in range(self.n_down)])
if self.vae:
_conv_linear_channels = self.latent_dim * 2
else:
_conv_linear_channels = self.latent_dim
self.conv_linear = nn.Conv1d(in_channels=self.hidden_units,
out_channels=_conv_linear_channels,
kernel_size=1)
self._latent_size = self._get_latent_size()
def forward(self, x):
self._verbose(self.verbose, 'Input:', x.size())
if self.transform == 'Conv1d':
rep = self.rep_relu(self.rep(x))
elif self.transform == 'STFT':
rep_re, rep_im = self.rep(x)
rep = torch.cat([rep_re, rep_im], dim=1)
self._verbose(self.verbose, 'Representation:', rep.size())
padded_rep = self.rep_padding(rep)
self._verbose(self.verbose, 'Padded Representation:', padded_rep.size())
x = F.leaky_relu(self.bn1(self.conv1(padded_rep)))
self._verbose(self.verbose, 'First Conv:', x.size())
for db in self.down_blocks:
x = db(x)
self._verbose(self.verbose, 'Down Block:', x.size())
x = self.conv_linear(x)
self._verbose(self.verbose, 'Latent x:', x.size(), '\n')
return x
@staticmethod
def _conv1d_out_size(in_size, k, s, p):
return (in_size - (k-1) + 2*p) // s + 1
def _get_latent_size(self):
shape = (1, *self.in_size[-2:])
x = torch.randn(shape)
x = self.forward(x)
if self.transform == 'Conv1d':
x = x.squeeze(dim=0)
elif self.transform == 'STFT':
x = x[-1].squeeze(dim=0)
return x.size()
@staticmethod
def _build_down_blocks(n_res, hidden_units, batch_norm=True):
pre_res_blocks = [ResidualBlock1d(hidden_units, hidden_units) for _ in range(n_res)]
post_res_blocks = [ResidualBlock1d(hidden_units, hidden_units) for _ in range(n_res)]
layers = [*pre_res_blocks,
nn.Conv1d(in_channels=hidden_units,
out_channels=hidden_units,
kernel_size=4,
stride=2,
padding=1),
nn.LeakyReLU(inplace=True),
*post_res_blocks]
if batch_norm:
layers.insert(n_res+1, nn.BatchNorm1d(hidden_units))
down_block = nn.Sequential(*layers)
return down_block