-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
408 lines (381 loc) · 16.2 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import torch.nn as nn
import math
import torch
import Res2Net as Pre_Res2Net
def default_conv(in_channels, out_channels, kernel_size, bias=True):
return nn.Conv2d(in_channels, out_channels, kernel_size, padding=(kernel_size // 2), bias=bias)
def dwt_init(x):
x01 = x[:, :, 0::2, :] / 2
x02 = x[:, :, 1::2, :] / 2
x1 = x01[:, :, :, 0::2]
x2 = x02[:, :, :, 0::2]
x3 = x01[:, :, :, 1::2]
x4 = x02[:, :, :, 1::2]
x_LL = x1 + x2 + x3 + x4
x_HL = -x1 - x2 + x3 + x4
x_LH = -x1 + x2 - x3 + x4
x_HH = x1 - x2 - x3 + x4
return x_LL, torch.cat((x_HL, x_LH, x_HH), 1)
class DWT(nn.Module):
def __init__(self):
super(DWT, self).__init__()
self.requires_grad = False
def forward(self, x):
return dwt_init(x)
class PALayer(nn.Module):
def __init__(self, channel):
super(PALayer, self).__init__()
self.pa = nn.Sequential(
nn.Conv2d(channel, channel // 8, 1, padding=0, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(channel // 8, 1, 1, padding=0, bias=True),
nn.Sigmoid()
)
def forward(self, x):
y = self.pa(x)
return x * y
class CALayer(nn.Module):
def __init__(self, channel):
super(CALayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.ca = nn.Sequential(
nn.Conv2d(channel, channel // 8, 1, padding=0, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(channel // 8, channel, 1, padding=0, bias=True),
nn.Sigmoid()
)
def forward(self, x):
y = self.avg_pool(x)
y = self.ca(y)
return x * y
class Bottle2neck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None, baseWidth=26, scale=4, stype='normal'):
super(Bottle2neck, self).__init__()
width = int(math.floor(planes * (baseWidth / 64.0)))
self.conv1 = nn.Conv2d(inplanes, width * scale, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(width * scale)
if scale == 1:
self.nums = 1
else:
self.nums = scale - 1
if stype == 'stage':
self.pool = nn.AvgPool2d(kernel_size=3, stride=stride, padding=1)
convs = []
bns = []
for i in range(self.nums):
convs.append(nn.Conv2d(width, width, kernel_size=3, stride=stride, padding=1, bias=False))
bns.append(nn.BatchNorm2d(width))
self.convs = nn.ModuleList(convs)
self.bns = nn.ModuleList(bns)
self.conv3 = nn.Conv2d(width * scale, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stype = stype
self.scale = scale
self.width = width
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
spx = torch.split(out, self.width, 1)
for i in range(self.nums):
if i == 0 or self.stype == 'stage':
sp = spx[i]
else:
sp = sp + spx[i]
sp = self.convs[i](sp)
sp = self.relu(self.bns[i](sp))
if i == 0:
out = sp
else:
out = torch.cat((out, sp), 1)
if self.scale != 1 and self.stype == 'normal':
out = torch.cat((out, spx[self.nums]), 1)
elif self.scale != 1 and self.stype == 'stage':
out = torch.cat((out, self.pool(spx[self.nums])), 1)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Res2Net(nn.Module):
def __init__(self, block, layers, baseWidth=26, scale=4, num_classes=1000):
self.inplanes = 64
super(Res2Net, self).__init__()
self.baseWidth = baseWidth
self.scale = scale
self.conv1 = nn.Sequential(
nn.Conv2d(3, 32, 3, 2, 1, bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, 3, 1, 1, bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 64, 3, 1, 1, bias=False)
)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.AvgPool2d(kernel_size=stride, stride=stride,
ceil_mode=True, count_include_pad=False),
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
stype='stage', baseWidth=self.baseWidth, scale=self.scale))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, baseWidth=self.baseWidth, scale=self.scale))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x_init = self.relu(x)
x = self.maxpool(x_init)
x_layer1 = self.layer1(x)
x_layer2 = self.layer2(x_layer1)
x_output= self.layer3(x_layer2)
return x_init, x_layer1, x_layer2, x_output
class CP_Attention_block(nn.Module):
def __init__(self, conv, dim, kernel_size):
super(CP_Attention_block, self).__init__()
self.conv1 = conv(dim, dim, kernel_size, bias=True)
self.act1 = nn.ReLU(inplace=True)
self.conv2 = conv(dim, dim, kernel_size, bias=True)
self.calayer = CALayer(dim)
self.palayer = PALayer(dim)
def forward(self, x):
res = self.act1(self.conv1(x))
res = res + x
res = self.conv2(res)
res = self.calayer(res)
res = self.palayer(res)
res += x
return res
class knowledge_adaptation_UNet(nn.Module):
def __init__(self):
super(knowledge_adaptation_UNet, self).__init__()
self.encoder = Res2Net(Bottle2neck, [3, 4, 23, 3], baseWidth=26, scale=4)
res2net101 = Pre_Res2Net.Res2Net(Bottle2neck, [3, 4, 23, 3], baseWidth=26, scale=4)
res2net101.load_state_dict(torch.load('./weights/res2net101_v1b_26w_4s-0812c246.pth'))
pretrained_dict = res2net101.state_dict()
model_dict = self.encoder.state_dict()
key_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(key_dict)
self.encoder.load_state_dict(model_dict)
self.up_block= nn.PixelShuffle(2)
self.attention0 = CP_Attention_block(default_conv, 1024, 3)
self.attention1 = CP_Attention_block(default_conv, 256, 3)
self.attention2 = CP_Attention_block(default_conv, 192, 3)
self.attention3 = CP_Attention_block(default_conv, 112, 3)
self.attention4 = CP_Attention_block(default_conv, 44, 3)
self.conv_process_1 = nn.Conv2d(44, 44, kernel_size=3,padding=1)
self.conv_process_2 = nn.Conv2d(44, 28, kernel_size=3,padding=1)
self.tail = nn.Sequential(nn.ReflectionPad2d(3), nn.Conv2d(28, 3, kernel_size=7, padding=0), nn.Tanh())
def forward(self, input):
x_inital, x_layer1, x_layer2, x_output = self.encoder(input)
x_mid = self.attention0(x_output)
x = self.up_block(x_mid)
x = self.attention1(x)
x = torch.cat((x, x_layer2), 1)
x = self.up_block(x)
x = self.attention2(x)
x = torch.cat((x, x_layer1), 1)
x = self.up_block(x)
x = self.attention3(x)
x = torch.cat((x, x_inital), 1)
x = self.up_block(x)
x = self.attention4(x)
x=self.conv_process_1(x)
out=self.conv_process_2(x)
return out
class DWT_transform(nn.Module):
def __init__(self, in_channels,out_channels):
super().__init__()
self.dwt = DWT()
self.conv1x1_low = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)
self.conv1x1_high = nn.Conv2d(in_channels*3, out_channels, kernel_size=1, padding=0)
def forward(self, x):
dwt_low_frequency,dwt_high_frequency = self.dwt(x)
dwt_low_frequency = self.conv1x1_low(dwt_low_frequency)
dwt_high_frequency = self.conv1x1_high(dwt_high_frequency)
return dwt_low_frequency,dwt_high_frequency
def blockUNet(in_c, out_c, name, transposed=False, bn=False, relu=True, dropout=False):
block = nn.Sequential()
if relu:
block.add_module('%s_relu' % name, nn.ReLU(inplace=True))
else:
block.add_module('%s_leakyrelu' % name, nn.LeakyReLU(0.2, inplace=True))
if not transposed:
block.add_module('%s_conv' % name, nn.Conv2d(in_c, out_c, 4, 2, 1, bias=False))
else:
block.add_module('%s_tconv' % name, nn.ConvTranspose2d(in_c, out_c, 4, 2, 1, bias=False))
if bn:
block.add_module('%s_bn' % name, nn.BatchNorm2d(out_c))
if dropout:
block.add_module('%s_dropout' % name, nn.Dropout2d(0.5, inplace=True))
return block
class dwt_UNet(nn.Module):
def __init__(self,output_nc=3, nf=16):
super(dwt_UNet, self).__init__()
layer_idx = 1
name = 'layer%d' % layer_idx
layer1 = nn.Sequential()
layer1.add_module(name, nn.Conv2d(16, nf-1, 4, 2, 1, bias=False))
layer_idx += 1
name = 'layer%d' % layer_idx
layer2 = blockUNet(nf, nf*2-2, name, transposed=False, bn=True, relu=False, dropout=False)
layer_idx += 1
name = 'layer%d' % layer_idx
layer3 = blockUNet(nf*2, nf*4-4, name, transposed=False, bn=True, relu=False, dropout=False)
layer_idx += 1
name = 'layer%d' % layer_idx
layer4 = blockUNet(nf*4, nf*8-8, name, transposed=False, bn=True, relu=False, dropout=False)
layer_idx += 1
name = 'layer%d' % layer_idx
layer5 = blockUNet(nf*8, nf*8-16, name, transposed=False, bn=True, relu=False, dropout=False)
layer_idx += 1
name = 'layer%d' % layer_idx
layer6 = blockUNet(nf*8, nf*8, name, transposed=False, bn=False, relu=False, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer6 = blockUNet(nf * 8, nf * 8, name, transposed=True, bn=True, relu=True, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer5 = blockUNet(nf * 16+16, nf * 8, name, transposed=True, bn=True, relu=True, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer4 = blockUNet(nf * 16+8, nf * 4, name, transposed=True, bn=True, relu=True, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer3 = blockUNet(nf * 8+4, nf * 2, name, transposed=True, bn=True, relu=True, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer2 = blockUNet(nf * 4+2, nf, name, transposed=True, bn=True, relu=True, dropout=False)
layer_idx -= 1
name = 'dlayer%d' % layer_idx
dlayer1 = blockUNet(nf * 2+1, nf * 2, name, transposed=True, bn=True, relu=True, dropout=False)
self.initial_conv=nn.Conv2d(3,16,3,padding=1)
self.bn1=nn.BatchNorm2d(16)
self.layer1 = layer1
self.DWT_down_0= DWT_transform(3,1)
self.layer2 = layer2
self.DWT_down_1 = DWT_transform(16, 2)
self.layer3 = layer3
self.DWT_down_2 = DWT_transform(32, 4)
self.layer4 = layer4
self.DWT_down_3 = DWT_transform(64, 8)
self.layer5 = layer5
self.DWT_down_4 = DWT_transform(128, 16)
self.layer6 = layer6
self.dlayer6 = dlayer6
self.dlayer5 = dlayer5
self.dlayer4 = dlayer4
self.dlayer3 = dlayer3
self.dlayer2 = dlayer2
self.dlayer1 = dlayer1
self.tail_conv1 = nn.Conv2d(48, 32, 3, padding=1, bias=True)
self.bn2=nn.BatchNorm2d(32)
self.tail_conv2 = nn.Conv2d(nf*2, output_nc, 3,padding=1, bias=True)
def forward(self, x):
conv_start=self.initial_conv(x)
conv_start=self.bn1(conv_start)
conv_out1 = self.layer1(conv_start)
dwt_low_0,dwt_high_0=self.DWT_down_0(x)
out1=torch.cat([conv_out1, dwt_low_0], 1)
conv_out2 = self.layer2(out1)
dwt_low_1,dwt_high_1= self.DWT_down_1(out1)
out2 = torch.cat([conv_out2, dwt_low_1], 1)
conv_out3 = self.layer3(out2)
dwt_low_2,dwt_high_2 = self.DWT_down_2(out2)
out3 = torch.cat([conv_out3, dwt_low_2], 1)
conv_out4 = self.layer4(out3)
dwt_low_3,dwt_high_3 = self.DWT_down_3(out3)
out4 = torch.cat([conv_out4, dwt_low_3], 1)
conv_out5 = self.layer5(out4)
dwt_low_4,dwt_high_4 = self.DWT_down_4(out4)
out5 = torch.cat([conv_out5, dwt_low_4], 1)
out6 = self.layer6(out5)
dout6 = self.dlayer6(out6)
Tout6_out5 = torch.cat([dout6, out5, dwt_high_4], 1)
Tout5 = self.dlayer5(Tout6_out5)
Tout5_out4 = torch.cat([Tout5, out4,dwt_high_3], 1)
Tout4 = self.dlayer4(Tout5_out4)
Tout4_out3 = torch.cat([Tout4, out3,dwt_high_2], 1)
Tout3 = self.dlayer3(Tout4_out3)
Tout3_out2 = torch.cat([Tout3, out2,dwt_high_1], 1)
Tout2 = self.dlayer2(Tout3_out2)
Tout2_out1 = torch.cat([Tout2, out1,dwt_high_0], 1)
Tout1 = self.dlayer1(Tout2_out1)
Tout1_outinit = torch.cat([Tout1, conv_start], 1)
tail1=self.tail_conv1(Tout1_outinit)
tail2=self.bn2(tail1)
dout1 = self.tail_conv2(tail2)
return dout1
class fusion_net(nn.Module):
def __init__(self):
super(fusion_net, self).__init__()
self.dwt_branch=dwt_UNet()
self.knowledge_adaptation_branch=knowledge_adaptation_UNet()
self.fusion = nn.Sequential(nn.ReflectionPad2d(3), nn.Conv2d(31, 3, kernel_size=7, padding=0), nn.Tanh())
def forward(self, input):
dwt_branch=self.dwt_branch(input)
knowledge_adaptation_branch=self.knowledge_adaptation_branch(input)
x = torch.cat([dwt_branch, knowledge_adaptation_branch], 1)
x = self.fusion(x)
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.LeakyReLU(0.2),
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2),
nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2),
nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2),
nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2),
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(512, 1024, kernel_size=1),
nn.LeakyReLU(0.2),
nn.Conv2d(1024, 1, kernel_size=1)
)
def forward(self, x):
batch_size = x.size(0)
return torch.sigmoid(self.net(x).view(batch_size))