-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocks.py
314 lines (255 loc) · 13.3 KB
/
blocks.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import torch
import torch.nn as nn
import math
from collections import OrderedDict
import sys
import torch.nn.functional as F
def activation(act_type='relu', inplace=True, slope=0.2, n_prelu=1):
act_type = act_type.lower()
layer = None
if act_type == 'relu':
layer = nn.ReLU(inplace)
elif act_type == 'lrelu':
layer = nn.LeakyReLU(slope, inplace)
elif act_type == 'prelu':
layer = nn.PReLU(num_parameters=n_prelu, init=slope)
else:
raise NotImplementedError('[ERROR] Activation layer [%s] is not implemented!'%act_type)
return layer
def norm(n_feature, norm_type='bn'):
norm_type = norm_type.lower()
layer = None
if norm_type =='bn':
layer = nn.BatchNorm2d(n_feature)
else:
raise NotImplementedError('[ERROR] Normalization layer [%s] is not implemented!'%norm_type)
return layer
def pad(pad_type, padding):
pad_type = pad_type.lower()
if padding == 0:
return None
layer = None
if pad_type == 'reflect':
layer = nn.ReflectionPad2d(padding)
elif pad_type == 'replicate':
layer = nn.ReplicationPad2d(padding)
else:
raise NotImplementedError('[ERROR] Padding layer [%s] is not implemented!'%pad_type)
return layer
def sequential(*args):
if len(args) == 1:
if isinstance(args[0], OrderedDict):
raise NotImplementedError('[ERROR] %s.sequential() does not support OrderedDict'%sys.modules[__name__])
else:
return args[0]
modules = []
for module in args:
if isinstance(module, nn.Sequential):
for submodule in module:
modules.append(submodule)
elif isinstance(module, nn.Module):
modules.append(module)
return nn.Sequential(*modules)
def ConvBlock(in_channels, out_channels, kernel_size, stride=1, dilation=1, bias=True, valid_padding=True, padding=0,\
act_type='relu', norm_type='bn', pad_type='zero', mode='CNA'):
assert (mode in ['CNA', 'NAC']), '[ERROR] Wrong mode in [%s]!'%sys.modules[__name__]
if valid_padding:
padding = get_valid_padding(kernel_size, dilation)
else:
pass
p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias)
if mode == 'CNA':
act = activation(act_type) if act_type else None
n = norm(out_channels, norm_type) if norm_type else None
return sequential(p, conv, n, act)
elif mode == 'NAC':
act = activation(act_type, inplace=False) if act_type else None
n = norm(in_channels, norm_type) if norm_type else None
return sequential(n, act, p, conv)
class MeanShift(nn.Conv2d):
def __init__(self, rgb_mean, rgb_std, sign=-1):
super(MeanShift, self).__init__(3, 3, kernel_size=1)
std = torch.Tensor(rgb_std)
self.weight.data = torch.eye(3).view(3, 3, 1, 1)
self.weight.data.div_(std.view(3, 1, 1, 1))
self.bias.data = sign * 255. * torch.Tensor(rgb_mean)
self.bias.data.div_(std)
self.requires_grad = False
################
# Advanced blocks
################
class ResBlock(nn.Module):
def __init__(self, in_channel, out_channle, mid_channel, kernel_size, stride=1, valid_padding=True, padding=0, dilation=1, bias=True, \
pad_type='zero', norm_type='bn', act_type='relu', mode='CNA', res_scale=1):
super(ResBlock, self).__init__()
conv0 = ConvBlock(in_channel, mid_channel, kernel_size, stride, dilation, bias, valid_padding, padding, act_type, norm_type, pad_type, mode)
act_type = None
norm_type = None
conv1 = ConvBlock(mid_channel, out_channle, kernel_size, stride, dilation, bias, valid_padding, padding, act_type, norm_type, pad_type, mode)
self.res = sequential(conv0, conv1)
self.res_scale = res_scale
def forward(self, x):
res = self.res(x).mul(self.res_scale)
return x + res
class UpprojBlock(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, stride=1, valid_padding=False, padding=0, bias=True, \
pad_type='zero', norm_type=None, act_type='prelu'):
super(UpprojBlock, self).__init__()
self.deconv_1 = DeconvBlock(in_channel, out_channel, kernel_size, stride=stride, \
padding=padding, norm_type=norm_type, act_type=act_type)
self.conv_1 = ConvBlock(out_channel, out_channel, kernel_size, stride=stride, padding=padding, \
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
self.deconv_2 = DeconvBlock(out_channel, out_channel, kernel_size, stride=stride, \
padding=padding, norm_type=norm_type, act_type=act_type)
def forward(self, x):
H_0_t = self.deconv_1(x)
L_0_t = self.conv_1(H_0_t)
H_1_t = self.deconv_2(L_0_t-x)
return H_0_t + H_1_t
class D_UpprojBlock(torch.nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, stride=1, valid_padding=False, padding=0, bias=True, \
pad_type='zero', norm_type=None, act_type='prelu'):
super(D_UpprojBlock, self).__init__()
self.conv_1 = ConvBlock(in_channel, out_channel, kernel_size=1, norm_type=norm_type, act_type=act_type)
self.deconv_1 = DeconvBlock(out_channel, out_channel, kernel_size, stride=stride, \
padding=padding, norm_type=norm_type, act_type=act_type)
self.conv_2 = ConvBlock(out_channel, out_channel, kernel_size, stride=stride, padding=padding, \
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
self.deconv_2 = DeconvBlock(out_channel, out_channel, kernel_size, stride=stride, \
padding=padding, norm_type=norm_type, act_type=act_type)
def forward(self, x):
x = self.conv_1(x)
H_0_t = self.deconv_1(x)
L_0_t = self.conv_2(H_0_t)
H_1_t = self.deconv_2(L_0_t - x)
return H_1_t + H_0_t
class DownprojBlock(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, stride=1, valid_padding=True,
padding=0, dilation=1, bias=True, \
pad_type='zero', norm_type=None, act_type='prelu', mode='CNA', res_scale=1):
super(DownprojBlock, self).__init__()
self.conv_1 = ConvBlock(in_channel, out_channel, kernel_size, stride=stride, padding=padding,
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
self.deconv_1 = DeconvBlock(out_channel, out_channel, kernel_size, stride=stride,
padding=padding, norm_type=norm_type, act_type=act_type)
self.conv_2 = ConvBlock(out_channel, out_channel, kernel_size, stride=stride, padding=padding,
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
def forward(self, x):
L_0_t = self.conv_1(x)
H_0_t = self.deconv_1(L_0_t)
L_1_t = self.conv_2(H_0_t - x)
return L_0_t + L_1_t
class D_DownprojBlock(torch.nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, stride=1, valid_padding=False, padding=0, bias=True, \
pad_type='zero', norm_type=None, act_type='prelu'):
super(D_DownprojBlock, self).__init__()
self.conv_1 = ConvBlock(in_channel, out_channel, kernel_size=1, norm_type=norm_type, act_type=act_type)
self.conv_2 = ConvBlock(out_channel, out_channel, kernel_size, stride=stride, padding=padding, \
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
self.deconv_1 = DeconvBlock(out_channel, out_channel, kernel_size, stride=stride, \
padding=padding, norm_type=norm_type, act_type=act_type)
self.conv_3 = ConvBlock(out_channel, out_channel, kernel_size, stride=stride, padding=padding, \
valid_padding=valid_padding, norm_type=norm_type, act_type=act_type)
def forward(self, x):
x = self.conv_1(x)
L_0_t = self.conv_2(x)
H_0_t = self.deconv_1(L_0_t)
L_1_t = self.conv_3(H_0_t - x)
return L_1_t + L_0_t
class DensebackprojBlock(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, bp_stages, stride=1, valid_padding=True,
padding=0, dilation=1, bias=True, \
pad_type='zero', norm_type=None, act_type='prelu', mode='CNA', res_scale=1):
super(DensebackprojBlock, self).__init__()
# This is an example that I have to create nn.ModuleList() to append a sequence of models instead of list()
self.upproj = nn.ModuleList()
self.downproj = nn.ModuleList()
self.bp_stages = bp_stages
self.upproj.append(UpprojBlock(in_channel, out_channel, kernel_size, stride=stride, valid_padding=False,
padding=padding, norm_type=norm_type, act_type=act_type))
for index in range(self.bp_stages - 1):
if index < 1:
self.upproj.append(UpprojBlock(out_channel, out_channel, kernel_size, stride=stride, valid_padding=False,
padding=padding, norm_type=norm_type, act_type=act_type))
else:
uc = ConvBlock(out_channel*(index+1), out_channel, kernel_size=1, norm_type=norm_type, act_type=act_type)
u = UpprojBlock(out_channel, out_channel, kernel_size, stride=stride, valid_padding=False,
padding=padding, norm_type=norm_type, act_type=act_type)
self.upproj.append(sequential(uc, u))
if index < 1:
self.downproj.append(DownprojBlock(out_channel, out_channel, kernel_size, stride=stride, valid_padding=False,
padding=padding, norm_type=norm_type, act_type=act_type))
else:
dc = ConvBlock(out_channel*(index+1), out_channel, kernel_size=1, norm_type=norm_type, act_type=act_type)
d = DownprojBlock(out_channel, out_channel, kernel_size, stride=stride, valid_padding=False,
padding=padding, norm_type=norm_type, act_type=act_type)
self.downproj.append(sequential(dc, d))
def forward(self, x):
low_features = []
high_features = []
H = self.upproj[0](x)
high_features.append(H)
for index in range(self.bp_stages - 1):
if index < 1:
L = self.downproj[index](H)
low_features.append(L)
H = self.upproj[index+1](L)
high_features.append(H)
else:
H_concat = torch.cat(tuple(high_features), 1)
L = self.downproj[index](H_concat)
low_features.append(L)
L_concat = torch.cat(tuple(low_features), 1)
H = self.upproj[index+1](L_concat)
high_features.append(H)
output = torch.cat(tuple(high_features), 1)
return output
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
class ConcatBlock(nn.Module):
def __init__(self, submodule):
super(ConcatBlock, self).__init__()
self.sub = submodule
def forward(self, x):
output = torch.cat((x, self.sub(x)), 1)
return output
################
# Upsampler
################
def UpsampleConvBlock(upscale_factor, in_channels, out_channels, kernel_size, stride, valid_padding=True, padding=0, bias=True,\
pad_type='zero', act_type='relu', norm_type=None, mode='nearest'):
upsample = nn.Upsample(scale_factor=upscale_factor, mode=mode)
conv = ConvBlock(in_channels, out_channels, kernel_size, stride, bias=bias, valid_padding=valid_padding, padding=padding, \
pad_type=pad_type, act_type=act_type, norm_type=norm_type)
return sequential(upsample, conv)
def PixelShuffleBlock():
pass
def DeconvBlock(in_channels, out_channels, kernel_size, stride=1, dilation=1, bias=True, padding=0, \
act_type='relu', norm_type='bn', pad_type='zero', mode='CNA'):
assert (mode in ['CNA', 'NAC']), '[ERROR] Wrong mode in [%s]!'%sys.modules[__name__]
p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
deconv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, dilation=dilation, bias=bias)
if mode == 'CNA':
act = activation(act_type) if act_type else None
n = norm(out_channels, norm_type) if norm_type else None
return sequential(p, deconv, n, act)
elif mode == 'NAC':
act = activation(act_type, inplace=False) if act_type else None
n = norm(in_channels, norm_type) if norm_type else None
return sequential(n, act, p, deconv)
################
# helper funcs
################
def get_valid_padding(kernel_size, dilation):
"""
Padding value to remain feature size.
"""
kernel_size = kernel_size + (kernel_size-1)*(dilation-1)
padding = (kernel_size-1) // 2
return padding