-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathphase_encoder.py
130 lines (110 loc) · 4.01 KB
/
phase_encoder.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
"""
Phase Encoder (PE).
[email protected], 2022
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
class ComplexConv2d(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size=(1, 1),
stride=(1, 1),
padding=(0, 0),
dilation=1,
groups=1,
causal=True,
complex_axis=1,
):
super(ComplexConv2d, self).__init__()
self.in_channels = in_channels//2
self.out_channels = out_channels//2
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.causal = causal
self.groups = groups
self.dilation = dilation
self.complex_axis = complex_axis
self.real_conv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size, self.stride, padding=[
self.padding[0], 0], dilation=self.dilation, groups=self.groups)
self.imag_conv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size, self.stride, padding=[
self.padding[0], 0], dilation=self.dilation, groups=self.groups)
nn.init.normal_(self.real_conv.weight.data, std=0.05)
nn.init.normal_(self.imag_conv.weight.data, std=0.05)
nn.init.constant_(self.real_conv.bias, 0.)
nn.init.constant_(self.imag_conv.bias, 0.)
def forward(self, inputs):
if self.padding[1] != 0 and self.causal:
inputs = F.pad(inputs, [self.padding[1], 0, 0, 0])
else:
inputs = F.pad(inputs, [self.padding[1], self.padding[1], 0, 0])
if self.complex_axis == 0:
real = self.real_conv(inputs)
imag = self.imag_conv(inputs)
real2real, imag2real = torch.chunk(real, 2, self.complex_axis)
real2imag, imag2imag = torch.chunk(imag, 2, self.complex_axis)
else:
if isinstance(inputs, torch.Tensor):
real, imag = torch.chunk(inputs, 2, self.complex_axis)
real2real = self.real_conv(real,)
imag2imag = self.imag_conv(imag,)
real2imag = self.imag_conv(real)
imag2real = self.real_conv(imag)
real = real2real - imag2imag
imag = real2imag + imag2real
out = torch.cat([real, imag], self.complex_axis)
return out
def complex_cat(inps, dim=1):
reals, imags = [], []
for inp in inps:
real, imag = inp.chunk(2, dim)
reals.append(real)
imags.append(imag)
reals = torch.cat(reals, dim)
imags = torch.cat(imags, dim)
return reals, imags
class ComplexLinearProjection(nn.Module):
def __init__(self, cin):
super(ComplexLinearProjection, self).__init__()
self.clp = ComplexConv2d(cin, cin)
def forward(self, real, imag):
"""
real, imag: B C F T
"""
inputs = torch.cat([real, imag], 1)
outputs = self.clp(inputs)
real, imag = outputs.chunk(2, dim=1)
outputs = torch.sqrt(real**2+imag**2+1e-8)
return outputs
class PhaseEncoder(nn.Module):
def __init__(self, cout, n_sig, cin=2, alpha=0.5):
super(PhaseEncoder, self).__init__()
self.complexnn = nn.ModuleList()
for _ in range(n_sig):
self.complexnn.append(
nn.Sequential(
nn.ConstantPad2d((2, 0, 0, 0), 0.0),
ComplexConv2d(cin, cout, (1, 3))
)
)
self.clp = ComplexLinearProjection(cout*n_sig)
self.alpha = alpha
def forward(self, cspecs):
"""
cspec: B C F T
"""
outs = []
for idx, layer in enumerate(self.complexnn):
outs.append(layer(cspecs[idx]))
real, imag = complex_cat(outs, dim=1)
amp = self.clp(real, imag)
return amp**self.alpha
if __name__ == "__main__":
net = PhaseEncoder(cout=4, n_sig=1)
# 32ms@48kHz, concatenation of [real, imag], dim=1
inps = torch.randn(3, 2, 769, 126)
outs = net([inps])
print(outs.shape)