This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
forked from fangchangma/sparse-to-dense.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
381 lines (333 loc) · 13.7 KB
/
models.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import torch
from torch import cat
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models
import collections
import math
from typing import Tuple, Any
oheight, owidth = 228, 304
class Unpool(nn.Module):
# Unpool: 2*2 unpooling with zero padding
def __init__(self, num_channels, stride=2):
super(Unpool, self).__init__()
self.num_channels = num_channels
self.stride = stride
# create kernel [1, 0; 0, 0]
self.weights = torch.autograd.Variable(
torch.zeros(
num_channels, 1, stride,
stride).cuda()) # currently not compatible with running on CPU
self.weights[:, :, 0, 0] = 1
def forward(self, x):
return F.conv_transpose2d(
x, self.weights, stride=self.stride, groups=self.num_channels)
def weights_init(m):
# Initialize filters with Gaussian random weights
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.ConvTranspose2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
SKIP_TYPES = ["none", "cat", "sum", "proj"]
class Decoder(nn.Module):
# Decoder is the base class for all decoders
names = ['deconv2', 'deconv3', 'upconv', 'upproj']
def __init__(self, skip_type, additional_input_ch):
super(Decoder, self).__init__()
assert (skip_type in SKIP_TYPES)
self.skip_type = skip_type
self.additional_input_ch = additional_input_ch
self.layer1 = None
self.layer2 = None
self.layer3 = None
self.layer4 = None
if self.skip_type == "proj":
self.proj_layers = dict()
def skip(self, x, x_):
if self.skip_type == "none" or x_ is None:
return x
if self.skip_type == "cat":
return cat([x, x_], dim=1)
if self.skip_type == "sum":
return x + x_[:, :x.shape[1], :, :]
if self.skip_type == "proj":
layer_key = (x_.shape[1], x.shape[1])
if not layer_key in self.proj_layers.keys():
self.proj_layers[layer_key] = nn.Conv2d(
x_.shape[1], x.shape[1], 1, bias=None)
self.proj_layers[layer_key].cuda()
return x + self.proj_layers[layer_key](x_)
raise ValueError(f"Invalid skip_type, {self.skip_type}")
def forward(self, x, x1=None, x2=None, x3=None, x4=None, x5=None):
x = self.skip(x, x1)
x = self.layer1(x)
#print("decoder layer1 output shape =", x.shape)
# print("x2.shape =",x2.shape)
x = self.skip(x, x2)
x = self.layer2(x)
#print("decoder layer2 output shape =", x.shape)
#print("x3.shape =", x3.shape)
x = self.skip(x, x3)
x = self.layer3(x)
#print("decoder layer3 output shape =", x.shape)
x = self.skip(x, x4)
x = self.layer4(x)
#print("decoder layer4 output shape =", x.shape)
x = self.skip(x, x5)
return x
class DeConv(Decoder):
def __init__(self, in_channels, kernel_size, skip_type,
additional_input_ch):
assert kernel_size >= 2, "kernel_size out of range: {}".format(
kernel_size)
super(DeConv, self).__init__(skip_type, additional_input_ch)
def convt(in_channels, i):
stride = 2
padding = (kernel_size - 1) // 2
output_padding = kernel_size % 2
assert -2 - 2 * padding + kernel_size + output_padding == 0, "deconv parameters incorrect"
module_name = "deconv{}".format(kernel_size)
return nn.Sequential(
collections.OrderedDict([
(module_name,
nn.ConvTranspose2d(
in_channels + self.additional_input_ch[i],
in_channels // 2,
kernel_size,
stride,
padding,
output_padding,
bias=False)),
('batchnorm', nn.BatchNorm2d(in_channels // 2)),
('relu', nn.ReLU(inplace=True)),
]))
self.layer1 = convt(in_channels, 0)
self.layer2 = convt(in_channels // 2, 1)
self.layer3 = convt(in_channels // (2**2), 2)
self.layer4 = convt(in_channels // (2**3), 3)
class UpConv(Decoder):
# UpConv decoder consists of 4 upconv modules with decreasing number of channels and increasing feature map size
def upconv_module(self, in_channels):
# UpConv module: unpool -> 5*5 conv -> batchnorm -> ReLU
upconv = nn.Sequential(
collections.OrderedDict([
('unpool', Unpool(in_channels)),
('conv',
nn.Conv2d(
in_channels,
in_channels // 2,
kernel_size=5,
stride=1,
padding=2,
bias=False)),
('batchnorm', nn.BatchNorm2d(in_channels // 2)),
('relu', nn.ReLU()),
]))
return upconv
def __init__(self, in_channels, skip_type, additional_input_ch):
super(UpConv, self).__init__(skip_type, additional_input_ch)
self.layer1 = self.upconv_module(in_channels + additional_input_ch[0])
self.layer2 = self.upconv_module(
in_channels // 2 + additional_input_ch[1])
self.layer3 = self.upconv_module(
in_channels // 4 + additional_input_ch[2])
self.layer4 = self.upconv_module(
in_channels // 8 + additional_input_ch[3])
class UpProj(Decoder):
# UpProj decoder consists of 4 upproj modules with decreasing number of channels and increasing feature map sizes
class UpProjModule(nn.Module):
# UpProj module has two branches, with a Unpool at the start and a ReLu at the end
# upper branch: 5*5 conv -> batchnorm -> ReLU -> 3*3 conv -> batchnorm
# bottom branch: 5*5 conv -> batchnorm
def __init__(self, in_channels, out_channels):
super(UpProj.UpProjModule, self).__init__()
self.unpool = Unpool(in_channels)
self.upper_branch = nn.Sequential(
collections.OrderedDict([
('conv1',
nn.Conv2d(
in_channels,
out_channels,
kernel_size=5,
stride=1,
padding=2,
bias=False)),
('batchnorm1', nn.BatchNorm2d(out_channels)),
('relu', nn.ReLU()),
('conv2',
nn.Conv2d(
out_channels,
out_channels,
kernel_size=3,
stride=1,
padding=1,
bias=False)),
('batchnorm2', nn.BatchNorm2d(out_channels)),
]))
self.bottom_branch = nn.Sequential(
collections.OrderedDict([
('conv',
nn.Conv2d(
in_channels,
out_channels,
kernel_size=5,
stride=1,
padding=2,
bias=False)),
('batchnorm', nn.BatchNorm2d(out_channels)),
]))
self.relu = nn.ReLU()
def forward(self, x):
x = self.unpool(x)
x1 = self.upper_branch(x)
x2 = self.bottom_branch(x)
x = x1 + x2
x = self.relu(x)
return x
def __init__(self, in_channels, skip_type, additional_input_ch):
super(UpProj, self).__init__(skip_type, additional_input_ch)
self.layer1 = self.UpProjModule(in_channels + additional_input_ch[0],
in_channels // 2)
self.layer2 = self.UpProjModule(
in_channels // 2 + additional_input_ch[1], in_channels // 4)
self.layer3 = self.UpProjModule(
in_channels // 4 + additional_input_ch[2], in_channels // 8)
self.layer4 = self.UpProjModule(
in_channels // 8 + additional_input_ch[3], in_channels // 16)
def choose_decoder(decoder, in_channels, additional_input_ch, skip_type):
# iheight, iwidth = 10, 8
if decoder[:6] == 'deconv':
assert len(decoder) == 7
kernel_size = int(decoder[6])
return DeConv(in_channels, kernel_size, skip_type, additional_input_ch)
elif decoder == "upproj":
return UpProj(in_channels, skip_type, additional_input_ch)
elif decoder == "upconv":
return UpConv(in_channels, skip_type, additional_input_ch)
else:
assert False, "invalid option for decoder: {}".format(decoder)
class ResNet(nn.Module):
def __init__(self,
layers: int,
decoder: str,
in_channels: int = 3,
out_channels: int = 1,
pretrained: bool = True,
image_shape: Tuple[int, int] = (oheight, owidth),
skip_type: str = "sum",
square_width=50) -> None:
self.image_shape = image_shape
self.square_width = square_width
if layers not in [18, 34, 50, 101, 152]:
raise RuntimeError(
'Only 18, 34, 50, 101, and 152 layer model are defined for ResNet. Got {}'.
format(layers))
super(ResNet, self).__init__()
pretrained_model = torchvision.models.__dict__['resnet{}'.format(
layers)](pretrained=pretrained)
if in_channels == 3:
self.conv1 = pretrained_model._modules['conv1']
self.bn1 = pretrained_model._modules['bn1']
else:
self.conv1 = nn.Conv2d(
in_channels,
64,
kernel_size=7,
stride=2,
padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
weights_init(self.conv1)
weights_init(self.bn1)
self.relu = pretrained_model._modules['relu']
self.maxpool = pretrained_model._modules['maxpool']
self.layer1 = pretrained_model._modules['layer1']
self.layer2 = pretrained_model._modules['layer2']
self.layer3 = pretrained_model._modules['layer3']
self.layer4 = pretrained_model._modules['layer4']
# clear memory
del pretrained_model
# define number of intermediate channels
if layers <= 34:
num_channels = 512
elif layers >= 50:
num_channels = 2048
self.conv2 = nn.Conv2d(
num_channels, num_channels // 2, kernel_size=1, bias=False)
self.bn2 = nn.BatchNorm2d(num_channels // 2)
extra_channels = (0, 256, 128, 64) if skip_type == "cat" else (0, 0, 0,
0)
self.decoder = choose_decoder(decoder, num_channels // 2,
extra_channels, skip_type)
# setting bias=true doesn't improve accuracy
self.conv3 = nn.Conv2d(
num_channels // 32,
out_channels,
kernel_size=3,
stride=1,
padding=1,
bias=False)
self.bilinear = nn.Upsample(
size=image_shape, mode='bilinear', align_corners=True)
# weight init
self.conv2.apply(weights_init)
self.bn2.apply(weights_init)
self.decoder.apply(weights_init)
self.conv3.apply(weights_init)
@property
def in_channels(self):
return self.conv1.weight.shape[1]
@property
def out_channels(self):
return self.conv3.weight.shape[1]
@property
def input_shape(self):
return (self.in_channels, *self.image_shape)
def forward(self, x: torch.cuda.FloatTensor):
# resnet
assert x.shape[1:] == self.input_shape
#print("input shape =", x.shape)
x0 = self.conv1(x)
self.conv1.output_shape = tuple(x0.shape[1:])
#print("conv1 shape =", x0.shape)
x = self.bn1(x0)
x = self.relu(x)
x = self.maxpool(x)
#print("maxpool output shape =", x.shape)
self.maxpool.output_shape = tuple(x.shape[1:])
x1 = self.layer1(x)
self.layer1.output_shape = tuple(x1.shape[1:])
#print("layer1 output shape =", x1.shape)
x2 = self.layer2(x1)
self.layer2.output_shape = tuple(x2.shape[1:])
#print("layer2 output shape =", x2.shape)
x3 = self.layer3(x2)
self.layer3.output_shape = tuple(x3.shape[1:])
#print("layer3 output shape =", x3.shape)
x4 = self.layer4(x3)
self.layer4.output_shape = tuple(x4.shape[1:])
#print("layer4 output shape =", x4.shape)
x = self.conv2(x4)
self.conv2.output_shape = tuple(x.shape[1:])
#print("conv2 output shape =", x.shape)
x = self.bn2(x)
# decoder
x = self.decoder(x, x2=x3, x3=x2, x4=x1)
self.decoder.output_shape = tuple(x.shape[1:])
#print("decoder output shape =", x.shape)
x = self.conv3(x)
self.conv3.output_shape = tuple(x.shape[1:])
x = self.bilinear(x)
self.bilinear.output_shape = tuple(x.shape[1:])
#print("output shape =", x.shape)
self.output_shape = self.bilinear.output_shape
return x