"vscode:/vscode.git/clone" did not exist on "b9247022c153c6109ca3e2900869f83144be5740"
transforms.py 1.88 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
2
3
import torch


wuyuefeng's avatar
wuyuefeng committed
4
def bbox3d_mapping_back(bboxes, scale_factor, flip_horizontal, flip_vertical):
5
6
7
8
9
10
11
12
13
14
15
    """Map bboxes from testing scale to original image scale.

    Args:
        bboxes (:obj:`BaseInstance3DBoxes`): Boxes to be mapped back.
        scale_factor (float): Scale factor.
        flip_horizontal (bool): Whether to flip horizontally.
        flip_vertical (bool): Whether to flip vertically.

    Returns:
        :obj:`BaseInstance3DBoxes`: Boxes mapped back.
    """
zhangwenwei's avatar
zhangwenwei committed
16
    new_bboxes = bboxes.clone()
wuyuefeng's avatar
wuyuefeng committed
17
18
19
20
    if flip_horizontal:
        new_bboxes.flip('horizontal')
    if flip_vertical:
        new_bboxes.flip('vertical')
zhangwenwei's avatar
zhangwenwei committed
21
22
23
24
25
    new_bboxes.scale(1 / scale_factor)

    return new_bboxes


wuyuefeng's avatar
wuyuefeng committed
26
27
28
29
def bbox3d2roi(bbox_list):
    """Convert a list of bboxes to roi format.

    Args:
liyinhao's avatar
liyinhao committed
30
31
        bbox_list (list[torch.Tensor]): a list of bboxes
            corresponding to a batch of images.
wuyuefeng's avatar
wuyuefeng committed
32
33

    Returns:
liyinhao's avatar
liyinhao committed
34
        torch.Tensor: shape (n, c), [batch_ind, x, y ...].
wuyuefeng's avatar
wuyuefeng committed
35
36
37
38
39
40
41
42
43
44
45
    """
    rois_list = []
    for img_id, bboxes in enumerate(bbox_list):
        if bboxes.size(0) > 0:
            img_inds = bboxes.new_full((bboxes.size(0), 1), img_id)
            rois = torch.cat([img_inds, bboxes], dim=-1)
        else:
            rois = torch.zeros_like(bboxes)
        rois_list.append(rois)
    rois = torch.cat(rois_list, 0)
    return rois
zhangwenwei's avatar
zhangwenwei committed
46
47
48
49
50
51


def bbox3d2result(bboxes, scores, labels):
    """Convert detection results to a list of numpy arrays.

    Args:
liyinhao's avatar
liyinhao committed
52
53
54
        bboxes (torch.Tensor): shape (n, 5)
        labels (torch.Tensor): shape (n, )
        scores (torch.Tensor): shape (n, )
zhangwenwei's avatar
zhangwenwei committed
55
56

    Returns:
57
58
59
60
61
        dict[str, torch.Tensor]: Bbox results in cpu mode.

            - boxes_3d (torch.Tensor): 3D boxes
            - scores (torch.Tensor): prediction scores
            - labels_3d (torch.Tensor): box labels
zhangwenwei's avatar
zhangwenwei committed
62
63
    """
    return dict(
64
65
66
        boxes_3d=bboxes.to('cpu'),
        scores_3d=scores.cpu(),
        labels_3d=labels.cpu())