-
Notifications
You must be signed in to change notification settings - Fork 1
/
frft.py
200 lines (158 loc) · 6.4 KB
/
frft.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
import torch
import torch.nn as nn
import math
# core module
class FRFT(nn.Module):
def __init__(self, in_channels, order=0.5):
super(FRFT, self).__init__()
C0 = int(in_channels/3)
C1 = int(in_channels) - 2*C0
self.conv_0 = nn.Conv2d(C0, C0, kernel_size=3, padding=1)
self.conv_05 = nn.Conv2d(2*C1, 2*C1, kernel_size=1, padding=0)
self.conv_1 = nn.Conv2d(2*C0, 2*C0, kernel_size=1, padding=0)
self.conv2 = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1)
self.order = nn.Parameter(torch.randn(1))
def dfrtmtrx(self, N, a):
# Approximation order
app_ord = 2
Evec = self.dis_s(N,app_ord).cuda()
Evec = Evec.to(dtype=torch.complex64)
even = 1 - (N%2)
l = torch.tensor(list(range(0,N-1)) + [N-1+even]).cuda()
f = torch.diag(torch.exp(-1j*math.pi/2*a*l))
F = N**(1/2)*torch.einsum("ij,jk,ni->nk", f, Evec.T, Evec)
return F
def dis_s(self, N, app_ord):
app_ord = int(app_ord / 2)
s = torch.cat((torch.tensor([0, 1]), torch.zeros(N-1-2*app_ord), torch.tensor([1])))
S = self.cconvm(N,s) + torch.diag((torch.fft.fft(s)).real);
p = N
r = math.floor(N/2)
P = torch.zeros((p,p))
P[0,0] = 1
even = 1 - (p%2)
for i in range(1,r-even+1):
P[i,i] = 1/(2**(1/2))
P[i,p-i] = 1/(2**(1/2))
if even:
P[r,r] = 1
for i in range(r+1,p):
P[i,i] = -1/(2**(1/2))
P[i,p-i] = 1/(2**(1/2))
CS = torch.einsum("ij,jk,ni->nk", S, P.T, P)
C2 = CS[0:math.floor(N/2+1), 0:math.floor(N/2+1)]
S2 = CS[math.floor(N/2+1):N, math.floor(N/2+1):N]
ec, vc = torch.linalg.eig(C2)
es, vs = torch.linalg.eig(S2)
ec = ec.real
vc = vc.real
es = es.real
vs = vs.real
qvc = torch.vstack((vc, torch.zeros([math.ceil(N/2-1), math.floor(N/2+1)])))
SC2 = P@qvc # Even Eigenvector of S
qvs = torch.vstack((torch.zeros([math.floor(N/2+1), math.ceil(N/2-1)]),vs))
SS2 = P@qvs # Odd Eigenvector of S
idx = torch.argsort(-ec)
SC2 = SC2[:,idx]
idx = torch.argsort(-es)
SS2 = SS2[:,idx]
if N%2 == 0:
S2C2 = torch.zeros([N,N+1])
SS2 = torch.hstack([SS2, torch.zeros((SS2.shape[0],1))])
S2C2[:,range(0,N+1,2)] = SC2;
S2C2[:,range(1,N,2)] = SS2
S2C2 = S2C2[:, torch.arange(S2C2.size(1)) != N-1]
else:
S2C2 = torch.zeros([N,N])
S2C2[:,range(0,N+1,2)] = SC2;
S2C2[:,range(1,N,2)] = SS2
return S2C2
def cconvm(self, N, s):
M = torch.zeros((N,N))
dum = s
for i in range(N):
M[:,i] = dum
dum = torch.roll(dum,1)
return M
def FRFT2D(self, matrix):
N, C, H, W = matrix.shape
h_test = self.dfrtmtrx(H, self.order).cuda()
w_test = self.dfrtmtrx(W, self.order).cuda()
h_test = torch.repeat_interleave(h_test.unsqueeze(dim=0), repeats=C, dim=0)
h_test = torch.repeat_interleave(h_test.unsqueeze(dim=0), repeats=N, dim=0)
w_test = torch.repeat_interleave(w_test.unsqueeze(dim=0), repeats=C, dim=0)
w_test = torch.repeat_interleave(w_test.unsqueeze(dim=0), repeats=N, dim=0)
out = []
matrix = torch.fft.fftshift(matrix, dim=(2, 3)).to(dtype=torch.complex64)
out = torch.matmul(h_test, matrix)
out = torch.matmul(out, w_test)
out = torch.fft.fftshift(out, dim=(2, 3))
return out
def IFRFT2D(self, matrix):
N, C, H, W = matrix.shape
h_test = self.dfrtmtrx(H, -self.order).cuda()
w_test = self.dfrtmtrx(W, -self.order).cuda()
h_test = torch.repeat_interleave(h_test.unsqueeze(dim=0), repeats=C, dim=0)
h_test = torch.repeat_interleave(h_test.unsqueeze(dim=0), repeats=N, dim=0)
w_test = torch.repeat_interleave(w_test.unsqueeze(dim=0), repeats=C, dim=0)
w_test = torch.repeat_interleave(w_test.unsqueeze(dim=0), repeats=N, dim=0)
out = []
matrix = torch.fft.fftshift(matrix, dim=(2, 3)).to(dtype=torch.complex64)
out = torch.matmul(h_test, matrix)
out = torch.matmul(out, w_test)
out = torch.fft.fftshift(out, dim=(2, 3))
return out
def forward(self, x):
N, C, H, W = x.shape
C0 = int(C/3)
x_0 = x[:, 0:C0, :, :]
x_05 = x[:, C0:C-C0, :, :]
x_1 = x[:, C-C0:C, :, :]
# order = 0
x_0 = self.conv_0(x_0)
# order = 0.5
Fre = self.FRFT2D(x_05)
Real = Fre.real
Imag = Fre.imag
Mix = torch.concat((Real, Imag), dim=1)
Mix = self.conv_05(Mix)
Real1, Imag1 = torch.chunk(Mix, 2, 1)
Fre_out = torch.complex(Real1, Imag1)
IFRFT = self.IFRFT2D(Fre_out)
IFRFT = torch.abs(IFRFT)/(H*W)
# order = 1
fre = torch.fft.rfft2(x_1, norm='backward')
real = fre.real
imag = fre.imag
mix = torch.concat((real, imag), dim=1)
mix = self.conv_1(mix)
real1, imag1 = torch.chunk(mix, 2, 1)
fre_out = torch.complex(real1, imag1)
x_1 = torch.fft.irfft2(fre_out, s=(H, W), norm='backward')
output = torch.cat([x_0, IFRFT, x_1], dim=1)
output = self.conv2(output)
return output
#--------------------------------------------------------------------------------------
# your original network
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(C0, C0, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(C0, C0, kernel_size=3, padding=1)
def forward(self, x):
out1 = self.conv1(x)
out = self.conv2(out1)
return out
#--------------------------------------------------------------------------------------
# plug in our FRFT module and baseline module
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(C0, C0, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(C0, C0, kernel_size=3, padding=1)
self.FRFT = FRFT(in_channels=C0, order=0.5)
def forward(self, x):
out1 = self.conv1(x) # out1.shape: B, C, H, W
out1 = self.FRFT(out1)
out = self.conv2(out1)
return out