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 |
+
##---------- Basic Layers ----------
|
5 |
+
def conv3x3(in_chn, out_chn, bias=True):
|
6 |
+
layer = nn.Conv2d(in_chn, out_chn, kernel_size=3, stride=1, padding=1, bias=bias)
|
7 |
+
return layer
|
8 |
+
|
9 |
+
def conv(in_channels, out_channels, kernel_size, bias=False, stride=1):
|
10 |
+
return nn.Conv2d(
|
11 |
+
in_channels, out_channels, kernel_size,
|
12 |
+
padding=(kernel_size // 2), bias=bias, stride=stride)
|
13 |
+
|
14 |
+
def bili_resize(factor):
|
15 |
+
return nn.Upsample(scale_factor=factor, mode='bilinear', align_corners=False)
|
16 |
+
|
17 |
+
##---------- Basic Blocks ----------
|
18 |
+
|
19 |
+
class UNetConvBlock(nn.Module):
|
20 |
+
def __init__(self, in_size, out_size, downsample):
|
21 |
+
super(UNetConvBlock, self).__init__()
|
22 |
+
self.downsample = downsample
|
23 |
+
self.block = SK_RDB(in_channels=in_size, growth_rate=out_size, num_layers=3)
|
24 |
+
if downsample:
|
25 |
+
self.downsample = PS_down(out_size, out_size, downscale=2)
|
26 |
+
|
27 |
+
def forward(self, x):
|
28 |
+
out = self.block(x)
|
29 |
+
if self.downsample:
|
30 |
+
out_down = self.downsample(out)
|
31 |
+
return out_down, out
|
32 |
+
else:
|
33 |
+
return out
|
34 |
+
|
35 |
+
class UNetUpBlock(nn.Module):
|
36 |
+
def __init__(self, in_size, out_size):
|
37 |
+
super(UNetUpBlock, self).__init__()
|
38 |
+
# self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2, bias=True)
|
39 |
+
self.up = PS_up(in_size, out_size, upscale=2)
|
40 |
+
self.conv_block = UNetConvBlock(in_size, out_size, False)
|
41 |
+
|
42 |
+
def forward(self, x, bridge):
|
43 |
+
up = self.up(x)
|
44 |
+
out = torch.cat([up, bridge], dim=1)
|
45 |
+
out = self.conv_block(out)
|
46 |
+
return out
|
47 |
+
|
48 |
+
##---------- Resizing Modules (Pixel(Un)Shuffle) ----------
|
49 |
+
class PS_down(nn.Module):
|
50 |
+
def __init__(self, in_size, out_size, downscale):
|
51 |
+
super(PS_down, self).__init__()
|
52 |
+
self.UnPS = nn.PixelUnshuffle(downscale)
|
53 |
+
self.conv1 = nn.Conv2d((downscale**2) * in_size, out_size, 1, 1, 0)
|
54 |
+
|
55 |
+
def forward(self, x):
|
56 |
+
x = self.UnPS(x) # h/2, w/2, 4*c
|
57 |
+
x = self.conv1(x)
|
58 |
+
return x
|
59 |
+
|
60 |
+
class PS_up(nn.Module):
|
61 |
+
def __init__(self, in_size, out_size, upscale):
|
62 |
+
super(PS_up, self).__init__()
|
63 |
+
|
64 |
+
self.PS = nn.PixelShuffle(upscale)
|
65 |
+
self.conv1 = nn.Conv2d(in_size//(upscale**2), out_size, 1, 1, 0)
|
66 |
+
|
67 |
+
def forward(self, x):
|
68 |
+
x = self.PS(x) # h/2, w/2, 4*c
|
69 |
+
x = self.conv1(x)
|
70 |
+
return x
|
71 |
+
|
72 |
+
##---------- Selective Kernel Feature Fusion (SKFF) ----------
|
73 |
+
class SKFF(nn.Module):
|
74 |
+
def __init__(self, in_channels, height=3, reduction=8, bias=False):
|
75 |
+
super(SKFF, self).__init__()
|
76 |
+
|
77 |
+
self.height = height
|
78 |
+
d = max(int(in_channels / reduction), 4)
|
79 |
+
|
80 |
+
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
81 |
+
self.conv_du = nn.Sequential(nn.Conv2d(in_channels, d, 1, padding=0, bias=bias), nn.PReLU())
|
82 |
+
|
83 |
+
self.fcs = nn.ModuleList([])
|
84 |
+
for i in range(self.height):
|
85 |
+
self.fcs.append(nn.Conv2d(d, in_channels, kernel_size=1, stride=1, bias=bias))
|
86 |
+
|
87 |
+
self.softmax = nn.Softmax(dim=1)
|
88 |
+
|
89 |
+
def forward(self, inp_feats):
|
90 |
+
batch_size, n_feats, H, W = inp_feats[1].shape
|
91 |
+
|
92 |
+
inp_feats = torch.cat(inp_feats, dim=1)
|
93 |
+
inp_feats = inp_feats.view(batch_size, self.height, n_feats, inp_feats.shape[2], inp_feats.shape[3])
|
94 |
+
|
95 |
+
feats_U = torch.sum(inp_feats, dim=1)
|
96 |
+
feats_S = self.avg_pool(feats_U)
|
97 |
+
feats_Z = self.conv_du(feats_S)
|
98 |
+
|
99 |
+
attention_vectors = [fc(feats_Z) for fc in self.fcs]
|
100 |
+
attention_vectors = torch.cat(attention_vectors, dim=1)
|
101 |
+
attention_vectors = attention_vectors.view(batch_size, self.height, n_feats, 1, 1)
|
102 |
+
|
103 |
+
attention_vectors = self.softmax(attention_vectors)
|
104 |
+
feats_V = torch.sum(inp_feats * attention_vectors, dim=1)
|
105 |
+
|
106 |
+
return feats_V
|
107 |
+
|
108 |
+
##---------- Dense Block ----------
|
109 |
+
class DenseLayer(nn.Module):
|
110 |
+
def __init__(self, in_channels, out_channels, I):
|
111 |
+
super(DenseLayer, self).__init__()
|
112 |
+
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=3 // 2)
|
113 |
+
self.relu = nn.ReLU(inplace=True)
|
114 |
+
self.sk = SKFF(out_channels, height=2, reduction=8, bias=False)
|
115 |
+
|
116 |
+
def forward(self, x):
|
117 |
+
x1 = self.relu(self.conv(x))
|
118 |
+
# output = torch.cat([x, x1], 1) # -> RDB
|
119 |
+
output = self.sk((x, x1))
|
120 |
+
return output
|
121 |
+
|
122 |
+
##---------- Selective Kernel Residual Dense Block (SK-RDB) ----------
|
123 |
+
class SK_RDB(nn.Module):
|
124 |
+
def __init__(self, in_channels, growth_rate, num_layers):
|
125 |
+
super(SK_RDB, self).__init__()
|
126 |
+
self.identity = nn.Conv2d(in_channels, growth_rate, 1, 1, 0)
|
127 |
+
self.layers = nn.Sequential(
|
128 |
+
*[DenseLayer(in_channels, in_channels, I=i) for i in range(num_layers)]
|
129 |
+
)
|
130 |
+
self.lff = nn.Conv2d(in_channels, growth_rate, kernel_size=1)
|
131 |
+
|
132 |
+
def forward(self, x):
|
133 |
+
res = self.identity(x)
|
134 |
+
x = self.layers(x)
|
135 |
+
x = self.lff(x)
|
136 |
+
return res + x
|
137 |
+
|
138 |
+
##---------- testNet ----------
|
139 |
+
class SRMNet(nn.Module):
|
140 |
+
def __init__(self, in_chn=3, wf=96, depth=4):
|
141 |
+
super(SRMNet, self).__init__()
|
142 |
+
self.depth = depth
|
143 |
+
self.down_path = nn.ModuleList()
|
144 |
+
self.bili_down = bili_resize(0.5)
|
145 |
+
self.conv_01 = nn.Conv2d(in_chn, wf, 3, 1, 1)
|
146 |
+
|
147 |
+
# encoder of UNet-64
|
148 |
+
prev_channels = 0
|
149 |
+
for i in range(depth): # 0,1,2,3
|
150 |
+
downsample = True if (i + 1) < depth else False
|
151 |
+
self.down_path.append(UNetConvBlock(prev_channels + wf, (2 ** i) * wf, downsample))
|
152 |
+
prev_channels = (2 ** i) * wf
|
153 |
+
|
154 |
+
# decoder of UNet-64
|
155 |
+
self.up_path = nn.ModuleList()
|
156 |
+
self.skip_conv = nn.ModuleList()
|
157 |
+
self.conv_up = nn.ModuleList()
|
158 |
+
self.bottom_conv = nn.Conv2d(prev_channels, wf, 3, 1, 1)
|
159 |
+
self.bottom_up = bili_resize(2 ** (depth-1))
|
160 |
+
|
161 |
+
for i in reversed(range(depth - 1)):
|
162 |
+
self.up_path.append(UNetUpBlock(prev_channels, (2 ** i) * wf))
|
163 |
+
self.skip_conv.append(nn.Conv2d((2 ** i) * wf, (2 ** i) * wf, 3, 1, 1))
|
164 |
+
self.conv_up.append(nn.Sequential(*[bili_resize(2 ** i), nn.Conv2d((2 ** i) * wf, wf, 3, 1, 1)]))
|
165 |
+
# *[nn.Conv2d((2 ** i) * wf, wf, 3, 1, 1), bili_resize(2 ** i)])
|
166 |
+
prev_channels = (2 ** i) * wf
|
167 |
+
|
168 |
+
self.final_ff = SKFF(in_channels=wf, height=depth)
|
169 |
+
self.last = conv3x3(prev_channels, in_chn, bias=True)
|
170 |
+
|
171 |
+
def forward(self, x):
|
172 |
+
img = x
|
173 |
+
scale_img = img
|
174 |
+
|
175 |
+
##### shallow conv #####
|
176 |
+
x1 = self.conv_01(img)
|
177 |
+
encs = []
|
178 |
+
######## UNet-64 ########
|
179 |
+
# Down-path (Encoder)
|
180 |
+
for i, down in enumerate(self.down_path):
|
181 |
+
if i == 0: # top layer
|
182 |
+
x1, x1_up = down(x1)
|
183 |
+
encs.append(x1_up)
|
184 |
+
elif (i + 1) < self.depth: # middle layer
|
185 |
+
scale_img = self.bili_down(scale_img)
|
186 |
+
left_bar = self.conv_01(scale_img)
|
187 |
+
x1 = torch.cat([x1, left_bar], dim=1)
|
188 |
+
x1, x1_up = down(x1)
|
189 |
+
encs.append(x1_up)
|
190 |
+
else: # lowest layer
|
191 |
+
scale_img = self.bili_down(scale_img)
|
192 |
+
left_bar = self.conv_01(scale_img)
|
193 |
+
x1 = torch.cat([x1, left_bar], dim=1)
|
194 |
+
x1 = down(x1)
|
195 |
+
|
196 |
+
# Up-path (Decoder)
|
197 |
+
ms_result = [self.bottom_up(self.bottom_conv(x1))]
|
198 |
+
for i, up in enumerate(self.up_path):
|
199 |
+
x1 = up(x1, self.skip_conv[i](encs[-i - 1]))
|
200 |
+
ms_result.append(self.conv_up[i](x1))
|
201 |
+
|
202 |
+
# Multi-scale selective feature fusion
|
203 |
+
msff_result = self.final_ff(ms_result)
|
204 |
+
|
205 |
+
##### Reconstruct #####
|
206 |
+
out_1 = self.last(msff_result) + img
|
207 |
+
|
208 |
+
return out_1
|
209 |
+
|
210 |
+
if __name__ == "__main__":
|
211 |
+
from thop import profile
|
212 |
+
input = torch.ones(1, 3, 256, 256, dtype=torch.float, requires_grad=False)
|
213 |
+
|
214 |
+
model = SRMNet(in_chn=3, wf=96, depth=4)
|
215 |
+
out = model(input)
|
216 |
+
flops, params = profile(model, inputs=(input,))
|
217 |
+
|
218 |
+
# RDBlayer = SK_RDB(in_channels=64, growth_rate=64, num_layers=3)
|
219 |
+
# print(RDBlayer)
|
220 |
+
# out = RDBlayer(input)
|
221 |
+
# flops, params = profile(RDBlayer, inputs=(input,))
|
222 |
+
print('input shape:', input.shape)
|
223 |
+
print('parameters:', params/1e6)
|
224 |
+
print('flops', flops/1e9)
|
225 |
+
print('output shape', out.shape)
|