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

from . import roiaware_pool3d_ext


6
7
def points_in_boxes_part(points, boxes):
    """Find the box in which each point is (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
            num_valid_boxes <= T, [x, y, z, x_size, y_size, z_size, rz] in
13
            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)

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

    return box_idxs_of_pts


def points_in_boxes_cpu(points, boxes):
54
55
    """Find all boxes in which each point is (CPU). The CPU version of
    :meth:`points_in_boxes_all`.
wuyuefeng's avatar
wuyuefeng committed
56

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

wuyuefeng's avatar
wuyuefeng committed
64
    Returns:
65
        box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0.
wuyuefeng's avatar
wuyuefeng committed
66
    """
67
68
69
70
    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, \
71
72
        f'boxes dimension should be 7, ' \
        f'got unexpected shape {boxes.shape[2]}'
73
    assert points.shape[2] == 3, \
74
75
        f'points dimension should be 3, ' \
        f'got unexpected shape {points.shape[2]}'
76
77
    batch_size, num_points, _ = points.shape
    num_boxes = boxes.shape[1]
wuyuefeng's avatar
wuyuefeng committed
78

79
    point_indices = points.new_zeros((batch_size, num_boxes, num_points),
wuyuefeng's avatar
wuyuefeng committed
80
                                     dtype=torch.int)
81
82
83
84
85
    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
86
87

    return point_indices
wuyuefeng's avatar
Votenet  
wuyuefeng committed
88
89


90
91
def points_in_boxes_all(points, boxes):
    """Find all boxes in which each point is (CUDA).
wuyuefeng's avatar
Votenet  
wuyuefeng committed
92
93

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

    Returns:
100
        box_idxs_of_pts (torch.Tensor): (B, M, T), default background = 0.
wuyuefeng's avatar
Votenet  
wuyuefeng committed
101
    """
102
103
104
105
106
107
108
109
110
    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
111
112
113
114
115
    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)
116
117
118
119
120
121
122
123

    # 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)

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

    return box_idxs_of_pts