Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PETR transforms and box handling to work with old checkpoints #2919

Open
wants to merge 2 commits into
base: dev-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions projects/PETR/petr/transforms_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,33 @@ def transform(self, results):
return results

def rotate_bev_along_z(self, results, angle):
rot_cos = torch.cos(torch.tensor(angle))
rot_sin = torch.sin(torch.tensor(angle))
rot_cos = np.cos(angle)
rot_sin = np.sin(angle)

rot_mat = torch.tensor([[rot_cos, -rot_sin, 0, 0],
[rot_sin, rot_cos, 0, 0], [0, 0, 1, 0],
[0, 0, 0, 1]])
rot_mat_inv = torch.inverse(rot_mat)
rot_mat = np.array([[rot_cos, rot_sin, 0, 0],
[-rot_sin, rot_cos, 0, 0], [0, 0, 1, 0],
[0, 0, 0, 1]])
rot_mat_inv = np.linalg.inverse(rot_mat)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

num_view = len(results['lidar2cam'])
for view in range(num_view):
results['lidar2cam'][view] = (
torch.tensor(np.array(results['lidar2cam'][view]).T).float()
@ rot_mat_inv).T.numpy()
results['lidar2cam'][view] @ rot_mat_inv)

return

def scale_xyz(self, results, scale_ratio):
rot_mat = torch.tensor([
scale_mat = np.array([
[scale_ratio, 0, 0, 0],
[0, scale_ratio, 0, 0],
[0, 0, scale_ratio, 0],
[0, 0, 0, 1],
])

rot_mat_inv = torch.inverse(rot_mat)
scale_mat_inv = np.linalg.inverse(scale_mat)
Copy link

@TillBeemelmanns TillBeemelmanns Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.linalg.inverse() does not exist AttributeError: module 'numpy.linalg' has no attribute 'inverse'
Should be np.linalg.inv()


num_view = len(results['lidar2cam'])
for view in range(num_view):
results['lidar2cam'][view] = (torch.tensor(
rot_mat_inv.T
@ results['lidar2cam'][view].T).float()).T.numpy()
results['lidar2cam'][view] = (
scale_mat_inv @ results['lidar2cam'][view])

return
12 changes: 8 additions & 4 deletions projects/PETR/petr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ def normalize_bbox(bboxes, pc_range):
width = bboxes[..., 4:5].log()
height = bboxes[..., 5:6].log()

# normalize boxes to match the old checkpoints trained prior to
# coordinate system refactoring (<v1.0.0)
rot = -bboxes[..., 6:7] - np.pi / 2
rot = limit_period(rot, period=np.pi * 2)
if bboxes.size(-1) > 7:
vx = bboxes[..., 7:8]
vy = bboxes[..., 8:9]
normalized_bboxes = torch.cat(
(cx, cy, length, width, cz, height, rot.sin(), rot.cos(), vx, vy),
(cx, cy, width, length, cz, height, rot.sin(), rot.cos(), vx, vy),
dim=-1)
else:
normalized_bboxes = torch.cat(
(cx, cy, length, width, cz, height, rot.sin(), rot.cos()), dim=-1)
(cx, cy, width, length, cz, height, rot.sin(), rot.cos()), dim=-1)
return normalized_bboxes


Expand All @@ -42,9 +44,11 @@ def denormalize_bbox(normalized_bboxes, pc_range):
cy = normalized_bboxes[..., 1:2]
cz = normalized_bboxes[..., 4:5]

# boxes are expected to match the format from old checkpoints,
# denormalize them to match the new format
# size
length = normalized_bboxes[..., 2:3]
width = normalized_bboxes[..., 3:4]
width = normalized_bboxes[..., 2:3]
length = normalized_bboxes[..., 3:4]
height = normalized_bboxes[..., 5:6]

width = width.exp()
Expand Down