-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
207 lines (158 loc) · 6.63 KB
/
model.py
File metadata and controls
207 lines (158 loc) · 6.63 KB
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
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision.models import resnet
import torch.nn.functional as F
from .eca_module import ECABlock
class BasicBlock(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, groups=1, bias=False):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, groups=groups, bias=bias)
self.bn1 = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size, 1, padding, groups=groups, bias=bias)
self.bn2 = nn.BatchNorm2d(out_planes)
self.downsample = None
if stride > 1:
self.downsample = nn.Sequential(nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_planes),)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Encoder(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, groups=1, bias=False):
super(Encoder, self).__init__()
self.block1 = BasicBlock(in_planes, out_planes, kernel_size, stride, padding, groups, bias)
self.block2 = BasicBlock(out_planes, out_planes, kernel_size, 1, padding, groups, bias)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
return x
class Dblock(nn.Module):
def __init__(self, channel):
super(Dblock, self).__init__()
self.dilate1 = nn.Conv2d(channel, channel, kernel_size=3, dilation=1, padding=1)
self.dilate2 = nn.Conv2d(channel, channel, kernel_size=3, dilation=2, padding=2)
self.dilate3 = nn.Conv2d(channel, channel, kernel_size=3, dilation=4, padding=4)
self.dilate4 = nn.Conv2d(channel, channel, kernel_size=3, dilation=8, padding=8)
# self.spm = SPBlock(channel, channel, norm_layer=nn.BatchNorm2d)
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
if m.bias is not None:
m.bias.data.zero_()
def forward(self, x):
dilate1_out = self.dilate1(x)
dilate2_out = self.dilate2(dilate1_out)
dilate3_out = self.dilate3(dilate2_out)
dilate4_out = self.dilate4(dilate3_out)
#spm_out = self.spm(x)
#out = (x + dilate1_out + dilate2_out + dilate3_out + dilate4_out) * spm_out
out = x + dilate1_out + dilate2_out + dilate3_out + dilate4_out
return out
class Decoder(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=False):
# TODO bias=True
super(Decoder, self).__init__()
self.conv1 = nn.Sequential(nn.Conv2d(in_planes, in_planes//4, 1, 1, 0, bias=bias),
nn.BatchNorm2d(in_planes//4),
nn.ReLU(inplace=True),)
self.tp_conv = nn.Sequential(nn.ConvTranspose2d(in_planes//4, in_planes//4, kernel_size, stride, padding, output_padding, bias=bias),
nn.BatchNorm2d(in_planes//4),
nn.ReLU(inplace=True),)
self.conv2 = nn.Sequential(nn.Conv2d(in_planes//4, out_planes, 1, 1, 0, bias=bias),
nn.BatchNorm2d(out_planes),
nn.ReLU(inplace=True),)
def forward(self, x):
x = self.conv1(x)
x = self.tp_conv(x)
x = self.conv2(x)
return x
class BR(nn.Module):
def __init__(self, out_c):
super(BR, self).__init__()
# self.bn = nn.BatchNorm2d(out_c)
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(out_c, out_c, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(out_c, out_c, kernel_size=3, padding=1)
def forward(self, x):
x_res = self.conv1(x)
x_res = self.relu(x_res)
x_res = self.conv2(x_res)
x = x + x_res
return x
class MSALNet(nn.Module):
"""
Generate Model Architecture
"""
def __init__(self, num_classes=1):
"""
Model initialization
:param x_n: number of input neurons
:type x_n: int
"""
super(MSALNet, self).__init__()
base = resnet.resnet34(pretrained=True)
self.in_block = nn.Sequential(
base.conv1,
base.bn1,
base.relu,
base.maxpool
)
self.encoder1 = base.layer1
self.encoder2 = base.layer2
self.encoder3 = base.layer3
self.encoder4 = base.layer4
self.decoder1 = Decoder(64, 64, 3, 1, 1, 0)
self.decoder2 = Decoder(128, 64, 3, 2, 1, 1)
self.decoder3 = Decoder(256, 128, 3, 2, 1, 1)
self.decoder4 = Decoder(512, 256, 3, 2, 1, 1)
self.dblock = Dblock(512)
# Classifier
self.tp_conv1 = nn.Sequential(nn.ConvTranspose2d(64, 32, 3, 2, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),)
self.conv2 = nn.Sequential(nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),)
self.tp_conv2 = nn.ConvTranspose2d(32, num_classes, 2, 2, 0)
self.ECABlock4 = ECABlock(512)
self.ECABlock3 = ECABlock(256)
self.ECABlock2 = ECABlock(128)
self.ECABlock1 = ECABlock(64)
self.br = BR(num_classes)
def forward(self, x):
# Initial block
x = self.in_block(x)
# Encoder blocks
e1 = self.encoder1(x)
e2 = self.encoder2(e1)
e3 = self.encoder3(e2)
e4 = self.encoder4(e3)
# center block
e4 = self.dblock(e4)
# Efficient channel Attention Block with Decoder
d4 = self.decoder4(e4) + e3
d4 = self.ECABlock4(d4)
d3 = self.decoder3(d4) + e2
d3 = self.ECABlock3(d3)
d2 = e1 + F.upsample(self.decoder2(d3), (e1.size(2), e1.size(3)), mode='bilinear')
#d2 = self.decoder2(d3) + e1
d2 = self.ECABlock2(d2)
d1 = self.decoder1(d2) + x
d1 = self.ECABlock1(d1)
# Classifier
y = self.tp_conv1(d1)
y = self.conv2(y)
y = self.tp_conv2(y)
y = self.br(y)
# y = self.lsm(y)
return F.sigmoid(y)