-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet3d.py
executable file
·128 lines (95 loc) · 3.91 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
import torch.nn as nn
import torch.nn.functional as F
import math
import torch
# adapt from https://github.com/MIC-DKFZ/BraTS2017
def normalization(planes, norm='gn'):
if norm == 'bn':
m = nn.BatchNorm3d(planes)
elif norm == 'gn':
m = nn.GroupNorm(4, planes)
elif norm == 'in':
m = nn.InstanceNorm3d(planes)
else:
raise ValueError('normalization type {} is not supported'.format(norm))
return m
class ConvD(nn.Module):
def __init__(self, inplanes, planes, dropout=0.0, norm='gn', first=False):
super(ConvD, self).__init__()
self.first = first
self.maxpool = nn.MaxPool3d(2, 2)
self.dropout = dropout
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv3d(inplanes, planes, 3, 1, 1, bias=False)
self.bn1 = normalization(planes, norm)
self.conv2 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn2 = normalization(planes, norm)
self.conv3 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn3 = normalization(planes, norm)
def forward(self, x):
if not self.first:
x = self.maxpool(x)
x = self.bn1(self.conv1(x))
y = self.relu(self.bn2(self.conv2(x)))
if self.dropout > 0:
y = F.dropout3d(y, self.dropout)
y = self.bn3(self.conv3(x))
return self.relu(x + y)
class ConvU(nn.Module):
def __init__(self, planes, norm='gn', first=False):
super(ConvU, self).__init__()
self.first = first
if not self.first:
self.conv1 = nn.Conv3d(2*planes, planes, 3, 1, 1, bias=False)
self.bn1 = normalization(planes, norm)
self.conv2 = nn.Conv3d(planes, planes//2, 1, 1, 0, bias=False)
self.bn2 = normalization(planes//2, norm)
self.conv3 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn3 = normalization(planes, norm)
self.relu = nn.ReLU(inplace=True)
def forward(self, x, prev):
# final output is the localization layer
if not self.first:
x = self.relu(self.bn1(self.conv1(x)))
y = F.upsample(x, scale_factor=2, mode='trilinear', align_corners=False)
y = self.relu(self.bn2(self.conv2(y)))
y = torch.cat([prev, y], 1)
y = self.relu(self.bn3(self.conv3(y)))
return y
class Unet3D(nn.Module):
def __init__(self, c=4, n=16, dropout=0.5, norm='gn', num_classes=5):
super(Unet3D, self).__init__()
self.upsample = nn.Upsample(scale_factor=2,
mode='trilinear', align_corners=False)
self.convd1 = ConvD(c, n, dropout, norm, first=True)
self.convd2 = ConvD(n, 2*n, dropout, norm)
self.convd3 = ConvD(2*n, 4*n, dropout, norm)
self.convd4 = ConvD(4*n, 8*n, dropout, norm)
self.convd5 = ConvD(8*n,16*n, dropout, norm)
self.convu4 = ConvU(16*n, norm, True)
self.convu3 = ConvU(8*n, norm)
self.convu2 = ConvU(4*n, norm)
self.convu1 = ConvU(2*n, norm)
self.seg3 = nn.Conv3d(8*n, num_classes, 1)
self.seg2 = nn.Conv3d(4*n, num_classes, 1)
self.seg1 = nn.Conv3d(2*n, num_classes, 1)
for m in self.modules():
if isinstance(m, nn.Conv3d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm3d) or isinstance(m, nn.GroupNorm):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x1 = self.convd1(x)
x2 = self.convd2(x1)
x3 = self.convd3(x2)
x4 = self.convd4(x3)
x5 = self.convd5(x4)
y4 = self.convu4(x5, x4)
y3 = self.convu3(y4, x3)
y2 = self.convu2(y3, x2)
y1 = self.convu1(y2, x1)
y3 = self.seg3(y3)
y2 = self.seg2(y2) + self.upsample(y3)
y1 = self.seg1(y1) + self.upsample(y2)
return y1