|
| 1 | +from functools import partial |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | + |
| 5 | +from ...utils.spconv_utils import replace_feature, spconv |
| 6 | + |
| 7 | + |
| 8 | +def post_act_block(in_channels, out_channels, kernel_size, indice_key=None, stride=1, padding=0, |
| 9 | + conv_type='subm', norm_fn=None): |
| 10 | + |
| 11 | + if conv_type == 'subm': |
| 12 | + conv = spconv.SubMConv2d(in_channels, out_channels, kernel_size, bias=False, indice_key=indice_key) |
| 13 | + elif conv_type == 'spconv': |
| 14 | + conv = spconv.SparseConv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, |
| 15 | + bias=False, indice_key=indice_key) |
| 16 | + elif conv_type == 'inverseconv': |
| 17 | + conv = spconv.SparseInverseConv2d(in_channels, out_channels, kernel_size, indice_key=indice_key, bias=False) |
| 18 | + else: |
| 19 | + raise NotImplementedError |
| 20 | + |
| 21 | + m = spconv.SparseSequential( |
| 22 | + conv, |
| 23 | + norm_fn(out_channels), |
| 24 | + nn.ReLU(), |
| 25 | + ) |
| 26 | + |
| 27 | + return m |
| 28 | + |
| 29 | + |
| 30 | +class SparseBasicBlock(spconv.SparseModule): |
| 31 | + expansion = 1 |
| 32 | + |
| 33 | + def __init__(self, inplanes, planes, stride=1, norm_fn=None, downsample=None, indice_key=None): |
| 34 | + super(SparseBasicBlock, self).__init__() |
| 35 | + |
| 36 | + assert norm_fn is not None |
| 37 | + bias = norm_fn is not None |
| 38 | + self.conv1 = spconv.SubMConv2d( |
| 39 | + inplanes, planes, kernel_size=3, stride=stride, padding=1, bias=bias, indice_key=indice_key |
| 40 | + ) |
| 41 | + self.bn1 = norm_fn(planes) |
| 42 | + self.relu = nn.ReLU() |
| 43 | + self.conv2 = spconv.SubMConv2d( |
| 44 | + planes, planes, kernel_size=3, stride=stride, padding=1, bias=bias, indice_key=indice_key |
| 45 | + ) |
| 46 | + self.bn2 = norm_fn(planes) |
| 47 | + self.downsample = downsample |
| 48 | + self.stride = stride |
| 49 | + |
| 50 | + def forward(self, x): |
| 51 | + identity = x |
| 52 | + |
| 53 | + out = self.conv1(x) |
| 54 | + out = replace_feature(out, self.bn1(out.features)) |
| 55 | + out = replace_feature(out, self.relu(out.features)) |
| 56 | + |
| 57 | + out = self.conv2(out) |
| 58 | + out = replace_feature(out, self.bn2(out.features)) |
| 59 | + |
| 60 | + if self.downsample is not None: |
| 61 | + identity = self.downsample(x) |
| 62 | + |
| 63 | + out = replace_feature(out, out.features + identity.features) |
| 64 | + out = replace_feature(out, self.relu(out.features)) |
| 65 | + |
| 66 | + return out |
| 67 | + |
| 68 | + |
| 69 | +class VoxelResBackBone8xVoxelNeXt2D(nn.Module): |
| 70 | + def __init__(self, model_cfg, input_channels, grid_size, **kwargs): |
| 71 | + super().__init__() |
| 72 | + self.model_cfg = model_cfg |
| 73 | + norm_fn = partial(nn.BatchNorm1d, eps=1e-3, momentum=0.01) |
| 74 | + self.sparse_shape = grid_size[[1, 0]] |
| 75 | + |
| 76 | + block = post_act_block |
| 77 | + |
| 78 | + spconv_kernel_sizes = model_cfg.get('SPCONV_KERNEL_SIZES', [3, 3, 3, 3]) |
| 79 | + |
| 80 | + self.conv1 = spconv.SparseSequential( |
| 81 | + SparseBasicBlock(32, 32, norm_fn=norm_fn, indice_key='res1'), |
| 82 | + SparseBasicBlock(32, 32, norm_fn=norm_fn, indice_key='res1'), |
| 83 | + SparseBasicBlock(32, 32, norm_fn=norm_fn, indice_key='res1'), |
| 84 | + ) |
| 85 | + |
| 86 | + self.conv2 = spconv.SparseSequential( |
| 87 | + # [1600, 1408] <- [800, 704] |
| 88 | + block(32, 64, spconv_kernel_sizes[0], norm_fn=norm_fn, stride=2, padding=int(spconv_kernel_sizes[0]//2), indice_key='spconv2', conv_type='spconv'), |
| 89 | + SparseBasicBlock(64, 64, norm_fn=norm_fn, indice_key='res2'), |
| 90 | + SparseBasicBlock(64, 64, norm_fn=norm_fn, indice_key='res2'), |
| 91 | + SparseBasicBlock(64, 64, norm_fn=norm_fn, indice_key='res2'), |
| 92 | + SparseBasicBlock(64, 64, norm_fn=norm_fn, indice_key='res2'), |
| 93 | + ) |
| 94 | + |
| 95 | + self.conv3 = spconv.SparseSequential( |
| 96 | + # [800, 704] <- [400, 352] |
| 97 | + block(64, 128, spconv_kernel_sizes[1], norm_fn=norm_fn, stride=2, padding=int(spconv_kernel_sizes[1]//2), indice_key='spconv3', conv_type='spconv'), |
| 98 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 99 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 100 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 101 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 102 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 103 | + SparseBasicBlock(128, 128, norm_fn=norm_fn, indice_key='res3'), |
| 104 | + ) |
| 105 | + |
| 106 | + self.conv4 = spconv.SparseSequential( |
| 107 | + # [400, 352] <- [200, 176] |
| 108 | + block(128, 256, spconv_kernel_sizes[2], norm_fn=norm_fn, stride=2, padding=int(spconv_kernel_sizes[2]//2), indice_key='spconv4', conv_type='spconv'), |
| 109 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res4'), |
| 110 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res4'), |
| 111 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res4'), |
| 112 | + ) |
| 113 | + |
| 114 | + self.conv5 = spconv.SparseSequential( |
| 115 | + # [400, 352] <- [200, 176] |
| 116 | + block(256, 256, spconv_kernel_sizes[3], norm_fn=norm_fn, stride=2, padding=int(spconv_kernel_sizes[3]//2), indice_key='spconv5', conv_type='spconv'), |
| 117 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res5'), |
| 118 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res5'), |
| 119 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res5'), |
| 120 | + ) |
| 121 | + |
| 122 | + self.conv6 = spconv.SparseSequential( |
| 123 | + # [400, 352] <- [200, 176] |
| 124 | + block(256, 256, spconv_kernel_sizes[3], norm_fn=norm_fn, stride=2, padding=int(spconv_kernel_sizes[3]//2), indice_key='spconv6', conv_type='spconv'), |
| 125 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res6'), |
| 126 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res6'), |
| 127 | + SparseBasicBlock(256, 256, norm_fn=norm_fn, indice_key='res6'), |
| 128 | + ) |
| 129 | + |
| 130 | + self.conv_out = spconv.SparseSequential( |
| 131 | + # [200, 150, 5] -> [200, 150, 2] |
| 132 | + spconv.SparseConv2d(256, 256, 3, stride=1, padding=1, bias=False, indice_key='spconv_down2'), |
| 133 | + norm_fn(256), |
| 134 | + nn.ReLU(), |
| 135 | + ) |
| 136 | + |
| 137 | + self.shared_conv = spconv.SparseSequential( |
| 138 | + spconv.SubMConv2d(256, 256, 3, stride=1, padding=1, bias=True), |
| 139 | + nn.BatchNorm1d(256), |
| 140 | + nn.ReLU(True), |
| 141 | + ) |
| 142 | + |
| 143 | + self.num_point_features = 256 |
| 144 | + self.backbone_channels = { |
| 145 | + 'x_conv1': 32, |
| 146 | + 'x_conv2': 64, |
| 147 | + 'x_conv3': 128, |
| 148 | + 'x_conv4': 256, |
| 149 | + 'x_conv5': 256 |
| 150 | + } |
| 151 | + self.forward_ret_dict = {} |
| 152 | + |
| 153 | + def bev_out(self, x_conv): |
| 154 | + features_cat = x_conv.features |
| 155 | + indices_cat = x_conv.indices |
| 156 | + |
| 157 | + indices_unique, _inv = torch.unique(indices_cat, dim=0, return_inverse=True) |
| 158 | + features_unique = features_cat.new_zeros((indices_unique.shape[0], features_cat.shape[1])) |
| 159 | + features_unique.index_add_(0, _inv, features_cat) |
| 160 | + |
| 161 | + x_out = spconv.SparseConvTensor( |
| 162 | + features=features_unique, |
| 163 | + indices=indices_unique, |
| 164 | + spatial_shape=x_conv.spatial_shape, |
| 165 | + batch_size=x_conv.batch_size |
| 166 | + ) |
| 167 | + return x_out |
| 168 | + |
| 169 | + def forward(self, batch_dict): |
| 170 | + pillar_features, pillar_coords = batch_dict['pillar_features'], batch_dict['pillar_coords'] |
| 171 | + batch_size = batch_dict['batch_size'] |
| 172 | + input_sp_tensor = spconv.SparseConvTensor( |
| 173 | + features=pillar_features, |
| 174 | + indices=pillar_coords.int(), |
| 175 | + spatial_shape=self.sparse_shape, |
| 176 | + batch_size=batch_size |
| 177 | + ) |
| 178 | + |
| 179 | + x_conv1 = self.conv1(input_sp_tensor) |
| 180 | + x_conv2 = self.conv2(x_conv1) |
| 181 | + x_conv3 = self.conv3(x_conv2) |
| 182 | + x_conv4 = self.conv4(x_conv3) |
| 183 | + x_conv5 = self.conv5(x_conv4) |
| 184 | + x_conv6 = self.conv6(x_conv5) |
| 185 | + |
| 186 | + x_conv5.indices[:, 1:] *= 2 |
| 187 | + x_conv6.indices[:, 1:] *= 4 |
| 188 | + x_conv4 = x_conv4.replace_feature(torch.cat([x_conv4.features, x_conv5.features, x_conv6.features])) |
| 189 | + x_conv4.indices = torch.cat([x_conv4.indices, x_conv5.indices, x_conv6.indices]) |
| 190 | + |
| 191 | + out = self.bev_out(x_conv4) |
| 192 | + |
| 193 | + out = self.conv_out(out) |
| 194 | + out = self.shared_conv(out) |
| 195 | + |
| 196 | + batch_dict.update({ |
| 197 | + 'encoded_spconv_tensor': out, |
| 198 | + 'encoded_spconv_tensor_stride': 8 |
| 199 | + }) |
| 200 | + batch_dict.update({ |
| 201 | + 'multi_scale_2d_features': { |
| 202 | + 'x_conv1': x_conv1, |
| 203 | + 'x_conv2': x_conv2, |
| 204 | + 'x_conv3': x_conv3, |
| 205 | + 'x_conv4': x_conv4, |
| 206 | + 'x_conv5': x_conv5, |
| 207 | + } |
| 208 | + }) |
| 209 | + batch_dict.update({ |
| 210 | + 'multi_scale_2d_strides': { |
| 211 | + 'x_conv1': 1, |
| 212 | + 'x_conv2': 2, |
| 213 | + 'x_conv3': 4, |
| 214 | + 'x_conv4': 8, |
| 215 | + 'x_conv5': 16, |
| 216 | + } |
| 217 | + }) |
| 218 | + |
| 219 | + return batch_dict |
0 commit comments