Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

laddernet.py 22 KB

You have to be logged in to leave a comment. Sign In
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. up_kwargs = {'mode': 'bilinear', 'align_corners': True}
  5. # from encoding.nn import SyncBatchNorm # FIXME - ORIGINAL CODE TORCH-ENCODING
  6. class LadderBottleneck(nn.Module):
  7. """ResNet Bottleneck
  8. """
  9. # pylint: disable=unused-argument
  10. expansion = 4
  11. def __init__(self, inplanes, planes, stride=1, dilation=1,
  12. downsample=None, previous_dilation=1, norm_layer=None):
  13. super().__init__()
  14. self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
  15. self.bn1 = norm_layer(planes)
  16. self.conv2 = nn.Conv2d(
  17. planes, planes, kernel_size=3, stride=stride,
  18. padding=dilation, dilation=dilation, bias=False)
  19. self.bn2 = norm_layer(planes)
  20. self.conv3 = nn.Conv2d(
  21. planes, planes * 4, kernel_size=1, bias=False)
  22. self.bn3 = norm_layer(planes * 4)
  23. self.relu = nn.ReLU(inplace=True)
  24. self.downsample = downsample
  25. self.dilation = dilation
  26. self.stride = stride
  27. def _sum_each(self, x, y):
  28. assert (len(x) == len(y))
  29. z = []
  30. for i in range(len(x)):
  31. z.append(x[i] + y[i])
  32. return z
  33. def forward(self, x):
  34. residual = x
  35. out = self.conv1(x)
  36. out = self.bn1(out)
  37. out = self.relu(out)
  38. out = self.conv2(out)
  39. out = self.bn2(out)
  40. out = self.relu(out)
  41. out = self.conv3(out)
  42. out = self.bn3(out)
  43. if self.downsample is not None:
  44. residual = self.downsample(x)
  45. out += residual
  46. out = self.relu(out)
  47. return out
  48. class LadderResNet(nn.Module):
  49. """Dilated Pre-trained ResNet Model, which preduces the stride of 8 featuremaps at conv5.
  50. Parameters
  51. ----------
  52. block : Block
  53. Class for the residual block. Options are BasicBlockV1, BottleneckV1.
  54. layers : list of int
  55. Numbers of layers in each block
  56. classes : int, default 1000
  57. Number of classification classes.
  58. dilated : bool, default False
  59. Applying dilation strategy to pretrained ResNet yielding a stride-8 model,
  60. typically used in Semantic Segmentation.
  61. norm_layer : object
  62. Normalization layer used in backbone network (default: :class:`mxnet.gluon.nn.BatchNorm`;
  63. for Synchronized Cross-GPU BachNormalization).
  64. Reference:
  65. - He, Kaiming, et al. "Deep residual learning for image recognition." Proceedings of the IEEE conference on computer vision and pattern recognition. 2016.
  66. - Yu, Fisher, and Vladlen Koltun. "Multi-scale context aggregation by dilated convolutions."
  67. """
  68. # pylint: disable=unused-variable
  69. # def __init__(self, block, layers, num_classes=1000, dilated=False, norm_layer=SyncBatchNorm): # FIXME - ORIGINAL CODE
  70. def __init__(self, block, layers, num_classes=1000, dilated=False,
  71. norm_layer=nn.BatchNorm2d): # FIXME - TIME MEASUREMENT CODE
  72. self.inplanes = 64
  73. super().__init__()
  74. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
  75. bias=False)
  76. self.bn1 = norm_layer(64)
  77. self.relu = nn.ReLU(inplace=True)
  78. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  79. self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer)
  80. self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
  81. if dilated:
  82. self.layer3 = self._make_layer(block, 256, layers[2], stride=1,
  83. dilation=2, norm_layer=norm_layer)
  84. self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
  85. dilation=4, norm_layer=norm_layer)
  86. else:
  87. self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
  88. norm_layer=norm_layer)
  89. self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
  90. norm_layer=norm_layer)
  91. self.avgpool = nn.AvgPool2d(7)
  92. self.fc = nn.Linear(512 * block.expansion, num_classes)
  93. for m in self.modules():
  94. import math
  95. if isinstance(m, nn.Conv2d):
  96. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  97. m.weight.data.normal_(0, math.sqrt(2. / n))
  98. elif isinstance(m, norm_layer):
  99. m.weight.data.fill_(1)
  100. m.bias.data.zero_()
  101. def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None):
  102. downsample = None
  103. if stride != 1 or self.inplanes != planes * block.expansion:
  104. downsample = nn.Sequential(
  105. nn.Conv2d(self.inplanes, planes * block.expansion,
  106. kernel_size=1, stride=stride, bias=False),
  107. norm_layer(planes * block.expansion),
  108. )
  109. layers = []
  110. if dilation == 1 or dilation == 2:
  111. layers.append(block(self.inplanes, planes, stride, dilation=1,
  112. downsample=downsample, previous_dilation=dilation, norm_layer=norm_layer))
  113. elif dilation == 4:
  114. layers.append(block(self.inplanes, planes, stride, dilation=2,
  115. downsample=downsample, previous_dilation=dilation, norm_layer=norm_layer))
  116. else:
  117. raise RuntimeError("=> unknown dilation size: {}".format(dilation))
  118. self.inplanes = planes * block.expansion
  119. for i in range(1, blocks):
  120. layers.append(block(self.inplanes, planes, dilation=dilation, previous_dilation=dilation,
  121. norm_layer=norm_layer))
  122. return nn.Sequential(*layers)
  123. def forward(self, x):
  124. x = self.conv1(x)
  125. x = self.bn1(x)
  126. x = self.relu(x)
  127. x = self.maxpool(x)
  128. x = self.layer1(x)
  129. x = self.layer2(x)
  130. x = self.layer3(x)
  131. x = self.layer4(x)
  132. x = self.avgpool(x)
  133. x = x.view(x.size(0), -1)
  134. x = self.fc(x)
  135. return x
  136. class LadderNetBackBone503433(LadderResNet):
  137. def __init__(self, num_classes: int):
  138. super().__init__(LadderBottleneck, [3, 4, 3, 3], num_classes=num_classes)
  139. class LadderNetBackBone50(LadderResNet):
  140. def __init__(self, num_classes: int):
  141. super().__init__(LadderBottleneck, [3, 4, 6, 3], num_classes=num_classes)
  142. class LadderNetBackBone101(LadderResNet):
  143. def __init__(self, num_classes: int):
  144. super().__init__(LadderBottleneck, [3, 4, 23, 3], num_classes=num_classes)
  145. class BaseNet(nn.Module):
  146. def __init__(self, nclass, backbone, aux, se_loss, dilated=True, norm_layer=None,
  147. base_size=576, crop_size=608, mean=[.485, .456, .406],
  148. std=[.229, .224, .225], root='~/.encoding/models'):
  149. super(BaseNet, self).__init__()
  150. self.nclass = nclass
  151. self.aux = aux
  152. self.se_loss = se_loss
  153. self.mean = mean
  154. self.std = std
  155. self.base_size = base_size
  156. self.crop_size = crop_size
  157. self.image_size = self.crop_size
  158. # copying modules from pretrained models
  159. if backbone == 'resnet50':
  160. self.backbone = LadderNetBackBone50(num_classes=1000)
  161. elif backbone == 'resnet50_3433':
  162. self.backbone = LadderNetBackBone503433(num_classes=1000)
  163. elif backbone == 'resnet101':
  164. self.backbone = LadderNetBackBone101(num_classes=1000)
  165. # elif backbone == 'resnet152':
  166. # self.pretrained = resnet.resnet152(pretrained=True, dilated=dilated,
  167. # norm_layer=norm_layer, root=root)
  168. # elif backbone == 'resnet18':
  169. # self.pretrained = resnet.resnet18(pretrained=True, dilated=dilated,
  170. # norm_layer=norm_layer, root=root)
  171. # elif backbone == 'resnet34':
  172. # self.pretrained = resnet.resnet34(pretrained=True, dilated=dilated,
  173. # norm_layer=norm_layer, root=root)
  174. else:
  175. raise RuntimeError('unknown backbone: {}'.format(backbone))
  176. # bilinear upsample options
  177. self._up_kwargs = up_kwargs
  178. def base_forward(self, x):
  179. x = self.backbone.conv1(x)
  180. x = self.backbone.bn1(x)
  181. x = self.backbone.relu(x)
  182. x = self.backbone.maxpool(x)
  183. c1 = self.backbone.layer1(x)
  184. c2 = self.backbone.layer2(c1)
  185. c3 = self.backbone.layer3(c2)
  186. c4 = self.backbone.layer4(c3)
  187. return c1, c2, c3, c4
  188. # def evaluate(self, x, target=None):
  189. # pred = self.forward(x)
  190. # if isinstance(pred, (tuple, list)):
  191. # pred = pred[0]
  192. # if target is None:
  193. # return pred
  194. # correct, labeled = batch_pix_accuracy(pred.data, target.data)
  195. # inter, union = batch_intersection_union(pred.data, target.data, self.nclass)
  196. # return correct, labeled, inter, union
  197. drop = 0.25
  198. def conv3x3(in_planes, out_planes, stride=1):
  199. """3x3 convolution with padding"""
  200. return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
  201. padding=1, bias=True)
  202. class BasicBlock(nn.Module):
  203. expansion = 1
  204. def __init__(self, inplanes, planes, stride=1, rate=1, downsample=None):
  205. super(BasicBlock, self).__init__()
  206. if inplanes != planes:
  207. self.conv0 = conv3x3(inplanes, planes, rate)
  208. self.inplanes = inplanes
  209. self.planes = planes
  210. self.conv1 = conv3x3(planes, planes, stride)
  211. self.bn1 = nn.BatchNorm2d(planes)
  212. self.relu = nn.ReLU(inplace=True)
  213. # self.conv2 = conv3x3(planes, planes)
  214. self.bn2 = nn.BatchNorm2d(planes)
  215. self.downsample = downsample
  216. self.stride = stride
  217. self.drop = nn.Dropout2d(p=drop)
  218. def forward(self, x):
  219. if self.inplanes != self.planes:
  220. x = self.conv0(x)
  221. x = F.relu(x)
  222. out = self.conv1(x)
  223. out = self.bn1(out)
  224. out = self.relu(out)
  225. out = self.drop(out)
  226. out1 = self.conv1(out)
  227. out1 = self.bn2(out1)
  228. # out1 = self.relu(out1)
  229. out2 = out1 + x
  230. return F.relu(out2)
  231. class Bottleneck(nn.Module):
  232. expansion = 4
  233. def __init__(self, inplanes, planes, stride=1, downsample=None):
  234. super(Bottleneck, self).__init__()
  235. self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
  236. self.bn1 = nn.BatchNorm2d(planes)
  237. self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
  238. padding=1, bias=False)
  239. self.bn2 = nn.BatchNorm2d(planes)
  240. self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
  241. self.bn3 = nn.BatchNorm2d(planes * self.expansion)
  242. self.relu = nn.ReLU(inplace=True)
  243. self.downsample = downsample
  244. self.stride = stride
  245. def forward(self, x):
  246. residual = x
  247. out = self.conv1(x)
  248. out = self.bn1(out)
  249. out = self.relu(out)
  250. out = self.conv2(out)
  251. out = self.bn2(out)
  252. out = self.relu(out)
  253. out = self.conv3(out)
  254. out = self.bn3(out)
  255. if self.downsample is not None:
  256. residual = self.downsample(x)
  257. out += residual
  258. out = self.relu(out)
  259. return out
  260. class Initial_LadderBlock(nn.Module):
  261. def __init__(self, planes, layers, kernel=3, block=BasicBlock, inplanes=3):
  262. super().__init__()
  263. self.planes = planes
  264. self.layers = layers
  265. self.kernel = kernel
  266. self.padding = int((kernel - 1) / 2)
  267. self.inconv = nn.Conv2d(in_channels=inplanes, out_channels=planes,
  268. kernel_size=3, stride=1, padding=1, bias=True)
  269. self.in_bn = nn.BatchNorm2d(planes)
  270. # create module list for down branch
  271. self.down_module_list = nn.ModuleList()
  272. for i in range(0, layers):
  273. self.down_module_list.append(block(planes * (2 ** i), planes * (2 ** i)))
  274. # use strided conv instead of poooling
  275. self.down_conv_list = nn.ModuleList()
  276. for i in range(0, layers):
  277. self.down_conv_list.append(
  278. nn.Conv2d(planes * 2 ** i, planes * 2 ** (i + 1), stride=2, kernel_size=kernel,
  279. padding=self.padding))
  280. # create module for bottom block
  281. self.bottom = block(planes * (2 ** layers), planes * (2 ** layers))
  282. # create module list for up branch
  283. self.up_conv_list = nn.ModuleList()
  284. self.up_dense_list = nn.ModuleList()
  285. for i in range(0, layers):
  286. self.up_conv_list.append(nn.ConvTranspose2d(in_channels=planes * 2 ** (layers - i),
  287. out_channels=planes * 2 ** max(0, layers - i - 1),
  288. kernel_size=3,
  289. stride=2, padding=1, output_padding=1, bias=True))
  290. self.up_dense_list.append(
  291. block(planes * 2 ** max(0, layers - i - 1), planes * 2 ** max(0, layers - i - 1)))
  292. def forward(self, x):
  293. out = self.inconv(x)
  294. out = self.in_bn(out)
  295. out = F.relu(out)
  296. down_out = []
  297. # down branch
  298. for i in range(0, self.layers):
  299. out = self.down_module_list[i](out)
  300. down_out.append(out)
  301. out = self.down_conv_list[i](out)
  302. out = F.relu(out)
  303. # bottom branch
  304. out = self.bottom(out)
  305. bottom = out
  306. # up branch
  307. up_out = []
  308. up_out.append(bottom)
  309. for j in range(0, self.layers):
  310. out = self.up_conv_list[j](out) + down_out[self.layers - j - 1]
  311. # out = F.relu(out)
  312. out = self.up_dense_list[j](out)
  313. up_out.append(out)
  314. return up_out
  315. class Decoder(nn.Module):
  316. def __init__(self, planes, layers, kernel=3, block=BasicBlock):
  317. super().__init__()
  318. self.planes = planes
  319. self.layers = layers
  320. self.kernel = kernel
  321. self.padding = int((kernel - 1) / 2)
  322. self.inconv = block(planes, planes)
  323. # create module for bottom block
  324. self.bottom = block(planes * (2 ** (layers - 1)), planes * (2 ** (layers - 1)))
  325. # create module list for up branch
  326. self.up_conv_list = nn.ModuleList()
  327. self.up_dense_list = nn.ModuleList()
  328. for i in range(0, layers - 1):
  329. self.up_conv_list.append(
  330. nn.ConvTranspose2d(planes * 2 ** (layers - 1 - i), planes * 2 ** max(0, layers - i - 2),
  331. kernel_size=3,
  332. stride=2, padding=1, output_padding=1, bias=True))
  333. self.up_dense_list.append(
  334. block(planes * 2 ** max(0, layers - i - 2), planes * 2 ** max(0, layers - i - 2)))
  335. def forward(self, x):
  336. # bottom branch
  337. out = self.bottom(x[-1])
  338. bottom = out
  339. # up branch
  340. up_out = []
  341. up_out.append(bottom)
  342. for j in range(0, self.layers - 1):
  343. out = self.up_conv_list[j](out) + x[self.layers - j - 2]
  344. # out = F.relu(out)
  345. out = self.up_dense_list[j](out)
  346. up_out.append(out)
  347. return up_out
  348. class LadderBlock(nn.Module):
  349. def __init__(self, planes, layers, kernel=3, block=BasicBlock):
  350. super().__init__()
  351. self.planes = planes
  352. self.layers = layers
  353. self.kernel = kernel
  354. self.padding = int((kernel - 1) / 2)
  355. self.inconv = block(planes, planes)
  356. # create module list for down branch
  357. self.down_module_list = nn.ModuleList()
  358. for i in range(0, layers - 1):
  359. self.down_module_list.append(block(planes * (2 ** i), planes * (2 ** i)))
  360. # use strided conv instead of pooling
  361. self.down_conv_list = nn.ModuleList()
  362. for i in range(0, layers - 1):
  363. self.down_conv_list.append(
  364. nn.Conv2d(planes * 2 ** i, planes * 2 ** (i + 1), stride=2, kernel_size=kernel,
  365. padding=self.padding))
  366. # create module for bottom block
  367. self.bottom = block(planes * (2 ** (layers - 1)), planes * (2 ** (layers - 1)))
  368. # create module list for up branch
  369. self.up_conv_list = nn.ModuleList()
  370. self.up_dense_list = nn.ModuleList()
  371. for i in range(0, layers - 1):
  372. self.up_conv_list.append(
  373. nn.ConvTranspose2d(planes * 2 ** (layers - i - 1), planes * 2 ** max(0, layers - i - 2),
  374. kernel_size=3,
  375. stride=2, padding=1, output_padding=1, bias=True))
  376. self.up_dense_list.append(
  377. block(planes * 2 ** max(0, layers - i - 2), planes * 2 ** max(0, layers - i - 2)))
  378. def forward(self, x):
  379. out = self.inconv(x[-1])
  380. down_out = []
  381. # down branch
  382. for i in range(0, self.layers - 1):
  383. out = out + x[-i - 1]
  384. out = self.down_module_list[i](out)
  385. down_out.append(out)
  386. out = self.down_conv_list[i](out)
  387. out = F.relu(out)
  388. # bottom branch
  389. out = self.bottom(out)
  390. bottom = out
  391. # up branch
  392. up_out = []
  393. up_out.append(bottom)
  394. for j in range(0, self.layers - 1):
  395. out = self.up_conv_list[j](out) + down_out[self.layers - j - 2]
  396. # out = F.relu(out)
  397. out = self.up_dense_list[j](out)
  398. up_out.append(out)
  399. return up_out
  400. class Final_LadderBlock(nn.Module):
  401. def __init__(self, planes, layers, kernel=3, block=BasicBlock, inplanes=3):
  402. super().__init__()
  403. self.block = LadderBlock(planes, layers, kernel=kernel, block=block)
  404. def forward(self, x):
  405. out = self.block(x)
  406. return out[-1]
  407. class FCNHead(nn.Module):
  408. def __init__(self, in_channels, out_channels, norm_layer):
  409. super(FCNHead, self).__init__()
  410. inter_channels = in_channels // 4
  411. self.conv5 = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
  412. norm_layer(inter_channels),
  413. nn.ReLU(),
  414. nn.Dropout2d(0.1, False),
  415. nn.Conv2d(inter_channels, out_channels, 1))
  416. def forward(self, x):
  417. return self.conv5(x)
  418. class LadderNet(BaseNet):
  419. def __init__(self, nclass, backbone, aux=True, se_loss=True, lateral=False, arch_params=None,
  420. # norm_layer=SyncBatchNorm, dilated=False, **kwargs): # FIXME - ORIGINAL CODE TORCH-ENCODING
  421. norm_layer=nn.BatchNorm2d, dilated=False, **kwargs): # FIXME - TIME MEASUREMENT CODE
  422. super().__init__(nclass, backbone, aux, se_loss, norm_layer=norm_layer, dilated=dilated, **kwargs)
  423. self.head = LadderHead(base_inchannels=256, base_outchannels=64, out_channels=nclass,
  424. norm_layer=norm_layer, se_loss=se_loss, nclass=nclass, up_kwargs=self._up_kwargs)
  425. if aux:
  426. self.auxlayer = FCNHead(1024, nclass, norm_layer=norm_layer)
  427. def forward(self, x):
  428. imsize = x.size()[2:]
  429. features = self.base_forward(x)
  430. x = list(self.head(features))
  431. x[0] = F.upsample(x[0], imsize, **self._up_kwargs)
  432. if self.aux:
  433. auxout = self.auxlayer(features[2])
  434. auxout = F.upsample(auxout, imsize, **self._up_kwargs)
  435. x.append(auxout)
  436. return tuple(x)
  437. class LadderHead(nn.Module):
  438. def __init__(self, base_inchannels, base_outchannels, out_channels, norm_layer, se_loss, nclass, up_kwargs):
  439. super(LadderHead, self).__init__()
  440. self.conv1 = nn.Conv2d(in_channels=base_inchannels, out_channels=base_outchannels, kernel_size=1, bias=False)
  441. self.conv2 = nn.Conv2d(in_channels=base_inchannels * 2, out_channels=base_outchannels * 2, kernel_size=1,
  442. bias=False)
  443. self.conv3 = nn.Conv2d(in_channels=base_inchannels * 2 ** 2, out_channels=base_outchannels * 2 ** 2,
  444. kernel_size=1, bias=False)
  445. self.conv4 = nn.Conv2d(in_channels=base_inchannels * 2 ** 3, out_channels=base_outchannels * 2 ** 3,
  446. kernel_size=1, bias=False)
  447. self.bn1 = norm_layer(base_outchannels)
  448. self.bn2 = norm_layer(base_outchannels * 2)
  449. self.bn3 = norm_layer(base_outchannels * 2 ** 2)
  450. self.bn4 = norm_layer(base_outchannels * 2 ** 3)
  451. self.decoder = Decoder(planes=base_outchannels, layers=4)
  452. self.ladder = LadderBlock(planes=base_outchannels, layers=4)
  453. self.final = nn.Conv2d(base_outchannels, out_channels, 1)
  454. self.se_loss = se_loss
  455. if self.se_loss:
  456. self.selayer = nn.Linear(base_outchannels * 2 ** 3, nclass)
  457. def forward(self, x):
  458. x1, x2, x3, x4 = x
  459. out1 = self.conv1(x1)
  460. out1 = self.bn1(out1)
  461. out1 = F.relu(out1)
  462. out2 = self.conv2(x2)
  463. out2 = self.bn2(out2)
  464. out2 = F.relu(out2)
  465. out3 = self.conv3(x3)
  466. out3 = self.bn3(out3)
  467. out3 = F.relu(out3)
  468. out4 = self.conv4(x4)
  469. out4 = self.bn4(out4)
  470. out4 = F.relu(out4)
  471. out = self.decoder([out1, out2, out3, out4])
  472. out = self.ladder(out)
  473. pred = [self.final(out[-1])]
  474. if self.se_loss:
  475. enc = F.max_pool2d(out[0], kernel_size=out[0].size()[2:])
  476. enc = torch.squeeze(enc, -1)
  477. enc = torch.squeeze(enc, -1)
  478. se = self.selayer(enc)
  479. pred.append(se)
  480. return pred
  481. class LadderNet50(LadderNet):
  482. def __init__(self, *args, **kwargs):
  483. super().__init__(backbone='resnet50', nclass=21, *args, **kwargs)
  484. class LadderNet503433(LadderNet):
  485. def __init__(self, *args, **kwargs):
  486. super().__init__(backbone='resnet50_3433', nclass=21, *args, **kwargs)
  487. class LadderNet101(LadderNet):
  488. def __init__(self, *args, **kwargs):
  489. super().__init__(backbone='resnet101', nclass=21, *args, **kwargs)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...