-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimproved_unet.py
More file actions
329 lines (297 loc) · 11.5 KB
/
improved_unet.py
File metadata and controls
329 lines (297 loc) · 11.5 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Improved U-Net model architecture definition
# U-Net model enhanced with modern CNN techniques from multiple research papers
# 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__()
expansion_ratio: int = 4
self.encoder_block: th.nn.Sequential = th.nn.Sequential(
# Convolutional layer 1: 3 convolutions:
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=in_channels,
out_channels=in_channels,
kernel_size=7,
stride=1,
padding=3,
groups=in_channels,
),
th.nn.BatchNorm2d(num_features=in_channels),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=in_channels,
out_channels=expansion_ratio*out_channels,
kernel_size=1,
stride=1,
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=expansion_ratio*out_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
),
# Convolutional layer 2: 3 convolutions:
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=out_channels,
out_channels=out_channels,
kernel_size=7,
stride=1,
padding=3,
groups=out_channels,
),
th.nn.BatchNorm2d(num_features=out_channels),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=out_channels,
out_channels=expansion_ratio*out_channels,
kernel_size=1,
stride=1,
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=expansion_ratio*out_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
),
)
# 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__()
expansion_ratio: int = 4
self.decoder_block: th.nn.Sequential = th.nn.Sequential(
# Convolutional layer 1: 3 convolutions:
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=in_channels,
out_channels=in_channels,
kernel_size=7,
stride=1,
padding=3,
groups=in_channels,
),
th.nn.BatchNorm2d(num_features=in_channels),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=in_channels,
out_channels=expansion_ratio*in_channels,
kernel_size=1,
stride=1,
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=expansion_ratio*in_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
),
# Convolutional layer 2: 3 convolutions:
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=out_channels,
out_channels=out_channels,
kernel_size=7,
stride=1,
padding=3,
groups=out_channels,
),
th.nn.BatchNorm2d(num_features=out_channels),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=out_channels,
out_channels=expansion_ratio*out_channels,
kernel_size=1,
stride=1,
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=expansion_ratio*out_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
),
)
# 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: 3 convolutions
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=8*n_filters,
out_channels=8*n_filters,
kernel_size=7,
stride=1,
padding=3,
groups=8*n_filters
),
th.nn.BatchNorm2d(num_features=8*n_filters),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=8*n_filters,
out_channels=4*8*n_filters,
kernel_size=1,
stride=1
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=4*8*n_filters,
out_channels=8*n_filters,
kernel_size=1,
stride=1
),
# Convolutional layer 2: 3 convolutions
th.nn.Conv2d( # 1. Depthwise convolution
in_channels=8*n_filters,
out_channels=8*n_filters,
kernel_size=7,
stride=1,
padding=3,
groups=8*n_filters
),
th.nn.BatchNorm2d(num_features=8*n_filters),
th.nn.Conv2d( # 2. Pointwise convolution
in_channels=8*n_filters,
out_channels=4*8*n_filters,
kernel_size=1,
stride=1
),
activation,
th.nn.Conv2d( # 3. Pointwise convolution
in_channels=4*8*n_filters,
out_channels=8*n_filters,
kernel_size=1,
stride=1
),
)
# Forward pass
def forward(self, x: Tensor) -> Tensor:
return self.bottleneck_block(x)
# Improved U-Net model ---------------------------------------------------------
class ImprovedUNet(th.nn.Module):
"""Improved modern U-Net model architecture
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: "ImprovedUNet")
Name of the model
"""
# Constructor
def __init__(self,
in_channels: int = 4,
out_channels: int = 3,
n_filters: int = 32,
activation: th.nn.Module = th.nn.ReLU(),
name: str = "ImprovedUNet",
) -> 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.UpsamplingBilinear2d = 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(8 * n_filters, 4 * n_filters, activation)
self.decoder3: decoder = decoder(4 * n_filters, 2 * n_filters, activation)
self.decoder2: decoder = decoder(2 * n_filters, 1 * n_filters, activation)
self.decoder1: decoder = decoder(1 * n_filters, 1 * n_filters, activation)
# Output convolutional layer
self.output: th.nn.Conv2d = th.nn.Conv2d(
in_channels=1 * 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.add(x, skip_4)
x = self.decoder4(x)
x = self.upsample(x)
x = th.add(x, skip_3)
x = self.decoder3(x)
x = self.upsample(x)
x = th.add(x, skip_2)
x = self.decoder2(x)
x = self.upsample(x)
x = th.add(x, skip_1)
x = self.decoder1(x)
x = self.output(x)
return x