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

[Feature] dev-1.x change np.transpose to torch.permute for speed up #2277

Merged
merged 2 commits into from
Feb 18, 2023
Merged
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
12 changes: 8 additions & 4 deletions mmdet3d/datasets/transforms/formating.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,19 @@ def pack_single_results(self, results: dict) -> dict:
if 'img' in results:
if isinstance(results['img'], list):
# process multiple imgs in single frame
imgs = [img.transpose(2, 0, 1) for img in results['img']]
imgs = np.ascontiguousarray(np.stack(imgs, axis=0))
imgs = [to_tensor(img) for img in results['img']]
ZwwWayne marked this conversation as resolved.
Show resolved Hide resolved
imgs = torch.stack(
imgs, dim=0).permute(0, 3, 1, 2).contiguous()
results['img'] = to_tensor(imgs)
else:
img = results['img']
if len(img.shape) < 3:
img = np.expand_dims(img, -1)
results['img'] = to_tensor(
np.ascontiguousarray(img.transpose(2, 0, 1)))
# To improve the computational speed by by 3-5 times, apply:
# `torch.permute()` rather than `np.transpose()`.
# Refer to https://github.com/open-mmlab/mmdetection/pull/9533
# for more details
results['img'] = to_tensor(img).permute(2, 0, 1).contiguous()

for key in [
'proposals', 'gt_bboxes', 'gt_bboxes_ignore', 'gt_labels',
Expand Down