forked from PaddlePaddle/Paddle3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dla.py
568 lines (461 loc) · 15 KB
/
dla.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
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
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle3d.apis import manager
from paddle3d.models.layers import group_norm, FrozenBatchNorm2d
from paddle3d.utils import checkpoint
__all__ = ["DLA", "DLA34", "DLABase34"]
@manager.BACKBONES.add_component
class DLA(nn.Layer):
def __init__(self,
levels,
channels,
block,
down_ratio=4,
last_level=5,
out_channel=0,
norm_type="gn",
pretrained=None):
super().__init__()
self.pretrained = pretrained
assert down_ratio in [2, 4, 8, 16]
self.first_level = int(np.log2(down_ratio))
self.last_level = last_level
if norm_type == "bn":
norm_func = nn.BatchNorm2D
elif norm_type == "gn":
norm_func = group_norm
elif norm_type == "frozen_bn":
norm_func = FrozenBatchNorm2d
else:
raise NotImplementedError()
self.base = DLABase(levels, channels, block, norm_type=norm_type)
scales = [2**i for i in range(len(channels[self.first_level:]))]
self.dla_up = DLAUp(
startp=self.first_level,
channels=channels[self.first_level:],
scales=scales,
norm_func=norm_func)
if out_channel == 0:
out_channel = channels[self.first_level]
up_scales = [2**i for i in range(self.last_level - self.first_level)]
self.ida_up = IDAUp(
in_channels=channels[self.first_level:self.last_level],
out_channel=out_channel,
up_f=up_scales,
norm_func=norm_func)
self.init_weight()
def forward(self, x):
x = self.base(x)
x = self.dla_up(x)
y = []
iter_levels = range(self.last_level - self.first_level)
for i in iter_levels:
y.append(x[i].clone())
self.ida_up(y, 0, len(y))
return y[-1]
def init_weight(self):
if self.pretrained is not None:
checkpoint.load_pretrained_model(self, self.pretrained)
class DLABase(nn.Layer):
"""DLA base module
"""
def __init__(self,
levels,
channels,
block=None,
residual_root=False,
norm_type=None,
out_features=None):
super().__init__()
self.channels = channels
self.level_length = len(levels)
if norm_type == "bn" or norm_type is None:
norm_func = nn.BatchNorm2D
elif norm_type == "gn":
norm_func = group_norm
elif norm_type == "frozen_bn":
norm_func = FrozenBatchNorm2d
else:
raise NotImplementedError()
if out_features is None:
self.out_features = [i for i in range(self.level_length)]
else:
self.out_features = out_features
if block is None:
block = BasicBlock
else:
block = eval(block)
self.base_layer = nn.Sequential(
nn.Conv2D(
3,
channels[0],
kernel_size=7,
stride=1,
padding=3,
bias_attr=False), norm_func(channels[0]), nn.ReLU())
self.level0 = _make_conv_level(
in_channels=channels[0],
out_channels=channels[0],
num_convs=levels[0],
norm_func=norm_func)
self.level1 = _make_conv_level(
in_channels=channels[0],
out_channels=channels[1],
num_convs=levels[0],
norm_func=norm_func,
stride=2)
self.level2 = Tree(
level=levels[2],
block=block,
in_channels=channels[1],
out_channels=channels[2],
norm_func=norm_func,
stride=2,
level_root=False,
root_residual=residual_root)
self.level3 = Tree(
level=levels[3],
block=block,
in_channels=channels[2],
out_channels=channels[3],
norm_func=norm_func,
stride=2,
level_root=True,
root_residual=residual_root)
self.level4 = Tree(
level=levels[4],
block=block,
in_channels=channels[3],
out_channels=channels[4],
norm_func=norm_func,
stride=2,
level_root=True,
root_residual=residual_root)
self.level5 = Tree(
level=levels[5],
block=block,
in_channels=channels[4],
out_channels=channels[5],
norm_func=norm_func,
stride=2,
level_root=True,
root_residual=residual_root)
def forward(self, x):
"""forward
"""
y = []
x = self.base_layer(x)
for i in range(self.level_length):
x = getattr(self, 'level{}'.format(i))(x)
if i in self.out_features:
y.append(x)
return y
class DLAUp(nn.Layer):
"""DLA Up module
"""
def __init__(self,
startp,
channels,
scales,
in_channels=None,
norm_func=None):
"""DLA Up module
"""
super(DLAUp, self).__init__()
self.startp = startp
if norm_func is None:
norm_func = nn.BatchNorm2d
if in_channels is None:
in_channels = channels
self.channels = channels
channels = list(channels)
scales = np.array(scales, dtype=int)
for i in range(len(channels) - 1):
j = -i - 2
setattr(
self, 'ida_{}'.format(i),
IDAUp(in_channels[j:], channels[j], scales[j:] // scales[j],
norm_func))
scales[j + 1:] = scales[j]
in_channels[j + 1:] = [channels[j] for _ in channels[j + 1:]]
def forward(self, layers):
"""forward
"""
out = [layers[-1]] # start with 32
for i in range(len(layers) - self.startp - 1):
ida = getattr(self, 'ida_{}'.format(i))
ida(layers, len(layers) - i - 2, len(layers))
out.insert(0, layers[-1])
return out
class BasicBlock(nn.Layer):
"""Basic Block
"""
def __init__(self,
in_channels,
out_channels,
norm_func,
stride=1,
dilation=1):
super().__init__()
self.conv1 = nn.Conv2D(
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=dilation,
bias_attr=False,
dilation=dilation)
self.norm1 = norm_func(out_channels)
self.relu = nn.ReLU()
self.conv2 = nn.Conv2D(
out_channels,
out_channels,
kernel_size=3,
stride=1,
padding=dilation,
bias_attr=False,
dilation=dilation)
self.norm2 = norm_func(out_channels)
def forward(self, x, residual=None):
"""forward
"""
if residual is None:
residual = x
out = self.conv1(x)
out = self.norm1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.norm2(out)
out += residual
out = self.relu(out)
return out
class Tree(nn.Layer):
def __init__(self,
level,
block,
in_channels,
out_channels,
norm_func,
stride=1,
level_root=False,
root_dim=0,
root_kernel_size=1,
dilation=1,
root_residual=False):
super(Tree, self).__init__()
if root_dim == 0:
root_dim = 2 * out_channels
if level_root:
root_dim += in_channels
if level == 1:
self.tree1 = block(
in_channels, out_channels, norm_func, stride, dilation=dilation)
self.tree2 = block(
out_channels,
out_channels,
norm_func,
stride=1,
dilation=dilation)
else:
new_level = level - 1
self.tree1 = Tree(
new_level,
block,
in_channels,
out_channels,
norm_func,
stride,
root_dim=0,
root_kernel_size=root_kernel_size,
dilation=dilation,
root_residual=root_residual)
self.tree2 = Tree(
new_level,
block,
out_channels,
out_channels,
norm_func,
root_dim=root_dim + out_channels,
root_kernel_size=root_kernel_size,
dilation=dilation,
root_residual=root_residual)
if level == 1:
self.root = Root(root_dim, out_channels, norm_func,
root_kernel_size, root_residual)
self.level_root = level_root
self.root_dim = root_dim
self.level = level
self.downsample = None
if stride > 1:
self.downsample = nn.MaxPool2D(stride, stride=stride)
self.project = None
# If 'self.tree1' is a Tree (not BasicBlock), then the output of project is not used.
if in_channels != out_channels and not isinstance(self.tree1, Tree):
self.project = nn.Sequential(
nn.Conv2D(
in_channels,
out_channels,
kernel_size=1,
stride=1,
bias_attr=False), norm_func(out_channels))
def forward(self, x, residual=None, children=None):
"""forward
"""
if children is None:
children = []
if self.downsample:
bottom = self.downsample(x)
else:
bottom = x
if self.project:
residual = self.project(bottom)
else:
residual = bottom
if self.level_root:
children.append(bottom)
x1 = self.tree1(x, residual)
if self.level == 1:
x2 = self.tree2(x1)
x = self.root(x2, x1, *children)
else:
children.append(x1)
x = self.tree2(x1, children=children)
return x
class Root(nn.Layer):
"""Root module
"""
def __init__(self, in_channels, out_channels, norm_func, kernel_size,
residual):
super(Root, self).__init__()
self.conv = nn.Conv2D(
in_channels,
out_channels,
kernel_size=1,
stride=1,
bias_attr=False,
padding=(kernel_size - 1) // 2)
self.norm = norm_func(out_channels)
self.relu = nn.ReLU()
self.residual = residual
def forward(self, *x):
"""forward
"""
children = x
x = self.conv(paddle.concat(x, 1))
x = self.norm(x)
if self.residual:
x += children[0]
x = self.relu(x)
return x
class IDAUp(nn.Layer):
"""IDAUp module
"""
def __init__(
self,
in_channels,
out_channel,
up_f, # todo: what is up_f here?
norm_func):
super().__init__()
for i in range(1, len(in_channels)):
in_channel = in_channels[i]
f = int(up_f[i])
#USE_DEFORMABLE_CONV = False
# so far only support normal convolution
proj = NormalConv(in_channel, out_channel, norm_func)
node = NormalConv(out_channel, out_channel, norm_func)
up = nn.Conv2DTranspose(
out_channel,
out_channel,
kernel_size=f * 2,
stride=f,
padding=f // 2,
output_padding=0,
groups=out_channel,
bias_attr=False)
# todo: uncommoment later
# _fill_up_weights(up)
setattr(self, 'proj_' + str(i), proj)
setattr(self, 'up_' + str(i), up)
setattr(self, 'node_' + str(i), node)
def forward(self, layers, startp, endp):
"""forward
"""
for i in range(startp + 1, endp):
upsample = getattr(self, 'up_' + str(i - startp))
project = getattr(self, 'proj_' + str(i - startp))
layers[i] = upsample(project(layers[i]))
node = getattr(self, 'node_' + str(i - startp))
layers[i] = node(layers[i] + layers[i - 1])
class NormalConv(nn.Layer):
"""Normal Conv without deformable
"""
def __init__(self, in_channels, out_channels, norm_func):
super(NormalConv, self).__init__()
self.norm = norm_func(out_channels)
self.relu = nn.ReLU()
self.conv = nn.Conv2D(
in_channels, out_channels, kernel_size=(3, 3), padding=1)
def forward(self, x):
"""forward
"""
x = self.conv(x)
x = self.norm(x)
x = self.relu(x)
return x
def _make_conv_level(in_channels,
out_channels,
num_convs,
norm_func,
stride=1,
dilation=1):
"""
make conv layers based on its number.
"""
layers = []
for i in range(num_convs):
layers.extend([
nn.Conv2D(
in_channels,
out_channels,
kernel_size=3,
stride=stride if i == 0 else 1,
padding=dilation,
bias_attr=False,
dilation=dilation),
norm_func(out_channels),
nn.ReLU()
])
in_channels = out_channels
return nn.Sequential(*layers)
@manager.BACKBONES.add_component
def DLA34(**kwargs):
model = DLA(
levels=[1, 1, 1, 2, 2, 1],
channels=[16, 32, 64, 128, 256, 512],
block="BasicBlock",
**kwargs)
return model
@manager.BACKBONES.add_component
def DLABase34(**kwargs):
model = DLABase(
levels=[1, 1, 1, 2, 2, 1],
channels=[16, 32, 64, 128, 256, 512],
block="BasicBlock",
**kwargs)
return model