-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUnet3D.py
151 lines (108 loc) · 5.46 KB
/
Unet3D.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
from torch.nn import Module, Sequential
from torch.nn import Conv3d, ConvTranspose3d, BatchNorm3d, MaxPool3d, AvgPool1d
from torch.nn import ReLU, Sigmoid
import torch
class UNet3D(Module):
# __ __
# 1|__ ________________ __|1
# 2|__ ____________ __|2
# 3|__ ______ __|3
# 4|__ __ __|4
# The convolution operations on either side are residual subject to 1*1 Convolution for channel homogeneity
def __init__(self,num_channels=32,feat_channels=[64, 128, 256, 512, 1024], residual='conv'):
#residual: 是否加入残差边,不加则为None
super(UNet3D, self).__init__()
# Encoder downsamplers
self.pool1 = MaxPool3d((1,2,2))
self.pool2 = MaxPool3d((1,2,2))
self.pool3 = MaxPool3d((1,2,2))
self.pool4 = MaxPool3d((1,2,2))
# Encoder convolutions
self.conv_blk1 = Conv3D_Block(num_channels, feat_channels[0], residual=residual)
self.conv_blk2 = Conv3D_Block(feat_channels[0], feat_channels[1], residual=residual)
self.conv_blk3 = Conv3D_Block(feat_channels[1], feat_channels[2], residual=residual)
self.conv_blk4 = Conv3D_Block(feat_channels[2], feat_channels[3], residual=residual)
self.conv_blk5 = Conv3D_Block(feat_channels[3], feat_channels[4], residual=residual)
# Decoder convolutions
self.dec_conv_blk4 = Conv3D_Block(2*feat_channels[3], feat_channels[3], residual=residual)
self.dec_conv_blk3 = Conv3D_Block(2*feat_channels[2], feat_channels[2], residual=residual)
self.dec_conv_blk2 = Conv3D_Block(2*feat_channels[1], feat_channels[1], residual=residual)
self.dec_conv_blk1 = Conv3D_Block(2*feat_channels[0], feat_channels[0], residual=residual)
# Decoder upsamplers
self.deconv_blk4 = Deconv3D_Block(feat_channels[4], feat_channels[3])
self.deconv_blk3 = Deconv3D_Block(feat_channels[3], feat_channels[2])
self.deconv_blk2 = Deconv3D_Block(feat_channels[2], feat_channels[1])
self.deconv_blk1 = Deconv3D_Block(feat_channels[1], feat_channels[0])
# Final 1*1 Conv Segmentation map
self.one_conv = Conv3d(feat_channels[0], num_channels, kernel_size=1, stride=1, padding=0, bias=True)
# Activation function
self.sigmoid = Sigmoid()
def forward(self, x):
# 编码器
x1 = self.conv_blk1(x)
x_low1 = self.pool1(x1)
x2 = self.conv_blk2(x_low1)
x_low2 = self.pool2(x2)
x3 = self.conv_blk3(x_low2)
x_low3 = self.pool3(x3)
x4 = self.conv_blk4(x_low3)
x_low4 = self.pool4(x4)
base = self.conv_blk5(x_low4)
# 解码器
d4 = torch.cat([self.deconv_blk4(base), x4], dim=1)
d_high4 = self.dec_conv_blk4(d4)
d3 = torch.cat([self.deconv_blk3(d_high4), x3], dim=1)
d_high3 = self.dec_conv_blk3(d3)
d2 = torch.cat([self.deconv_blk2(d_high3), x2], dim=1)
d_high2 = self.dec_conv_blk2(d2)
d1 = torch.cat([self.deconv_blk1(d_high2), x1], dim=1)
d_high1 = self.dec_conv_blk1(d1)
seg = self.sigmoid(self.one_conv(d_high1))
return seg
class Conv3D_Block(Module):
def __init__(self, inp_feat, out_feat, kernel=3, stride=1, padding=1, residual=None):
super(Conv3D_Block, self).__init__()
self.conv1 = Sequential(
Conv3d(inp_feat, out_feat, kernel_size=kernel,
stride=stride, padding=padding, bias=True),
BatchNorm3d(out_feat),
ReLU())
self.conv2 = Sequential(
Conv3d(out_feat, out_feat, kernel_size=kernel,
stride=stride, padding=padding, bias=True),
BatchNorm3d(out_feat),
ReLU())
self.residual = residual
if self.residual is not None:
self.residual_upsampler = Conv3d(inp_feat, out_feat, kernel_size=1, bias=False)
def forward(self, x):
res = x
if not self.residual:
return self.conv2(self.conv1(x))
else:
return self.conv2(self.conv1(x)) + self.residual_upsampler(res)
class Deconv3D_Block(Module):
def __init__(self, inp_feat, out_feat, kernel=4, stride=2, padding=1):
super(Deconv3D_Block, self).__init__()
self.deconv = Sequential(
#3D反卷积
ConvTranspose3d(inp_feat, out_feat, kernel_size=(1,kernel,kernel),
stride=(1,stride,stride), padding=(0, padding, padding), output_padding=0, bias=True),
ReLU())
def forward(self, x):
return self.deconv(x)
class ChannelPool3d(AvgPool1d):
def __init__(self, kernel_size, stride, padding):
super(ChannelPool3d, self).__init__(kernel_size, stride, padding)
self.pool_1d = AvgPool1d(self.kernel_size, self.stride, self.padding, self.ceil_mode)
def forward(self, inp):
n, c, d, w, h = inp.size()
inp = inp.view(n,c,d*w*h).permute(0,2,1)
pooled = self.pool_1d(inp)
c = int(c/self.kernel_size[0])
return inp.view(n,c,d,w,h)
if __name__=='__main__':
net = UNet3D(residual='pool')
import torch
x = torch.ones(4, 32, 1, 128, 128)
print (net.forward(x).size())