points_in_boxes.py 4.85 KB
Newer Older
wuyuefeng's avatar
wuyuefeng committed
1
2
3
4
5
6
import torch

from . import roiaware_pool3d_ext


def points_in_boxes_gpu(points, boxes):
wuyuefeng's avatar
wuyuefeng committed
7
    """Find points that are in boxes (CUDA)
wuyuefeng's avatar
wuyuefeng committed
8

wuyuefeng's avatar
wuyuefeng committed
9
    Args:
10
        points (torch.Tensor): [B, M, 3], [x, y, z] in LiDAR/DEPTH coordinate
wuyuefeng's avatar
wuyuefeng committed
11
        boxes (torch.Tensor): [B, T, 7],
12
13
            num_valid_boxes <= T, [x, y, z, dx, dy, dz, rz] in
            LiDAR/DEPTH coordinate, (x, y, z) is the bottom center
wuyuefeng's avatar
wuyuefeng committed
14

wuyuefeng's avatar
wuyuefeng committed
15
16
17
    Returns:
        box_idxs_of_pts (torch.Tensor): (B, M), default background = -1
    """
18
    assert points.shape[0] == boxes.shape[0], \
19
        f'Points and boxes should have the same batch size, ' \
20
        f'got {points.shape[0]} and {boxes.shape[0]}'
21
22
23
24
25
26
    assert boxes.shape[2] == 7, \
        f'boxes dimension should be 7, ' \
        f'got unexpected shape {boxes.shape[2]}'
    assert points.shape[2] == 3, \
        f'points dimension should be 3, ' \
        f'got unexpected shape {points.shape[2]}'
wuyuefeng's avatar
wuyuefeng committed
27
28
29
30
    batch_size, num_points, _ = points.shape

    box_idxs_of_pts = points.new_zeros((batch_size, num_points),
                                       dtype=torch.int).fill_(-1)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

    # If manually put the tensor 'points' or 'boxes' on a device
    # which is not the current device, some temporary variables
    # will be created on the current device in the cuda op,
    # and the output will be incorrect.
    # Therefore, we force the current device to be the same
    # as the device of the tensors if it was not.
    # Please refer to https://github.com/open-mmlab/mmdetection3d/issues/305
    # for the incorrect output before the fix.
    points_device = points.get_device()
    assert points_device == boxes.get_device(), \
        'Points and boxes should be put on the same device'
    if torch.cuda.current_device() != points_device:
        torch.cuda.set_device(points_device)

wuyuefeng's avatar
wuyuefeng committed
46
47
48
49
50
51
52
53
    roiaware_pool3d_ext.points_in_boxes_gpu(boxes.contiguous(),
                                            points.contiguous(),
                                            box_idxs_of_pts)

    return box_idxs_of_pts


def points_in_boxes_cpu(points, boxes):
wuyuefeng's avatar
wuyuefeng committed
54
55
    """Find points that are in boxes (CPU)

wuyuefeng's avatar
wuyuefeng committed
56
    Args:
57
58
59
60
61
        points (torch.Tensor): [B, M, 3], [x, y, z] in
            LiDAR/DEPTH coordinate
        boxes (torch.Tensor): [B, T, 7],
            num_valid_boxes <= T, [x, y, z, dx, dy, dz, rz],
            (x, y, z) is the bottom center.
wuyuefeng's avatar
wuyuefeng committed
62

wuyuefeng's avatar
wuyuefeng committed
63
    Returns:
64
        box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0
wuyuefeng's avatar
wuyuefeng committed
65
    """
66
67
68
69
    assert points.shape[0] == boxes.shape[0], \
        f'Points and boxes should have the same batch size, ' \
        f'got {points.shape[0]} and {boxes.shape[0]}'
    assert boxes.shape[2] == 7, \
70
71
        f'boxes dimension should be 7, ' \
        f'got unexpected shape {boxes.shape[2]}'
72
    assert points.shape[2] == 3, \
73
74
        f'points dimension should be 3, ' \
        f'got unexpected shape {points.shape[2]}'
75
76
    batch_size, num_points, _ = points.shape
    num_boxes = boxes.shape[1]
wuyuefeng's avatar
wuyuefeng committed
77

78
    point_indices = points.new_zeros((batch_size, num_boxes, num_points),
wuyuefeng's avatar
wuyuefeng committed
79
                                     dtype=torch.int)
80
81
82
83
84
    for b in range(batch_size):
        roiaware_pool3d_ext.points_in_boxes_cpu(boxes[b].float().contiguous(),
                                                points[b].float().contiguous(),
                                                point_indices[b])
    point_indices = point_indices.transpose(1, 2)
wuyuefeng's avatar
wuyuefeng committed
85
86

    return point_indices
wuyuefeng's avatar
Votenet  
wuyuefeng committed
87
88
89
90
91
92


def points_in_boxes_batch(points, boxes):
    """Find points that are in boxes (CUDA)

    Args:
93
        points (torch.Tensor): [B, M, 3], [x, y, z] in LiDAR/DEPTH coordinate
wuyuefeng's avatar
Votenet  
wuyuefeng committed
94
        boxes (torch.Tensor): [B, T, 7],
95
            num_valid_boxes <= T, [x, y, z, dx, dy, dz, rz],
wuyuefeng's avatar
wuyuefeng committed
96
            (x, y, z) is the bottom center.
wuyuefeng's avatar
Votenet  
wuyuefeng committed
97
98
99
100

    Returns:
        box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0
    """
101
102
103
104
105
106
107
108
109
    assert boxes.shape[0] == points.shape[0], \
        f'Points and boxes should have the same batch size, ' \
        f'got {boxes.shape[0]} and {boxes.shape[0]}'
    assert boxes.shape[2] == 7, \
        f'boxes dimension should be 7, ' \
        f'got unexpected shape {boxes.shape[2]}'
    assert points.shape[2] == 3, \
        f'points dimension should be 3, ' \
        f'got unexpected shape {points.shape[2]}'
wuyuefeng's avatar
Votenet  
wuyuefeng committed
110
111
112
113
114
    batch_size, num_points, _ = points.shape
    num_boxes = boxes.shape[1]

    box_idxs_of_pts = points.new_zeros((batch_size, num_points, num_boxes),
                                       dtype=torch.int).fill_(0)
115
116
117
118
119
120
121
122

    # Same reason as line 25-32
    points_device = points.get_device()
    assert points_device == boxes.get_device(), \
        'Points and boxes should be put on the same device'
    if torch.cuda.current_device() != points_device:
        torch.cuda.set_device(points_device)

wuyuefeng's avatar
Votenet  
wuyuefeng committed
123
124
125
126
127
    roiaware_pool3d_ext.points_in_boxes_batch(boxes.contiguous(),
                                              points.contiguous(),
                                              box_idxs_of_pts)

    return box_idxs_of_pts