-
Notifications
You must be signed in to change notification settings - Fork 20
/
aia_trans.py
328 lines (261 loc) · 12.9 KB
/
aia_trans.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
import torch
import torch.nn as nn
from torch.autograd import Variable
from aia_net import AIA_Transformer, AIA_Transformer_merge, AHAM, AHAM_ori
from ptflops import get_model_complexity_info
from Backup_pesq import numParams
class dual_aia_complex_trans(nn.Module):
def __init__(self):
super(dual_aia_complex_trans, self).__init__()
self.en_ri = dense_encoder()
self.en_mag = dense_encoder_mag()
self.dual_trans = AIA_Transformer(64, 64, num_layers=4)
self.aham = AHAM(input_channel=64)
self.dual_trans_mag = AIA_Transformer(64, 64, num_layers=4)
self.aham_mag = AHAM(input_channel=64)
self.de1 = dense_decoder()
self.de2 = dense_decoder()
self.de_mag_mask = dense_decoder_masking()
def forward(self, x):
batch_size, _, seq_len, _ = x.shape
x_mag_ori = torch.norm(x,dim=1)
x_mag = x_mag_ori.unsqueeze(dim = 1)
x_ri = self.en_ri(x) #BCTF
x_last , x_outputlist = self.dual_trans(x_ri) #BCTF, #BCTFG
x_ri = self.aham(x_outputlist) #BCTF
x_mag_en = self.en_mag(x_mag)
x_last_mag, x_outputlist_mag = self.dual_trans_mag(x_mag_en) # BCTF, #BCTFG
x_mag_en = self.aham_mag(x_outputlist_mag) # BCTF
x_mag_mask = self.de_mag_mask(x_mag_en)
x_mag = x_mag_mask *x_mag
x_mag = x_mag.squeeze(dim =1)
x_real = self.de1(x_ri)
x_imag = self.de2(x_ri)
x_real = x_real.squeeze(dim = 1)
x_imag = x_imag.squeeze(dim=1)
x_com=torch.stack((x_real, x_imag), dim=1)
pre_mag, pre_phase = torch.norm(x_com, dim=1), torch.atan2(x_com[:,-1,:,:], x_com[:,0,:,:])
x_mag_out=(x_mag + pre_mag)/2
x_r_out,x_i_out = x_mag_out * torch.cos(pre_phase), x_mag_out * torch.sin(pre_phase)
x_com_out = torch.stack((x_r_out,x_i_out),dim=1)
return x_com_out
class dual_aia_trans_merge_crm(nn.Module):
def __init__(self):
super(dual_aia_trans_merge_crm, self).__init__()
self.en_ri = dense_encoder()
self.en_mag = dense_encoder_mag()
self.aia_trans_merge = AIA_Transformer_merge(128, 64, num_layers=4)
self.aham = AHAM_ori(input_channel=64)
self.aham_mag = AHAM_ori(input_channel=64)
self.de1 = dense_decoder()
self.de2 = dense_decoder()
self.de_mag_mask = dense_decoder_masking()
def forward(self, x):
batch_size, _, seq_len, _ = x.shape
x_r_input, x_i_input = x[:,0,:,:], x[:,1,:,:]
x_mag_ori, x_phase_ori = torch.norm(x, dim=1), torch.atan2(x[:, -1, :, :], x[:, 0, :, :])
x_mag = x_mag_ori.unsqueeze(dim = 1)
# ri/mag components enconde+ aia_transformer_merge
x_ri = self.en_ri(x) #BCTF
x_mag_en = self.en_mag(x_mag)
x_last_mag, x_outputlist_mag, x_last_ri, x_outputlist_ri = self.aia_trans_merge(x_mag_en, x_ri) # BCTF, #BCTFG
x_ri = self.aham(x_outputlist_ri) #BCT
x_mag_en = self.aham_mag(x_outputlist_mag) # BCTF
x_mag_mask = self.de_mag_mask(x_mag_en)
x_mag_mask = x_mag_mask.squeeze(dim=1)
# real and imag decode
x_real = self.de1(x_ri)
x_imag = self.de2(x_ri)
x_real = x_real.squeeze(dim = 1)
x_imag = x_imag.squeeze(dim=1)
# magnitude and ri components interaction
x_mag_out=x_mag_mask * x_mag_ori
x_r_out,x_i_out = (x_mag_out * torch.cos(x_phase_ori) + x_real), (x_mag_out * torch.sin(x_phase_ori)+ x_imag)
x_com_out = torch.stack((x_r_out,x_i_out),dim=1)
return x_com_out
class aia_complex_trans_mag(nn.Module):
def __init__(self):
super(aia_complex_trans_mag, self).__init__()
self.en_mag = dense_encoder_mag()
self.dual_trans_mag = AIA_Transformer(64, 64, num_layers=4)
self.aham_mag = AHAM(input_channel=64)
self.de_mag_mask = dense_decoder_masking()
def forward(self, x):
batch_size, _, seq_len, _ = x.shape
x_r_input, x_i_input = x[:,0,:,:], x[:,1,:,:]
x_mag_ori, x_phase_ori = torch.norm(x, dim=1), torch.atan2(x[:, -1, :, :], x[:, 0, :, :])
x_mag = x_mag_ori.unsqueeze(dim = 1)
# magnitude enconde+ aia_transformer + masking module
x_mag_en = self.en_mag(x_mag)
x_last_mag, x_outputlist_mag = self.dual_trans_mag(x_mag_en) # BCTF, #BCTFG
x_mag_en = self.aham_mag(x_outputlist_mag) # BCTF
x_mag_mask = self.de_mag_mask(x_mag_en)
x_mag_mask = x_mag_mask.squeeze(dim=1)
# real and imag decode
# magnitude and ri components interaction
x_mag_out=x_mag_mask * x_mag_ori
x_r_out,x_i_out = (x_mag_out * torch.cos(x_phase_ori) ), (x_mag_out * torch.sin(x_phase_ori))
x_com_out = torch.stack((x_r_out,x_i_out),dim=1)
return x_com_out
class aia_complex_trans_ri(nn.Module):
def __init__(self):
super(aia_complex_trans_ri, self).__init__()
self.en_ri = dense_encoder()
self.dual_trans = AIA_Transformer(64, 64, num_layers=4)
self.aham = AHAM(input_channel=64)
self.de1 = dense_decoder()
self.de2 = dense_decoder()
def forward(self, x):
batch_size, _, seq_len, _ = x.shape
x_r_input, x_i_input = x[:,0,:,:], x[:,1,:,:]
x_mag_ori, x_phase_ori = torch.norm(x, dim=1), torch.atan2(x[:, -1, :, :], x[:, 0, :, :])
x_mag = x_mag_ori.unsqueeze(dim = 1)
# ri components enconde+ aia_transformer
x_ri = self.en_ri(x) #BCTF
x_last , x_outputlist = self.dual_trans(x_ri) #BCTF, #BCTFG
x_ri = self.aham(x_outputlist) #BCTF
# real and imag decode
x_real = self.de1(x_ri)
x_imag = self.de2(x_ri)
x_real = x_real.squeeze(dim = 1)
x_imag = x_imag.squeeze(dim=1)
x_com=torch.stack((x_real, x_imag), dim=1)
return x_com
class dense_encoder(nn.Module):
def __init__(self, width =64):
super(dense_encoder, self).__init__()
self.in_channels = 2
self.out_channels = 1
self.width = width
self.inp_conv = nn.Conv2d(in_channels=self.in_channels, out_channels=self.width, kernel_size=(1, 1)) # [b, 64, nframes, 512]
self.inp_norm = nn.LayerNorm(161)
self.inp_prelu = nn.PReLU(self.width)
self.enc_dense1 = DenseBlock(161, 4, self.width) # [b, 64, nframes, 512]
self.enc_conv1 = nn.Conv2d(in_channels=self.width, out_channels=self.width, kernel_size=(1, 3), stride=(1, 2)) # [b, 64, nframes, 256]
self.enc_norm1 = nn.LayerNorm(80)
self.enc_prelu1 = nn.PReLU(self.width)
def forward(self, x):
out = self.inp_prelu(self.inp_norm(self.inp_conv(x))) # [b, 64, T, F]
out = self.enc_dense1(out) # [b, 64, T, F]
x = self.enc_prelu1(self.enc_norm1(self.enc_conv1(out))) # [b, 64, T, F]
return x
class dense_encoder_mag(nn.Module):
def __init__(self, width =64):
super(dense_encoder_mag, self).__init__()
self.in_channels = 1
self.out_channels = 1
self.width = width
self.inp_conv = nn.Conv2d(in_channels=self.in_channels, out_channels=self.width, kernel_size=(1, 1)) # [b, 64, nframes, 512]
self.inp_norm = nn.LayerNorm(161)
self.inp_prelu = nn.PReLU(self.width)
self.enc_dense1 = DenseBlock(161, 4, self.width) # [b, 64, nframes, 512]
self.enc_conv1 = nn.Conv2d(in_channels=self.width, out_channels=self.width, kernel_size=(1, 3), stride=(1, 2)) # [b, 64, nframes, 256]
self.enc_norm1 = nn.LayerNorm(80)
self.enc_prelu1 = nn.PReLU(self.width)
def forward(self, x):
out = self.inp_prelu(self.inp_norm(self.inp_conv(x))) # [b, 64, T, F]
out = self.enc_dense1(out) # [b, 64, T, F]
x = self.enc_prelu1(self.enc_norm1(self.enc_conv1(out))) # [b, 64, T, F]
return x
class dense_decoder(nn.Module):
def __init__(self, width =64):
super(dense_decoder, self).__init__()
self.in_channels = 1
self.out_channels = 1
self.pad = nn.ConstantPad2d((1, 1, 0, 0), value=0.)
self.pad1 = nn.ConstantPad2d((1, 0, 0, 0), value=0.)
self.width =width
self.dec_dense1 = DenseBlock(80, 4, self.width)
self.dec_conv1 = SPConvTranspose2d(in_channels=self.width, out_channels=self.width, kernel_size=(1, 3), r=2)
self.dec_norm1 = nn.LayerNorm(161)
self.dec_prelu1 = nn.PReLU(self.width)
self.out_conv = nn.Conv2d(in_channels=self.width, out_channels=self.out_channels, kernel_size=(1, 1))
def forward(self, x):
out = self.dec_dense1(x)
out = self.dec_prelu1(self.dec_norm1(self.pad1(self.dec_conv1(self.pad(out)))))
out = self.out_conv(out)
out.squeeze(dim=1)
return out
class dense_decoder_masking(nn.Module):
def __init__(self, width =64):
super(dense_decoder_masking, self).__init__()
self.in_channels = 1
self.out_channels = 1
self.pad = nn.ConstantPad2d((1, 1, 0, 0), value=0.)
self.pad1 = nn.ConstantPad2d((1, 0, 0, 0), value=0.)
self.width =width
self.dec_dense1 = DenseBlock(80, 4, self.width)
self.dec_conv1 = SPConvTranspose2d(in_channels=self.width, out_channels=self.width, kernel_size=(1, 3), r=2)
self.dec_norm1 = nn.LayerNorm(161)
self.dec_prelu1 = nn.PReLU(self.width)
self.mask1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=1, kernel_size= (1,1)),
nn.Sigmoid()
)
self.mask2 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=1, kernel_size= (1,1)),
nn.Tanh()
)
self.maskconv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=(1,1))
#self.maskrelu = nn.ReLU(inplace=True)
self.maskrelu = nn.Sigmoid()
self.out_conv = nn.Conv2d(in_channels=self.width, out_channels=self.out_channels, kernel_size=(1, 1))
def forward(self, x):
out = self.dec_dense1(x)
out = self.dec_prelu1(self.dec_norm1(self.pad1(self.dec_conv1(self.pad(out)))))
out = self.out_conv(out)
out.squeeze(dim=1)
out = self.mask1(out) * self.mask2(out)
out = self.maskrelu(self.maskconv(out)) # mask
return out
class SPConvTranspose2d(nn.Module): #sub-pixel convolution
def __init__(self, in_channels, out_channels, kernel_size, r=1):
# upconvolution only along second dimension of image
# Upsampling using sub pixel layers
super(SPConvTranspose2d, self).__init__()
self.out_channels = out_channels
self.conv = nn.Conv2d(in_channels, out_channels * r, kernel_size=kernel_size, stride=(1, 1))
self.r = r
def forward(self, x):
out = self.conv(x)
batch_size, nchannels, H, W = out.shape
out = out.view((batch_size, self.r, nchannels // self.r, H, W))
out = out.permute(0, 2, 3, 4, 1)
out = out.contiguous().view((batch_size, nchannels // self.r, H, -1))
return out
class DenseBlock(nn.Module): #dilated dense block
def __init__(self, input_size, depth=5, in_channels=64):
super(DenseBlock, self).__init__()
self.depth = depth
self.in_channels = in_channels
self.pad = nn.ConstantPad2d((1, 1, 1, 0), value=0.)
self.twidth = 2
self.kernel_size = (self.twidth, 3)
for i in range(self.depth):
dil = 2 ** i
pad_length = self.twidth + (dil - 1) * (self.twidth - 1) - 1
setattr(self, 'pad{}'.format(i + 1), nn.ConstantPad2d((1, 1, pad_length, 0), value=0.))
setattr(self, 'conv{}'.format(i + 1),
nn.Conv2d(self.in_channels * (i + 1), self.in_channels, kernel_size=self.kernel_size,
dilation=(dil, 1)))
setattr(self, 'norm{}'.format(i + 1), nn.LayerNorm(input_size))
setattr(self, 'prelu{}'.format(i + 1), nn.PReLU(self.in_channels))
def forward(self, x):
skip = x
for i in range(self.depth):
out = getattr(self, 'pad{}'.format(i + 1))(skip)
out = getattr(self, 'conv{}'.format(i + 1))(out)
out = getattr(self, 'norm{}'.format(i + 1))(out)
out = getattr(self, 'prelu{}'.format(i + 1))(out)
skip = torch.cat([out, skip], dim=1)
return out
if __name__ == '__main__':
model = dual_aia_trans_merge_crm()
model.eval()
x = torch.FloatTensor(4, 2, 10, 161)
#output = model(x)
x = model(x)
print(str(x.shape))
print('The number of parameters of the model is:%.5d' % numParams(model))
macs, params = get_model_complexity_info(model, (2, 100, 161), as_strings=True,
print_per_layer_stat=True, verbose=True)