mit-han-lab / torchsparse

[MICRO'23, MLSys'22] TorchSparse: Efficient Training and Inference Framework for Sparse Convolution on GPUs.
https://torchsparse.mit.edu
MIT License
1.16k stars 132 forks source link

[BUG] <title>How to solve "File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense AssertionError" #252

Closed nancyhxn closed 8 months ago

nancyhxn commented 9 months ago

Is there an existing issue for this?

Current Behavior

File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense AssertionError

Expected Behavior

No response

Environment

- GCC:
- NVCC:
- PyTorch:
- PyTorch CUDA:
- TorchSparse:

Anything else?

No response

zhijian-liu commented 9 months ago

Thanks for reporting this issue! Could you please provide a short code snippet to reproduce this error?

nancyhxn commented 8 months ago

Code: input = torch.randn(20, 32, 7, 64, 64).cuda(2) shape = input.shape coors = torch.nonzero(input[:, 0, :, :, :]) for i in range(input.shape[1]): fea = torch.zeros(coors.shape[0]) if i == 0: feats = torch.unsqueeze(input[coors[:, 0], i, coors[:, 1], coors[:, 2], coors[:, 3]], dim=0) else: fea = torch.unsqueeze(input[coors[:, 0], i, coors[:, 1], coors[:, 2], coors[:, 3]], dim=0) feats = torch.cat([feats, fea], dim=0) feats = torch.transpose(feats, dim0=0, dim1=1) coors = coors.int() x = tsp.SparseTensor(feats, coors) X = Conv3d(32, 32, 3, 1, 1) y = X(x) dense = x.dense() print(dense.shape)

Error: Traceback (most recent call last): File "/workspace/thirdpaper/VSR-main/test_sparse.py", line 42, in dense = x.dense() File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense AssertionError

treemanzzz commented 8 months ago

I also encountered the same problem of 'AssertionError', below is my code:

sp_tensor = SparseTensor(coors = coors, feats = feats)

dense_tensor = sp_tensor.dense()

ys-2020 commented 8 months ago

Hi @nancyhxn , @treemanzzz . You are meeting this error because you didn't set the spatial_range when constructing the sparse tensor. Thus, when you are calling .dense(). The function cannot know the spatial shape of the corresponding dense tensor you want.

Try to see if this code can solve you problem:

import torch
import torchsparse
import torchsparse.nn as spnn
import torchsparse.nn.functional as F

torch.manual_seed(42)

num_points = 100
dim = 4
feats = torch.randn([num_points, dim]).cuda()
coords = torch.randint(0, 10, [num_points, 4]).cuda()
coords[:, 0] = 0        # Set batch_idx to 0

spatial_range = (1, 10, 10, 10)
x = torchsparse.SparseTensor(feats, coords, spatial_range = spatial_range)

y = x.dense()
treemanzzz commented 8 months ago

Thank you so much for your response!✨ By the way, regarding the 'spatial_range' in line 14 spatial_range = (1, 10, 10, 10), I understand that the last three numbers (10, 10, 10) represent the depth, height, and width of the space in Voxel units. However, what does the first number (1) refer to? Is that the batch_index?

ys-2020 commented 8 months ago

Yes. It is batch size

treemanzzz commented 8 months ago

I got that, thank you so much!!!