second.py 3.17 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
import warnings
3

zhangwenwei's avatar
zhangwenwei committed
4
from mmcv.cnn import build_conv_layer, build_norm_layer
5
from mmcv.runner import BaseModule
zhangwenwei's avatar
zhangwenwei committed
6
from torch import nn as nn
zhangwenwei's avatar
zhangwenwei committed
7

zhangwenwei's avatar
zhangwenwei committed
8
from mmdet.models import BACKBONES
zhangwenwei's avatar
zhangwenwei committed
9
10


11
@BACKBONES.register_module()
12
class SECOND(BaseModule):
zhangwenwei's avatar
zhangwenwei committed
13
    """Backbone network for SECOND/PointPillars/PartA2/MVXNet.
zhangwenwei's avatar
zhangwenwei committed
14
15

    Args:
wangtai's avatar
wangtai committed
16
17
18
19
20
21
        in_channels (int): Input channels.
        out_channels (list[int]): Output channels for multi-scale feature maps.
        layer_nums (list[int]): Number of layers in each stage.
        layer_strides (list[int]): Strides of each stage.
        norm_cfg (dict): Config dict of normalization layers.
        conv_cfg (dict): Config dict of convolutional layers.
zhangwenwei's avatar
zhangwenwei committed
22
23
24
25
    """

    def __init__(self,
                 in_channels=128,
zhangwenwei's avatar
zhangwenwei committed
26
                 out_channels=[128, 128, 256],
zhangwenwei's avatar
zhangwenwei committed
27
28
                 layer_nums=[3, 5, 5],
                 layer_strides=[2, 2, 2],
zhangwenwei's avatar
zhangwenwei committed
29
                 norm_cfg=dict(type='BN', eps=1e-3, momentum=0.01),
30
31
32
33
                 conv_cfg=dict(type='Conv2d', bias=False),
                 init_cfg=None,
                 pretrained=None):
        super(SECOND, self).__init__(init_cfg=init_cfg)
zhangwenwei's avatar
zhangwenwei committed
34
        assert len(layer_strides) == len(layer_nums)
zhangwenwei's avatar
zhangwenwei committed
35
        assert len(out_channels) == len(layer_nums)
zhangwenwei's avatar
zhangwenwei committed
36

zhangwenwei's avatar
zhangwenwei committed
37
        in_filters = [in_channels, *out_channels[:-1]]
zhangwenwei's avatar
zhangwenwei committed
38
39
40
41
42
        # note that when stride > 1, conv2d with same padding isn't
        # equal to pad-conv2d. we should use pad-conv2d.
        blocks = []
        for i, layer_num in enumerate(layer_nums):
            block = [
zhangwenwei's avatar
zhangwenwei committed
43
44
45
46
47
48
49
50
                build_conv_layer(
                    conv_cfg,
                    in_filters[i],
                    out_channels[i],
                    3,
                    stride=layer_strides[i],
                    padding=1),
                build_norm_layer(norm_cfg, out_channels[i])[1],
zhangwenwei's avatar
zhangwenwei committed
51
52
53
54
                nn.ReLU(inplace=True),
            ]
            for j in range(layer_num):
                block.append(
zhangwenwei's avatar
zhangwenwei committed
55
56
57
58
59
60
61
                    build_conv_layer(
                        conv_cfg,
                        out_channels[i],
                        out_channels[i],
                        3,
                        padding=1))
                block.append(build_norm_layer(norm_cfg, out_channels[i])[1])
zhangwenwei's avatar
zhangwenwei committed
62
63
64
65
66
67
68
                block.append(nn.ReLU(inplace=True))

            block = nn.Sequential(*block)
            blocks.append(block)

        self.blocks = nn.ModuleList(blocks)

69
70
        assert not (init_cfg and pretrained), \
            'init_cfg and pretrained cannot be setting at the same time'
zhangwenwei's avatar
zhangwenwei committed
71
        if isinstance(pretrained, str):
72
73
74
75
76
            warnings.warn('DeprecationWarning: pretrained is a deprecated, '
                          'please use "init_cfg" instead')
            self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
        else:
            self.init_cfg = dict(type='Kaiming', layer='Conv2d')
zhangwenwei's avatar
zhangwenwei committed
77
78

    def forward(self, x):
79
80
81
82
83
84
85
86
        """Forward function.

        Args:
            x (torch.Tensor): Input with shape (N, C, H, W).

        Returns:
            tuple[torch.Tensor]: Multi-scale features.
        """
zhangwenwei's avatar
zhangwenwei committed
87
88
89
90
91
        outs = []
        for i in range(len(self.blocks)):
            x = self.blocks[i](x)
            outs.append(x)
        return tuple(outs)