-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunet3plus.py
224 lines (192 loc) · 8.43 KB
/
unet3plus.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
222
223
224
import torch
import torch.nn as nn
import torch.nn.functional as F
def ConvBlock(in_channels, out_channels, kernel_size=(3, 3), stride=(1, 1), padding='same',
is_bn=True, is_relu=True, n=2):
""" Custom function for conv2d:
Apply 3*3 convolutions with BN and ReLU.
"""
layers = []
for i in range(1, n + 1):
conv = nn.Conv2d(in_channels=in_channels if i == 1 else out_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding if padding != 'same' else 'same',
bias=not is_bn) # Disable bias when using BatchNorm
layers.append(conv)
if is_bn:
layers.append(nn.BatchNorm2d(out_channels))
if is_relu:
layers.append(nn.ReLU(inplace=True))
return nn.Sequential(*layers)
def dot_product(seg, cls):
b, n, h, w = seg.shape
seg = seg.view(b, n, -1)
cls = cls.unsqueeze(-1) # Add an extra dimension for broadcasting
final = torch.einsum("bik,bi->bik", seg, cls)
final = final.view(b, n, h, w)
return final
class UNet3Plus(nn.Module):
def __init__(self, input_shape, output_channels, deep_supervision=False, cgm=False, training=False):
super(UNet3Plus, self).__init__()
self.deep_supervision = deep_supervision
self.CGM = deep_supervision and cgm
self.training = training
self.filters = [64, 128, 256, 512, 1024]
self.cat_channels = self.filters[0]
self.cat_blocks = len(self.filters)
self.upsample_channels = self.cat_blocks * self.cat_channels
# Encoder
self.e1 = ConvBlock(input_shape[0], self.filters[0])
self.e2 = nn.Sequential(
nn.MaxPool2d(2),
ConvBlock(self.filters[0], self.filters[1])
)
self.e3 = nn.Sequential(
nn.MaxPool2d(2),
ConvBlock(self.filters[1], self.filters[2])
)
self.e4 = nn.Sequential(
nn.MaxPool2d(2),
ConvBlock(self.filters[2], self.filters[3])
)
self.e5 = nn.Sequential(
nn.MaxPool2d(2),
ConvBlock(self.filters[3], self.filters[4])
)
# Classification Guided Module
self.cgm = nn.Sequential(
nn.Dropout(0.5),
nn.Conv2d(self.filters[4], 2, kernel_size=1, padding=0),
nn.AdaptiveMaxPool2d(1),
nn.Flatten(),
nn.Sigmoid()
) if self.CGM else None
# Decoder
self.d4 = nn.ModuleList([
ConvBlock(self.filters[0], self.cat_channels, n=1),
ConvBlock(self.filters[1], self.cat_channels, n=1),
ConvBlock(self.filters[2], self.cat_channels, n=1),
ConvBlock(self.filters[3], self.cat_channels, n=1),
ConvBlock(self.filters[4], self.cat_channels, n=1)
])
self.d4_conv = ConvBlock(self.upsample_channels, self.upsample_channels, n=1)
self.d3 = nn.ModuleList([
ConvBlock(self.filters[0], self.cat_channels, n=1),
ConvBlock(self.filters[1], self.cat_channels, n=1),
ConvBlock(self.filters[2], self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.filters[4], self.cat_channels, n=1)
])
self.d3_conv = ConvBlock(self.upsample_channels, self.upsample_channels, n=1)
self.d2 = nn.ModuleList([
ConvBlock(self.filters[0], self.cat_channels, n=1),
ConvBlock(self.filters[1], self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.filters[4], self.cat_channels, n=1)
])
self.d2_conv = ConvBlock(self.upsample_channels, self.upsample_channels, n=1)
self.d1 = nn.ModuleList([
ConvBlock(self.filters[0], self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.upsample_channels, self.cat_channels, n=1),
ConvBlock(self.filters[4], self.cat_channels, n=1)
])
self.d1_conv = ConvBlock(self.upsample_channels, self.upsample_channels, n=1)
self.final = nn.Conv2d(self.upsample_channels, output_channels, kernel_size=1)
# Deep Supervision
self.deep_sup = nn.ModuleList([
ConvBlock(self.upsample_channels, output_channels, n=1, is_bn=False, is_relu=False)
for _ in range(3)
] + [ConvBlock(self.filters[4], output_channels, n=1, is_bn=False, is_relu=False)]
) if self.deep_supervision else None
def forward(self, x) -> torch.Tensor:
training = self.training
# Encoder
e1 = self.e1(x)
e2 = self.e2(e1)
e3 = self.e3(e2)
e4 = self.e4(e3)
e5 = self.e5(e4)
# Classification Guided Module
if self.CGM:
cls = self.cgm(e5)
cls = torch.argmax(cls, dim=1).float()
# Decoder
d4 = [
F.max_pool2d(e1, 8),
F.max_pool2d(e2, 4),
F.max_pool2d(e3, 2),
e4,
F.interpolate(e5, scale_factor=2, mode='bilinear', align_corners=True)
]
d4 = [conv(d) for conv, d in zip(self.d4, d4)]
d4 = torch.cat(d4, dim=1)
d4 = self.d4_conv(d4)
d3 = [
F.max_pool2d(e1, 4),
F.max_pool2d(e2, 2),
e3,
F.interpolate(d4, scale_factor=2, mode='bilinear', align_corners=True),
F.interpolate(e5, scale_factor=4, mode='bilinear', align_corners=True)
]
d3 = [conv(d) for conv, d in zip(self.d3, d3)]
d3 = torch.cat(d3, dim=1)
d3 = self.d3_conv(d3)
d2 = [
F.max_pool2d(e1, 2),
e2,
F.interpolate(d3, scale_factor=2, mode='bilinear', align_corners=True),
F.interpolate(d4, scale_factor=4, mode='bilinear', align_corners=True),
F.interpolate(e5, scale_factor=8, mode='bilinear', align_corners=True)
]
d2 = [conv(d) for conv, d in zip(self.d2, d2)]
d2 = torch.cat(d2, dim=1)
d2 = self.d2_conv(d2)
d1 = [
e1,
F.interpolate(d2, scale_factor=2, mode='bilinear', align_corners=True),
F.interpolate(d3, scale_factor=4, mode='bilinear', align_corners=True),
F.interpolate(d4, scale_factor=8, mode='bilinear', align_corners=True),
F.interpolate(e5, scale_factor=16, mode='bilinear', align_corners=True)
]
d1 = [conv(d) for conv, d in zip(self.d1, d1)]
d1 = torch.cat(d1, dim=1)
d1 = self.d1_conv(d1)
d1 = self.final(d1)
outputs = [d1]
# Deep Supervision
if self.deep_supervision and training:
outputs.extend([
F.interpolate(self.deep_sup[0](d2), scale_factor=2, mode='bilinear', align_corners=True),
F.interpolate(self.deep_sup[1](d3), scale_factor=4, mode='bilinear', align_corners=True),
F.interpolate(self.deep_sup[2](d4), scale_factor=8, mode='bilinear', align_corners=True),
F.interpolate(self.deep_sup[3](e5), scale_factor=16, mode='bilinear', align_corners=True)
])
# Classification Guided Module
if self.CGM:
outputs = [dot_product(out, cls) for out in outputs]
outputs = [F.sigmoid(out) for out in outputs]
if self.deep_supervision and training:
return torch.cat(outputs, dim=0)
else:
return outputs[0]
if __name__ == "__main__":
INPUT_SHAPE = [1, 320, 320]
OUTPUT_CHANNELS = 2
unet_3P = UNet3Plus(INPUT_SHAPE, OUTPUT_CHANNELS, deep_supervision=False, cgm=False, training=True)
unet_3P_deep_sup = UNet3Plus(INPUT_SHAPE, OUTPUT_CHANNELS, deep_supervision=True, cgm=False, training=True)
unet_3P_deep_sup_cgm = UNet3Plus(INPUT_SHAPE, OUTPUT_CHANNELS, deep_supervision=True, cgm=True, training=True)
# print(unet_3P)
# Example input tensor
x = torch.randn(4, *INPUT_SHAPE)
# Forward pass
output = unet_3P(x)
print(f"Output shape: {output.shape}")
output = unet_3P_deep_sup(x)
print(f"Output shape: {output.shape}")
output = unet_3P_deep_sup_cgm(x)
print(f"Output shape: {output.shape}")