-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
283 lines (240 loc) · 10.3 KB
/
model.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
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
"""Definition of the UNet"""
import math
import torch
import torch.nn as nn
import numpy as np
class ConvBlock(torch.nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str = "same",
):
""" A convolution block for a U-Net. Contains two convolutions, each followed by a ReLU.
Args:
in_channels (int): The number of input channels for this conv block. Depends on
the layer and side of the U-Net and the hyperparameters.
out_channels (int): The number of output channels for this conv block. Depends on
the layer and side of the U-Net and the hyperparameters.
kernel_size (int): The size of the kernel. A kernel size of N signifies an
NxN square kernel.
padding (str, optional): The type of padding to use. Options are "same" or "valid".
Defaults to "same".
"""
super().__init__()
# determine padding size based on method
if padding in ("VALID", "valid"):
pad = 0
elif padding in ("SAME", "same"):
pad = kernel_size // 2
else:
raise RuntimeError("invalid string value for padding")
# define layers in conv pass
self.conv_pass = torch.nn.Sequential(
torch.nn.Conv2d(
in_channels, out_channels, kernel_size=kernel_size, padding=pad
),
torch.nn.ReLU(),
torch.nn.Conv2d(
out_channels, out_channels, kernel_size=kernel_size, padding=pad
),
torch.nn.ReLU(),
)
for _name, layer in self.named_modules():
if isinstance(layer, torch.nn.Conv2d):
torch.nn.init.kaiming_normal_(layer.weight, nonlinearity="relu")
def forward(self, x):
return self.conv_pass(x)
class Downsample(torch.nn.Module):
def __init__(self, downsample_factor: int):
super().__init__()
self.downsample_factor = downsample_factor
self.down = torch.nn.MaxPool2d(
downsample_factor
)
def check_valid(self, image_size: tuple[int, int]) -> bool:
"""Check if the downsample factor evenly divides each image dimension
"""
for dim in image_size:
if dim % self.downsample_factor != 0:
return False
return True
def forward(self, x):
if not self.check_valid(tuple(x.size()[-2:])):
raise RuntimeError(
"Can not downsample shape %s with factor %s"
% (x.size(), self.downsample_factor)
)
return self.down(x)
class CropAndConcat(torch.nn.Module):
def crop(self, x, y):
"""Center-crop x to match spatial dimensions given by y."""
x_target_size = x.size()[:-2] + y.size()[-2:]
offset = tuple((a - b) // 2 for a, b in zip(x.size(), x_target_size))
slices = tuple(slice(o, o + s) for o, s in zip(offset, x_target_size))
return x[slices]
def forward(self, encoder_output, upsample_output):
encoder_cropped = self.crop(encoder_output, upsample_output)
return torch.cat([encoder_cropped, upsample_output], dim=1)
class OutputConv(torch.nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
activation: str | None = None, # Accepts the name of any torch activation function (e.g., ``ReLU`` for ``torch.nn.ReLU``).
):
super().__init__()
self.final_conv = torch.nn.Conv2d(in_channels, out_channels, 1, padding=0) # leave this out
if activation is None:
self.activation = None
else:
self.activation = getattr(torch.nn, activation)()
def forward(self, x):
x = self.final_conv(x)
if self.activation is not None:
x = self.activation(x)
return x
class UNet(torch.nn.Module):
def __init__(
self,
depth: int,
in_channels: int,
out_channels: int = 1,
final_activation: str | None = None,
num_fmaps: int = 64,
fmap_inc_factor: int = 2,
downsample_factor: int = 2,
kernel_size: int = 3,
padding: str = "same",
upsample_mode: str = "nearest",
):
"""A U-Net for 2D input that expects tensors shaped like::
``(batch, channels, height, width)``.
Args:
depth:
The number of levels in the U-Net. 2 is the smallest that really
makes sense for the U-Net architecture, as a one layer U-Net is
basically just 2 conv blocks.
in_channels:
The number of input channels in your dataset.
out_channels (optional):
How many output channels you want. Depends on your task. Defaults to 1.
final_activation (optional):
What activation to use in your final output block. Depends on your task.
Defaults to None.
num_fmaps (optional):
The number of feature maps in the first layer. Defaults to 64.
fmap_inc_factor (optional):
By how much to multiply the number of feature maps between
layers. Layer ``l`` will have ``num_fmaps*fmap_inc_factor**l``
feature maps. Defaults to 2.
downsample_factor (optional):
Factor to use for down- and up-sampling the feature maps between layers.
Defaults to 2.
kernel_size (optional):
Kernel size to use in convolutions on both sides of the UNet.
Defaults to 3.
padding (optional):
How to pad convolutions. Either 'same' or 'valid'. Defaults to "same."
upsample_mode (optional):
The upsampling mode to pass to torch.nn.Upsample. Usually "nearest"
or "bilinear." Defaults to "nearest."
"""
super().__init__()
self.depth = depth
self.in_channels = in_channels
self.out_channels = out_channels
self.final_activation = final_activation
self.num_fmaps = num_fmaps
self.fmap_inc_factor = fmap_inc_factor
self.downsample_factor = downsample_factor
self.kernel_size = kernel_size
self.padding = padding
self.upsample_mode = upsample_mode
# left convolutional passes
self.left_convs = torch.nn.ModuleList()
for level in range(self.depth):
fmaps_in, fmaps_out = self.compute_fmaps_encoder(level)
self.left_convs.append(
ConvBlock(
fmaps_in,
fmaps_out,
self.kernel_size,
self.padding
)
)
# right convolutional passes
self.right_convs = torch.nn.ModuleList()
for level in range(self.depth - 1):
fmaps_in, fmaps_out = self.compute_fmaps_decoder(level)
self.right_convs.append(
ConvBlock(
fmaps_in,
fmaps_out,
self.kernel_size,
self.padding,
)
)
self.downsample = Downsample(self.downsample_factor)
self.upsample = torch.nn.Upsample(
scale_factor=self.downsample_factor,
mode=self.upsample_mode,
)
self.crop_and_concat = CropAndConcat()
self.final_conv = OutputConv(
self.compute_fmaps_decoder(0)[1], self.out_channels, self.final_activation
)
def compute_fmaps_encoder(self, level: int) -> tuple[int, int]:
"""Compute the number of input and output feature maps for
a conv block at a given level of the UNet encoder (left side).
Args:
level (int): The level of the U-Net which we are computing
the feature maps for. Level 0 is the input level, level 1 is
the first downsampled layer, and level=depth - 1 is the bottom layer.
Output (tuple[int, int]): The number of input and output feature maps
of the encoder convolutional pass in the given level.
"""
if level == 0: # Leave out function
fmaps_in = self.in_channels
else:
fmaps_in = self.num_fmaps * self.fmap_inc_factor ** (level - 1)
fmaps_out = self.num_fmaps * self.fmap_inc_factor**level
return fmaps_in, fmaps_out
def compute_fmaps_decoder(self, level: int) -> tuple[int, int]:
"""Compute the number of input and output feature maps for a conv block
at a given level of the UNet decoder (right side). Note:
The bottom layer (depth - 1) is considered an "encoder" conv pass,
so this function is only valid up to depth - 2.
Args:
level (int): The level of the U-Net which we are computing
the feature maps for. Level 0 is the input level, level 1 is
the first downsampled layer, and level=depth - 1 is the bottom layer.
Output (tuple[int, int]): The number of input and output feature maps
of the encoder convolutional pass in the given level.
"""
fmaps_out = self.num_fmaps * self.fmap_inc_factor ** (level) # Leave out function
concat_fmaps = self.compute_fmaps_encoder(level)[
1
] # The channels that come from the skip connection
fmaps_in = concat_fmaps + self.num_fmaps * self.fmap_inc_factor ** (level + 1)
return fmaps_in, fmaps_out
def forward(self, x):
# left side
convolution_outputs = []
layer_input = x
for i in range(self.depth - 1): # leave out center of for loop
conv_out = self.left_convs[i](layer_input)
convolution_outputs.append(conv_out)
downsampled = self.downsample(conv_out)
layer_input = downsampled
# bottom
conv_out = self.left_convs[-1](layer_input)
layer_input = conv_out
# right
for i in range(0, self.depth-1)[::-1]: # leave out center of for loop
upsampled = self.upsample(layer_input)
concat = self.crop_and_concat(convolution_outputs[i], upsampled)
conv_output = self.right_convs[i](concat)
layer_input = conv_output
return self.final_conv(layer_input)