-
Notifications
You must be signed in to change notification settings - Fork 23
/
net.py
179 lines (144 loc) · 6.48 KB
/
net.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import fusion_strategy
class UpsampleReshape_eval(torch.nn.Module):
def __init__(self):
super(UpsampleReshape_eval, self).__init__()
self.up = nn.Upsample(scale_factor=2)
def forward(self, x1, x2):
x2 = self.up(x2)
shape_x1 = x1.size()
shape_x2 = x2.size()
left = 0
right = 0
top = 0
bot = 0
if shape_x1[3] != shape_x2[3]:
lef_right = shape_x1[3] - shape_x2[3]
if lef_right%2 is 0.0:
left = int(lef_right/2)
right = int(lef_right/2)
else:
left = int(lef_right / 2)
right = int(lef_right - left)
if shape_x1[2] != shape_x2[2]:
top_bot = shape_x1[2] - shape_x2[2]
if top_bot%2 is 0.0:
top = int(top_bot/2)
bot = int(top_bot/2)
else:
top = int(top_bot / 2)
bot = int(top_bot - top)
reflection_padding = [left, right, top, bot]
reflection_pad = nn.ReflectionPad2d(reflection_padding)
x2 = reflection_pad(x2)
return x2
# Convolution operation
class ConvLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, is_last=False):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
self.dropout = nn.Dropout2d(p=0.5)
self.is_last = is_last
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
if self.is_last is False:
out = F.relu(out, inplace=True)
return out
# light version
class DenseBlock_light(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(DenseBlock_light, self).__init__()
out_channels_def = int(in_channels / 2)
# out_channels_def = out_channels
denseblock = []
denseblock += [ConvLayer(in_channels, out_channels_def, kernel_size, stride),
ConvLayer(out_channels_def, out_channels, 1, stride)]
self.denseblock = nn.Sequential(*denseblock)
def forward(self, x):
out = self.denseblock(x)
return out
# NestFuse network - light, no desnse
class NestFuse_autoencoder(nn.Module):
def __init__(self, nb_filter, input_nc=1, output_nc=1, deepsupervision=True):
super(NestFuse_autoencoder, self).__init__()
self.deepsupervision = deepsupervision
block = DenseBlock_light
output_filter = 16
kernel_size = 3
stride = 1
self.pool = nn.MaxPool2d(2, 2)
self.up = nn.Upsample(scale_factor=2)
self.up_eval = UpsampleReshape_eval()
# encoder
self.conv0 = ConvLayer(input_nc, output_filter, 1, stride)
self.DB1_0 = block(output_filter, nb_filter[0], kernel_size, 1)
self.DB2_0 = block(nb_filter[0], nb_filter[1], kernel_size, 1)
self.DB3_0 = block(nb_filter[1], nb_filter[2], kernel_size, 1)
self.DB4_0 = block(nb_filter[2], nb_filter[3], kernel_size, 1)
# self.DB5_0 = block(nb_filter[3], nb_filter[4], kernel_size, 1)
# decoder
self.DB1_1 = block(nb_filter[0] + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_1 = block(nb_filter[1] + nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB3_1 = block(nb_filter[2] + nb_filter[3], nb_filter[2], kernel_size, 1)
self.DB1_2 = block(nb_filter[0] * 2 + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_2 = block(nb_filter[1] * 2 + nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB1_3 = block(nb_filter[0] * 3 + nb_filter[1], nb_filter[0], kernel_size, 1)
if self.deepsupervision:
self.conv1 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv2 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv3 = ConvLayer(nb_filter[0], output_nc, 1, stride)
else:
self.conv_out = ConvLayer(nb_filter[0], output_nc, 1, stride)
def encoder(self, input):
x = self.conv0(input)
x1_0 = self.DB1_0(x)
x2_0 = self.DB2_0(self.pool(x1_0))
x3_0 = self.DB3_0(self.pool(x2_0))
x4_0 = self.DB4_0(self.pool(x3_0))
# x5_0 = self.DB5_0(self.pool(x4_0))
return [x1_0, x2_0, x3_0, x4_0]
def fusion(self, en1, en2, p_type):
# attention weight
fusion_function = fusion_strategy.attention_fusion_weight
f1_0 = fusion_function(en1[0], en2[0], p_type)
f2_0 = fusion_function(en1[1], en2[1], p_type)
f3_0 = fusion_function(en1[2], en2[2], p_type)
f4_0 = fusion_function(en1[3], en2[3], p_type)
return [f1_0, f2_0, f3_0, f4_0]
def decoder_train(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up(f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up(f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up(x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up(f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up(x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up(x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
# output4 = self.conv4(x1_4)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]
def decoder_eval(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up_eval(f_en[0], f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up_eval(f_en[1], f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up_eval(f_en[0], x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up_eval(f_en[2], f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up_eval(f_en[1], x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up_eval(f_en[0], x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]