utils.py 1.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch

from mmdet3d.structures.bbox_3d.utils import limit_period


def normalize_bbox(bboxes, pc_range):

    cx = bboxes[..., 0:1]
    cy = bboxes[..., 1:2]
    cz = bboxes[..., 2:3]
13
14
15
    length = bboxes[..., 3:4].log()
    width = bboxes[..., 4:5].log()
    height = bboxes[..., 5:6].log()
16

17
18
    rot = -bboxes[..., 6:7] - np.pi / 2
    rot = limit_period(rot, period=np.pi * 2)
19
20
21
22
    if bboxes.size(-1) > 7:
        vx = bboxes[..., 7:8]
        vy = bboxes[..., 8:9]
        normalized_bboxes = torch.cat(
23
24
            (cx, cy, length, width, cz, height, rot.sin(), rot.cos(), vx, vy),
            dim=-1)
25
26
    else:
        normalized_bboxes = torch.cat(
27
            (cx, cy, length, width, cz, height, rot.sin(), rot.cos()), dim=-1)
28
29
30
31
32
33
34
35
36
    return normalized_bboxes


def denormalize_bbox(normalized_bboxes, pc_range):
    # rotation
    rot_sine = normalized_bboxes[..., 6:7]

    rot_cosine = normalized_bboxes[..., 7:8]
    rot = torch.atan2(rot_sine, rot_cosine)
37
38
    rot = -rot - np.pi / 2
    rot = limit_period(rot, period=np.pi * 2)
39
40
41
42
43
44
45

    # center in the bev
    cx = normalized_bboxes[..., 0:1]
    cy = normalized_bboxes[..., 1:2]
    cz = normalized_bboxes[..., 4:5]

    # size
46
47
48
    length = normalized_bboxes[..., 2:3]
    width = normalized_bboxes[..., 3:4]
    height = normalized_bboxes[..., 5:6]
49

50
    width = width.exp()
51
    length = length.exp()
52
    height = height.exp()
53
54
55
56
57
    if normalized_bboxes.size(-1) > 8:
        # velocity
        vx = normalized_bboxes[:, 8:9]
        vy = normalized_bboxes[:, 9:10]
        denormalized_bboxes = torch.cat(
58
            [cx, cy, cz, length, width, height, rot, vx, vy], dim=-1)
59
    else:
60
61
        denormalized_bboxes = torch.cat(
            [cx, cy, cz, length, width, height, rot], dim=-1)
62
63

    return denormalized_bboxes