facebookresearch / SparseConvNet

Submanifold sparse convolutional networks
https://github.com/facebookresearch/SparseConvNet
Other
2.06k stars 334 forks source link

Can't restore the dense feature map #248

Open WuZhaoyue opened 3 months ago

WuZhaoyue commented 3 months ago

Hello, thanks a lot for this great work. I have a question for you. When I use it on an image where some pixels are 0, I use scn.DenseToSparse to convert it to sparse input format, but when I don't do any processing and directly use scn.SparseToDense to restore this sparse tensor to the original dense tensor, it doesn't work as shown. I would like to ask what is the reason? Thanks a lot!!! 截图 2024-08-16 20-11-37

bottler commented 3 months ago

Not sure anyone can help, but can you share your code? It might help see what is happening.

WuZhaoyue commented 3 months ago

Not sure anyone can help, but can you share your code? It might help see what is happening. Hello, thank you for your kindly reply. Here is the code of a simple example

############################## import torch import sparseconvnet as scn import matplotlib.pyplot as plt

Creating a simple dense image data and zeroing out parts of the image to simulate sparse data

image = torch.randn(1, 3, 100, 100) # (batch_size, channels, height, width) image[:, :, 20:40, 30:50] = 0

dimension = 2 spatial_size = torch.Size([100, 100]) # spatial size dense_to_sparse = scn.DenseToSparse(dimension) sparse_image = dense_to_sparse(image)

sparse_to_dense = scn.SparseToDense(dimension, 3) # 3 is the number of channels restored_image = sparse_to_dense(sparse_image)

restored_image = restored_image.squeeze(0)

Visualize original and recovered images

original_image_np = image.squeeze(0).permute(1, 2, 0).numpy() restored_image_np = restored_image.permute(1, 2, 0).numpy()

plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(original_image_np)

plt.subplot(1, 2, 2) plt.title("Restored Image") plt.imshow(restored_image_np)

plt.show() #########################################

bottler commented 3 months ago

Looking at the code in https://github.com/facebookresearch/SparseConvNet/blob/main/sparseconvnet/sparseToDense.py and https://github.com/facebookresearch/SparseConvNet/blob/main/sparseconvnet/denseToSparse.py, it seems that the shapes expected and produced are not quite the same. I think you need to take this into account manually.

btgraham commented 3 months ago

Sorry, I am unable to maintain SparseConvNet any more. However, more recent implementations of submanifold SparseConvnets are more highly optimized , ie. TorchSparse https://github.com/mit-han-lab/torchsparse, MinkowskiEngine https://github.com/NVIDIA/MinkowskiEngine SpConv https://github.com/traveller59/spconv

WuZhaoyue commented 3 months ago

Ok, thank you very much for your friendly answers, I will try another solution based on your suggestions.