sparse_block.py 6.68 KB
Newer Older
1
from mmcv.cnn import build_norm_layer
wuyuefeng's avatar
wuyuefeng committed
2
3
4
from torch import nn

import mmdet3d.ops.spconv as spconv
wuyuefeng's avatar
wuyuefeng committed
5
from mmdet.models.backbones.resnet import BasicBlock, Bottleneck
wuyuefeng's avatar
wuyuefeng committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19


def conv3x3(in_planes, out_planes, stride=1, indice_key=None):
    """3x3 submanifold sparse convolution with padding.

    Args:
        in_planes (int): the number of input channels
        out_planes (int): the number of output channels
        stride (int): the stride of convolution
        indice_key (str): the indice key used for sparse tensor

    Returns:
        spconv.conv.SubMConv3d: 3x3 submanifold sparse convolution ops
    """
20
    # TODO: deprecate this class
wuyuefeng's avatar
wuyuefeng committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    return spconv.SubMConv3d(
        in_planes,
        out_planes,
        kernel_size=3,
        stride=stride,
        padding=1,
        bias=False,
        indice_key=indice_key)


def conv1x1(in_planes, out_planes, stride=1, indice_key=None):
    """1x1 submanifold sparse convolution with padding.

    Args:
        in_planes (int): the number of input channels
        out_planes (int): the number of output channels
        stride (int): the stride of convolution
        indice_key (str): the indice key used for sparse tensor

    Returns:
        spconv.conv.SubMConv3d: 1x1 submanifold sparse convolution ops
    """
43
    # TODO: deprecate this class
wuyuefeng's avatar
wuyuefeng committed
44
45
46
47
48
49
50
51
52
53
    return spconv.SubMConv3d(
        in_planes,
        out_planes,
        kernel_size=1,
        stride=stride,
        padding=1,
        bias=False,
        indice_key=indice_key)


wuyuefeng's avatar
wuyuefeng committed
54
class SparseBasicBlockV0(spconv.SparseModule):
wuyuefeng's avatar
wuyuefeng committed
55
56
57
58
59
60
61
62
63
64
65
66
67
    expansion = 1

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 downsample=None,
                 indice_key=None,
                 norm_cfg=None):
        """Sparse basic block for PartA^2.

        Sparse basic block implemented with submanifold sparse convolution.
        """
68
        # TODO: deprecate this class
wuyuefeng's avatar
wuyuefeng committed
69
        super().__init__()
wuyuefeng's avatar
wuyuefeng committed
70
71
72
73
74
75
76
77
78
79
80
81
82
        self.conv1 = conv3x3(inplanes, planes, stride, indice_key=indice_key)
        norm_name1, norm_layer1 = build_norm_layer(norm_cfg, planes)
        self.bn1 = norm_layer1
        self.relu = nn.ReLU()
        self.conv2 = conv3x3(planes, planes, indice_key=indice_key)
        norm_name2, norm_layer2 = build_norm_layer(norm_cfg, planes)
        self.bn2 = norm_layer2
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x.features

83
        assert x.features.dim() == 2, f'x.features.dim()={x.features.dim()}'
wuyuefeng's avatar
wuyuefeng committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

        out = self.conv1(x)
        out.features = self.bn1(out.features)
        out.features = self.relu(out.features)

        out = self.conv2(out)
        out.features = self.bn2(out.features)

        if self.downsample is not None:
            identity = self.downsample(x)

        out.features += identity
        out.features = self.relu(out.features)

        return out


wuyuefeng's avatar
wuyuefeng committed
101
class SparseBottleneckV0(spconv.SparseModule):
wuyuefeng's avatar
wuyuefeng committed
102
103
104
105
106
107
108
109
110
111
112
113
114
    expansion = 4

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 downsample=None,
                 indice_key=None,
                 norm_fn=None):
        """Sparse bottleneck block for PartA^2.

        Bottleneck block implemented with submanifold sparse convolution.
        """
115
        # TODO: deprecate this class
wuyuefeng's avatar
wuyuefeng committed
116
        super().__init__()
wuyuefeng's avatar
wuyuefeng committed
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
        self.conv1 = conv1x1(inplanes, planes, indice_key=indice_key)
        self.bn1 = norm_fn(planes)
        self.conv2 = conv3x3(planes, planes, stride, indice_key=indice_key)
        self.bn2 = norm_fn(planes)
        self.conv3 = conv1x1(
            planes, planes * self.expansion, indice_key=indice_key)
        self.bn3 = norm_fn(planes * self.expansion)
        self.relu = nn.ReLU()
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x.features

        out = self.conv1(x)
        out.features = self.bn1(out.features)
        out.features = self.relu(out.features)

        out = self.conv2(out)
        out.features = self.bn2(out.features)
        out.features = self.relu(out.features)

        out = self.conv3(out)
        out.features = self.bn3(out.features)

        if self.downsample is not None:
            identity = self.downsample(x)

        out.features += identity
        out.features = self.relu(out.features)

        return out
wuyuefeng's avatar
wuyuefeng committed
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


class SparseBottleneck(Bottleneck, spconv.SparseModule):
    expansion = 4

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 downsample=None,
                 conv_cfg=None,
                 norm_cfg=None):
        """Sparse bottleneck block for PartA^2.

        Bottleneck block implemented with submanifold sparse convolution.
        """
        spconv.SparseModule.__init__(self)
        Bottleneck.__init__(
            self,
            inplanes,
            planes,
            stride=stride,
            downsample=downsample,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg)

    def forward(self, x):
        identity = x.features

        out = self.conv1(x)
        out.features = self.bn1(out.features)
        out.features = self.relu(out.features)

        out = self.conv2(out)
        out.features = self.bn2(out.features)
        out.features = self.relu(out.features)

        out = self.conv3(out)
        out.features = self.bn3(out.features)

        if self.downsample is not None:
            identity = self.downsample(x)

        out.features += identity
        out.features = self.relu(out.features)

        return out


class SparseBasicBlock(BasicBlock, spconv.SparseModule):
    expansion = 1

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 downsample=None,
                 conv_cfg=None,
                 norm_cfg=None):
        """Sparse basic block for PartA^2.

        Sparse basic block implemented with submanifold sparse convolution.
        """
        spconv.SparseModule.__init__(self)
        BasicBlock.__init__(
            self,
            inplanes,
            planes,
            stride=stride,
            downsample=downsample,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg)

    def forward(self, x):
        identity = x.features

225
        assert x.features.dim() == 2, f'x.features.dim()={x.features.dim()}'
wuyuefeng's avatar
wuyuefeng committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

        out = self.conv1(x)
        out.features = self.norm1(out.features)
        out.features = self.relu(out.features)

        out = self.conv2(out)
        out.features = self.norm2(out.features)

        if self.downsample is not None:
            identity = self.downsample(x)

        out.features += identity
        out.features = self.relu(out.features)

        return out