dgcnn_fp_module.py 2.11 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
from typing import List

4
from mmcv.cnn import ConvModule
5
from mmengine.model import BaseModule
6
from torch import Tensor
7
8
from torch import nn as nn

9
10
from mmdet3d.utils import ConfigType, OptMultiConfig

11
12
13
14
15
16
17

class DGCNNFPModule(BaseModule):
    """Point feature propagation module used in DGCNN.

    Propagate the features from one set to another.

    Args:
18
19
20
21
        mlp_channels (List[int]): List of mlp channels.
        norm_cfg (:obj:`ConfigDict` or dict): Config dict for normalization
            layer. Defaults to dict(type='BN1d').
        act_cfg (:obj:`ConfigDict` or dict): Config dict for activation layer.
22
            Defaults to dict(type='ReLU').
23
24
        init_cfg (:obj:`ConfigDict` or dict or List[:obj:`Contigdict` or dict],
            optional): Initialization config dict. Defaults to None.
25
26
27
    """

    def __init__(self,
28
29
30
31
32
                 mlp_channels: List[int],
                 norm_cfg: ConfigType = dict(type='BN1d'),
                 act_cfg: ConfigType = dict(type='ReLU'),
                 init_cfg: OptMultiConfig = None) -> None:
        super(DGCNNFPModule, self).__init__(init_cfg=init_cfg)
33
34
35
36
37
38
39
40
41
42
43
44
45
        self.mlps = nn.Sequential()
        for i in range(len(mlp_channels) - 1):
            self.mlps.add_module(
                f'layer{i}',
                ConvModule(
                    mlp_channels[i],
                    mlp_channels[i + 1],
                    kernel_size=(1, ),
                    stride=(1, ),
                    conv_cfg=dict(type='Conv1d'),
                    norm_cfg=norm_cfg,
                    act_cfg=act_cfg))

46
47
    def forward(self, points: Tensor) -> Tensor:
        """Forward.
48
49

        Args:
50
            points (Tensor): (B, N, C) Tensor of the input points.
51
52

        Returns:
53
            Tensor: (B, N, M) M = mlp[-1]. Tensor of the new points.
54
55
56
57
58
59
60
61
62
63
        """

        if points is not None:
            new_points = points.transpose(1, 2).contiguous()  # (B, C, N)
            new_points = self.mlps(new_points)
            new_points = new_points.transpose(1, 2).contiguous()
        else:
            new_points = points

        return new_points