File size: 5,249 Bytes
a6dd040 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import numpy as np
import unittest
import time
import torch
import MinkowskiEngineTest._C
from utils import load_file, batched_coordinates
class CoordinateMapTestCase(unittest.TestCase):
def test_batch_insert(self):
assert torch.cuda.is_available()
coordinates = torch.IntTensor([[0, 1], [1, 2], [2, 3], [2, 3]]).to(0)
num, _ = MinkowskiEngineTest._C.coordinate_map_batch_insert_test(coordinates)
self.assertEqual(num, 3)
def test_mapping(self):
assert torch.cuda.is_available()
coordinates = torch.IntTensor(
[[0, 1], [1, 2], [2, 3], [2, 3], [3, 2], [3, 2]]
).to(0)
(
mapping,
inverse_mapping,
) = MinkowskiEngineTest._C.coordinate_map_inverse_map_test(coordinates)
print(mapping)
print(inverse_mapping)
self.assertEqual(len(mapping), 4)
self.assertTrue(
torch.all(
coordinates[mapping.long()][inverse_mapping.long()] == coordinates
)
)
def test_pcd_insert(self):
coords, colors, pcd = load_file("1.ply")
BATCH_SIZE = 1
voxel_size = 0.02
bcoords = [np.floor(coords / voxel_size) for i in range(BATCH_SIZE)]
bcoords = batched_coordinates(bcoords).to(0)
num, _ = MinkowskiEngineTest._C.coordinate_map_batch_insert_test(bcoords)
self.assertEqual(num, 161890)
for batch_size in [1, 2, 4, 8, 16, 20, 40, 80, 160, 320]:
for voxel_size in [0.02]:
py_min_time = 1000
dcoords = torch.from_numpy(np.floor(coords / voxel_size)).int()
bcoords = batched_coordinates([dcoords for i in range(batch_size)])
for i in range(10):
s = time.time()
bcoords = bcoords.to(0)
(
num,
cpp_time,
) = MinkowskiEngineTest._C.coordinate_map_batch_insert_test(bcoords)
py_min_time = min(time.time() - s, py_min_time)
print(f"{len(bcoords)}\t{num}\t{py_min_time}\t{cpp_time}")
def test_batch_find(self):
coordinates = torch.IntTensor([[0, 1], [1, 2], [2, 3], [2, 3]]).to(0)
queries = torch.IntTensor([[-1, 1], [1, 2], [2, 3], [2, 3], [0, 0]]).to(0)
(
valid_query_index,
query_value,
) = MinkowskiEngineTest._C.coordinate_map_batch_find_test(coordinates, queries)
self.assertEqual(len(valid_query_index), len(query_value))
self.assertEqual(len(valid_query_index), 3)
self.assertEqual(valid_query_index[0], 1)
self.assertEqual(valid_query_index[1], 2)
self.assertEqual(valid_query_index[2], 3)
self.assertEqual(query_value[0], 1)
self.assertEqual(query_value[1], 2)
self.assertEqual(query_value[2], 2)
def test_stride(self):
coordinates = torch.IntTensor([[0, 1], [0, 2], [0, 3], [0, 3]]).to(0)
stride = [1]
with self.assertRaises(TypeError):
MinkowskiEngineTest._C.coordinate_map_stride_test(coordinates, stride)
stride = torch.IntTensor([-1])
with self.assertRaises(RuntimeError):
MinkowskiEngineTest._C.coordinate_map_stride_test(coordinates, stride)
stride = torch.IntTensor([1, 1])
with self.assertRaises(RuntimeError):
MinkowskiEngineTest._C.coordinate_map_stride_test(coordinates, stride)
stride = torch.IntTensor([2])
map_size, tensor_stride = MinkowskiEngineTest._C.coordinate_map_stride_test(
coordinates, stride
)
self.assertEqual(map_size, 2)
self.assertEqual(tensor_stride, [2])
coordinates = torch.IntTensor(
[[0, 1, 1], [0, 2, 1], [0, 1, 0], [1, 0, 3], [1, 0, 2]]
)
stride = torch.IntTensor([1])
with self.assertRaises(RuntimeError):
MinkowskiEngineTest._C.coordinate_map_stride_test(coordinates, stride)
coordinates = torch.IntTensor(
[[0, 1, 1], [0, 2, 1], [0, 1, 0], [1, 0, 3], [1, 0, 2]]
).to(0)
stride = torch.IntTensor([1, 1])
map_size, tensor_stride = MinkowskiEngineTest._C.coordinate_map_stride_test(
coordinates, stride
)
self.assertEqual(map_size, 5)
self.assertEqual(tensor_stride, [1, 1])
stride = torch.IntTensor([2, 1])
map_size, tensor_stride = MinkowskiEngineTest._C.coordinate_map_stride_test(
coordinates, stride
)
self.assertEqual(map_size, 5)
self.assertEqual(tensor_stride, [2, 1])
stride = torch.IntTensor([4, 4])
map_size, tensor_stride = MinkowskiEngineTest._C.coordinate_map_stride_test(
coordinates, stride
)
self.assertEqual(map_size, 2)
self.assertEqual(tensor_stride, [4, 4])
coordinates = torch.IntTensor([[0, -1], [0, -2], [0, 1], [0, 0]]).to(0)
stride = torch.IntTensor([2])
map_size, tensor_stride = MinkowskiEngineTest._C.coordinate_map_stride_test(
coordinates, stride
)
self.assertEqual(map_size, 2)
self.assertEqual(tensor_stride, [2])
|