-
Notifications
You must be signed in to change notification settings - Fork 0
/
classic_unet.py
245 lines (212 loc) · 7.62 KB
/
classic_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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Classic U-Net model architecture
# Inspired by the paper 'U-Net: Convolutional Networks for Biomedical Image Segmentation'
# by Ronneberger et al. (2015)
# Imports ----------------------------------------------------------------------
# Common Python imports
import numpy as np
# Torch imports
import torch as th
from torch import Tensor
# Typining hints
from typing import List, Union, Callable, Tuple
# Encoder block ----------------------------------------------------------------
class encoder(th.nn.Module):
"""Encoder block of the U-Net model
Parameters
----------
in_channels: int
Number of input channels
out_channels: int
Number of output channels
activation: th.nn.Module, optional (default: th.nn.ReLU())
Activation function to use in the convolutional layers
"""
# Constructor
def __init__(self,
in_channels: int,
out_channels: int,
activation: th.nn.Module = th.nn.ReLU(),
) -> None:
super().__init__()
self.encoder_block: th.nn.Sequential = th.nn.Sequential(
# Convolutional layer 1
th.nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
),
activation,
# Convolutional layer 2
th.nn.Conv2d(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
),
activation,
)
# Forward pass
def forward(self, x: Tensor) -> Tensor:
return self.encoder_block(x)
# Decoder block ----------------------------------------------------------------
class decoder(th.nn.Module):
"""Decoder block of the U-Net model
Parameters
----------
in_channels: int
Number of input channels
out_channels: int
Number of output channels
activation: th.nn.Module, optional (default: th.nn.ReLU())
Activation function to use in the convolutional layers
"""
# Constructor
def __init__(self,
in_channels: int,
out_channels: int,
activation: th.nn.Module = th.nn.ReLU(),
) -> None:
super().__init__()
self.decoder_block: th.nn.Sequential = th.nn.Sequential(
# Convolutional layer 1
th.nn.Conv2d(
in_channels=in_channels,
out_channels=in_channels // 2,
kernel_size=3,
stride=1,
padding=1,
),
activation,
# Convolutional layer 2
th.nn.Conv2d(
in_channels=in_channels // 2,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
),
activation,
)
# Forward pass
def forward(self, x: Tensor) -> Tensor:
return self.decoder_block(x)
# Bottleneck block -------------------------------------------------------------
class bottleneck(th.nn.Module):
"""Bottleneck block of the U-Net model
Parameters
----------
n_filters: int
Number of filters to use in the convolutional layers
activation: th.nn.Module, optional (default: th.nn.ReLU())
Activation function to use in the convolutional layers
"""
# Constructor
def __init__(self,
n_filters: int,
activation: th.nn.Module = th.nn.ReLU(),
) -> None:
super().__init__()
self.bottleneck_block: th.nn.Sequential = th.nn.Sequential(
# Convolutional layer 1
th.nn.Conv2d(
in_channels = 8 * n_filters,
out_channels = 16 * n_filters,
kernel_size = 3,
stride = 1,
padding = 1,
),
activation,
# Convolutional layer 2
th.nn.Conv2d(
in_channels = 16 * n_filters,
out_channels = 8 * n_filters,
kernel_size = 3,
stride = 1,
padding = 1,
),
activation,
)
# Forward pass
def forward(self, x: Tensor) -> Tensor:
return self.bottleneck_block(x)
# Classic U-Net model ----------------------------------------------------------
class ClassicUNet(th.nn.Module):
"""U-Net model architecture (Ronneberger et al., 2015)
Parameters
----------
in_channels: int, optional (default: 4 [BraTS2020])
Number of input channels
out_channels: int, optional (default: 3 [RGB])
Number of output channels
n_filters: int, optional (default: 32)
Number of filters to use in the convolutional layers
activation: th.nn.Module, optional (default: th.nn.ReLU())
Activation function to use in the convolutional layers
name: str, optional (default: "ClassicUNet")
Name of the model
"""
# Constructor
def __init__(self,
in_channels: int = 4, # BraTS2020 dataset images channels
out_channels: int = 3,
n_filters: int = 32,
activation: th.nn.Module = th.nn.ReLU(),
name: str = "ClassicUNet",
) -> None:
super().__init__()
# Model name
self.name: str = name
# Downsampling and Upsampling methods
self.downsample: th.nn.MaxPool2d = th.nn.MaxPool2d(kernel_size=2, stride=2)
self.upsample: th.nn.Upsample = th.nn.UpsamplingBilinear2d(scale_factor=2)
# Encoder blocks
self.encoder1: encoder = encoder(in_channels, n_filters, activation)
self.encoder2: encoder = encoder(1 * n_filters, 2 * n_filters, activation)
self.encoder3: encoder = encoder(2 * n_filters, 4 * n_filters, activation)
self.encoder4: encoder = encoder(4 * n_filters, 8 * n_filters, activation)
# Bottolneck block
self.bottleneck: bottleneck = bottleneck(n_filters, activation)
# Decoder blocks
self.decoder4: decoder = decoder(16 * n_filters, 4 * n_filters, activation)
self.decoder3: decoder = decoder(8 * n_filters, 2 * n_filters, activation)
self.decoder2: decoder = decoder(4 * n_filters, 1 * n_filters, activation)
self.decoder1: decoder = decoder(2 * n_filters, 1 * n_filters, activation)
# Output convolutional layer
self.output: th.nn.Conv2d = th.nn.Conv2d(
in_channels = n_filters,
out_channels = out_channels,
kernel_size = 1,
stride = 1,
padding = 0,
)
# Forward pass
def forward(self, x: Tensor) -> Tensor:
# Encoder
skip_1 = self.encoder1(x)
x = self.downsample(skip_1)
skip_2 = self.encoder2(x)
x = self.downsample(skip_2)
skip_3 = self.encoder3(x)
x = self.downsample(skip_3)
skip_4 = self.encoder4(x)
x = self.downsample(skip_4)
# Bottleneck
x = self.bottleneck(x)
# Decoder
x = self.upsample(x)
x = th.cat((x, skip_4), axis=1)
x = self.decoder4(x)
x = self.upsample(x)
x = th.cat((x, skip_3), axis=1)
x = self.decoder3(x)
x = self.upsample(x)
x = th.cat((x, skip_2), axis=1)
x = self.decoder2(x)
x = self.upsample(x)
x = th.cat((x, skip_1), axis=1)
x = self.decoder1(x)
x = self.output(x)
return x