iou3d_utils.py 1.9 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
2
3
4
5
6
import torch

from . import iou3d_cuda


def boxes_iou_bev(boxes_a, boxes_b):
7
    """Calculate boxes IoU in the bird view.
zhangwenwei's avatar
zhangwenwei committed
8

9
10
11
12
13
14
15
16
17
    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
18
19
20
21
22
23
24
25

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

    return ans_iou


def nms_gpu(boxes, scores, thresh):
26
27
28
29
30
31
32
33
34
    """Non maximum suppression on GPU.

    Args:
        boxes (torch.Tensor): Input boxes with shape (N, 5).
        scores (torch.Tensor): Scores of predicted boxes with shape (N).
        thresh (torch.Tensor): Threshold of non maximum suppression.

    Returns:
        torch.Tensor: Remaining indices with scores in descending order.
zhangwenwei's avatar
zhangwenwei committed
35
36
37
38
39
    """
    order = scores.sort(0, descending=True)[1]

    boxes = boxes[order].contiguous()

40
41
42
    keep = boxes.new_zeros(boxes.size(0))
    num_out = iou3d_cuda.nms_gpu(boxes, keep, thresh, boxes.device.index)
    return order[keep[:num_out].cuda(boxes.device)].contiguous()
zhangwenwei's avatar
zhangwenwei committed
43
44
45


def nms_normal_gpu(boxes, scores, thresh):
46
47
48
49
50
51
52
53
54
    """Normal non maximum suppression on GPU.

    Args:
        boxes (torch.Tensor): Input boxes with shape (N, 5).
        scores (torch.Tensor): Scores of predicted boxes with shape (N).
        thresh (torch.Tensor): Threshold of non maximum suppression.

    Returns:
        torch.Tensor: Remaining indices with scores in descending order.
zhangwenwei's avatar
zhangwenwei committed
55
56
57
58
59
    """
    order = scores.sort(0, descending=True)[1]

    boxes = boxes[order].contiguous()

60
61
62
63
    keep = boxes.new_zeros(boxes.size(0))
    num_out = iou3d_cuda.nms_normal_gpu(boxes, keep, thresh,
                                        boxes.device.index)
    return order[keep[:num_out].cuda(boxes.device)].contiguous()