-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
172 lines (137 loc) · 6.02 KB
/
model.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
## This code is based on `Deep Residual Fourier Transformation for Single Image Deblurring`(https://github.com/INVOKERer/DeepRFT)
from layers import *
class EBlock(nn.Module):
def __init__(self, out_channel, num_res=8, ResBlock=ResBlock):
super(EBlock, self).__init__()
layers = [ResBlock(out_channel) for _ in range(num_res)]
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
class DBlock(nn.Module):
def __init__(self, channel, num_res=8, ResBlock=ResBlock):
super(DBlock, self).__init__()
layers = [ResBlock(channel) for _ in range(num_res)]
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
class AFF(nn.Module):
def __init__(self, in_channel, out_channel, BasicConv=BasicConv):
super(AFF, self).__init__()
self.conv = nn.Sequential(
BasicConv(in_channel, out_channel, kernel_size=1, stride=1, relu=True),
BasicConv(out_channel, out_channel, kernel_size=3, stride=1, relu=False)
)
def forward(self, x1, x2, x4):
x = torch.cat([x1, x2, x4], dim=1)
return self.conv(x)
class SCM(nn.Module):
def __init__(self, out_plane, BasicConv=BasicConv, inchannel=1):
super(SCM, self).__init__()
self.main = nn.Sequential(
BasicConv(inchannel, out_plane//4, kernel_size=3, stride=1, relu=True),
BasicConv(out_plane // 4, out_plane // 2, kernel_size=1, stride=1, relu=True),
BasicConv(out_plane // 2, out_plane // 2, kernel_size=3, stride=1, relu=True),
BasicConv(out_plane // 2, out_plane-inchannel, kernel_size=1, stride=1, relu=True)
)
self.conv = BasicConv(out_plane, out_plane, kernel_size=1, stride=1, relu=False)
def forward(self, x):
x = torch.cat([x, self.main(x)], dim=1)
return self.conv(x)
class FAM(nn.Module):
def __init__(self, channel, BasicConv=BasicConv):
super(FAM, self).__init__()
self.merge = BasicConv(channel, channel, kernel_size=3, stride=1, relu=False)
def forward(self, x1, x2):
x = x1 * x2
out = x1 + self.merge(x)
return out
class DeepRFT(nn.Module):
def __init__(self, num_res=8, inference=False):
super(DeepRFT, self).__init__()
self.inference = inference
if not inference:
BasicConv = BasicConv_do
ResBlock = ResBlock_do_fft_bench
else:
BasicConv = BasicConv_do_eval
ResBlock = ResBlock_do_fft_bench_eval
base_channel = 32
self.Encoder = nn.ModuleList([
EBlock(base_channel, num_res, ResBlock=ResBlock),
EBlock(base_channel*2, num_res, ResBlock=ResBlock),
EBlock(base_channel*4, num_res, ResBlock=ResBlock),
])
self.feat_extract = nn.ModuleList([
BasicConv(1, base_channel, kernel_size=3, relu=True, stride=1),
BasicConv(base_channel, base_channel*2, kernel_size=3, relu=True, stride=2),
BasicConv(base_channel*2, base_channel*4, kernel_size=3, relu=True, stride=2),
BasicConv(base_channel*4, base_channel*2, kernel_size=4, relu=True, stride=2, transpose=True),
BasicConv(base_channel*2, base_channel, kernel_size=4, relu=True, stride=2, transpose=True),
BasicConv(base_channel, 3, kernel_size=3, relu=False, stride=1)
])
self.Decoder = nn.ModuleList([
DBlock(base_channel * 4, num_res, ResBlock=ResBlock),
DBlock(base_channel * 2, num_res, ResBlock=ResBlock),
DBlock(base_channel, num_res, ResBlock=ResBlock)
])
self.Convs = nn.ModuleList([
BasicConv(base_channel * 4, base_channel * 2, kernel_size=1, relu=True, stride=1),
BasicConv(base_channel * 2, base_channel, kernel_size=1, relu=True, stride=1),
])
self.ConvsOut = nn.ModuleList(
[
BasicConv(base_channel * 4, 1, kernel_size=3, relu=False, stride=1),
BasicConv(base_channel * 2, 1, kernel_size=3, relu=False, stride=1),
]
)
self.AFFs = nn.ModuleList([
AFF(base_channel * 7, base_channel*1, BasicConv=BasicConv),
AFF(base_channel * 7, base_channel*2, BasicConv=BasicConv)
])
self.FAM1 = FAM(base_channel * 4, BasicConv=BasicConv)
self.SCM1 = SCM(base_channel * 4, BasicConv=BasicConv)
self.FAM2 = FAM(base_channel * 2, BasicConv=BasicConv)
self.SCM2 = SCM(base_channel * 2, BasicConv=BasicConv)
def forward(self, x):
x_2 = F.interpolate(x, scale_factor=0.5)
x_4 = F.interpolate(x_2, scale_factor=0.5)
z2 = self.SCM2(x_2)
z4 = self.SCM1(x_4)
outputs = list()
x_ = self.feat_extract[0](x)
res1 = self.Encoder[0](x_)
z = self.feat_extract[1](res1)
z = self.FAM2(z, z2)
res2 = self.Encoder[1](z)
z = self.feat_extract[2](res2)
z = self.FAM1(z, z4)
z = self.Encoder[2](z)
z12 = F.interpolate(res1, scale_factor=0.5)
z21 = F.interpolate(res2, scale_factor=2)
z42 = F.interpolate(z, scale_factor=2)
z41 = F.interpolate(z42, scale_factor=2)
res2 = self.AFFs[1](z12, res2, z42)
res1 = self.AFFs[0](res1, z21, z41)
z = self.Decoder[0](z)
z_ = self.ConvsOut[0](z)
z = self.feat_extract[3](z)
if not self.inference:
outputs.append(z_+x_4)
z = torch.cat([z, res2], dim=1)
z = self.Convs[0](z)
z = self.Decoder[1](z)
z_ = self.ConvsOut[1](z)
z = self.feat_extract[4](z)
if not self.inference:
outputs.append(z_+x_2)
z = torch.cat([z, res1], dim=1)
z = self.Convs[1](z)
z = self.Decoder[2](z)
z = self.feat_extract[5](z)
if not self.inference:
outputs.append(z+x)
#print(outputs[::-1][0])
#print((z+x).shape)
return (z+x)[:,0,:,:]#outputs[::-1]
else:
return z+x