-
Notifications
You must be signed in to change notification settings - Fork 9
/
LiMuSE.py
511 lines (422 loc) · 20.6 KB
/
LiMuSE.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#coding:utf-8
import torch
import torch.nn as nn
# from torch.nn.common_types import T
import torch.nn.functional as F
from utils import *
from min_max_quantization import *
class GlobalLayerNorm(nn.Module):
def __init__(self, dim, shape, eps=1e-8, elementwise_affine=True):
super(GlobalLayerNorm, self).__init__()
self.dim = dim
self.eps = eps
self.elementwise_affine = elementwise_affine
if self.elementwise_affine:
if shape == 3:
self.weight = nn.Parameter(torch.ones(self.dim, 1))
self.bias = nn.Parameter(torch.zeros(self.dim, 1))
if shape == 4:
self.weight = nn.Parameter(torch.ones(self.dim, 1, 1))
self.bias = nn.Parameter(torch.zeros(self.dim, 1, 1))
else:
self.register_parameter('weight', None)
self.register_parameter('bias', None)
def forward(self, x):
if x.dim() == 4:
mean = torch.mean(x, (1, 2, 3), keepdim=True)
var = torch.mean((x-mean)**2, (1, 2, 3), keepdim=True)
if self.elementwise_affine:
x = self.weight*(x-mean)/torch.sqrt(var+self.eps)+self.bias
else:
x = (x-mean)/torch.sqrt(var+self.eps)
if x.dim() == 3:
mean = torch.mean(x, (1, 2), keepdim=True)
var = torch.mean((x-mean)**2, (1, 2), keepdim=True)
if self.elementwise_affine:
x = self.weight*(x-mean)/torch.sqrt(var+self.eps)+self.bias
else:
x = (x-mean)/torch.sqrt(var+self.eps)
return x
class CumulativeLayerNorm(nn.LayerNorm):
def __init__(self, dim, elementwise_affine=True):
super(CumulativeLayerNorm, self).__init__(
dim, elementwise_affine=elementwise_affine)
def forward(self, x):
x = torch.transpose(x, 1, 2)
x = super().forward(x)
x = torch.transpose(x, 1, 2)
return x
def select_norm(norm, dim, shape):
if norm == 'gln':
return GlobalLayerNorm(dim, shape, elementwise_affine=True)
if norm == 'cln':
return CumulativeLayerNorm(dim, elementwise_affine=True)
if norm == 'ln':
return nn.GroupNorm(1, dim, eps=1e-8)
else:
return nn.BatchNorm2d(dim)
class Conv1D_Q(nn.Module):
def __init__(self, input_channel, hidden_channel, kernel, QA_flag=False, ak=8):
super(Conv1D_Q, self).__init__()
self.QA_flag = QA_flag
self.ak = ak
self.conv1d = nn.Conv1d(input_channel, hidden_channel, kernel)
def forward(self, x):
if x.dim() not in [2, 3]:
raise RuntimeError("{} accept 2/3D tensor as input".format(
self.__name__))
if self.QA_flag:
x = min_max_quantize(x, self.ak)
output = self.conv1d(x)
return output
class Conv1D(nn.Conv1d):
def __init__(self, *args, **kwargs):
super(Conv1D, self).__init__(*args, **kwargs)
def forward(self, x, squeeze=False):
if x.dim() not in [2, 3]:
raise RuntimeError("{} accept 2/3D tensor as input".format(
self.__name__))
x = super().forward(x if x.dim() == 3 else torch.unsqueeze(x, 1))
if squeeze:
x = torch.squeeze(x, dim=1)
return x
class ConvTrans1D(nn.ConvTranspose1d):
def __init__(self, *args, **kwargs):
super(ConvTrans1D, self).__init__(*args, **kwargs)
def forward(self, x, squeeze=False):
if x.dim() not in [2, 3]:
raise RuntimeError("{} accept 2/3D tensor as input".format(
self.__name__))
x = super().forward(x if x.dim() == 3 else torch.unsqueeze(x, 1))
if squeeze:
x = torch.squeeze(x, dim=1)
return x
class DepthConv1d(nn.Module):
def __init__(self, input_channel, hidden_channel, kernel, padding, dilation=1, skip=False, causal=False):
super(DepthConv1d, self).__init__()
self.causal = causal
self.skip = skip
self.conv1d = nn.Conv1d(input_channel, hidden_channel, 1)
if self.causal:
self.padding = (kernel - 1) * dilation
else:
self.padding = padding
self.dconv1d = nn.Conv1d(hidden_channel, hidden_channel, kernel, dilation=dilation,
groups=hidden_channel,
padding=self.padding)
self.res_out = nn.Conv1d(hidden_channel, input_channel, 1)
self.nonlinearity1 = nn.PReLU()
self.nonlinearity2 = nn.PReLU()
if self.causal:
self.reg1 = select_norm(norm='cln', dim=hidden_channel, shape=3)
self.reg2 = select_norm(norm='cln', dim=hidden_channel, shape=3)
else:
self.reg1 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
self.reg2 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
if self.skip:
self.skip_out = nn.Conv1d(hidden_channel, input_channel, 1)
def forward(self, input):
output = self.reg1(self.nonlinearity1(self.conv1d(input)))
if self.causal:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)[:,:,:-self.padding]))
else:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)))
residual = self.res_out(output)
if self.skip:
skip = self.skip_out(output)
return residual, skip
else:
return residual
class DepthConv1d_Q(nn.Module):
def __init__(self, input_channel, hidden_channel, kernel, padding, dilation=1, skip=False, causal=False, QA_flag=False, ak=8):
super(DepthConv1d_Q, self).__init__()
self.causal = causal
self.skip = skip
self.conv1d = nn.Conv1d(input_channel, hidden_channel, 1)
if self.causal:
self.padding = (kernel - 1) * dilation
else:
self.padding = padding
self.dconv1d = nn.Conv1d(hidden_channel, hidden_channel, kernel, dilation=dilation,
groups=hidden_channel,
padding=self.padding)
self.res_out = nn.Conv1d(hidden_channel, input_channel, 1)
self.nonlinearity1 = nn.PReLU()
self.nonlinearity2 = nn.PReLU()
if self.causal:
self.reg1 = select_norm(norm='cln', dim=hidden_channel, shape=3)
self.reg2 = select_norm(norm='cln', dim=hidden_channel, shape=3)
else:
self.reg1 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
self.reg2 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
if self.skip:
self.skip_out = nn.Conv1d(hidden_channel, input_channel, 1)
self.QA_flag = QA_flag
self.ak = ak
def forward(self, input):
if self.QA_flag:
input = min_max_quantize(input, self.ak)
output = self.reg1(self.nonlinearity1(self.conv1d(input)))
if self.QA_flag:
output = min_max_quantize(output, self.ak)
if self.causal:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)[:,:,:-self.padding]))
else:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)))
if self.QA_flag:
output = min_max_quantize(output, self.ak)
residual = self.res_out(output)
if self.skip:
skip = self.skip_out(output)
return residual, skip
else:
return residual
# GC-equipped TCN
class GC_TCN(nn.Module):
def __init__(self, input_dim, hidden_dim,
layer, stack, kernel=3, skip=False,
causal=False, dilated=True, num_group=2):
super(GC_TCN, self).__init__()
self.receptive_field = 0
self.dilated = dilated
self.num_group = num_group
self.TAC = nn.ModuleList([])
self.TCN = nn.ModuleList([])
for s in range(stack):
for i in range(layer):
if self.dilated:
self.TCN.append(DepthConv1d(input_dim//num_group, hidden_dim//num_group, kernel, dilation=2**i, padding=2**i, skip=skip, causal=causal))
else:
self.TCN.append(DepthConv1d(input_dim//num_group, hidden_dim//num_group, kernel, dilation=1, padding=1, skip=skip, causal=causal))
self.TAC.append(TAC(input_dim//num_group, hidden_dim*3//num_group))
if i == 0 and s == 0:
self.receptive_field += kernel
else:
if self.dilated:
self.receptive_field += (kernel - 1) * 2**i
else:
self.receptive_field += (kernel - 1)
#print("Receptive field: {:3d} frames.".format(self.receptive_field))
# output layer
self.skip = skip
def forward(self, input):
batch_size, N, L = input.shape
output = input.view(batch_size, self.num_group, -1, L)
if self.skip:
skip_connection = 0.
for i in range(len(self.TCN)):
output = self.TAC[i](output)
output = output.view(batch_size*self.num_group, -1, L)
residual, skip = self.TCN[i](output)
output = (output + residual).view(batch_size, self.num_group, -1, L)
skip_connection = skip_connection + skip
else:
for i in range(len(self.TCN)):
output = self.TAC[i](output)
output = output.view(batch_size*self.num_group, -1, L)
residual = self.TCN[i](output)
output = (output + residual).view(batch_size, self.num_group, -1, L)
output = output.view(batch_size, -1, L)
return output
class GC_TCN_Q(nn.Module):
def __init__(self, input_dim, hidden_dim,
layer, stack, kernel=3, skip=False,
causal=False, dilated=True, num_group=2, QA_flag=False, ak=8):
super(GC_TCN_Q, self).__init__()
self.receptive_field = 0
self.dilated = dilated
self.num_group = num_group
self.skip = skip
self.QA_flag = QA_flag
self.ak = ak
self.TAC = nn.ModuleList([])
self.TCN = nn.ModuleList([])
for s in range(stack):
for i in range(layer):
if self.dilated:
self.TCN.append(DepthConv1d_Q(input_dim//num_group, hidden_dim//num_group, kernel, dilation=2**i, padding=2**i, skip=skip, causal=causal, QA_flag=QA_flag, ak=ak))
else:
self.TCN.append(DepthConv1d_Q(input_dim//num_group, hidden_dim//num_group, kernel, dilation=1, padding=1, skip=skip, causal=causal, QA_flag=QA_flag, ak=ak))
self.TAC.append(TAC_Q(input_dim//num_group, hidden_dim*3//num_group, QA_flag=QA_flag, ak=ak))
if i == 0 and s == 0:
self.receptive_field += kernel
else:
if self.dilated:
self.receptive_field += (kernel - 1) * 2**i
else:
self.receptive_field += (kernel - 1)
#print("Receptive field: {:3d} frames.".format(self.receptive_field))
def forward(self, input):
batch_size, N, L = input.shape # B, context*L, N
output = input.view(batch_size, self.num_group, -1, L) # B, context, L, N
if self.skip:
skip_connection = 0.
for i in range(len(self.TCN)):
output = self.TAC[i](output)
output = output.view(batch_size*self.num_group, -1, L)
residual, skip = self.TCN[i](output)
output = (output + residual).view(batch_size, self.num_group, -1, L)
skip_connection = skip_connection + skip
else:
for i in range(len(self.TCN)):
output = self.TAC[i](output)
output = output.view(batch_size*self.num_group, -1, L)
residual = self.TCN[i](output)
output = (output + residual).view(batch_size, self.num_group, -1, L)
output = output.view(batch_size, -1, L) # B, N, L
return output
class TCN_Q(nn.Module):
def __init__(self, input_dim, hidden_dim, layer, stack,
kernel=3, skip=True, causal=False, dilated=True, QA_flag=False, ak=8):
super(TCN_Q, self).__init__()
if not causal:
self.LN = nn.GroupNorm(1, input_dim, eps=1e-8)
else:
self.LN = select_norm(norm='cln', dim=input_dim, shape=3)
# TCN for feature extraction
self.receptive_field = 0
self.dilated = dilated
self.skip = skip
self.TCN = nn.ModuleList([])
for s in range(stack):
for i in range(layer):
if self.dilated:
self.TCN.append(DepthConv1d_Q(input_dim, hidden_dim, kernel, dilation=2**i, padding=2**i, skip=skip, causal=causal, QA_flag=QA_flag, ak=ak))
else:
self.TCN.append(DepthConv1d_Q(input_dim, hidden_dim, kernel, dilation=1, padding=1, skip=skip, causal=causal, QA_flag=QA_flag, ak=ak))
if i == 0 and s == 0:
self.receptive_field += kernel
else:
if self.dilated:
self.receptive_field += (kernel - 1) * 2**i
else:
self.receptive_field += (kernel - 1)
#print("Receptive field: {:3d} frames.".format(self.receptive_field))
self.QA_flag = QA_flag
self.ak = ak
def forward(self, input):
if self.skip:
skip_connection = 0.
for i in range(len(self.TCN)):
residual, skip = self.TCN[i](input)
input = input + residual
skip_connection = skip_connection + skip
else:
for i in range(len(self.TCN)):
residual = self.TCN[i](input)
output = input + residual
return output
class LiMuSE(nn.Module):
def __init__(self,
N=128,
hidden_dim=256,
K=32,
E=50,
layer=24,
num_spks=2,
context_size=32,
group_size=16,
activate="relu",
causal=False,
QA_flag=False,
ak=8):
super(LiMuSE, self).__init__()
self.E = E
self.N = N
self.hidden_dim = hidden_dim
self.group_size= group_size
self.num_spks = num_spks
self.context_size = context_size
self.layer = layer
self.encoder = Conv1D(2, N, K, stride=K // 2, padding=0)
self.voiceprint_encoder = nn.Conv1d(in_channels=512, out_channels=N, kernel_size=1, stride=1, padding=0)
self.visual_encoder = nn.Linear(256, N)
self.Normal_S = select_norm('gln', N, 3)
# context encoder/decoder
self.context_enc_1 = GC_TCN_Q(self.N, self.hidden_dim, layer=2, stack=1, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
self.context_dec_1 = GC_TCN_Q(self.N, self.hidden_dim, layer=2, stack=1, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
self.context_enc_2 = GC_TCN_Q(3*self.N, 3*self.hidden_dim, layer=2, stack=1, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
self.context_dec_2 = GC_TCN_Q(3*self.N, 3*self.hidden_dim, layer=2, stack=1, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
# Separation block
self.audio_block = GC_TCN_Q(self.N, self.N*4, layer=6, stack=2, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
self.fusion_block = GC_TCN_Q(3*self.N, self.N*12, layer=6, stack=1, kernel=3, skip=False, causal=causal, num_group=self.group_size, QA_flag=QA_flag, ak=ak)
self.gen_masks = Conv1D_Q(3*N, N, 1, QA_flag=QA_flag, ak=ak)
self.decoder = ConvTrans1D(N, 1, K, stride=K//2)
# activation function
active_f = {
'relu': nn.ReLU(),
'sigmoid': nn.Sigmoid(),
'softmax': nn.Softmax(dim=0)
}
self.activation_type = activate
self.activation = active_f[activate]
def forward(self, mix, aux, visual):
enc_out = self.encoder(mix) # B x N x T
batch_size, num_channel, T = enc_out.shape
aux = aux.transpose(1,2)
aux = self.voiceprint_encoder(aux) # B x N x 1
aux = aux.repeat(1,1,T) # B x N x T
visual = self.visual_encoder(visual) # B x max_video_len x T
visual = F.interpolate(visual.transpose(1,2), T, mode='linear', align_corners=False) # B x N x T
audio = self.Normal_S(enc_out) # B, N, T
aux = self.Normal_S(aux)
visual = self.Normal_S(visual)
########### Part 1 ###########
# context encoding
squeeze_block, squeeze_rest = split_feature(audio, self.context_size) # B, N, context, L torch.Size([B, 128, 32, 376])
squeeze_frame = squeeze_block.shape[-1] # L
squeeze_input = squeeze_block.permute(0,3,1,2).contiguous().view(batch_size*squeeze_frame, self.N,
self.context_size) # B*L, N, context
squeeze_output = self.context_enc_1(squeeze_input) # B*L, N, context
squeeze_mean = squeeze_output.mean(2).view(batch_size, squeeze_frame,
self.N).transpose(1,2).contiguous() # B, N, L
# sequence modeling
feature_output = self.audio_block(squeeze_mean).view(batch_size, -1, squeeze_frame) # B, N, L
# context decoding
feature_output = feature_output.unsqueeze(2) + squeeze_block # B, N, context, L
feature_output = feature_output.permute(0,3,1,2).contiguous().view(batch_size*squeeze_frame, self.N,
self.context_size) # B*L, N, context
unsqueeze_output = self.context_dec_1(feature_output).view(batch_size, squeeze_frame,
self.N, -1) # B, L, N, context
unsqueeze_output = unsqueeze_output.permute(0,2,3,1).contiguous() # B, N, context, L
unsqueeze_output = merge_feature(unsqueeze_output, squeeze_rest) # B, N, T
########### Fusion ###########
feature_fusion = torch.cat((unsqueeze_output, aux, visual), dim=2)
feature_fusion = feature_fusion.reshape(batch_size, -1, T)
########### Part 2 ###########
# context encoding
squeeze_block_2, squeeze_rest_2 = split_feature(feature_fusion, self.context_size) # B, N, context, L
squeeze_frame_2 = squeeze_block_2.shape[-1]
squeeze_input_2 = squeeze_block_2.permute(0,3,1,2).contiguous().view(batch_size*squeeze_frame_2, -1,self.context_size) # B*L, N, context
squeeze_output_2 = self.context_enc_2(squeeze_input_2) # B*L, N, context
squeeze_mean_2 = squeeze_output_2.mean(2).view(batch_size, squeeze_frame_2,
-1).transpose(1,2).contiguous() # B, N, L
# Fusion Block
fusion_output = self.fusion_block(squeeze_mean_2).view(batch_size, -1, squeeze_frame_2) # B, 3*N, T
# context decoding
fusion_output = fusion_output.unsqueeze(2) + squeeze_block_2 # B, 3N, context, L
fusion_output = fusion_output.permute(0,3,1,2).contiguous().view(batch_size*squeeze_frame_2, -1,
self.context_size) # B*L, N, context
unsqueeze_output_2 = self.context_dec_2(fusion_output).view(batch_size, squeeze_frame_2, -1,
self.context_size) # B, L, N, context
unsqueeze_output_2 = unsqueeze_output_2.permute(0,2,3,1).contiguous() # B, N, context, L
unsqueeze_output_2 = merge_feature(unsqueeze_output_2, squeeze_rest_2) # B, N, T
# Mask Generation
masks = self.gen_masks(unsqueeze_output_2)
mask_output = masks * enc_out # B, N, T
# Waveform Decoder
output = self.decoder(mask_output, squeeze=False) # B, 1, T_wav
return output
def check_parameters(net):
parameters = sum(param.numel() for param in net.parameters())
return parameters / 10**6
def test_limuse():
mix = torch.randn(4,2,48000)
aux = torch.randn(4, 1, 512)
visual = torch.randn(4, 75, 256)
nnet = LiMuSE()
s = nnet(mix,aux,visual)
print(str(check_parameters(nnet))+' Mb')
# print(nnet)
if __name__ == "__main__":
test_limuse()