-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
214 lines (167 loc) · 6.93 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
"""The implementation of U-Net and FCRN-A models."""
from typing import Tuple
import torch
from torch import nn
def conv_block(channels: Tuple[int, int],
size: Tuple[int, int],
stride: Tuple[int, int]=(1, 1),
N: int=1):
"""
Create a block with N convolutional layers with ReLU activation function.
The first layer is IN x OUT, and all others - OUT x OUT.
Args:
channels: (IN, OUT) - no. of input and output channels
size: kernel size (fixed for all convolution in a block)
stride: stride (fixed for all convolution in a block)
N: no. of convolutional layers
Returns:
A sequential container of N convolutional layers.
"""
# a single convolution + batch normalization + ReLU block
block = lambda in_channels: nn.Sequential(
nn.Conv2d(in_channels=in_channels,
out_channels=channels[1],
kernel_size=size,
stride=stride,
bias=False,
padding=(size[0] // 2, size[1] // 2)),
nn.BatchNorm2d(num_features=channels[1]),
nn.ReLU()
)
# create and return a sequential container of convolutional layers
# input size = channels[0] for first block and channels[1] for all others
return nn.Sequential(*[block(channels[bool(i)]) for i in range(N)])
class ConvCat(nn.Module):
"""Convolution with upsampling + concatenate block."""
def __init__(self,
channels: Tuple[int, int],
size: Tuple[int, int],
stride: Tuple[int, int]=(1, 1),
N: int=1):
"""
Create a sequential container with convolutional block (see conv_block)
with N convolutional layers and upsampling by factor 2.
"""
super(ConvCat, self).__init__()
self.conv = nn.Sequential(
conv_block(channels, size, stride, N),
nn.Upsample(scale_factor=2)
)
def forward(self, to_conv: torch.Tensor, to_cat: torch.Tensor):
"""Forward pass.
Args:
to_conv: input passed to convolutional block and upsampling
to_cat: input concatenated with the output of a conv block
"""
return torch.cat([self.conv(to_conv), to_cat], dim=1)
class FCRN_A(nn.Module):
"""
Fully Convolutional Regression Network A
Ref. W. Xie et al. 'Microscopy Cell Counting with Fully Convolutional
Regression Networks'
"""
def __init__(self, N: int=1, input_filters: int=3, **kwargs):
"""
Create FCRN-A model with:
* fixed kernel size = (3, 3)
* fixed max pooling kernel size = (2, 2) and upsampling factor = 2
* no. of filters as defined in an original model:
input size -> 32 -> 64 -> 128 -> 512 -> 128 -> 64 -> 1
Args:
N: no. of convolutional layers per block (see conv_block)
input_filters: no. of input channels
"""
super(FCRN_A, self).__init__()
self.model = nn.Sequential(
# downsampling
conv_block(channels=(input_filters, 32), size=(3, 3), N=N),
nn.MaxPool2d(2),
conv_block(channels=(32, 64), size=(3, 3), N=N),
nn.MaxPool2d(2),
conv_block(channels=(64, 128), size=(3, 3), N=N),
nn.MaxPool2d(2),
# "convolutional fully connected"
conv_block(channels=(128, 512), size=(3, 3), N=N),
# upsampling
nn.Upsample(scale_factor=2),
conv_block(channels=(512, 128), size=(3, 3), N=N),
nn.Upsample(scale_factor=2),
conv_block(channels=(128, 64), size=(3, 3), N=N),
nn.Upsample(scale_factor=2),
conv_block(channels=(64, 1), size=(3, 3), N=N),
)
def forward(self, input: torch.Tensor):
"""Forward pass."""
return self.model(input)
class UNet(nn.Module):
"""
U-Net implementation.
Ref. O. Ronneberger et al. "U-net: Convolutional networks for biomedical
image segmentation."
"""
def __init__(self, filters: int=64, input_filters: int=3, **kwargs):
"""
Create U-Net model with:
* fixed kernel size = (3, 3)
* fixed max pooling kernel size = (2, 2) and upsampling factor = 2
* fixed no. of convolutional layers per block = 2 (see conv_block)
* constant no. of filters for convolutional layers
Args:
filters: no. of filters for convolutional layers
input_filters: no. of input channels
"""
super(UNet, self).__init__()
# first block channels size
initial_filters = (input_filters, filters)
# channels size for downsampling
down_filters = (filters, filters)
# channels size for upsampling (input doubled because of concatenate)
up_filters = (2 * filters, filters)
# downsampling
self.block1 = conv_block(channels=initial_filters, size=(3, 3), N=2)
self.block2 = conv_block(channels=down_filters, size=(3, 3), N=2)
self.block3 = conv_block(channels=down_filters, size=(3, 3), N=2)
# upsampling
self.block4 = ConvCat(channels=down_filters, size=(3, 3), N=2)
self.block5 = ConvCat(channels=up_filters, size=(3, 3), N=2)
self.block6 = ConvCat(channels=up_filters, size=(3, 3), N=2)
# density prediction
self.block7 = conv_block(channels=up_filters, size=(3, 3), N=2)
self.density_pred = nn.Conv2d(in_channels=filters, out_channels=1,
kernel_size=(1, 1), bias=False, padding=(1, 1))
def forward(self, input: torch.Tensor):
"""Forward pass."""
# use the same max pooling kernel size (2, 2) across the network
pool = nn.MaxPool2d(2, ceil_mode=True)
# downsampling
block1 = self.block1(input)
pool1 = pool(block1)
block2 = self.block2(pool1)
pool2 = pool(block2)
block3 = self.block3(pool2)
pool3 = pool(block3)
# upsampling
block4 = self.block4(pool3, block3)
block5 = self.block5(block4, block2)
block6 = self.block6(block5, block1)
# density prediction
block7 = self.block7(block6)
return self.density_pred(block7)
# --- PYTESTS --- #
def run_network(network: nn.Module, input_channels: int):
"""Generate a random image, run through network, and check output size."""
sample = torch.ones((1, input_channels, 224, 224))
result = network(input_filters=input_channels)(sample)
assert result.shape == (1, 1, 224, 224)
def test_UNet_color():
"""Test U-Net on RGB images."""
run_network(UNet, 3)
def test_UNet_grayscale():
"""Test U-Net on grayscale images."""
run_network(UNet, 1)
def test_FRCN_color():
"""Test FCRN-A on RGB images."""
run_network(FCRN_A, 3)
def test_FRCN_grayscale():
"""Test FCRN-A on grayscale images."""
run_network(FCRN_A, 1)