-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodule.py
309 lines (297 loc) · 16 KB
/
module.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
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import config
"""
we use skip-connection in TCN block by default
"""
class cLN(nn.Module):
def __init__(self, dimension, eps = 1e-8, trainable=True):
super(cLN, self).__init__()
self.eps = eps
if trainable:
self.gain = nn.Parameter(torch.ones(1, dimension, 1))
self.bias = nn.Parameter(torch.zeros(1, dimension, 1))
else:
self.gain = Variable(torch.ones(1, dimension, 1), requires_grad=False)
self.bias = Variable(torch.zeros(1, dimension, 1), requires_grad=False)
def forward(self, input):
# input size: (Batch, Freq, Time)
# cumulative mean for each time step
batch_size, channel, time_step = input.size(0), input.size(1), input.size(2)
step_sum = input.sum(1) # B, T
step_pow_sum = input.pow(2).sum(1) # B, T
cum_sum = torch.cumsum(step_sum, dim=1) # B, T
cum_pow_sum = torch.cumsum(step_pow_sum, dim=1) # B, T
entry_cnt = np.arange(channel, channel*(time_step+1), channel)
entry_cnt = torch.from_numpy(entry_cnt).type(input.type())
entry_cnt = entry_cnt.view(1, -1).expand_as(cum_sum)
cum_mean = cum_sum / entry_cnt # B, T
cum_var = (cum_pow_sum - 2*cum_mean*cum_sum) / entry_cnt + cum_mean.pow(2) # B, T
cum_std = (cum_var + self.eps).sqrt() # B, T
cum_mean = cum_mean.unsqueeze(1)
cum_std = cum_std.unsqueeze(1)
x = (input - cum_mean.expand_as(input)) / cum_std.expand_as(input)
return x * self.gain.expand_as(x).type(x.type()) + self.bias.expand_as(x).type(x.type())
class DepthConv1d(nn.Module):
def __init__(self, input_channel, hidden_channel, kernel, padding, dilation=1, skip=True, 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
if config.inference:
self.dconv1d = nn.Conv1d(hidden_channel, hidden_channel, kernel, dilation=dilation,
groups=hidden_channel, padding=0) # the key point exist in padding method
else:
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()
# now, cLN, BN and LN all can meet streaming inference strategy, but BN and LN are simple
print("normalization type: {},".format(config.mode_LN))
if config.mode_LN == 'cLN':
self.reg1 = cLN(hidden_channel, eps=1e-08)
self.reg2 = cLN(hidden_channel, eps=1e-08)
elif config.mode_LN == 'BN':
self.reg1 = nn.BatchNorm1d(hidden_channel, eps=1e-08)
self.reg2 = nn.BatchNorm1d(hidden_channel, eps=1e-08)
elif config.mode_LN == 'gLN':
self.reg1 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
self.reg2 = nn.GroupNorm(1, hidden_channel, eps=1e-08)
else:
self.reg1 = nn.LayerNorm(hidden_channel, eps=1e-08)
self.reg2 = nn.LayerNorm(hidden_channel, eps=1e-08)
# skip-path
if self.skip:
self.skip_out = nn.Conv1d(hidden_channel, input_channel, 1)
def forward(self, input, cache_last=None):
if config.mode_LN == 'LN':
output = self.nonlinearity1(self.conv1d(input))
output = output.transpose(1, 2)
output = self.reg1(output)
output = output.transpose(1, 2)
else:
output = self.reg1(self.nonlinearity1(self.conv1d(input)))
if config.inference:
cache_next = output
output = torch.cat((cache_last, output, cache_last), dim=2)
if self.causal:
if config.mode_LN == 'LN':
output = self.nonlinearity2(self.dconv1d(output)[:,:,:-self.padding])
output = output.transpose(1, 2)
output = self.reg2(output)
output = output.transpose(1, 2)
else:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)[:,:,:-self.padding]))
else:
if config.mode_LN == 'LN':
output = self.nonlinearity2(self.dconv1d(output))
output = output.transpose(1, 2)
output = self.reg2(output)
output = output.transpose(1, 2)
else:
output = self.reg2(self.nonlinearity2(self.dconv1d(output)))
residual = self.res_out(output)
if self.skip:
skip = self.skip_out(output)
if config.inference:
return residual, skip, cache_next
else:
return residual, skip
else:
if config.inference:
return residual, cache_next
else:
return residual
class TCN_audio(nn.Module):
def __init__(self, BN_dim, hidden_dim,
layer, stack, num_spk=2, kernel=3, skip=True,
causal=True, dilated=True):
super(TCN_audio, self).__init__()
# the input is a sequence of features of shape (B, N, L)
self.receptive_field = 0
self.dilated = dilated
self.layer = layer
self.stack = stack
self.skip = skip
self.causal = causal
if not self.causal:
self.visual_dim = config.VISUAL_DIM * 2
else:
self.visual_dim = config.VISUAL_DIM
# the method of deep concatenate fusion (DCF) use target and other speaker's visual information
if config.MODAL_FUSION == 'DCF':
self.fc = nn.Linear(128 + num_spk * self.visual_dim, 128, bias=True)
# the method of concatenate fusion (CF) use target speaker's visual information only
if config.MODAL_FUSION == 'CF':
self.fc = nn.Linear(BN_dim + self.visual_dim, BN_dim, bias=True)
self.TCN = nn.ModuleList([])
# TCN module that be used to process audio
base = 2
for s in range(self.stack):
for i in range(self.layer):
if self.dilated:
if config.low_latency and (s == 0 and i in [0, 1, 2]):
print('use low_latency model in audio path')
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=base**i, padding=base**i, skip=self.skip, causal=False))
else:
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=base**i, padding=base**i, skip=self.skip, causal=self.causal))
else:
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=1, padding=1, skip=self.skip, causal=self.causal))
if i == 0 and s == 0:
self.receptive_field = self.receptive_field + kernel
else:
if self.dilated:
self.receptive_field = self.receptive_field + (kernel - 1) * base**i
else:
self.receptive_field = self.receptive_field + (kernel - 1)
print("Receptive field in audio path: {:3d} frames.".format(self.receptive_field))
def forward(self, input, query, num_spk, CACHE_audio=None):
# input shape: (B, N, L)
# select the multi-modal fusion position
fusion_position = [config.FUSION_POSITION]
output = input
if self.skip:
skip_connection = 0.
for i in range(len(self.TCN)):
if str(i) in fusion_position:
if config.MODAL_FUSION == 'CF':
output = torch.cat((output, query), dim=1)
output = output.transpose(1, 2)
output = self.fc(output)
output = output.transpose(1, 2)
if config.MODAL_FUSION == 'DCF':
shape = output.shape
output = output.view(-1, num_spk, shape[1], shape[2])
qshape = query.shape
query = query.view(-1, num_spk, qshape[1], qshape[2]) # shape:[B,num_spk,N,L]
if num_spk == 1:
output = torch.cat((output, query), dim=2)
output = output.view(-1, shape[1] + qshape[1], shape[2])
if num_spk == 2:
output0 = torch.cat((output[:,0,:,:],query[:,0,:,:],query[:,1,:,:]), dim=1)
output1 = torch.cat((output[:,1,:,:],query[:,1,:,:],query[:,0,:,:]), dim=1)
output = torch.cat((output0.unsqueeze(1), output1.unsqueeze(1)), dim=1)
output = output.view(-1, shape[1] + 2*qshape[1], shape[2])
if num_spk == 3:
output0 = torch.cat((output[:,0,:,:],query[:,0,:,:],query[:,1,:,:]+query[:,2,:,:]), dim=1)
output1 = torch.cat((output[:,1,:,:],query[:,1,:,:],query[:,0,:,:]+query[:,2,:,:]), dim=1)
output2 = torch.cat((output[:,2,:,:],query[:,2,:,:],query[:,0,:,:]+query[:,1,:,:]), dim=1)
output = torch.cat((output0.unsqueeze(1), output1.unsqueeze(1), output2.unsqueeze(1)), dim=1)
output = output.view(-1, shape[1] + 2*qshape[1], shape[2])
output = output.transpose(1, 2)
output = self.fc(output)
output = output.transpose(1, 2)
if self.causal and config.inference:
residual, skip, cache = self.TCN[i](output, CACHE_audio[str(i)])
CACHE_audio[str(i)] = torch.cat((CACHE_audio[str(i)], cache[:,:,-2*2**(i%self.layer):]), dim=2)[:,:,-2*2**(i%self.layer):]
else:
residual, skip = self.TCN[i](output) # causal=False, causal=True and inference=False
output = output + residual
skip_connection = skip_connection + skip
else:
for i in range(len(self.TCN)):
if str(i) in fusion_position:
if config.MODAL_FUSION == 'CF':
output = torch.cat((output, query), dim=1)
output = output.transpose(1, 2)
output = self.fc(output)
output = output.transpose(1, 2)
if config.MODAL_FUSION == 'DCF':
shape = output.shape
output = output.view(-1, num_spk, shape[1], shape[2])
qshape = query.shape
query = query.view(-1, num_spk, qshape[1], qshape[2]) # shape:[B,num_spk,N,L]
if num_spk == 1:
output = torch.cat((output, query), dim=2)
output = output.view(-1, shape[1] + qshape[1], shape[2])
if num_spk == 2:
output0 = torch.cat((output[:,0,:,:],query[:,0,:,:],query[:,1,:,:]), dim=1)
output1 = torch.cat((output[:,1,:,:],query[:,1,:,:],query[:,0,:,:]), dim=1)
output = torch.cat((output0.unsqueeze(1), output1.unsqueeze(1)), dim=1)
output = output.view(-1, shape[1] + 2*qshape[1], shape[2])
if num_spk == 3:
output0 = torch.cat((output[:,0,:,:],query[:,0,:,:],query[:,1,:,:]+query[:,2,:,:]), dim=1)
output1 = torch.cat((output[:,1,:,:],query[:,1,:,:],query[:,0,:,:]+query[:,2,:,:]), dim=1)
output2 = torch.cat((output[:,2,:,:],query[:,2,:,:],query[:,0,:,:]+query[:,1,:,:]), dim=1)
output = torch.cat((output0.unsqueeze(1), output1.unsqueeze(1), output2.unsqueeze(1)), dim=1)
output = output.view(-1, shape[1] + 2*qshape[1], shape[2])
output = output.transpose(1, 2)
output = self.fc(output)
output = output.transpose(1, 2)
if self.causal and config.inference:
residual, cache = self.TCN[i](output, CACHE_audio[str(i)])
CACHE_audio[str(i)] = torch.cat((CACHE_audio[str(i)], cache[:,:,-2*2**(i%self.layer):]), dim=2)[:,:,-2*2**(i%self.layer):]
else:
residual = self.TCN[i](output) # causal=False, causal=True and inference=False
output = output + residual
# output layer
if self.skip:
output = skip_connection
else:
output = output
return output, CACHE_audio
class TCN_visual(nn.Module):
def __init__(self, BN_dim, hidden_dim, layer, stack, kernel=3, skip=True,
causal=False, dilated=True):
super(TCN_visual, self).__init__()
# TCN module for processing visual features
self.receptive_field = 0
self.dilated = dilated
self.TCN = nn.ModuleList([])
self.layer = layer
self.stack = stack
self.causal = causal
self.skip = skip
base = 2
for s in range(self.stack):
for i in range(self.layer):
if self.dilated:
if config.low_latency and (s == 0 and i == 1):
print('use low_latency model in visual path')
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=base**i, padding=base**i, skip=self.skip, causal=False))
else:
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=base**i, padding=base**i, skip=self.skip, causal=causal))
else:
self.TCN.append(DepthConv1d(BN_dim, hidden_dim, kernel, dilation=1, padding=1, skip=self.skip, causal=causal))
if i == 0 and s == 0:
self.receptive_field = self.receptive_field + kernel
else:
if self.dilated:
self.receptive_field = self.receptive_field + (kernel - 1) * base**i
else:
self.receptive_field = self.receptive_field + (kernel - 1)
print("Receptive field in visual path: {:3d} frames.".format(self.receptive_field))
def forward(self, input, CACHE_visual=None):
# input shape: (B, N, L)
output = input
if self.skip:
skip_connection = 0.
for i in range(len(self.TCN)):
if self.causal and config.inference:
residual, skip, cache = self.TCN[i](output, CACHE_visual[str(i)])
CACHE_visual[str(i)] = torch.cat((CACHE_visual[str(i)], cache[:,:,-2*2**(i%self.layer):]), dim=2)[:,:,-2*2**(i%self.layer):]
else:
residual, skip = self.TCN[i](output)
output = output + residual
skip_connection = skip_connection + skip
else:
for i in range(len(self.TCN)):
if self.causal and config.inference:
residual, cache = self.TCN[i](output, CACHE_visual[str(i)])
CACHE_visual[str(i)] = torch.cat((CACHE_visual[str(i)], cache[:,:,-2*2**(i%self.layer):]), dim=2)[:,:,-2*2**(i%self.layer):]
else:
residual = self.TCN[i](output)
output = output + residual
# output layer
if self.skip:
output = skip_connection
else:
output = output
return output, CACHE_visual