-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
220 lines (185 loc) · 7.79 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : hao zhang
# @File : model.py
import torch
import torch.nn as nn
from functools import partial
import torch.nn.functional as F
nonlinearity = partial(F.relu, inplace=True)
def downsample():
return nn.MaxPool3d(kernel_size=2, stride=2)
def deconv(in_channels, out_channels):
return nn.ConvTranspose3d(in_channels, out_channels, kernel_size=2, stride=2)
def initialize_weights(*models):
for model in models:
for m in model.modules():
if isinstance(m, nn.Conv3d) or isinstance(m, nn.Linear):
nn.init.kaiming_normal(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm3d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class ResDecoder(nn.Module):
def __init__(self, in_channels):
super(ResDecoder, self).__init__()
self.conv1 = nn.Conv3d(in_channels, in_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm3d(in_channels)
self.conv2 = nn.Conv3d(in_channels, in_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm3d(in_channels)
self.relu = nn.ReLU(inplace=False)
self.conv1x1 = nn.Conv3d(in_channels, in_channels, kernel_size=1)
def forward(self, x):
residual = self.conv1x1(x)
out = self.relu(self.bn1(self.conv1(x)))
out = self.relu(self.bn2(self.conv2(out)))
out += residual
out = self.relu(out)
return out
class SFConv(nn.Module):
def __init__(self, features, M=2, r=4, L=32):
""" Constructor
Args:
features: input channel dimensionality.
WH: input spatial dimensionality, used for GAP kernel size.
M: the number of branchs.
G: num of convolution groups.
r: the radio for compute d, the length of z.
stride: stride, default 1.
L: the minimum dim of the vector z in paper, default 32.
"""
super(SFConv, self).__init__()
d = max(int(features / r), L)
self.M = M
self.features = features
# self.convs = nn.ModuleList([])
# for i in range(M):
# self.convs.append(nn.Sequential(
# nn.Conv2d(features, features, kernel_size=3 + i * 2, stride=stride, padding=1 + i, groups=G),
# nn.BatchNorm2d(features),
# nn.ReLU(inplace=False)
# ))
# self.gap = nn.AvgPool2d(int(WH/stride))
self.fc = nn.Linear(features, d)
self.fcs = nn.ModuleList([])
for i in range(M):
self.fcs.append(
nn.Linear(d, features)
)
self.softmax = nn.Softmax(dim=1)
def forward(self, x1, x2):
# for i, conv in enumerate(self.convs):
# fea = conv(x).unsqueeze_(dim=1)
# if i == 0:
# feas = fea
# else:
# feas = torch.cat([feas, fea], dim=1)
feas = torch.cat((x1.unsqueeze_(dim=1), x2.unsqueeze_(dim=1)), dim=1)
fea_U = torch.sum(feas, dim=1)
# fea_s = self.gap(fea_U).squeeze_()
fea_s = fea_U.mean(-1).mean(-1).mean((-1))
fea_z = self.fc(fea_s)
for i, fc in enumerate(self.fcs):
vector = fc(fea_z).unsqueeze_(dim=1)
if i == 0:
attention_vectors = vector
else:
attention_vectors = torch.cat([attention_vectors, vector], dim=1)
attention_vectors = self.softmax(attention_vectors)
attention_vectors = attention_vectors.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
fea_v = (feas * attention_vectors).sum(dim=1)
return fea_v
class SF_Decoder(nn.Module):
def __init__(self, out_channels):
super(SF_Decoder, self).__init__()
self.conv1 = SFConv(out_channels)
self.bn1 = nn.BatchNorm3d(out_channels)
# self.conv2 = nn.Conv3d(out_channels, out_channels // 2, kernel_size=3, padding=1)
# self.bn2 = nn.BatchNorm3d(out_channels // 2)
self.relu = nn.ReLU(inplace=False)
self.ResDecoder = ResDecoder(out_channels)
# self.conv1x1 = nn.Conv3d(in_channels, out_channels, kernel_size=1)
def forward(self, x1, x2):
# residual = self.conv1x1(x)
out = self.relu(self.bn1(self.conv1(x1, x2)))
out = self.ResDecoder(out)
# out = self.relu(self.bn2(self.conv2(out)))
# out += residual
# out = self.relu(out)
return out
class ResEncoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(ResEncoder, self).__init__()
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm3d(out_channels)
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm3d(out_channels)
self.relu = nn.ReLU(inplace=False)
self.conv1x1 = nn.Conv3d(in_channels, out_channels, kernel_size=1)
def forward(self, x):
residual = self.conv1x1(x)
out = self.relu(self.bn1(self.conv1(x)))
out = self.relu(self.bn2(self.conv2(out)))
out += residual
out = self.relu(out)
return out
class ER_Net(nn.Module):
def __init__(self, classes, channels):
# def __init__(self):
super(ER_Net, self).__init__()
self.encoder1 = ResEncoder(channels, 32)
self.encoder2 = ResEncoder(32, 64)
self.encoder3 = ResEncoder(64, 128)
self.bridge = ResEncoder(128, 256)
self.conv1_1 = nn.Conv3d(256, 1, kernel_size=1)
self.conv2_2 = nn.Conv3d(128, 1, kernel_size=1)
self.conv3_3 = nn.Conv3d(64, 1, kernel_size=1)
self.convTrans1 = nn.ConvTranspose3d(1, 1, kernel_size=2, stride=2)
self.convTrans2 = nn.ConvTranspose3d(1, 1, kernel_size=2, stride=2)
self.convTrans3 = nn.ConvTranspose3d(1, 1, kernel_size=2, stride=2)
self.decoder3 = SF_Decoder(128)
self.decoder2 = SF_Decoder(64)
self.decoder1 = SF_Decoder(32)
self.down = downsample()
self.up3 = deconv(256, 128)
self.up2 = deconv(128, 64)
self.up1 = deconv(64, 32)
self.final = nn.Conv3d(32, classes, kernel_size=1, padding=0)
initialize_weights(self)
def forward(self, x):
enc1 = self.encoder1(x)
down1 = self.down(enc1)
enc2 = self.encoder2(down1)
down2 = self.down(enc2)
con3_3 = self.conv3_3(enc2)
convTrans3 = self.convTrans3(con3_3)
x3 = -1 * (torch.sigmoid(convTrans3)) + 1
x3 = x3.expand(-1, 32, -1, -1, -1).mul(enc1)
x3 = x3 + enc1
enc3 = self.encoder3(down2)
down3 = self.down(enc3)
con2_2 = self.conv2_2(enc3)
convTrans2 = self.convTrans2(con2_2)
x2 = -1 * (torch.sigmoid(convTrans2)) + 1
x2 = x2.expand(-1, 64, -1, -1, -1).mul(enc2)
x2 = x2 + enc2
bridge = self.bridge(down3)
conv1_1 = self.conv1_1(bridge)
convTrans1 = self.convTrans1(conv1_1)
x = -1 * (torch.sigmoid(convTrans1)) + 1
x = x.expand(-1, 128, -1, -1, -1).mul(enc3)
x = x + enc3
up3 = self.up3(bridge)
# up3 = SKII_Decoder(up3,)
# up3 = torch.cat((up3, x), dim=1)
dec3 = self.decoder3(up3, x)
up2 = self.up2(dec3)
# up2 = torch.cat((up2, x2), dim=1)
dec2 = self.decoder2(up2, x2)
up1 = self.up1(dec2)
# up1 = torch.cat((up1, x3), dim=1)
dec1 = self.decoder1(up1, x3)
final = self.final(dec1)
final = F.sigmoid(final)
return final