torchsparse_block.py 7.21 KB
Newer Older
1
2
3
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Sequence, Union

4
from mmcv.cnn import build_activation_layer, build_norm_layer
5
6
7
from mmengine.model import BaseModule
from torch import nn

8
from mmdet3d.utils import ConfigType, OptConfigType
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from .torchsparse import IS_TORCHSPARSE_AVAILABLE

if IS_TORCHSPARSE_AVAILABLE:
    import torchsparse.nn as spnn
    from torchsparse.tensor import SparseTensor
else:
    SparseTensor = None


class TorchSparseConvModule(BaseModule):
    """A torchsparse conv block that bundles conv/norm/activation layers.

    Args:
        in_channels (int): In channels of block.
        out_channels (int): Out channels of block.
        kernel_size (int or Tuple[int]): Kernel_size of block.
        stride (int or Tuple[int]): Stride of the first block. Defaults to 1.
        dilation (int): Dilation of block. Defaults to 1.
27
        bias (bool): Whether use bias in conv. Defaults to False.
28
29
        transposed (bool): Whether use transposed convolution operator.
            Defaults to False.
30
        norm_cfg (:obj:`ConfigDict` or dict): The config of normalization.
31
32
33
34
        init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config.
            Defaults to None.
    """

35
36
37
38
39
40
41
42
43
44
45
46
47
    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, Sequence[int]],
                 stride: Union[int, Sequence[int]] = 1,
                 dilation: int = 1,
                 bias: bool = False,
                 transposed: bool = False,
                 norm_cfg: ConfigType = dict(type='TorchSparseBN'),
                 act_cfg: ConfigType = dict(
                     type='TorchSparseReLU', inplace=True),
                 init_cfg: OptConfigType = None,
                 **kwargs) -> None:
48
        super().__init__(init_cfg)
49
        layers = [
50
            spnn.Conv3d(in_channels, out_channels, kernel_size, stride,
51
52
53
54
55
56
57
58
59
                        dilation, bias, transposed)
        ]
        if norm_cfg is not None:
            _, norm = build_norm_layer(norm_cfg, out_channels)
            layers.append(norm)
        if act_cfg is not None:
            activation = build_activation_layer(act_cfg)
            layers.append(activation)
        self.net = nn.Sequential(*layers)
60
61
62
63
64
65

    def forward(self, x: SparseTensor) -> SparseTensor:
        out = self.net(x)
        return out


66
class TorchSparseBasicBlock(BaseModule):
67
68
69
70
71
72
73
74
    """Torchsparse residual basic block for MinkUNet.

    Args:
        in_channels (int): In channels of block.
        out_channels (int): Out channels of block.
        kernel_size (int or Tuple[int]): Kernel_size of block.
        stride (int or Tuple[int]): Stride of the first block. Defaults to 1.
        dilation (int): Dilation of block. Defaults to 1.
75
76
        bias (bool): Whether use bias in conv. Defaults to False.
        norm_cfg (:obj:`ConfigDict` or dict): The config of normalization.
77
78
79
80
        init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config.
            Defaults to None.
    """

81
82
83
84
85
86
87
88
89
90
    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, Sequence[int]] = 3,
                 stride: Union[int, Sequence[int]] = 1,
                 dilation: int = 1,
                 bias: bool = False,
                 norm_cfg: ConfigType = dict(type='TorchSparseBN'),
                 init_cfg: OptConfigType = None,
                 **kwargs) -> None:
91
        super().__init__(init_cfg)
92
93
94
        _, norm1 = build_norm_layer(norm_cfg, out_channels)
        _, norm2 = build_norm_layer(norm_cfg, out_channels)

95
96
        self.net = nn.Sequential(
            spnn.Conv3d(in_channels, out_channels, kernel_size, stride,
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
                        dilation, bias), norm1, spnn.ReLU(inplace=True),
            spnn.Conv3d(
                out_channels,
                out_channels,
                kernel_size,
                stride=1,
                dilation=dilation,
                bias=bias), norm2)

        if in_channels == out_channels and stride == 1:
            self.downsample = nn.Identity()
        else:
            _, norm3 = build_norm_layer(norm_cfg, out_channels)
            self.downsample = nn.Sequential(
                spnn.Conv3d(
                    in_channels,
                    out_channels,
                    kernel_size=1,
                    stride=stride,
                    dilation=dilation,
                    bias=bias), norm3)

        self.relu = spnn.ReLU(inplace=True)

    def forward(self, x: SparseTensor) -> SparseTensor:
        out = self.relu(self.net(x) + self.downsample(x))
        return out


class TorchSparseBottleneck(BaseModule):
    """Torchsparse residual basic block for MinkUNet.

    Args:
        in_channels (int): In channels of block.
        out_channels (int): Out channels of block.
        kernel_size (int or Tuple[int]): Kernel_size of block.
        stride (int or Tuple[int]): Stride of the second block. Defaults to 1.
        dilation (int): Dilation of block. Defaults to 1.
        bias (bool): Whether use bias in conv. Defaults to False.
        norm_cfg (:obj:`ConfigDict` or dict): The config of normalization.
        init_cfg (:obj:`ConfigDict` or dict, optional): Initialization config.
            Defaults to None.
    """

    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 kernel_size: Union[int, Sequence[int]] = 3,
                 stride: Union[int, Sequence[int]] = 1,
                 dilation: int = 1,
                 bias: bool = False,
                 norm_cfg: ConfigType = dict(type='TorchSparseBN'),
                 init_cfg: OptConfigType = None,
                 **kwargs) -> None:
        super().__init__(init_cfg)
        _, norm1 = build_norm_layer(norm_cfg, out_channels)
        _, norm2 = build_norm_layer(norm_cfg, out_channels)
        _, norm3 = build_norm_layer(norm_cfg, out_channels)

        self.net = nn.Sequential(
            spnn.Conv3d(
                in_channels,
                out_channels,
                kernel_size=1,
                stride=1,
                dilation=dilation,
                bias=bias), norm1, spnn.ReLU(inplace=True),
164
165
166
167
            spnn.Conv3d(
                out_channels,
                out_channels,
                kernel_size,
168
169
170
171
172
173
174
                stride,
                dilation=dilation,
                bias=bias), norm2, spnn.ReLU(inplace=True),
            spnn.Conv3d(
                out_channels,
                out_channels,
                kernel_size=1,
175
176
                stride=1,
                dilation=dilation,
177
178
                bias=bias), norm3)

179
180
181
        if in_channels == out_channels and stride == 1:
            self.downsample = nn.Identity()
        else:
182
            _, norm4 = build_norm_layer(norm_cfg, out_channels)
183
184
185
186
187
188
189
            self.downsample = nn.Sequential(
                spnn.Conv3d(
                    in_channels,
                    out_channels,
                    kernel_size=1,
                    stride=stride,
                    dilation=dilation,
190
                    bias=bias), norm4)
191
192
193
194
195
196

        self.relu = spnn.ReLU(inplace=True)

    def forward(self, x: SparseTensor) -> SparseTensor:
        out = self.relu(self.net(x) + self.downsample(x))
        return out