iou3d_utils.py 2.67 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
zhangwenwei's avatar
zhangwenwei committed
2
3
4
5
6
7
import torch

from . import iou3d_cuda


def boxes_iou_bev(boxes_a, boxes_b):
8
    """Calculate boxes IoU in the Bird's Eye View.
zhangwenwei's avatar
zhangwenwei committed
9

10
11
12
13
14
15
16
17
18
    Args:
        boxes_a (torch.Tensor): Input boxes a with shape (M, 5).
        boxes_b (torch.Tensor): Input boxes b with shape (N, 5).

    Returns:
        ans_iou (torch.Tensor): IoU result with shape (M, N).
    """
    ans_iou = boxes_a.new_zeros(
        torch.Size((boxes_a.shape[0], boxes_b.shape[0])))
zhangwenwei's avatar
zhangwenwei committed
19
20
21
22
23
24
25

    iou3d_cuda.boxes_iou_bev_gpu(boxes_a.contiguous(), boxes_b.contiguous(),
                                 ans_iou)

    return ans_iou


26
27
28
29
30
def nms_gpu(boxes, scores, thresh, pre_max_size=None, post_max_size=None):
    """NMS function GPU implementation (for BEV boxes). The overlap of two
    boxes for IoU calculation is defined as the exact overlapping area of the
    two boxes. In this function, one can also set `pre_max_size` and
    `post_max_size`.
31
32

    Args:
33
34
35
36
        boxes (torch.Tensor): Input boxes with the shape of [N, 5]
            ([x1, y1, x2, y2, ry]).
        scores (torch.Tensor): Scores of boxes with the shape of [N].
        thresh (int): Threshold.
37
38
39
40
        pre_max_size (int, optional): Max size of boxes before NMS.
            Default: None.
        post_max_size (int, optional): Max size of boxes after NMS.
            Default: None.
41
42

    Returns:
43
        torch.Tensor: Indexes after NMS.
zhangwenwei's avatar
zhangwenwei committed
44
45
46
    """
    order = scores.sort(0, descending=True)[1]

47
48
    if pre_max_size is not None:
        order = order[:pre_max_size]
zhangwenwei's avatar
zhangwenwei committed
49
50
    boxes = boxes[order].contiguous()

51
    keep = torch.zeros(boxes.size(0), dtype=torch.long)
52
    num_out = iou3d_cuda.nms_gpu(boxes, keep, thresh, boxes.device.index)
53
54
55
56
    keep = order[keep[:num_out].cuda(boxes.device)].contiguous()
    if post_max_size is not None:
        keep = keep[:post_max_size]
    return keep
zhangwenwei's avatar
zhangwenwei committed
57
58
59


def nms_normal_gpu(boxes, scores, thresh):
60
61
62
    """Normal NMS function GPU implementation (for BEV boxes). The overlap of
    two boxes for IoU calculation is defined as the exact overlapping area of
    the two boxes WITH their yaw angle set to 0.
63
64
65
66

    Args:
        boxes (torch.Tensor): Input boxes with shape (N, 5).
        scores (torch.Tensor): Scores of predicted boxes with shape (N).
67
        thresh (torch.Tensor): Threshold of NMS.
68
69
70

    Returns:
        torch.Tensor: Remaining indices with scores in descending order.
zhangwenwei's avatar
zhangwenwei committed
71
72
73
74
75
    """
    order = scores.sort(0, descending=True)[1]

    boxes = boxes[order].contiguous()

76
    keep = torch.zeros(boxes.size(0), dtype=torch.long)
77
78
79
    num_out = iou3d_cuda.nms_normal_gpu(boxes, keep, thresh,
                                        boxes.device.index)
    return order[keep[:num_out].cuda(boxes.device)].contiguous()