-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
425 lines (347 loc) · 15 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from __future__ import absolute_import, division, print_function
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import importlib
class conv(nn.Module):
def __init__(self, num_in_layers, num_out_layers, kernel_size, stride):
super(conv, self).__init__()
self.kernel_size = kernel_size
self.conv_base = nn.Conv2d(num_in_layers, num_out_layers, kernel_size=kernel_size, stride=stride)
self.normalize = nn.BatchNorm2d(num_out_layers)
def forward(self, x):
p = int(np.floor((self.kernel_size-1)/2))
p2d = (p, p, p, p)
x = self.conv_base(F.pad(x, p2d))
x = self.normalize(x)
return F.elu(x, inplace=True)
class convblock(nn.Module):
def __init__(self, num_in_layers, num_out_layers, kernel_size):
super(convblock, self).__init__()
self.conv1 = conv(num_in_layers, num_out_layers, kernel_size, 1)
self.conv2 = conv(num_out_layers, num_out_layers, kernel_size, 2)
def forward(self, x):
x = self.conv1(x)
return self.conv2(x)
class maxpool(nn.Module):
def __init__(self, kernel_size):
super(maxpool, self).__init__()
self.kernel_size = kernel_size
def forward(self, x):
p = int(np.floor((self.kernel_size-1) / 2))
p2d = (p, p, p, p)
return F.max_pool2d(F.pad(x, p2d), self.kernel_size, stride=2)
class resconv(nn.Module):
def __init__(self, num_in_layers, num_out_layers, stride):
super(resconv, self).__init__()
self.num_out_layers = num_out_layers
self.stride = stride
self.conv1 = conv(num_in_layers, num_out_layers, 1, 1)
self.conv2 = conv(num_out_layers, num_out_layers, 3, stride)
self.conv3 = nn.Conv2d(num_out_layers, 4*num_out_layers, kernel_size=1, stride=1)
self.conv4 = nn.Conv2d(num_in_layers, 4*num_out_layers, kernel_size=1, stride=stride)
self.normalize = nn.BatchNorm2d(4*num_out_layers)
def forward(self, x):
# do_proj = x.size()[1] != self.num_out_layers or self.stride == 2
do_proj = True
shortcut = []
x_out = self.conv1(x)
x_out = self.conv2(x_out)
x_out = self.conv3(x_out)
if do_proj:
shortcut = self.conv4(x)
else:
shortcut = x
return F.elu(self.normalize(x_out + shortcut), inplace=True)
class resconv_basic(nn.Module):
# for resnet18
def __init__(self, num_in_layers, num_out_layers, stride):
super(resconv_basic, self).__init__()
self.num_out_layers = num_out_layers
self.stride = stride
self.conv1 = conv(num_in_layers, num_out_layers, 3, stride)
self.conv2 = conv(num_out_layers, num_out_layers, 3, 1)
self.conv3 = nn.Conv2d(num_in_layers, num_out_layers, kernel_size=1, stride=stride)
self.normalize = nn.BatchNorm2d(num_out_layers)
def forward(self, x):
# do_proj = x.size()[1] != self.num_out_layers or self.stride == 2
do_proj = True
shortcut = []
x_out = self.conv1(x)
x_out = self.conv2(x_out)
if do_proj:
shortcut = self.conv3(x)
else:
shortcut = x
return F.elu(self.normalize(x_out + shortcut), inplace=True)
def resblock(num_in_layers, num_out_layers, num_blocks, stride):
layers = []
layers.append(resconv(num_in_layers, num_out_layers, stride))
for i in range(1, num_blocks - 1):
layers.append(resconv(4 * num_out_layers, num_out_layers, 1))
layers.append(resconv(4 * num_out_layers, num_out_layers, 1))
return nn.Sequential(*layers)
def resblock_basic(num_in_layers, num_out_layers, num_blocks, stride):
layers = []
layers.append(resconv_basic(num_in_layers, num_out_layers, stride))
for i in range(1, num_blocks):
layers.append(resconv_basic(num_out_layers, num_out_layers, 1))
return nn.Sequential(*layers)
class upconv(nn.Module):
def __init__(self, num_in_layers, num_out_layers, kernel_size, scale):
super(upconv, self).__init__()
self.scale = scale
self.conv1 = conv(num_in_layers, num_out_layers, kernel_size, 1)
def forward(self, x):
x = nn.functional.interpolate(x, scale_factor=self.scale, mode='bilinear', align_corners=True)
return self.conv1(x)
class get_disp(nn.Module):
def __init__(self, num_in_layers):
super(get_disp, self).__init__()
self.conv1 = nn.Conv2d(num_in_layers, 2, kernel_size=3, stride=1)
self.normalize = nn.BatchNorm2d(2)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
p = 1
p2d = (p, p, p, p)
x = self.conv1(F.pad(x, p2d))
x = self.normalize(x)
return 0.3 * self.sigmoid(x)
class Resnet50_md(nn.Module):
def __init__(self, num_in_layers):
super(Resnet50_md, self).__init__()
# encoder
self.conv1 = conv(num_in_layers, 64, 7, 2) # H/2 - 64D
self.pool1 = maxpool(3) # H/4 - 64D
self.conv2 = resblock(64, 64, 3, 2) # H/8 - 256D
self.conv3 = resblock(256, 128, 4, 2) # H/16 - 512D
self.conv4 = resblock(512, 256, 6, 2) # H/32 - 1024D
self.conv5 = resblock(1024, 512, 3, 2) # H/64 - 2048D
# decoder
self.upconv6 = upconv(2048, 512, 3, 2)
self.iconv6 = conv(1024 + 512, 512, 3, 1)
self.upconv5 = upconv(512, 256, 3, 2)
self.iconv5 = conv(512+256, 256, 3, 1)
self.upconv4 = upconv(256, 128, 3, 2)
self.iconv4 = conv(256+128, 128, 3, 1)
self.disp4_layer = get_disp(128)
self.upconv3 = upconv(128, 64, 3, 2)
self.iconv3 = conv(64+64+2, 64, 3, 1)
self.disp3_layer = get_disp(64)
self.upconv2 = upconv(64, 32, 3, 2)
self.iconv2 = conv(32+64+2, 32, 3, 1)
self.disp2_layer = get_disp(32)
self.upconv1 = upconv(32, 16, 3, 2)
self.iconv1 = conv(16+2, 16, 3, 1)
self.disp1_layer = get_disp(16)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
def forward(self, x):
# encoder
x1 = self.conv1(x)
x_pool1 = self.pool1(x1)
x2 = self.conv2(x_pool1)
x3 = self.conv3(x2)
x4 = self.conv4(x3)
x5 = self.conv5(x4)
# skips
skip1 = x1
skip2 = x_pool1
skip3 = x2
skip4 = x3
skip5 = x4
# decoder
upconv6 = self.upconv6(x5)
concat6 = torch.cat((upconv6, skip5), 1)
iconv6 = self.iconv6(concat6)
upconv5 = self.upconv5(iconv6)
concat5 = torch.cat((upconv5, skip4), 1)
iconv5 = self.iconv5(concat5)
upconv4 = self.upconv4(iconv5)
concat4 = torch.cat((upconv4, skip3), 1)
iconv4 = self.iconv4(concat4)
self.disp4 = self.disp4_layer(iconv4)
self.udisp4 = nn.functional.interpolate(self.disp4, scale_factor=2, mode='bilinear', align_corners=True)
upconv3 = self.upconv3(iconv4)
concat3 = torch.cat((upconv3, skip2, self.udisp4), 1)
iconv3 = self.iconv3(concat3)
self.disp3 = self.disp3_layer(iconv3)
self.udisp3 = nn.functional.interpolate(self.disp3, scale_factor=2, mode='bilinear', align_corners=True)
upconv2 = self.upconv2(iconv3)
concat2 = torch.cat((upconv2, skip1, self.udisp3), 1)
iconv2 = self.iconv2(concat2)
self.disp2 = self.disp2_layer(iconv2)
self.udisp2 = nn.functional.interpolate(self.disp2, scale_factor=2, mode='bilinear', align_corners=True)
upconv1 = self.upconv1(iconv2)
concat1 = torch.cat((upconv1, self.udisp2), 1)
iconv1 = self.iconv1(concat1)
self.disp1 = self.disp1_layer(iconv1)
return self.disp1, self.disp2, self.disp3, self.disp4
class Resnet18_md(nn.Module):
def __init__(self, num_in_layers):
super(Resnet18_md, self).__init__()
# encoder
self.conv1 = conv(num_in_layers, 64, 7, 2) # H/2 - 64D
self.pool1 = maxpool(3) # H/4 - 64D
self.conv2 = resblock_basic(64, 64, 2, 2) # H/8 - 64D
self.conv3 = resblock_basic(64, 128, 2, 2) # H/16 - 128D
self.conv4 = resblock_basic(128, 256, 2, 2) # H/32 - 256D
self.conv5 = resblock_basic(256, 512, 2, 2) # H/64 - 512D
# decoder
self.upconv6 = upconv(512, 512, 3, 2)
self.iconv6 = conv(256+512, 512, 3, 1)
self.upconv5 = upconv(512, 256, 3, 2)
self.iconv5 = conv(128+256, 256, 3, 1)
self.upconv4 = upconv(256, 128, 3, 2)
self.iconv4 = conv(64+128, 128, 3, 1)
self.disp4_layer = get_disp(128)
self.upconv3 = upconv(128, 64, 3, 2)
self.iconv3 = conv(64+64 + 2, 64, 3, 1)
self.disp3_layer = get_disp(64)
self.upconv2 = upconv(64, 32, 3, 2)
self.iconv2 = conv(64+32 + 2, 32, 3, 1)
self.disp2_layer = get_disp(32)
self.upconv1 = upconv(32, 16, 3, 2)
self.iconv1 = conv(16+2, 16, 3, 1)
self.disp1_layer = get_disp(16)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
def forward(self, x):
# encoder
x1 = self.conv1(x)
x_pool1 = self.pool1(x1)
x2 = self.conv2(x_pool1)
x3 = self.conv3(x2)
x4 = self.conv4(x3)
x5 = self.conv5(x4)
# skips
skip1 = x1
skip2 = x_pool1
skip3 = x2
skip4 = x3
skip5 = x4
# decoder
upconv6 = self.upconv6(x5)
concat6 = torch.cat((upconv6, skip5), 1)
iconv6 = self.iconv6(concat6)
upconv5 = self.upconv5(iconv6)
concat5 = torch.cat((upconv5, skip4), 1)
iconv5 = self.iconv5(concat5)
upconv4 = self.upconv4(iconv5)
concat4 = torch.cat((upconv4, skip3), 1)
iconv4 = self.iconv4(concat4)
self.disp4 = self.disp4_layer(iconv4)
self.udisp4 = nn.functional.interpolate(self.disp4, scale_factor=2, mode='bilinear', align_corners=True)
upconv3 = self.upconv3(iconv4)
concat3 = torch.cat((upconv3, skip2, self.udisp4), 1)
iconv3 = self.iconv3(concat3)
self.disp3 = self.disp3_layer(iconv3)
self.udisp3 = nn.functional.interpolate(self.disp3, scale_factor=2, mode='bilinear', align_corners=True)
upconv2 = self.upconv2(iconv3)
concat2 = torch.cat((upconv2, skip1, self.udisp3), 1)
iconv2 = self.iconv2(concat2)
self.disp2 = self.disp2_layer(iconv2)
self.udisp2 = nn.functional.interpolate(self.disp2, scale_factor=2, mode='bilinear', align_corners=True)
upconv1 = self.upconv1(iconv2)
concat1 = torch.cat((upconv1, self.udisp2), 1)
iconv1 = self.iconv1(concat1)
self.disp1 = self.disp1_layer(iconv1)
return self.disp1, self.disp2, self.disp3, self.disp4
def class_for_name(module_name, class_name):
# load the module, will raise ImportError if module cannot be loaded
m = importlib.import_module(module_name)
# get the class, will raise AttributeError if class cannot be found
return getattr(m, class_name)
class ResnetModel(nn.Module):
def __init__(self, num_in_layers, encoder='resnet18', pretrained=False):
super(ResnetModel, self).__init__()
assert encoder in ['resnet18', 'resnet34', 'resnet50',\
'resnet101', 'resnet152'],\
"Incorrect encoder type"
if encoder in ['resnet18', 'resnet34']:
filters = [64, 128, 256, 512]
else:
filters = [256, 512, 1024, 2048]
resnet = class_for_name("torchvision.models", encoder)\
(pretrained=pretrained)
if num_in_layers != 3: # Number of input channels
self.firstconv = nn.Conv2d(num_in_layers, 64,
kernel_size=(7, 7), stride=(2, 2),
padding=(3, 3), bias=False)
else:
self.firstconv = resnet.conv1 # H/2
self.firstbn = resnet.bn1
self.firstrelu = resnet.relu
self.firstmaxpool = resnet.maxpool # H/4
# encoder
self.encoder1 = resnet.layer1 # H/4
self.encoder2 = resnet.layer2 # H/8
self.encoder3 = resnet.layer3 # H/16
self.encoder4 = resnet.layer4 # H/32
# decoder
self.upconv6 = upconv(filters[3], 512, 3, 2)
self.iconv6 = conv(filters[2] + 512, 512, 3, 1)
self.upconv5 = upconv(512, 256, 3, 2)
self.iconv5 = conv(filters[1] + 256, 256, 3, 1)
self.upconv4 = upconv(256, 128, 3, 2)
self.iconv4 = conv(filters[0] + 128, 128, 3, 1)
self.disp4_layer = get_disp(128)
self.upconv3 = upconv(128, 64, 3, 1) #
self.iconv3 = conv(64 + 64 + 2, 64, 3, 1)
self.disp3_layer = get_disp(64)
self.upconv2 = upconv(64, 32, 3, 2)
self.iconv2 = conv(64 + 32 + 2, 32, 3, 1)
self.disp2_layer = get_disp(32)
self.upconv1 = upconv(32, 16, 3, 2)
self.iconv1 = conv(16 + 2, 16, 3, 1)
self.disp1_layer = get_disp(16)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
def forward(self, x):
# encoder
x_first_conv = self.firstconv(x)
x = self.firstbn(x_first_conv)
x = self.firstrelu(x)
x_pool1 = self.firstmaxpool(x)
x1 = self.encoder1(x_pool1)
x2 = self.encoder2(x1)
x3 = self.encoder3(x2)
x4 = self.encoder4(x3)
# skips
skip1 = x_first_conv
skip2 = x_pool1
skip3 = x1
skip4 = x2
skip5 = x3
# decoder
upconv6 = self.upconv6(x4)
concat6 = torch.cat((upconv6, skip5), 1)
iconv6 = self.iconv6(concat6)
upconv5 = self.upconv5(iconv6)
concat5 = torch.cat((upconv5, skip4), 1)
iconv5 = self.iconv5(concat5)
upconv4 = self.upconv4(iconv5)
concat4 = torch.cat((upconv4, skip3), 1)
iconv4 = self.iconv4(concat4)
self.disp4 = self.disp4_layer(iconv4)
self.udisp4 = nn.functional.interpolate(self.disp4, scale_factor=1, mode='bilinear', align_corners=True)
self.disp4 = nn.functional.interpolate(self.disp4, scale_factor=0.5, mode='bilinear', align_corners=True)
upconv3 = self.upconv3(iconv4)
concat3 = torch.cat((upconv3, skip2, self.udisp4), 1)
iconv3 = self.iconv3(concat3)
self.disp3 = self.disp3_layer(iconv3)
self.udisp3 = nn.functional.interpolate(self.disp3, scale_factor=2, mode='bilinear', align_corners=True)
upconv2 = self.upconv2(iconv3)
concat2 = torch.cat((upconv2, skip1, self.udisp3), 1)
iconv2 = self.iconv2(concat2)
self.disp2 = self.disp2_layer(iconv2)
self.udisp2 = nn.functional.interpolate(self.disp2, scale_factor=2, mode='bilinear', align_corners=True)
upconv1 = self.upconv1(iconv2)
concat1 = torch.cat((upconv1, self.udisp2), 1)
iconv1 = self.iconv1(concat1)
self.disp1 = self.disp1_layer(iconv1)
return self.disp1, self.disp2, self.disp3, self.disp4