-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNet.py
146 lines (140 loc) · 7.2 KB
/
UNet.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
class UNet(nn.Module):
"""
Ronneberger, Olaf, Philipp Fischer, and Thomas Brox.
"U-Net: Convolutional networks for biomedical image segmentation."
International Conference on Medical image computing and computer-assisted intervention.
Springer, Cham, 2015.
"""
"""U-Net: Convolutional networks for biomedical image segmentation.
https://arxiv.org/abs/1505.04597
TO DO:
Args:
num_classes (int): number of classes to segment
n_init_features (int): number of input features in the fist convolution
drop_rate (float): dropout rate of each encoder/decoder module
filter_config (list of 5 ints): number of output features at each level
"""
def __init__(self):
super().__init__()
# encoder (downsampling)
self.enc_conv0 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(inplace=True)
)
self.pool0 = nn.MaxPool2d(kernel_size=(2, 2), stride=2) # 256 -> 128
self.enc_conv1 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True)
)
self.pool1 = nn.MaxPool2d(kernel_size=(2, 2), stride=2) # 128 -> 64
self.enc_conv2 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True)
)
self.pool2 = nn.MaxPool2d(kernel_size=(2, 2), stride=2) # 64 -> 32
self.enc_conv3 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True)
)
self.pool3 = nn.MaxPool2d(kernel_size=(2, 2), stride=2) # 32 -> 16
# bottleneck
self.bottleneck_conv = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=1024),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=1024, out_channels=1024, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=1024),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=1024, out_channels=1024, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=1024),
nn.ReLU(inplace=True)
)
# decoder (upsampling)
self.upsample0 = nn.Upsample(scale_factor=2, mode="bilinear") # 16 -> 32
self.dec_conv0 = nn.Sequential(
nn.Conv2d(in_channels=1024, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=512),
nn.ReLU(inplace=True)
)
self.upsample1 = nn.Upsample(scale_factor=2, mode="bilinear") # 32 -> 64
self.dec_conv1 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True)
)
self.upsample2 = nn.Upsample(scale_factor=2, mode="bilinear") # 64 -> 128
self.dec_conv2 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True)
)
self.upsample3 = nn.Upsample(scale_factor=2, mode="bilinear") # 128 -> 256
self.dec_conv3 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=64, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 3), stride=1, padding=1),
nn.BatchNorm2d(num_features=64),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=1, kernel_size=(1, 1,), stride=1, padding=0),
)
def forward(self, x):
# encoder
e0 = self.enc_conv0(x)
e1 = self.pool0(self.enc_conv1(e0))
e2 = self.pool1(self.enc_conv2(e1))
e3 =self.pool2(self.enc_conv3(e2))
# bottleneck
b = self.pool3(self.bottleneck_conv(e3))
# decoder
d0 = self.dec_conv0(torch.cat((e3, self.upsample0(b)[:,512:,:,:]), dim=1))
d1 = self.dec_conv1(torch.cat((e2, self.upsample1(d0)[:,256:,:,:]), dim=1))
d2 = self.dec_conv2(torch.cat((e1, self.upsample2(d1)[:,128:,:,:]), dim=1))
d3 = self.dec_conv3(torch.cat((e0, self.upsample3(d2)[:,64:,:,:]), dim=1)) # no activation
return d3