mvx_faster_rcnn.py 1.9 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
import torch
zhangwenwei's avatar
zhangwenwei committed
2
from torch.nn import functional as F
zhangwenwei's avatar
zhangwenwei committed
3

zhangwenwei's avatar
zhangwenwei committed
4
from mmdet.models import DETECTORS
zhangwenwei's avatar
zhangwenwei committed
5
6
7
from .mvx_two_stage import MVXTwoStageDetector


zhangwenwei's avatar
zhangwenwei committed
8
9
@DETECTORS.register_module()
class MVXFasterRCNN(MVXTwoStageDetector):
zhangwenwei's avatar
zhangwenwei committed
10
    """Multi-modality VoxelNet using Faster R-CNN."""
zhangwenwei's avatar
zhangwenwei committed
11
12
13
14
15

    def __init__(self, **kwargs):
        super(MVXFasterRCNN, self).__init__(**kwargs)


16
@DETECTORS.register_module()
zhangwenwei's avatar
zhangwenwei committed
17
class DynamicMVXFasterRCNN(MVXTwoStageDetector):
zhangwenwei's avatar
zhangwenwei committed
18
    """Multi-modality VoxelNet using Faster R-CNN and dynamic voxelization."""
zhangwenwei's avatar
zhangwenwei committed
19
20
21
22
23
24

    def __init__(self, **kwargs):
        super(DynamicMVXFasterRCNN, self).__init__(**kwargs)

    @torch.no_grad()
    def voxelize(self, points):
zhangwenwei's avatar
zhangwenwei committed
25
26
27
28
29
30
31
32
        """Apply dynamic voxelization to points.

        Args:
            points (list[torch.Tensor]): Points of each sample.

        Returns:
            tuple[torch.Tensor]: Concatenated points and coordinates.
        """
zhangwenwei's avatar
zhangwenwei committed
33
34
35
36
37
38
39
40
41
42
43
44
45
        coors = []
        # dynamic voxelization only provide a coors mapping
        for res in points:
            res_coors = self.pts_voxel_layer(res)
            coors.append(res_coors)
        points = torch.cat(points, dim=0)
        coors_batch = []
        for i, coor in enumerate(coors):
            coor_pad = F.pad(coor, (1, 0), mode='constant', value=i)
            coors_batch.append(coor_pad)
        coors_batch = torch.cat(coors_batch, dim=0)
        return points, coors_batch

zhangwenwei's avatar
zhangwenwei committed
46
    def extract_pts_feat(self, points, img_feats, img_metas):
zhangwenwei's avatar
zhangwenwei committed
47
        """Extract point features."""
zhangwenwei's avatar
zhangwenwei committed
48
49
50
51
        if not self.with_pts_bbox:
            return None
        voxels, coors = self.voxelize(points)
        voxel_features, feature_coors = self.pts_voxel_encoder(
zhangwenwei's avatar
zhangwenwei committed
52
            voxels, coors, points, img_feats, img_metas)
zhangwenwei's avatar
zhangwenwei committed
53
54
55
56
57
58
        batch_size = coors[-1, 0] + 1
        x = self.pts_middle_encoder(voxel_features, feature_coors, batch_size)
        x = self.pts_backbone(x)
        if self.with_pts_neck:
            x = self.pts_neck(x)
        return x