transforms.py 2.35 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
zhangwenwei's avatar
zhangwenwei committed
2
3
4
import torch


wuyuefeng's avatar
wuyuefeng committed
5
def bbox3d_mapping_back(bboxes, scale_factor, flip_horizontal, flip_vertical):
6
7
8
9
10
11
12
13
14
15
16
    """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
17
    new_bboxes = bboxes.clone()
wuyuefeng's avatar
wuyuefeng committed
18
19
20
21
    if flip_horizontal:
        new_bboxes.flip('horizontal')
    if flip_vertical:
        new_bboxes.flip('vertical')
zhangwenwei's avatar
zhangwenwei committed
22
23
24
25
26
    new_bboxes.scale(1 / scale_factor)

    return new_bboxes


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

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

    Returns:
35
        torch.Tensor: Region of interests in shape (n, c), where
36
            the channels are in order of [batch_ind, x, y ...].
wuyuefeng's avatar
wuyuefeng committed
37
38
39
40
41
42
43
44
45
46
47
    """
    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
48
49


50
def bbox3d2result(bboxes, scores, labels, attrs=None):
zhangwenwei's avatar
zhangwenwei committed
51
52
53
    """Convert detection results to a list of numpy arrays.

    Args:
wangtai's avatar
wangtai committed
54
55
56
        bboxes (torch.Tensor): Bounding boxes with shape of (n, 5).
        labels (torch.Tensor): Labels with shape of (n, ).
        scores (torch.Tensor): Scores with shape of (n, ).
57
        attrs (torch.Tensor, optional): Attributes with shape of (n, ).
58
            Defaults to None.
zhangwenwei's avatar
zhangwenwei committed
59
60

    Returns:
61
        dict[str, torch.Tensor]: Bounding box results in cpu mode.
62

63
64
65
            - boxes_3d (torch.Tensor): 3D boxes.
            - scores (torch.Tensor): Prediction scores.
            - labels_3d (torch.Tensor): Box labels.
66
            - attrs_3d (torch.Tensor, optional): Box attributes.
zhangwenwei's avatar
zhangwenwei committed
67
    """
68
    result_dict = dict(
69
70
71
        boxes_3d=bboxes.to('cpu'),
        scores_3d=scores.cpu(),
        labels_3d=labels.cpu())
72
73
74
75
76

    if attrs is not None:
        result_dict['attrs_3d'] = attrs.cpu()

    return result_dict