forked from mobarakol/SVLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
221 lines (188 loc) · 9.6 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
import importlib
import torch
import torch.nn as nn
from torch.nn import functional as F
def conv3d(in_channels, out_channels, kernel_size, bias, padding=1):
return nn.Conv3d(in_channels, out_channels, kernel_size, padding=padding, bias=bias)
def create_conv(in_channels, out_channels, kernel_size, order, num_groups, padding=1):
assert 'c' in order, "Conv layer MUST be present"
assert order[0] not in 'rle', 'Non-linearity cannot be the first operation in the layer'
modules = []
for i, char in enumerate(order):
if char == 'r':
modules.append(('ReLU', nn.ReLU(inplace=True)))
elif char == 'l':
modules.append(('LeakyReLU', nn.LeakyReLU(negative_slope=0.1, inplace=True)))
elif char == 'e':
modules.append(('ELU', nn.ELU(inplace=True)))
elif char == 'c':
# add learnable bias only in the absence of gatchnorm/groupnorm
bias = not ('g' in order or 'b' in order)
modules.append(('conv', conv3d(in_channels, out_channels, kernel_size, bias, padding=padding)))
elif char == 'g':
is_before_conv = i < order.index('c')
assert not is_before_conv, 'GroupNorm MUST go after the Conv3d'
# number of groups must be less or equal the number of channels
if out_channels < num_groups:
num_groups = out_channels
modules.append(('groupnorm', nn.GroupNorm(num_groups=num_groups, num_channels=out_channels)))
elif char == 'b':
is_before_conv = i < order.index('c')
if is_before_conv:
modules.append(('batchnorm', nn.BatchNorm3d(in_channels)))
else:
modules.append(('batchnorm', nn.BatchNorm3d(out_channels)))
else:
raise ValueError("Unsupported layer type '{char}'. MUST be one of ['b', 'g', 'r', 'l', 'e', 'c']")
return modules
class SingleConv(nn.Sequential):
def __init__(self, in_channels, out_channels, kernel_size=3, order='crg', num_groups=8, padding=1):
super(SingleConv, self).__init__()
for name, module in create_conv(in_channels, out_channels, kernel_size, order, num_groups, padding=padding):
self.add_module(name, module)
class DoubleConv(nn.Sequential):
def __init__(self, in_channels, out_channels, encoder, kernel_size=3, order='crg', num_groups=8):
super(DoubleConv, self).__init__()
if encoder:
# we're in the encoder path
conv1_in_channels = in_channels
conv1_out_channels = out_channels // 2
if conv1_out_channels < in_channels:
conv1_out_channels = in_channels
conv2_in_channels, conv2_out_channels = conv1_out_channels, out_channels
else:
conv1_in_channels, conv1_out_channels = in_channels, out_channels
conv2_in_channels, conv2_out_channels = out_channels, out_channels
# conv1
self.add_module('SingleConv1',
SingleConv(conv1_in_channels, conv1_out_channels, kernel_size, order, num_groups))
# conv2
self.add_module('SingleConv2',
SingleConv(conv2_in_channels, conv2_out_channels, kernel_size, order, num_groups))
class Encoder(nn.Module):
def __init__(self, in_channels, out_channels, conv_kernel_size=3, apply_pooling=True,
pool_kernel_size=(2, 2, 2), pool_type='max', basic_module=DoubleConv, conv_layer_order='crg',
num_groups=8):
super(Encoder, self).__init__()
assert pool_type in ['max', 'avg']
if apply_pooling:
if pool_type == 'max':
self.pooling = nn.MaxPool3d(kernel_size=pool_kernel_size)
else:
self.pooling = nn.AvgPool3d(kernel_size=pool_kernel_size)
else:
self.pooling = None
self.basic_module = basic_module(in_channels, out_channels,
encoder=True,
kernel_size=conv_kernel_size,
order=conv_layer_order,
num_groups=num_groups)
def forward(self, x):
if self.pooling is not None:
x = self.pooling(x)
x = self.basic_module(x)
return x
class Decoder(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3,
scale_factor=(2, 2, 2), basic_module=DoubleConv, conv_layer_order='crg', num_groups=8):
super(Decoder, self).__init__()
if basic_module == DoubleConv:
# if DoubleConv is the basic_module use nearest neighbor interpolation for upsampling
self.upsample = None
else:
self.upsample = nn.ConvTranspose3d(in_channels,
out_channels,
kernel_size=kernel_size,
stride=scale_factor,
padding=1,
output_padding=1)
# adapt the number of in_channels for the ExtResNetBlock
in_channels = out_channels
#self.scse = SCSEBlock(in_channels)
self.basic_module = basic_module(in_channels, out_channels,
encoder=False,
kernel_size=kernel_size,
order=conv_layer_order,
num_groups=num_groups)
def forward(self, encoder_features, x):
if self.upsample is None:
# use nearest neighbor interpolation and concatenation joining
output_size = encoder_features.size()[2:]
x = F.interpolate(x, size=output_size, mode='nearest')
# concatenate encoder_features (encoder path) with the upsampled input across channel dimension
x = torch.cat((encoder_features, x), dim=1)
else:
# use ConvTranspose3d and summation joining
x = self.upsample(x)
x += encoder_features
#x = self.scse(x)
x = self.basic_module(x)
return x
class FinalConv(nn.Sequential):
def __init__(self, in_channels, out_channels, kernel_size=3, order='crg', num_groups=8):
super(FinalConv, self).__init__()
# conv1
self.add_module('SingleConv', SingleConv(in_channels, in_channels, kernel_size, order, num_groups))
# in the last layer a 1×1 convolution reduces the number of output channels to out_channels
final_conv = nn.Conv3d(in_channels, out_channels, 1)
self.add_module('final_conv', final_conv)
def create_feature_maps(init_channel_number, number_of_fmaps):
return [init_channel_number * 2 ** k for k in range(number_of_fmaps)]
class UNet3D(nn.Module):
"""
3DUnet model from
`"3D U-Net: Learning Dense Volumetric Segmentation from Sparse Annotation"
<https://arxiv.org/pdf/1606.06650.pdf>`.
Args:
inplanes (int): number of input channels
num_classes (int): number of output segmentation masks
f_maps (int, tuple): number of feature maps at each level of the encoder
layer_order (string): determines the order of layers
in `SingleConv` module. e.g. 'crg' stands for Conv3d+ReLU+GroupNorm3d.
See `SingleConv` for more info
init_channel_number (int): number of feature maps in the first conv layer of the encoder; default: 64
num_groups (int): number of groups for the GroupNorm
"""
def __init__(self, inplanes, num_classes, f_maps=16, layer_order='crg', num_groups=8, **kwargs):
super(UNet3D, self).__init__()
if isinstance(f_maps, int):
# use 4 levels in the encoder path as suggested in the paper
f_maps = create_feature_maps(f_maps, number_of_fmaps=6)
# create encoder
encoders = []
for i, out_feature_num in enumerate(f_maps):
if i == 0:
encoder = Encoder(inplanes, out_feature_num, apply_pooling=False, basic_module=DoubleConv,
conv_layer_order=layer_order, num_groups=num_groups)
else:
encoder = Encoder(f_maps[i - 1], out_feature_num, basic_module=DoubleConv,
conv_layer_order=layer_order, num_groups=num_groups)
encoders.append(encoder)
self.encoders = nn.ModuleList(encoders)
# create decoder
decoders = []
reversed_f_maps = list(reversed(f_maps))
for i in range(len(reversed_f_maps) - 1):
in_feature_num = reversed_f_maps[i] + reversed_f_maps[i + 1]
out_feature_num = reversed_f_maps[i + 1]
decoder = Decoder(in_feature_num, out_feature_num, basic_module=DoubleConv,
conv_layer_order=layer_order, num_groups=num_groups)
decoders.append(decoder)
self.decoders = nn.ModuleList(decoders)
# in the last layer
self.final_conv = nn.Conv3d(f_maps[0], num_classes, 1)
def forward(self, x):
# encoder part
encoders_features = []
for encoder in self.encoders:
x = encoder(x)
# reverse the encoder outputs
encoders_features.insert(0, x)
# remove the last encoder's output
encoders_features = encoders_features[1:]
# decoder part
for decoder, encoder_features in zip(self.decoders, encoders_features):
# pass the output from the corresponding encoder and the output
x = decoder(encoder_features, x)
x = self.final_conv(x)
return x