transforms.py 6.54 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
6
    """Map bboxes from testing scale to original image scale"""
    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


zhangwenwei's avatar
zhangwenwei committed
16
def transform_lidar_to_cam(boxes_lidar):
liyinhao's avatar
liyinhao committed
17
18
19
20
21
22
23
24
25
26
27
    """Transform boxes from lidar coords to cam coords.
        Only transform format, not exactly in camera coords.

    Args:
        boxes_lidar (torch.Tensor): (N, 3 or 7) [x, y, z, w, l, h, ry]
            in LiDAR coords.
        boxes_cam (torch.Tensor): (N, 3 or 7) [x, y, z, h, w, l, ry]
            in camera coords.

    Returns:
        torch.Tensor: Boxes in camera coords.
zhangwenwei's avatar
zhangwenwei committed
28
29
30
31
32
33
34
35
36
37
38
39
    """
    # boxes_cam = boxes_lidar.new_tensor(boxes_lidar.data)
    boxes_cam = boxes_lidar.clone().detach()
    boxes_cam[:, 0] = -boxes_lidar[:, 1]
    boxes_cam[:, 1] = -boxes_lidar[:, 2]
    boxes_cam[:, 2] = boxes_lidar[:, 0]
    if boxes_cam.shape[1] > 3:
        boxes_cam[:, [3, 4, 5]] = boxes_lidar[:, [5, 3, 4]]
    return boxes_cam


def boxes3d_to_bev_torch(boxes3d):
liyinhao's avatar
liyinhao committed
40
41
42
43
44
45
46
47
48
    """Transform 3d boxes to bev in camera coords.

    Args:
        boxes3d (torch.Tensor): 3d boxes in camera coords
            with the shape of [N, 7] (x, y, z, h, w, l, ry).

    Returns:
        torch.Tensor: Bev boxes with the shape of [N, 5]
            (x1, y1, x2, y2, ry).
zhangwenwei's avatar
zhangwenwei committed
49
50
51
52
53
54
55
56
57
58
59
60
    """
    boxes_bev = boxes3d.new(torch.Size((boxes3d.shape[0], 5)))

    cu, cv = boxes3d[:, 0], boxes3d[:, 2]
    half_l, half_w = boxes3d[:, 5] / 2, boxes3d[:, 4] / 2
    boxes_bev[:, 0], boxes_bev[:, 1] = cu - half_l, cv - half_w
    boxes_bev[:, 2], boxes_bev[:, 3] = cu + half_l, cv + half_w
    boxes_bev[:, 4] = boxes3d[:, 6]
    return boxes_bev


def boxes3d_to_bev_torch_lidar(boxes3d):
liyinhao's avatar
liyinhao committed
61
62
63
64
65
66
67
    """Transform 3d boxes to bev in lidar coords.

    Args:
        boxes3d (torch.Tensor): 3d boxes in lidar coords
            with the shape of [N, 7] (x, y, z, h, w, l, ry).

    Returns: Bev boxes with the shape of [N, 5] (x1, y1, x2, y2, ry).
zhangwenwei's avatar
zhangwenwei committed
68
69
70
71
72
73
74
75
76
    """
    boxes_bev = boxes3d.new(torch.Size((boxes3d.shape[0], 5)))

    cu, cv = boxes3d[:, 0], boxes3d[:, 1]
    half_l, half_w = boxes3d[:, 4] / 2, boxes3d[:, 3] / 2
    boxes_bev[:, 0], boxes_bev[:, 1] = cu - half_w, cv - half_l
    boxes_bev[:, 2], boxes_bev[:, 3] = cu + half_w, cv + half_l
    boxes_bev[:, 4] = boxes3d[:, 6]
    return boxes_bev
wuyuefeng's avatar
wuyuefeng committed
77
78
79
80
81
82


def bbox3d2roi(bbox_list):
    """Convert a list of bboxes to roi format.

    Args:
liyinhao's avatar
liyinhao committed
83
84
        bbox_list (list[torch.Tensor]): a list of bboxes
            corresponding to a batch of images.
wuyuefeng's avatar
wuyuefeng committed
85
86

    Returns:
liyinhao's avatar
liyinhao committed
87
        torch.Tensor: shape (n, c), [batch_ind, x, y ...].
wuyuefeng's avatar
wuyuefeng committed
88
89
90
91
92
93
94
95
96
97
98
    """
    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
99
100
101
102
103
104


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

    Args:
liyinhao's avatar
liyinhao committed
105
106
107
        bboxes (torch.Tensor): shape (n, 5)
        labels (torch.Tensor): shape (n, )
        scores (torch.Tensor): shape (n, )
zhangwenwei's avatar
zhangwenwei committed
108
109

    Returns:
liyinhao's avatar
liyinhao committed
110
        dict(torch.Tensor): Bbox results in cpu mode.
zhangwenwei's avatar
zhangwenwei committed
111
112
    """
    return dict(
113
114
115
        boxes_3d=bboxes.to('cpu'),
        scores_3d=scores.cpu(),
        labels_3d=labels.cpu())
wuyuefeng's avatar
Votenet  
wuyuefeng committed
116
117
118
119
120
121
122
123


def upright_depth_to_lidar_torch(points=None,
                                 bboxes=None,
                                 to_bottom_center=False):
    """Convert points and boxes in upright depth coordinate to lidar.

    Args:
liyinhao's avatar
liyinhao committed
124
125
        points (None | torch.Tensor): points in upright depth coordinate.
        bboxes (None | torch.Tensor): bboxes in upright depth coordinate.
wuyuefeng's avatar
Votenet  
wuyuefeng committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        to_bottom_center (bool): covert bboxes to bottom center.

    Returns:
        tuple: points and bboxes in lidar coordinate.
    """
    if points is not None:
        points_lidar = points.clone()
        points_lidar = points_lidar[..., [1, 0, 2]]
        points_lidar[..., 1] *= -1
    else:
        points_lidar = None

    if bboxes is not None:
        bboxes_lidar = bboxes.clone()
        bboxes_lidar = bboxes_lidar[..., [1, 0, 2, 4, 3, 5, 6]]
        bboxes_lidar[..., 1] *= -1
        if to_bottom_center:
            bboxes_lidar[..., 2] -= 0.5 * bboxes_lidar[..., 5]
    else:
        bboxes_lidar = None

    return points_lidar, bboxes_lidar


def box3d_to_corner3d_upright_depth(boxes3d):
    """Convert box3d to corner3d in upright depth coordinate

    Args:
liyinhao's avatar
liyinhao committed
154
155
        boxes3d (torch.Tensor): boxes with shape [n,7] in
            upright depth coordinate.
wuyuefeng's avatar
Votenet  
wuyuefeng committed
156
157

    Returns:
liyinhao's avatar
liyinhao committed
158
        torch.Tensor: boxes with [n, 8, 3] in upright depth coordinate
wuyuefeng's avatar
Votenet  
wuyuefeng committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    """
    boxes_num = boxes3d.shape[0]
    ry = boxes3d[:, 6:7]
    l, w, h = boxes3d[:, 3:4], boxes3d[:, 4:5], boxes3d[:, 5:6]
    zeros = boxes3d.new_zeros((boxes_num, 1))
    ones = boxes3d.new_ones((boxes_num, 1))
    # zeros = torch.cuda.FloatTensor(boxes_num, 1).fill_(0)
    # ones = torch.cuda.FloatTensor(boxes_num, 1).fill_(1)
    x_corners = torch.cat(
        [-l / 2., l / 2., l / 2., -l / 2., -l / 2., l / 2., l / 2., -l / 2.],
        dim=1)  # (N, 8)
    y_corners = torch.cat(
        [w / 2., w / 2., -w / 2., -w / 2., w / 2., w / 2., -w / 2., -w / 2.],
        dim=1)  # (N, 8)
    z_corners = torch.cat(
        [h / 2., h / 2., h / 2., h / 2., -h / 2., -h / 2., -h / 2., -h / 2.],
        dim=1)  # (N, 8)
    temp_corners = torch.cat(
        (x_corners.unsqueeze(dim=2), y_corners.unsqueeze(dim=2),
         z_corners.unsqueeze(dim=2)),
        dim=2)  # (N, 8, 3)

    cosa, sina = torch.cos(-ry), torch.sin(-ry)
    raw_1 = torch.cat([cosa, -sina, zeros], dim=1)  # (N, 3)
    raw_2 = torch.cat([sina, cosa, zeros], dim=1)  # (N, 3)
    raw_3 = torch.cat([zeros, zeros, ones], dim=1)  # (N, 3)
    R = torch.cat((raw_1.unsqueeze(dim=1), raw_2.unsqueeze(dim=1),
                   raw_3.unsqueeze(dim=1)),
                  dim=1)  # (N, 3, 3)
    rotated_corners = torch.matmul(temp_corners, R)  # (N, 8, 3)
    x_corners = rotated_corners[:, :, 0]
    y_corners = rotated_corners[:, :, 1]
    z_corners = rotated_corners[:, :, 2]
    x_loc, y_loc, z_loc = boxes3d[:, 0], boxes3d[:, 1], boxes3d[:, 2]

    x = x_loc.view(-1, 1) + x_corners.view(-1, 8)
    y = y_loc.view(-1, 1) + y_corners.view(-1, 8)
    z = z_loc.view(-1, 1) + z_corners.view(-1, 8)
    corners3d = torch.cat(
        (x.view(-1, 8, 1), y.view(-1, 8, 1), z.view(-1, 8, 1)), dim=2)

    return corners3d