NVIDIA / MinkowskiEngine

Minkowski Engine is an auto-diff neural network library for high-dimensional sparse tensors
https://nvidia.github.io/MinkowskiEngine
Other
2.5k stars 369 forks source link

"Duplicate coordinates found" error when initializing "SparseTensor" #116

Closed jingyi-zhang closed 3 years ago

jingyi-zhang commented 4 years ago

Thanks for your good code! but I got an error occasionally when use function 'SparseTensor' I dont know what caused this error ca image

jingyi-zhang commented 4 years ago

This is a part code in 'getitem' function of DataSet

        coords = lidar_camera[:,0:2]
        #print(coords.shape[0])
        features = lidar_camera[:,2:4]
        labels = object_list
        coords /= self.quantization_size
        #print(coords.shape)
        mapping = ME.utils.sparse_quantize(coords=coords,
            return_index=True)
        #print(len(mapping))
        return coords[mapping], features[mapping], labels`

this my test code

train_set = SparseKittiDataset(classes=FLAGS.classes, split='training', augment=False, cfg=cfg)
train_loader = DataLoader(dataset=train_set, batch_size=FLAGS.batch_size, num_workers=FLAGS.workers,\
    shuffle=True, drop_last=True,collate_fn=ME.utils.batch_sparse_collate)
for batch_idx, data_label in enumerate(train_loader):
    coords, features, label = data_label
    #print(features.shape)
    #print(coords.shape)
    input = ME.SparseTensor(features, coords, force_creation=True)
    print(input)
    break
chrischoy commented 4 years ago

It happens when you have duplicate coordinates.

The standard way to fix this error would be sampling one point per voxel. This process is known as the voxel down-sampling. In addition, a sparse tensor requires a list of integers to represent a coordinate, so you need to floor a coordinate to a quantized indices.

The engine supports such down-sampling and quantization. Please refer to sparse_quantize.

In most cases, a training pipeline consists of

  1. quantization https://github.com/StanfordVL/MinkowskiEngine/blob/415098d4e457a1373e0a0499414cee0cbfbd6144/examples/training.py#L139
  2. collation and batched coordinate generation https://github.com/StanfordVL/MinkowskiEngine/blob/415098d4e457a1373e0a0499414cee0cbfbd6144/examples/training.py#L154
  3. SparseTensor generation https://github.com/StanfordVL/MinkowskiEngine/blob/415098d4e457a1373e0a0499414cee0cbfbd6144/examples/training.py#L199

However, for classification like tasks where you can safely ignore duplicate coordinates, you can use force_creation=True. This should not be used for segmentation like tasks.

Please let me know if you have further questions.

chrischoy commented 4 years ago

In your example, you missed batched_coordinates which was in the training collation function.

This results in 2D sparse tensor. Coordinates must have size N x 4, not N x 3 to create a 3D sparse tensor.

jingyi-zhang commented 4 years ago

Thanks for your patience! You are so nice~

Took your advice, I used 'batched_coordinates' instead of 'batch_sparse_collate' to load data, then, the error has been solved.

But actually, I am still confused about the difference between 'batch_sparse_collate', 'SparseCollation' and 'batched_coordinates', I checked their definition in 'collation.py', seems like they have same function.