-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
279 lines (238 loc) · 13.8 KB
/
block.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
import torch.nn as nn
from collections import OrderedDict
import torch
from . import restormer
def conv_layer(in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True):
padding = int((kernel_size - 1) / 2) * dilation
return nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding, bias=bias, dilation=dilation,
groups=groups)
def norm(norm_type, nc):
norm_type = norm_type.lower()
if norm_type == 'batch':
layer = nn.BatchNorm2d(nc, affine=True)
elif norm_type == 'instance':
layer = nn.InstanceNorm2d(nc, affine=False)
else:
raise NotImplementedError('normalization layer [{:s}] is not found'.format(norm_type))
return layer
def pad(pad_type, padding):
pad_type = pad_type.lower()
if padding == 0:
return None
if pad_type == 'reflect':
layer = nn.ReflectionPad2d(padding)
elif pad_type == 'replicate':
layer = nn.ReplicationPad2d(padding)
else:
raise NotImplementedError('padding layer [{:s}] is not implemented'.format(pad_type))
return layer
def get_valid_padding(kernel_size, dilation):
kernel_size = kernel_size + (kernel_size - 1) * (dilation - 1)
padding = (kernel_size - 1) // 2
return padding
def conv_block(in_nc, out_nc, kernel_size, stride=1, dilation=1, groups=1, bias=True,
pad_type='zero', norm_type=None, act_type='relu'):
padding = get_valid_padding(kernel_size, dilation)
p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
padding = padding if pad_type == 'zero' else 0
c = nn.Conv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
dilation=dilation, bias=bias, groups=groups)
a = activation(act_type) if act_type else None
n = norm(norm_type, out_nc) if norm_type else None
return sequential(p, c, n, a)
def activation(act_type, inplace=True, neg_slope=0.05, n_prelu=1):
act_type = act_type.lower()
if act_type == 'relu':
layer = nn.ReLU(inplace)
elif act_type == 'lrelu':
layer = nn.LeakyReLU(neg_slope, inplace)
elif act_type == 'prelu':
layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
else:
raise NotImplementedError('activation layer [{:s}] is not found'.format(act_type))
return layer
class ShortcutBlock(nn.Module):
def __init__(self, submodule):
super(ShortcutBlock, self).__init__()
self.sub = submodule
def forward(self, x):
output = x + self.sub(x)
return output
def mean_channels(F):
assert(F.dim() == 4)
spatial_sum = F.sum(3, keepdim=True).sum(2, keepdim=True)
return spatial_sum / (F.size(2) * F.size(3))
def stdv_channels(F):
assert(F.dim() == 4)
F_mean = mean_channels(F)
F_variance = (F - F_mean).pow(2).sum(3, keepdim=True).sum(2, keepdim=True) / (F.size(2) * F.size(3))
return F_variance.pow(0.5)
def sequential(*args):
if len(args) == 1:
if isinstance(args[0], OrderedDict):
raise NotImplementedError('sequential does not support OrderedDict input.')
return args[0]
modules = []
for module in args:
if isinstance(module, nn.Sequential):
for submodule in module.children():
modules.append(submodule)
elif isinstance(module, nn.Module):
modules.append(module)
return nn.Sequential(*modules)
# contrast-aware channel attention module
class CCALayer(nn.Module):
def __init__(self, channel, reduction=16):
super(CCALayer, self).__init__()
self.contrast = stdv_channels
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv_du = nn.Sequential(
nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
nn.Sigmoid()
)
def forward(self, x):
y = self.contrast(x) + self.avg_pool(x)
y = self.conv_du(y)
return x * y
class IMDModule(nn.Module):
def __init__(self, in_channels, distillation_rate=0.25):
super(IMDModule, self).__init__()
self.distilled_channels = int(in_channels * distillation_rate)
self.remaining_channels = int(in_channels - self.distilled_channels)
self.c1 = conv_layer(in_channels, in_channels, 3)
self.c2 = conv_layer(self.remaining_channels, in_channels, 3)
self.c3 = conv_layer(self.remaining_channels, in_channels, 3)
self.c4 = conv_layer(self.remaining_channels, self.distilled_channels, 3)
self.act = activation('lrelu', neg_slope=0.05)
self.c5 = conv_layer(in_channels, in_channels, 1)
self.cca = CCALayer(self.distilled_channels * 4)
def forward(self, input):
out_c1 = self.act(self.c1(input))
distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
out_c2 = self.act(self.c2(remaining_c1))
distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
out_c3 = self.act(self.c3(remaining_c2))
distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
out_c4 = self.c4(remaining_c3)
out = torch.cat([distilled_c1, distilled_c2, distilled_c3, out_c4], dim=1)
out_fused = self.c5(self.cca(out)) + input
return out_fused
class IMDModule_mul23(nn.Module):
def __init__(self, in_channels, distillation_rate=0.25):
super(IMDModule_mul23, self).__init__()
self.distilled_channels = int(in_channels * distillation_rate)
self.remaining_channels = int(in_channels - self.distilled_channels)
self.c1 = conv_layer(in_channels, in_channels, 3)
self.d1 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.r1 = conv_layer(in_channels,in_channels,1)
self.d2 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.r2 = conv_layer(in_channels,in_channels,1)
self.d3 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.c4 = conv_layer(in_channels, self.distilled_channels, 1)
self.act = activation('lrelu', neg_slope=0.05)
self.transformer=swinir.RSTB()
def forward(self, input):
input_resolution=[input.size()[2],input.size()[3]]
out_c1 = self.act(self.c1(input))
distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c1 = self.act(self.d1(distilled_c1))
distilled_c11, distilled12 = torch.split(distilled_c1, (self.distilled_channels, self.distilled_channels), dim=1)
out_c2 = self.act(self.r1(torch.cat([distilled12,remaining_c1], dim=1)))
distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c2 = self.act(self.d2(distilled_c2))
distilled_c21, distilled22 = torch.split(distilled_c2, (self.distilled_channels, self.distilled_channels), dim=1)
out_c3 = self.act(self.r2(torch.cat([distilled22,remaining_c2], dim=1)))
distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c3 = self.act(self.d3(distilled_c3))
distilled_c31, distilled32 = torch.split(distilled_c3, (self.distilled_channels, self.distilled_channels), dim=1)
out_c4 = self.act(self.c4(torch.cat([distilled32,remaining_c3], dim=1)))
out = torch.cat([distilled_c11, distilled_c21, distilled_c31, out_c4], dim=1)
out_fused = self.transformer(out,input_resolution) + input
return out_fused
class IMDModule_mul_L(nn.Module):
def __init__(self, in_channels, distillation_rate=0.25):
super(IMDModule_mul_L, self).__init__()
self.distilled_channels = int(in_channels * distillation_rate)
self.remaining_channels = int(in_channels - self.distilled_channels)
self.c1 = conv_layer(in_channels, in_channels, 3)
self.d1 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.r1 = conv_layer(in_channels,in_channels,1)
self.d2 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.r2 = conv_layer(in_channels,in_channels,1)
self.d3 = conv_layer(self.distilled_channels,self.distilled_channels*2,1)
self.c4 = conv_layer(in_channels, self.distilled_channels, 1)
self.act = activation('lrelu', neg_slope=0.05)
self.transformer=nn.Sequential(restormer.TB(),restormer.TB())
def forward(self, input):
out_c1 = self.act(self.c1(input))
distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c1 = self.act(self.d1(distilled_c1))
distilled_c11, distilled12 = torch.split(distilled_c1, (self.distilled_channels, self.distilled_channels), dim=1)
out_c2 = self.act(self.r1(torch.cat([distilled12,remaining_c1], dim=1)))
distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c2 = self.act(self.d2(distilled_c2))
distilled_c21, distilled22 = torch.split(distilled_c2, (self.distilled_channels, self.distilled_channels), dim=1)
out_c3 = self.act(self.r2(torch.cat([distilled22,remaining_c2], dim=1)))
distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
distilled_c3 = self.act(self.d3(distilled_c3))
distilled_c31, distilled32 = torch.split(distilled_c3, (self.distilled_channels, self.distilled_channels), dim=1)
out_c4 = self.act(self.c4(torch.cat([distilled32,remaining_c3], dim=1)))
out = torch.cat([distilled_c11, distilled_c21, distilled_c31, out_c4], dim=1)
out_fused = self.transformer(out) + input
return out_fused
class IMDModule_speed(nn.Module):
def __init__(self, in_channels, distillation_rate=0.25):
super(IMDModule_speed, self).__init__()
self.distilled_channels = int(in_channels * distillation_rate)
self.remaining_channels = int(in_channels - self.distilled_channels)
self.c1 = conv_layer(in_channels, in_channels, 3)
self.c2 = conv_layer(self.remaining_channels, in_channels, 3)
self.c3 = conv_layer(self.remaining_channels, in_channels, 3)
self.c4 = conv_layer(self.remaining_channels, self.distilled_channels, 3)
self.act = activation('lrelu', neg_slope=0.05)
self.c5 = conv_layer(self.distilled_channels * 4, in_channels, 1)
def forward(self, input):
out_c1 = self.act(self.c1(input))
distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
out_c2 = self.act(self.c2(remaining_c1))
distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
out_c3 = self.act(self.c3(remaining_c2))
distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
out_c4 = self.c4(remaining_c3)
out = torch.cat([distilled_c1, distilled_c2, distilled_c3, out_c4], dim=1)
out_fused = self.c5(out) + input
return out_fused
class IMDModule_Large(nn.Module):
def __init__(self, in_channels, distillation_rate=1/4):
super(IMDModule_Large, self).__init__()
self.distilled_channels = int(in_channels * distillation_rate) # 6
self.remaining_channels = int(in_channels - self.distilled_channels) # 18
self.c1 = conv_layer(in_channels, in_channels, 3, bias=False) # 24 --> 24
self.c2 = conv_layer(self.remaining_channels, in_channels, 3, bias=False) # 18 --> 24
self.c3 = conv_layer(self.remaining_channels, in_channels, 3, bias=False) # 18 --> 24
self.c4 = conv_layer(self.remaining_channels, self.remaining_channels, 3, bias=False) # 15 --> 15
self.c5 = conv_layer(self.remaining_channels-self.distilled_channels, self.remaining_channels-self.distilled_channels, 3, bias=False) # 10 --> 10
self.c6 = conv_layer(self.distilled_channels, self.distilled_channels, 3, bias=False) # 5 --> 5
self.act = activation('relu')
self.c7 = conv_layer(self.distilled_channels * 6, in_channels, 1, bias=False)
def forward(self, input):
out_c1 = self.act(self.c1(input)) # 24 --> 24
distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1) # 6, 18
out_c2 = self.act(self.c2(remaining_c1)) # 18 --> 24
distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1) # 6, 18
out_c3 = self.act(self.c3(remaining_c2)) # 18 --> 24
distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1) # 6, 18
out_c4 = self.act(self.c4(remaining_c3)) # 18 --> 18
distilled_c4, remaining_c4 = torch.split(out_c4, (self.distilled_channels, self.remaining_channels-self.distilled_channels), dim=1) # 6, 12
out_c5 = self.act(self.c5(remaining_c4)) # 12 --> 12
distilled_c5, remaining_c5 = torch.split(out_c5, (self.distilled_channels, self.remaining_channels-self.distilled_channels*2), dim=1) # 6, 6
out_c6 = self.act(self.c6(remaining_c5)) # 6 --> 6
out = torch.cat([distilled_c1, distilled_c2, distilled_c3, distilled_c4, distilled_c5, out_c6], dim=1)
out_fused = self.c7(out) + input
return out_fused
def pixelshuffle_block(in_channels, out_channels, upscale_factor=2, kernel_size=3, stride=1):
conv = conv_layer(in_channels, out_channels * (upscale_factor ** 2), kernel_size, stride)
pixel_shuffle = nn.PixelShuffle(upscale_factor)
return sequential(conv, pixel_shuffle)