-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
339 lines (297 loc) · 12 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import math
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
class sub_pixel(nn.Module):
def __init__(self, scale, act=False):
super(sub_pixel, self).__init__()
modules = []
modules.append(nn.PixelShuffle(scale))
self.body = nn.Sequential(*modules)
def forward(self, x):
x = self.body(x)
return x
class make_dense(nn.Module):
def __init__(self, nChannels, growthRate, kernel_size=3):
super(make_dense, self).__init__()
self.conv = nn.Conv2d(nChannels, growthRate, kernel_size=kernel_size, padding=(kernel_size-1)//2, bias=False)
def forward(self, x):
out = F.relu(self.conv(x))
out = torch.cat((x, out), 1)
return out
class CALayer(nn.Module):
def __init__(self, channel, reduction=16):
super(CALayer, self).__init__()
# global average pooling: feature --> point
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# feature channel downscale and upscale --> channel weight
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.avg_pool(x)
y = self.conv_du(y)
return x * y
class make_dense_ca(nn.Module):
def __init__(self, nChannels, growthRate, kernel_size=3):
super(make_dense_ca, self).__init__()
self.conv = nn.Conv2d(nChannels, growthRate, kernel_size=kernel_size, padding=(kernel_size - 1) // 2,
bias=False)
self.CA = CALayer(growthRate)
def forward(self, x):
out = F.relu(self.conv(x))
out = self.CA(out)
out = torch.cat((x, out), 1)
return out
# Residual dense block (RDB) architecture
class RDB_CA(nn.Module):
def __init__(self, nChannels, nDenselayer, growthRate):
super(RDB_CA, self).__init__()
nChannels_ = nChannels
modules = []
for i in range(nDenselayer):
modules.append(make_dense_ca(nChannels_, growthRate))
nChannels_ += growthRate
self.dense_layers = nn.Sequential(*modules)
self.conv_1x1 = nn.Conv2d(nChannels_, nChannels, kernel_size=1, padding=0, bias=False)
def forward(self, x):
out = self.dense_layers(x)
out = self.conv_1x1(out)
out = out + x
return out
class RDN_CA(nn.Module):
def __init__(self, args):
super(RDN_CA, self).__init__()
nChannel = args.nChannel
nDenselayer = args.nDenselayer
nBlock = args.nBlock
nFeat = args.nFeat
scale = args.scale
growthRate = args.growthRate
self.args = args
# F-1
self.conv1 = nn.Conv2d(nChannel, nFeat, kernel_size=3, padding=1, bias=True)
# F0
self.conv2 = nn.Conv2d(nFeat, nFeat, kernel_size=3, padding=1, bias=True)
# RDBs 3
self.RDB1 = RDB_CA(nFeat, nDenselayer, growthRate)
'''
self.RDB2 = RDB(nFeat, nDenselayer, growthRate)
self.RDB3 = RDB(nFeat, nDenselayer, growthRate)
'''
# global feature fusion (GFF)
self.GFF_1x1 = nn.Conv2d(nFeat*nBlock, nFeat, kernel_size=1, padding=0, bias=True)
self.GFF_3x3 = nn.Conv2d(nFeat, nFeat, kernel_size=3, padding=1, bias=True)
# Upsampler
self.conv_up = nn.Conv2d(nFeat, nFeat*scale*scale, kernel_size=3, padding=1, bias=True)
self.upsample = sub_pixel(scale)
# conv
self.conv3 = nn.Conv2d(nFeat, nChannel, kernel_size=3, padding=1, bias=True)
#self.CAT = torch.cat(feature, 1)
def make_layer(self, block, num_of_layer):
layers = []
for _ in range(num_of_layer):
layers.append(block(self.args.nFeat, self.args.nDenselayer, self.args.growthRate))
return nn.Sequential(*layers)
def forward(self, x):
#print(x.data.numpy().shape)
F_ = self.conv1(x)
#print(F_.data.numpy().shape)
F_0 = self.conv2(F_)
#print(F_0.data.numpy().shape)
'''
F_1 = self.RDB1(F_0)
F_2 = self.RDB2(F_1)
F_3 = self.RDB3(F_2)
FF = torch.cat((F_1, F_2, F_3), 1)
'''
feature = []
for i in range(self.args.nBlock):
F_0 = self.RDB1(F_0)
feature.append(F_0)
#print(F_0.data.numpy().shape)
#print(FF.data.numpy().shape)
FF = torch.cat(feature, 1)
#print(FF.data.numpy().shape)
FdLF = self.GFF_1x1(FF)
FGF = self.GFF_3x3(FdLF)
FDF = FGF + F_
us = self.conv_up(FDF)
us = self.upsample(us)
#output = self.conv3(us)
return us
# Residual dense block (RDB) architecture
class RDB(nn.Module):
def __init__(self, nChannels, nDenselayer, growthRate):
super(RDB, self).__init__()
nChannels_ = nChannels
modules = []
for i in range(nDenselayer):
modules.append(make_dense(nChannels_, growthRate))
nChannels_ += growthRate
self.dense_layers = nn.Sequential(*modules)
self.conv_1x1 = nn.Conv2d(nChannels_, nChannels, kernel_size=1, padding=0, bias=False)
def forward(self, x):
out = self.dense_layers(x)
out = self.conv_1x1(out)
out = out + x
return out
# Residual Dense Network
class RDN(nn.Module):
def __init__(self, args):
super(RDN, self).__init__()
nChannel = args.nChannel
nDenselayer = args.nDenselayer
nBlock = args.nBlock
nFeat = args.nFeat
scale = args.scale
growthRate = args.growthRate
self.args = args
# F-1
self.conv1 = nn.Conv2d(nChannel, nFeat, kernel_size=3, padding=1, bias=True)
# F0
self.conv2 = nn.Conv2d(nFeat, nFeat, kernel_size=3, padding=1, bias=True)
# RDBs 3
self.RDB1 = RDB(nFeat, nDenselayer, growthRate)
'''
self.RDB2 = RDB(nFeat, nDenselayer, growthRate)
self.RDB3 = RDB(nFeat, nDenselayer, growthRate)
'''
# global feature fusion (GFF)
self.GFF_1x1 = nn.Conv2d(nFeat*nBlock, nFeat, kernel_size=1, padding=0, bias=True)
self.GFF_3x3 = nn.Conv2d(nFeat, nFeat, kernel_size=3, padding=1, bias=True)
# Upsampler
self.conv_up = nn.Conv2d(nFeat, nFeat*scale*scale, kernel_size=3, padding=1, bias=True)
self.upsample = sub_pixel(scale)
# conv
self.conv3 = nn.Conv2d(nFeat, nChannel, kernel_size=3, padding=1, bias=True)
#self.CAT = torch.cat(feature, 1)
def make_layer(self, block, num_of_layer):
layers = []
for _ in range(num_of_layer):
layers.append(block(self.args.nFeat, self.args.nDenselayer, self.args.growthRate))
return nn.Sequential(*layers)
def forward(self, x):
#print(x.data.numpy().shape)
F_ = self.conv1(x)
#print(F_.data.numpy().shape)
F_0 = self.conv2(F_)
#print(F_0.data.numpy().shape)
'''
F_1 = self.RDB1(F_0)
F_2 = self.RDB2(F_1)
F_3 = self.RDB3(F_2)
FF = torch.cat((F_1, F_2, F_3), 1)
'''
feature = []
for i in range(self.args.nBlock):
F_0 = self.RDB1(F_0)
feature.append(F_0)
#print(F_0.data.numpy().shape)
#print(FF.data.numpy().shape)
FF = torch.cat(feature, 1)
#print(FF.data.numpy().shape)
FdLF = self.GFF_1x1(FF)
FGF = self.GFF_3x3(FdLF)
FDF = FGF + F_
us = self.conv_up(FDF)
us = self.upsample(us)
#output = self.conv3(us)
return us
class _Residual_Block(nn.Module):
def __init__(self):
super(_Residual_Block, self).__init__()
self.conv1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=0, bias=False)
self.bn2 = nn.BatchNorm2d(64)
# np.transpose(x,(2,3,0,1)) = torch.transpose(torch.transpose(x,0,2),1,3)
def forward(self, x):
identity_data = x
output = self.relu(self.bn1(self.conv1(F.pad(x, (1, 1, 1, 1), "replicate"))))
# print("conv1 output for conv2: ")
# print output.shape
output = self.bn2(self.conv2(F.pad(output, (1, 1, 1, 1), "replicate")))
output = torch.add(output, identity_data)
return output
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv_input = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=9, stride=1, padding=0, bias=False)
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.residual = self.make_layer(_Residual_Block, 16)
self.conv_mid = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=0, bias=False)
self.bn_mid = nn.BatchNorm2d(64)
# self.upscale4x = nn.Sequential(
# nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=0, bias=False),
# nn.PixelShuffle(2),
# nn.LeakyReLU(0.2, inplace=True),
# nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=0, bias=False),
# nn.PixelShuffle(2),
# nn.LeakyReLU(0.2, inplace=True),
# )
self.upscale4x_a = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=0, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
)
self.upscale4x_b = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=0, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
)
self.conv_output = nn.Conv2d(in_channels=64, out_channels=6, kernel_size=9, stride=1, padding=0, bias=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
# init.orthogonal(m.weight, math.sqrt(2))
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.zero_()
def make_layer(self, block, num_of_layer):
layers = []
for _ in range(num_of_layer):
layers.append(block())
return nn.Sequential(*layers)
def forward(self, x):
out = self.relu(self.conv_input(F.pad(x, (4, 4, 4, 4), "replicate")))
# print("out for conv_mid: ")
# print out.shape
residual = out
out = self.residual(out)
out = self.bn_mid(self.conv_mid(F.pad(out, (1, 1, 1, 1), "replicate")))
# print("out for upscale4x: ")
# print out.shape
out = torch.add(out, residual)
# out = self.upscale4x_11(F.pad(out,(1,1,1,1),"replicate"))
out = self.upscale4x_a(F.pad(out, (1, 1, 1, 1), "replicate"))
out = self.upscale4x_b(F.pad(out, (1, 1, 1, 1), "replicate"))
# print("out for conv_output: ")
# print out.shape
#out = self.conv_output(F.pad(out, (4, 4, 4, 4), "replicate"))
return out
class final_layer(nn.Module):
def __init__(self, out_channels):
super(final_layer, self).__init__()
self.conv_output = nn.Conv2d(in_channels=64, out_channels=out_channels, kernel_size=9, stride=1, padding=0, bias=False)
def forward(self, x):
out = self.conv_output(F.pad(x, (4, 4, 4, 4), "replicate"))
return out
class final_layer1(nn.Module):
def __init__(self, out_channels):
super(final_layer1, self).__init__()
self.conv_output = nn.Conv2d(in_channels=64, out_channels=out_channels, kernel_size=3, stride=1, padding=1, bias=True)
def forward(self, x):
out = self.conv_output(x)
return out