ZBang commited on
Commit
6e2bd14
·
verified ·
1 Parent(s): e9b4c5e

Upload 2 files

Browse files
chkpt/USEF-SepFormer/whamr!/config.yaml ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ sample_rate: 8000
2
+
3
+ wsj0-2mix: data/test/wsj0-2mix
4
+ wham!: data/test/wham!
5
+ whamr!: data/test/whamr!
6
+
7
+ mix_scp: mix.scp
8
+ ref_scp: ref.scp
9
+ aux_scp: aux.scp
10
+
11
+ # Encoder parameters
12
+ N_encoder_out: 256
13
+ out_channels: 256
14
+ kernel_size: 16
15
+ kernel_stride: 8
16
+ embd: 256
17
+
18
+
19
+ # Specifying the network
20
+ Encoder: !new:models.model.Encoder
21
+ kernel_size: !ref <kernel_size>
22
+ out_channels: !ref <N_encoder_out>
23
+
24
+ Decoder: !new:models.model.Decoder
25
+ in_channels: !ref <N_encoder_out>
26
+ out_channels: 1
27
+ kernel_size: !ref <kernel_size>
28
+ stride: !ref <kernel_stride>
29
+ bias: False
30
+
31
+
32
+ Intra_enc_model: !new:models.local.TransformerEncoder.TransformerEncoder
33
+ num_layers: 8
34
+ d_model: !ref <out_channels>
35
+ nhead: 8
36
+ d_ffn: 1024
37
+ dropout: 0
38
+ #normalize_before: True
39
+
40
+ Inter_enc_model: !new:models.local.TransformerEncoder.TransformerEncoder
41
+ num_layers: 8
42
+ d_model: !ref <out_channels>
43
+ nhead: 8
44
+ d_ffn: 1024
45
+ dropout: 0
46
+ #normalize_before: True
47
+
48
+ Fusion_mdl: !new:models.local.TransformerEncoderCross.TransformerEncoderCross
49
+ num_layers: 4
50
+ d_model: !ref <out_channels>
51
+ nhead: 8
52
+ d_ffn: 1024
53
+ dropout: 0
54
+ #normalize_before: True
55
+
56
+
57
+ FiLM_front: !new:models.model_pre.FiLM
58
+ size: !ref <out_channels>
59
+
60
+
61
+ MaskNet: !new:chkpt.sef_sepformer_imp.whamr.model.Tar_Model
62
+ encoder: !ref <Encoder>
63
+ decoder: !ref <Decoder>
64
+ intra_enc: !ref <Intra_enc_model>
65
+ inter_enc: !ref <Inter_enc_model>
66
+ fusion_mdl: !ref <Fusion_mdl>
67
+ film_front: !ref <FiLM_front>
68
+ # film_end: !ref <FiLM_end>
69
+ in_channels: !ref <N_encoder_out>
70
+ out_channels: !ref <out_channels>
71
+ num_layers: 2
72
+ norm: ln
73
+ num_spks: 1
74
+ K: 250
75
+
76
+ modules:
77
+ masknet: !ref <MaskNet>
chkpt/USEF-SepFormer/whamr!/model.py ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ import copy
6
+
7
+ from models.local.PositionalEncoding import PositionalEncoding
8
+
9
+
10
+ EPS = 1e-8
11
+
12
+ def select_norm(norm, dim, shape, eps=1e-8):
13
+ """Just a wrapper to select the normalization type.
14
+ """
15
+
16
+ if norm == "gln":
17
+ return GlobalLayerNorm(dim, shape, elementwise_affine=True, eps=eps)
18
+ if norm == "cln":
19
+ return CumulativeLayerNorm(dim, elementwise_affine=True, eps=eps)
20
+ if norm == "ln":
21
+ return nn.GroupNorm(1, dim, eps=eps)
22
+ else:
23
+ return nn.BatchNorm1d(dim)
24
+
25
+ class FiLM(nn.Module):
26
+ def __init__(self, size = 256):
27
+ super(FiLM, self).__init__()
28
+ self.linear1 = nn.Linear(size,size)
29
+ self.linear2 = nn.Linear(size,size)
30
+
31
+ def forward(self,x,aux):
32
+ x = x * self.linear1(aux) + self.linear2(aux)
33
+ return x
34
+
35
+
36
+ class Encoder(nn.Module):
37
+ """Convolutional Encoder Layer.
38
+
39
+ Arguments
40
+ ---------
41
+ kernel_size : int
42
+ Length of filters.
43
+ in_channels : int
44
+ Number of input channels.
45
+ out_channels : int
46
+ Number of output channels.
47
+
48
+ Example
49
+ -------
50
+ >>> x = torch.randn(2, 1000)
51
+ >>> encoder = Encoder(kernel_size=4, out_channels=64)
52
+ >>> h = encoder(x)
53
+ >>> h.shape
54
+ torch.Size([2, 64, 499])
55
+ """
56
+
57
+ def __init__(self, kernel_size=2, out_channels=64, in_channels=1):
58
+ super(Encoder, self).__init__()
59
+ self.conv1d = nn.Conv1d(
60
+ in_channels=in_channels,
61
+ out_channels=out_channels,
62
+ kernel_size=kernel_size,
63
+ stride=kernel_size // 2,
64
+ groups=1,
65
+ bias=False,
66
+ )
67
+ self.in_channels = in_channels
68
+
69
+ def forward(self, x):
70
+ """Return the encoded output.
71
+
72
+ Arguments
73
+ ---------
74
+ x : torch.Tensor
75
+ Input tensor with dimensionality [B, L].
76
+ Return
77
+ ------
78
+ x : torch.Tensor
79
+ Encoded tensor with dimensionality [B, N, T_out].
80
+
81
+ where B = Batchsize
82
+ L = Number of timepoints
83
+ N = Number of filters
84
+ T_out = Number of timepoints at the output of the encoder
85
+ """
86
+ # B x L -> B x 1 x L
87
+ if self.in_channels == 1:
88
+ x = torch.unsqueeze(x, dim=1)
89
+ # B x 1 x L -> B x N x T_out
90
+ x = self.conv1d(x)
91
+ x = F.relu(x)
92
+
93
+ return x
94
+
95
+
96
+ class Decoder(nn.ConvTranspose1d):
97
+ """A decoder layer that consists of ConvTranspose1d.
98
+
99
+ Arguments
100
+ ---------
101
+ kernel_size : int
102
+ Length of filters.
103
+ in_channels : int
104
+ Number of input channels.
105
+ out_channels : int
106
+ Number of output channels.
107
+
108
+
109
+ Example
110
+ ---------
111
+ >>> x = torch.randn(2, 100, 1000)
112
+ >>> decoder = Decoder(kernel_size=4, in_channels=100, out_channels=1)
113
+ >>> h = decoder(x)
114
+ >>> h.shape
115
+ torch.Size([2, 1003])
116
+ """
117
+
118
+ def __init__(self, *args, **kwargs):
119
+ super(Decoder, self).__init__(*args, **kwargs)
120
+
121
+ def forward(self, x):
122
+ """Return the decoded output.
123
+
124
+ Arguments
125
+ ---------
126
+ x : torch.Tensor
127
+ Input tensor with dimensionality [B, N, L].
128
+ where, B = Batchsize,
129
+ N = number of filters
130
+ L = time points
131
+ """
132
+
133
+ if x.dim() not in [2, 3]:
134
+ raise RuntimeError(
135
+ "{} accept 3/4D tensor as input".format(self.__name__)
136
+ )
137
+ x = super().forward(x if x.dim() == 3 else torch.unsqueeze(x, 1))
138
+
139
+ if torch.squeeze(x).dim() == 1:
140
+ x = torch.squeeze(x, dim=1)
141
+ else:
142
+ x = torch.squeeze(x)
143
+ return x
144
+
145
+
146
+ class Interblock(nn.Module):
147
+ def __init__(
148
+ self,
149
+ d_model,
150
+ intra_enc,
151
+ inter_enc,
152
+ max_length = 20000,
153
+ ):
154
+ super(Interblock, self).__init__()
155
+
156
+ self.intra_mdl = intra_enc
157
+ self.inter_mdl = inter_enc
158
+
159
+ self.intra_linear = nn.Linear(
160
+ d_model, d_model
161
+ )
162
+ self.inter_linear = nn.Linear(
163
+ d_model, d_model
164
+ )
165
+
166
+ self.intra_norm = select_norm("ln", d_model, 4)
167
+ self.inter_norm = select_norm("ln", d_model, 4)
168
+
169
+ self.pos_enc = PositionalEncoding(d_model,max_length)
170
+
171
+
172
+ def forward(self, x):
173
+
174
+ B,N,K,S = x.shape
175
+
176
+ # intra_module
177
+ intra = x.permute(0, 3, 2, 1).contiguous().view(B * S, K, N)
178
+ intra = self.intra_mdl(intra + self.pos_enc(intra))[0]
179
+ intra = self.intra_linear(intra)
180
+ intra = intra.view(B, S, K, N)
181
+ intra = intra.permute(0, 3, 2, 1).contiguous()
182
+ intra = self.intra_norm(intra) + x
183
+
184
+ inter = intra.permute(0, 2, 3, 1).contiguous().view(B * K, S, N)
185
+ inter = self.inter_mdl(inter+self.pos_enc(inter))[0]
186
+ inter = self.inter_linear(inter)
187
+ inter = inter.view(B, K, S, N)
188
+ inter = inter.permute(0, 3, 1, 2).contiguous()
189
+ inter = self.inter_norm(inter)
190
+
191
+ out = inter + intra
192
+
193
+ return out
194
+
195
+
196
+
197
+ class Tar_Model(nn.Module):
198
+
199
+ def __init__(
200
+ self,
201
+ encoder,
202
+ decoder,
203
+ intra_enc,
204
+ inter_enc,
205
+ fusion_mdl,
206
+ film_front,
207
+ # film_end,
208
+ in_channels,
209
+ out_channels,
210
+ K,
211
+ num_layers=2,
212
+ norm="ln",
213
+ num_spks=1,
214
+ max_length=20000,
215
+ ):
216
+ super(Tar_Model, self).__init__()
217
+ self.num_spks = num_spks
218
+ self.num_layers = num_layers
219
+
220
+ # self.pre_train_mdl = pre_train_mdl
221
+ self.pos_enc = PositionalEncoding(out_channels,max_length)
222
+
223
+ self.norm_m = select_norm(norm, in_channels, 3)
224
+ self.norm_a = select_norm(norm, in_channels, 3)
225
+ self.conv1d1 = nn.Conv1d(in_channels, out_channels, 1, bias=False)
226
+ self.conv1d1_aux = nn.Conv1d(in_channels, out_channels, 1, bias=False)
227
+
228
+ self.K = K
229
+ self.encoder = encoder
230
+ self.encoder_aux = Encoder(16,256,1)
231
+ self.decoder = decoder
232
+
233
+
234
+ self.conv2d = nn.Conv2d(
235
+ out_channels, out_channels*num_spks, kernel_size=1
236
+ )
237
+ self.end_conv1x1 = nn.Conv1d(out_channels, out_channels, 1, bias=False)
238
+ self.prelu = nn.PReLU()
239
+ self.activation = nn.ReLU()
240
+ # gated output layer
241
+ self.output = nn.Sequential(
242
+ nn.Conv1d(out_channels, out_channels, 1), nn.Tanh()
243
+ )
244
+ self.output_gate = nn.Sequential(
245
+ nn.Conv1d(out_channels, out_channels, 1), nn.Sigmoid()
246
+ )
247
+
248
+ self.fusion_mdl = fusion_mdl
249
+
250
+ self.fusion_front_norm = select_norm("ln", out_channels, 3)
251
+
252
+ self.film_front = film_front
253
+
254
+
255
+ self.dual_mdl = nn.ModuleList([])
256
+ for i in range(num_layers):
257
+ self.dual_mdl.append(
258
+ copy.deepcopy(
259
+ Interblock(
260
+ out_channels,
261
+ intra_enc,
262
+ inter_enc,
263
+ )
264
+ )
265
+ )
266
+
267
+
268
+
269
+ def forward(self, input, aux):
270
+
271
+ # before each line we indicate the shape after executing the line
272
+
273
+ # [B, N, L]
274
+ # print(input.shape, aux.shape)
275
+ mix_w = self.encoder(input)
276
+ aux = self.encoder_aux(aux)
277
+
278
+ x = self.norm_m(mix_w)
279
+ aux = self.norm_a(aux)
280
+ #B,N,L = x.shape
281
+
282
+ # [B, N, L]
283
+ x = self.conv1d1(x)
284
+ aux = self.conv1d1_aux(aux)
285
+
286
+ x = x.permute(0,2,1).contiguous()
287
+ aux = aux.permute(0,2,1).contiguous()
288
+
289
+
290
+ aux = self.fusion_mdl(x, aux)[0]
291
+ x = self.film_front(x,aux)
292
+ x = self.fusion_front_norm(x.permute(0,2,1).contiguous())
293
+
294
+ x, gap_x = self._Segmentation(x, self.K)
295
+
296
+ for i in range(self.num_layers):
297
+
298
+ x = self.dual_mdl[i](x)
299
+
300
+ x = self.prelu(x)
301
+ x = self.conv2d(x)
302
+ B, _, K, S = x.shape
303
+ x = x.view(B * self.num_spks, -1, K, S)
304
+
305
+ x = self._over_add(x, gap_x)
306
+ x = self.output(x) * self.output_gate(x)
307
+ x = self.end_conv1x1(x)
308
+ _, N, L = x.shape
309
+
310
+ x = x.view(B, self.num_spks, N, L)
311
+ x = self.activation(x)
312
+
313
+ x = x.transpose(0, 1)
314
+
315
+ mix_w = torch.stack([mix_w] * self.num_spks)
316
+ x = mix_w * x
317
+
318
+ est_source = torch.cat(
319
+ [
320
+ self.decoder(x[i]).unsqueeze(-1)
321
+ for i in range(self.num_spks)
322
+ ],
323
+ dim=-1,
324
+ )
325
+
326
+ T_origin = input.size(1)
327
+ T_est = est_source.size(1)
328
+ if T_origin > T_est:
329
+ est_source = F.pad(est_source, (0, 0, 0, T_origin - T_est))
330
+ else:
331
+ est_source = est_source[:, :T_origin, :]
332
+
333
+
334
+ return est_source.squeeze(-1)
335
+
336
+ def _padding(self, input, K):
337
+ """Padding the audio times.
338
+
339
+ Arguments
340
+ ---------
341
+ K : int
342
+ Chunks of length.
343
+ P : int
344
+ Hop size.
345
+ input : torch.Tensor
346
+ Tensor of size [B, N, L].
347
+ where, B = Batchsize,
348
+ N = number of filters
349
+ L = time points
350
+ """
351
+ B, N, L = input.shape
352
+ P = K // 2
353
+ gap = K - (P + L % K) % K
354
+ if gap > 0:
355
+ pad = torch.Tensor(torch.zeros(B, N, gap)).type(input.type())
356
+ input = torch.cat([input, pad], dim=2)
357
+
358
+ _pad = torch.Tensor(torch.zeros(B, N, P)).type(input.type())
359
+ input = torch.cat([_pad, input, _pad], dim=2)
360
+
361
+ return input, gap
362
+
363
+ def _Segmentation(self, input, K):
364
+ """The segmentation stage splits
365
+
366
+ Arguments
367
+ ---------
368
+ K : int
369
+ Length of the chunks.
370
+ input : torch.Tensor
371
+ Tensor with dim [B, N, L].
372
+
373
+ Return
374
+ -------
375
+ output : torch.tensor
376
+ Tensor with dim [B, N, K, S].
377
+ where, B = Batchsize,
378
+ N = number of filters
379
+ K = time points in each chunk
380
+ S = the number of chunks
381
+ L = the number of time points
382
+ """
383
+ B, N, L = input.shape
384
+ P = K // 2
385
+ input, gap = self._padding(input, K)
386
+ # [B, N, K, S]
387
+ input1 = input[:, :, :-P].contiguous().view(B, N, -1, K)
388
+ input2 = input[:, :, P:].contiguous().view(B, N, -1, K)
389
+ input = (
390
+ torch.cat([input1, input2], dim=3).view(B, N, -1, K).transpose(2, 3)
391
+ )
392
+
393
+ return input.contiguous(), gap
394
+
395
+ def _over_add(self, input, gap):
396
+ """Merge the sequence with the overlap-and-add method.
397
+
398
+ Arguments
399
+ ---------
400
+ input : torch.tensor
401
+ Tensor with dim [B, N, K, S].
402
+ gap : int
403
+ Padding length.
404
+
405
+ Return
406
+ -------
407
+ output : torch.tensor
408
+ Tensor with dim [B, N, L].
409
+ where, B = Batchsize,
410
+ N = number of filters
411
+ K = time points in each chunk
412
+ S = the number of chunks
413
+ L = the number of time points
414
+
415
+ """
416
+ B, N, K, S = input.shape
417
+ P = K // 2
418
+ # [B, N, S, K]
419
+ input = input.transpose(2, 3).contiguous().view(B, N, -1, K * 2)
420
+
421
+ input1 = input[:, :, :, :K].contiguous().view(B, N, -1)[:, :, P:]
422
+ input2 = input[:, :, :, K:].contiguous().view(B, N, -1)[:, :, :-P]
423
+ input = input1 + input2
424
+ # [B, N, L]
425
+ if gap > 0:
426
+ input = input[:, :, :-gap]
427
+
428
+ return input