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

how to inverse a permutation #55

Open
lix19937 opened this issue Nov 5, 2024 · 0 comments
Open

how to inverse a permutation #55

lix19937 opened this issue Nov 5, 2024 · 0 comments

Comments

@lix19937
Copy link
Owner

lix19937 commented Nov 5, 2024

import torch

# https://stackoverflow.com/questions/66832716/how-to-quickly-inverse-a-permutation-by-using-pytorch
# https://discuss.pytorch.org/t/how-to-quickly-inverse-a-permutation-by-using-pytorch/116205/2

def inverse_permutation(perm):
    inv = torch.empty_like(perm)
    inv[perm] = torch.arange(perm.size(0), device=perm.device)
    return inv


bev_feat_base = torch.randn(4, 2, 1, 3, 4)

bev_feat = bev_feat_base
bev_feat = bev_feat.squeeze(2)
print(bev_feat.shape)


bev_feat = bev_feat.permute(0, 2, 3, 1)
print(bev_feat.shape)

bev_feat_rp = inverse_permutation(torch.tensor([0, 2, 3, 1]))
print(bev_feat_rp)

bev_feat = bev_feat.permute(0, 3, 1, 2).contiguous()
bev_feat = bev_feat.unsqueeze(2)
print(bev_feat.shape)


print(torch.equal(bev_feat, bev_feat_base))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant