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

#683 Bugfix/INFRA-1707_fixing-docker-publish

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

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