-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.py
112 lines (90 loc) · 3.65 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import fusion_strategy
# 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.normalize(out)
out = F.relu(out, inplace=True)
#out = self.dropout(out)
return out
# Dense convolution unit
class DenseConv2d(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(DenseConv2d, self).__init__()
self.dense_conv = ConvLayer(in_channels, out_channels, kernel_size, stride)
def forward(self, x):
out = self.dense_conv(x)
out = torch.cat([x, out], 1)
return out
# Dense Block unit
class DenseBlock(torch.nn.Module):
def __init__(self, in_channels, kernel_size, stride):
super(DenseBlock, self).__init__()
out_channels_def = 16
denseblock = []
denseblock += [DenseConv2d(in_channels, out_channels_def, kernel_size, stride),
DenseConv2d(in_channels+out_channels_def, out_channels_def, kernel_size, stride),
DenseConv2d(in_channels+out_channels_def*2, out_channels_def, kernel_size, stride)]
self.denseblock = nn.Sequential(*denseblock)
def forward(self, x):
out = self.denseblock(x)
return out
# DenseFuse network
class DenseFuse_net(nn.Module):
def __init__(self, input_nc=1, output_nc=1):
super(DenseFuse_net, self).__init__()
denseblock = DenseBlock
nb_filter = [16, 64, 32, 16]
kernel_size = 3
stride = 1
# encoder1
self.conv1 = ConvLayer(input_nc, nb_filter[0], kernel_size, stride)
self.DB1 = denseblock(nb_filter[0], kernel_size, stride)
# encoder2
self.conv1_1 = ConvLayer(input_nc, nb_filter[0], kernel_size, stride)
self.DB2 = denseblock(nb_filter[0], kernel_size, stride)
# decoder
self.conv2 = ConvLayer(nb_filter[1], nb_filter[1], kernel_size, stride)
self.conv3 = ConvLayer(nb_filter[1], nb_filter[2], kernel_size, stride)
self.conv4 = ConvLayer(nb_filter[2], nb_filter[3], kernel_size, stride)
self.conv5 = ConvLayer(nb_filter[3], output_nc, kernel_size, stride)
def encoder1(self, input):
x1 = self.conv1(input)
x_DB1 = self.DB1(x1)
return [x_DB1]
def encoder2(self, input):
x2 = self.conv1_1(input)
x_DB2 = self.DB2(x2)
return [x_DB2]
# def fusion(self, en1, en2, strategy_type='addition'):
# # addition
# if strategy_type is 'attention_weight':
# # attention weight
# fusion_function = fusion_strategy.attention_fusion_weight
# else:
# fusion_function = fusion_strategy.addition_fusion
#
# f_0 = fusion_function(en1[0], en2[0])
# return [f_0]
def fusion(self, en1, en2, strategy_type='addition'):
f_0 = (en1[0] + en2[0])/2
return [f_0]
def decoder(self, f_en):
x2 = self.conv2(f_en[0])
x3 = self.conv3(x2)
x4 = self.conv4(x3)
output = self.conv5(x4)
return [output]