transforms.py 1.41 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):
zhangwenwei's avatar
zhangwenwei committed
5
    """Map bboxes from testing scale to original image scale."""
zhangwenwei's avatar
zhangwenwei committed
6
    new_bboxes = bboxes.clone()
wuyuefeng's avatar
wuyuefeng committed
7
8
9
10
    if flip_horizontal:
        new_bboxes.flip('horizontal')
    if flip_vertical:
        new_bboxes.flip('vertical')
zhangwenwei's avatar
zhangwenwei committed
11
12
13
14
15
    new_bboxes.scale(1 / scale_factor)

    return new_bboxes


wuyuefeng's avatar
wuyuefeng committed
16
17
18
19
def bbox3d2roi(bbox_list):
    """Convert a list of bboxes to roi format.

    Args:
liyinhao's avatar
liyinhao committed
20
21
        bbox_list (list[torch.Tensor]): a list of bboxes
            corresponding to a batch of images.
wuyuefeng's avatar
wuyuefeng committed
22
23

    Returns:
liyinhao's avatar
liyinhao committed
24
        torch.Tensor: shape (n, c), [batch_ind, x, y ...].
wuyuefeng's avatar
wuyuefeng committed
25
26
27
28
29
30
31
32
33
34
35
    """
    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
36
37
38
39
40
41


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

    Args:
liyinhao's avatar
liyinhao committed
42
43
44
        bboxes (torch.Tensor): shape (n, 5)
        labels (torch.Tensor): shape (n, )
        scores (torch.Tensor): shape (n, )
zhangwenwei's avatar
zhangwenwei committed
45
46

    Returns:
liyinhao's avatar
liyinhao committed
47
        dict(torch.Tensor): Bbox results in cpu mode.
zhangwenwei's avatar
zhangwenwei committed
48
49
    """
    return dict(
50
51
52
        boxes_3d=bboxes.to('cpu'),
        scores_3d=scores.cpu(),
        labels_3d=labels.cpu())