second.py 2.97 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
from mmcv.cnn import build_conv_layer, build_norm_layer
zhangwenwei's avatar
zhangwenwei committed
2
from mmcv.runner import load_checkpoint
zhangwenwei's avatar
zhangwenwei committed
3
from torch import nn as nn
zhangwenwei's avatar
zhangwenwei committed
4

zhangwenwei's avatar
zhangwenwei committed
5
from mmdet.models import BACKBONES
zhangwenwei's avatar
zhangwenwei committed
6
7


8
@BACKBONES.register_module()
zhangwenwei's avatar
zhangwenwei committed
9
class SECOND(nn.Module):
zhangwenwei's avatar
zhangwenwei committed
10
    """Backbone network for SECOND/PointPillars/PartA2/MVXNet.
zhangwenwei's avatar
zhangwenwei committed
11
12
13
14
15
16
17
18

    Args:
        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
19
20
21
22
    """

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

zhangwenwei's avatar
zhangwenwei committed
32
        in_filters = [in_channels, *out_channels[:-1]]
zhangwenwei's avatar
zhangwenwei committed
33
34
35
36
37
        # 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
38
39
40
41
42
43
44
45
                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
46
47
48
49
                nn.ReLU(inplace=True),
            ]
            for j in range(layer_num):
                block.append(
zhangwenwei's avatar
zhangwenwei committed
50
51
52
53
54
55
56
                    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
57
58
59
60
61
62
63
64
                block.append(nn.ReLU(inplace=True))

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

        self.blocks = nn.ModuleList(blocks)

    def init_weights(self, pretrained=None):
65
        """Initialize weights of the 2D backbone."""
zhangwenwei's avatar
zhangwenwei committed
66
67
        # Do not initialize the conv layers
        # to follow the original implementation
zhangwenwei's avatar
zhangwenwei committed
68
        if isinstance(pretrained, str):
zhangwenwei's avatar
zhangwenwei committed
69
            from mmdet3d.utils import get_root_logger
zhangwenwei's avatar
zhangwenwei committed
70
71
72
73
            logger = get_root_logger()
            load_checkpoint(self, pretrained, strict=False, logger=logger)

    def forward(self, x):
74
75
76
77
78
79
80
81
        """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
82
83
84
85
86
        outs = []
        for i in range(len(self.blocks)):
            x = self.blocks[i](x)
            outs.append(x)
        return tuple(outs)