-
Notifications
You must be signed in to change notification settings - Fork 5
/
Model2.py
269 lines (210 loc) · 9.45 KB
/
Model2.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
from torchvision.models.densenet import DenseNet
from torch.utils.checkpoint import checkpoint_sequential
alambda = 0.5
class ResduialDeconv(nn.Module):
def __init__(self,scale=4):
super(ResduialDeconv, self).__init__()
self.scale = scale
self.upsamp1 = nn.Upsample(scale_factor=2)
self.deconv1 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=2, stride=2, padding=0, bias=False)
self.prelu1 = nn.PReLU()
self.conv1 = nn.Conv2d(in_channels=256,out_channels=256,kernel_size=1,stride=1,padding=0,bias=False)
self.upsamp2 = nn.Upsample(scale_factor=2)
self.deconv2 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=2, stride=2, padding=0, bias=False)
self.prelu2 = nn.PReLU()
self.conv2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=1, stride=1, padding=0, bias=False)
def forward(self,x):
upx1 = self.upsamp1(x)
upx1 = self.conv1(upx1)
x = self.deconv1(x)
x = self.prelu1(x)
x = x + upx1
upx2 = self.upsamp2(x)
upx2 = self.conv2(upx2)
x = self.deconv2(x)
x = self.prelu2(x)
x = x + upx2
return x
class _DenseLayer(nn.Sequential):
def __init__(self, num_input_features, growth_rate, bn_size, weightcount=1):
super(_DenseLayer, self).__init__()
self.add_module('relu1', nn.ReLU(inplace=True)),
self.add_module('conv1', nn.Conv2d(num_input_features, bn_size *
growth_rate, kernel_size=1, stride=1, bias=False)),
self.add_module('relu2', nn.ReLU(inplace=True)),
self.add_module('conv2', nn.Conv2d(bn_size * growth_rate, growth_rate,
kernel_size=3, stride=1, padding=1, bias=False)),
self.weightcount = weightcount
if weightcount != 0:
w = torch.ones(weightcount,1,1,1,1)
self.weighted = nn.Parameter(w)
def forward(self, x):
if self.weightcount == 0:
new_features = super(_DenseLayer, self).forward(x)
new_features = torch.unsqueeze(new_features, dim=1)
return new_features
else:
b,g,c,w,h = x .size()
xin = x.permute(1, 2, 3, 4,0)
# print torch.squeeze(self.weighted)
xin = self.weighted * xin
xin = xin.permute(4, 0, 1, 2,3)
xin = xin.view(b,g*c,w,h).contiguous()
new_features = super(_DenseLayer, self).forward(xin)
new_features = torch.unsqueeze(new_features,dim=1)
return torch.cat([x, new_features], 1)
class _Transition(nn.Sequential):
def __init__(self, num_input_features, num_output_features):
super(_Transition, self).__init__()
self.map = nn.Sequential(#nn.BatchNorm2d(num_input_features),
nn.ReLU(inplace=True),
nn.Conv2d(num_input_features, num_output_features,
kernel_size=1, stride=1, bias=False)
)
self.Capattention = nn.Sequential(
nn.Conv2d(num_output_features, 2 * num_output_features, 3, stride=1, padding=1, bias=False),
#nn.BatchNorm2d(2 * num_output_features),
nn.ReLU(inplace=True),
nn.Conv2d(2 * num_output_features, num_output_features, 3, stride=1, padding=1, bias=False),
#nn.BatchNorm2d(num_output_features),
nn.ReLU(inplace=True),
nn.Conv2d(num_output_features, num_output_features, 1, stride=1, padding=0, bias=False),
)
def forward(self,prevousinput, x):
b, g, c, w, h = x.size()
x = x.view(b,g*c,w,h)
x = self.map(x)
atten = torch.abs(x - prevousinput)
atten = self.Capattention(atten)
atten = F.tanh(atten)
atten = alambda * (x * atten)
x = atten + x
return x
class _DenseBlock(nn.Sequential):
def __init__(self, num_layers, num_input_features, bn_size, growth_rate):
super(_DenseBlock, self).__init__()
for i in range(num_layers):
if i ==0 :
layer = _DenseLayer(num_input_features, growth_rate, bn_size, weightcount=0)
else:
layer = _DenseLayer(i * growth_rate, growth_rate, bn_size,weightcount=i)
self.add_module('denselayer%d' % (i + 1), layer)
class SpatialAttention(nn.Sequential):
def __init__(self, num_input_features, num_output_features):
super(SpatialAttention, self).__init__()
self.bottleneck = nn.Sequential(
nn.ReLU(inplace=True),
nn.Conv2d(num_input_features, num_output_features,
kernel_size=1, stride=1, bias=False)
)
self.Capattention = nn.Sequential(
nn.Conv2d(num_output_features, 2 * num_output_features, 3, stride=1, padding=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(2 * num_output_features, num_output_features, 3, stride=1, padding=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(num_output_features, num_output_features, 1, stride=1, padding=0, bias=False),
)
def forward(self,prevousinput, x):
b, g, c, w, h = x.size()
x = x.view(b,g*c,w,h)
x = self.bottleneck(x)
atten = torch.abs(x - prevousinput)
atten = self.Capattention(atten)
atten = F.tanh(atten)
atten = alambda * (x * atten)
x = atten + x
return x
class WDB(nn.Sequential):
def __init__(self, num_layers, num_input_features, bn_size, growth_rate):
super(WDB, self).__init__()
for i in range(num_layers):
if i ==0 :
layer = _DenseLayer(num_input_features, growth_rate, bn_size, weightcount=0)
else:
layer = _DenseLayer(i * growth_rate, growth_rate, bn_size,weightcount=i)
self.add_module('denselayer%d' % (i + 1), layer)
class DenseNet(nn.Module):
def __init__(self, growth_rate=32, block_config=(6,12,48,32),
num_init_features=32, bn_size=2,
):
super(DenseNet, self).__init__()
# First convolution
self.features = nn.Sequential(OrderedDict([
('conv0', nn.Conv2d(3, num_init_features, kernel_size=3, stride=1, padding=1, bias=False)),
('relu0', nn.ReLU()),
]))
num_features = num_init_features
# WDB1
self.block1 = WDB(num_layers=block_config[0],
num_input_features=num_features,
bn_size=bn_size, growth_rate=growth_rate)
num_features = block_config[0] * growth_rate
# SA1
self.SA1 = SpatialAttention(num_input_features=num_features,
num_output_features=growth_rate)
num_outfeatures = growth_rate + num_init_features
INP = num_outfeatures
# WDB2
self.block2 = WDB(num_layers=block_config[1],
num_input_features=INP,
bn_size=bn_size, growth_rate=growth_rate)
num_features = block_config[1] * growth_rate
# SA2
self.SA2 = SpatialAttention(num_input_features=num_features,
num_output_features=INP)
num_outfeatures = INP + INP
INP = num_outfeatures
# WDB3
self.block3 = WDB(num_layers=block_config[2],
num_input_features=INP,
bn_size=bn_size, growth_rate=growth_rate)
num_features = block_config[2] * growth_rate
# SA3
self.SA3 = SpatialAttention(num_input_features=num_features,
num_output_features=INP)
num_outfeatures = INP + INP
INP = num_outfeatures
# WDB4
self.block4 = WDB(num_layers=block_config[3],
num_input_features=INP,
bn_size=bn_size, growth_rate=growth_rate)
num_features = block_config[3] * growth_rate
# SA4
self.SA4 = SpatialAttention(num_input_features=num_features,
num_output_features=INP)
num_outfeatures = INP + INP
# final bottleneck
self.bottleneck = nn.Sequential(
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num_outfeatures, out_channels=256, kernel_size=1, stride=1, padding=0, bias=False)
)
# residual deconv
self.deconv = ResduialDeconv()
# reconstruction
self.reconstruction = nn.Conv2d(in_channels=256, out_channels=3, kernel_size=3, stride=1, padding=1, bias=False)
def forward(self, x):
primary = self.features(x)
pre_input = primary
b1 = self.block1(primary)
t1 = self.trans1(pre_input, b1)
concat = torch.cat([pre_input, t1], 1)
pre_input1 = concat
b2 = self.block2(concat)
t2 = self.trans2(pre_input1, b2)
concat = torch.cat([pre_input1, t2], 1)
pre_input2 = concat
b3 = self.block3(concat)
t3 = self.trans3(pre_input2, b3)
concat = torch.cat([pre_input2, t3], 1)
pre_input3 = concat
b4 = self.block4(concat)
t4 = self.trans4(pre_input3, b4)
concat = torch.cat([pre_input3, t4], 1)
bottleneck = self.bottleneck(concat)
de = self.deconv(bottleneck)
out = self.reconstruction(de)
return out