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 do Sparse Tensor Indexing with Coordinates Tensor? #137

Closed
rancheng opened this issue May 6, 2020 · 3 comments
Closed

How to do Sparse Tensor Indexing with Coordinates Tensor? #137

rancheng opened this issue May 6, 2020 · 3 comments

Comments

@rancheng
Copy link
Contributor

rancheng commented May 6, 2020

Hi I recently have some tried to do Spatial Propagation on sparse tensor and am wondering if you have implemented the sparse tensor indexing function somewhere so that I can directly use.

Here's a simple example case:

# generate 15 random coordinate entries range from 0 to 3
a_C = torch.empty(15, 2, dtype=torch.long).random_(3)
# generate 15 feature values
a_F = torch.rand(15,1)
# generate sparse tensor
a_S = ME.SparseTensor(a_F, a_C)
# generate random coordinate to do indexing
b_C = torch.empty(2, 2, dtype=torch.long).random_(3)
# I want to select features according to b_C
b_F = a_S[b_C]  # <<<< this is what I want

I want to select the feature according to the coordinates of a given tensor, and those coordinates doesn't have values, it can be set as nan or a customized value.

If you know how to do this, I'm willing to implement the function and start a pull request.

@chrischoy
Copy link
Contributor

chrischoy commented May 7, 2020

Currently, there is no function that exactly does that, but you can make one with get_coords_map in https://github.com/StanfordVL/MinkowskiEngine/blob/master/MinkowskiEngine/MinkowskiCoords.py#L291.

  1. create a coords_key of a_S (which is already done when you call ME.SparseTensor)
a_key = a_S.coords_key
  1. create a coords_key of b_C by
b_key = a_S.coords_man.create_coords_key(b_C.int())  # it has to be int
  1. Find input to output mapping or A to B mapping
a_indices, b_indices = a_S.coords_man.get_coords_map(a_key, b_key)

If there is no match, the lists will be empty. But if you have matches, the order should be

b_F = torch.zeros(len(b_C), a_F.shape[1])  #  placeholder
if len(b_indices) > 0:
  b_F[b_indices, :] = a_F[a_indices, :]

I'll put this into a function in the next cycle.

@rancheng
Copy link
Contributor Author

rancheng commented May 7, 2020

Thanks very much for the quick reply! I'll close this issue since it perfectly solved my issue.

@chrischoy
Copy link
Contributor

Hi I just made a function "features_at_coords(query_coords: IntTensor)". Could you see if it works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants