|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import torch |
|
|
import MinkowskiEngine as ME |
|
|
|
|
|
data_batch_0 = [ |
|
|
[0, 0, 2.1, 0, 0], |
|
|
[0, 1, 1.4, 3, 0], |
|
|
[0, 0, 4.0, 0, 0] |
|
|
] |
|
|
|
|
|
data_batch_1 = [ |
|
|
[1, 0, 0], |
|
|
[0, 2, 0], |
|
|
[0, 0, 3] |
|
|
] |
|
|
|
|
|
|
|
|
def to_sparse_coo(data): |
|
|
|
|
|
coords, feats = [], [] |
|
|
for i, row in enumerate(data): |
|
|
for j, val in enumerate(row): |
|
|
if val != 0: |
|
|
coords.append([i, j]) |
|
|
feats.append([val]) |
|
|
return torch.IntTensor(coords), torch.FloatTensor(feats) |
|
|
|
|
|
|
|
|
def sparse_tensor_initialization(): |
|
|
coords, feats = to_sparse_coo(data_batch_0) |
|
|
|
|
|
|
|
|
coords, feats = ME.utils.sparse_collate(coords=[coords], feats=[feats]) |
|
|
sparse_tensor = ME.SparseTensor(coordinates=coords, features=feats) |
|
|
|
|
|
|
|
|
def sparse_tensor_arithmetics(): |
|
|
coords0, feats0 = to_sparse_coo(data_batch_0) |
|
|
coords0, feats0 = ME.utils.sparse_collate(coords=[coords0], feats=[feats0]) |
|
|
|
|
|
coords1, feats1 = to_sparse_coo(data_batch_1) |
|
|
coords1, feats1 = ME.utils.sparse_collate(coords=[coords1], feats=[feats1]) |
|
|
|
|
|
|
|
|
A = ME.SparseTensor(coordinates=coords0, features=feats0) |
|
|
B = ME.SparseTensor(coordinates=coords1, features=feats1) |
|
|
|
|
|
|
|
|
try: |
|
|
C = A + B |
|
|
except AssertionError: |
|
|
pass |
|
|
|
|
|
B = ME.SparseTensor( |
|
|
coordinates=coords1, |
|
|
features=feats1, |
|
|
coordinate_manager=A.coordinate_manager |
|
|
) |
|
|
|
|
|
C = A + B |
|
|
C = A - B |
|
|
C = A * B |
|
|
C = A / B |
|
|
|
|
|
|
|
|
|
|
|
D = ME.SparseTensor( |
|
|
|
|
|
features=feats0, |
|
|
coordinate_manager=A.coordinate_manager, |
|
|
coordinate_map_key=A.coordinate_map_key |
|
|
) |
|
|
|
|
|
A += D |
|
|
A -= D |
|
|
A *= D |
|
|
A /= D |
|
|
|
|
|
|
|
|
E = ME.cat(A, D) |
|
|
|
|
|
|
|
|
def operation_mode(): |
|
|
|
|
|
ME.set_sparse_tensor_operation_mode( |
|
|
ME.SparseTensorOperationMode.SHARE_COORDINATE_MANAGER) |
|
|
print(ME.sparse_tensor_operation_mode()) |
|
|
|
|
|
coords0, feats0 = to_sparse_coo(data_batch_0) |
|
|
coords0, feats0 = ME.utils.sparse_collate(coords=[coords0], feats=[feats0]) |
|
|
|
|
|
coords1, feats1 = to_sparse_coo(data_batch_1) |
|
|
coords1, feats1 = ME.utils.sparse_collate(coords=[coords1], feats=[feats1]) |
|
|
|
|
|
for _ in range(2): |
|
|
|
|
|
A = ME.SparseTensor(coordinates=coords0, features=feats0) |
|
|
B = ME.SparseTensor( |
|
|
coordinates=coords1, |
|
|
features=feats1, |
|
|
|
|
|
) |
|
|
|
|
|
C = A + B |
|
|
|
|
|
|
|
|
ME.clear_global_coordinate_manager() |
|
|
|
|
|
|
|
|
def decomposition(): |
|
|
coords0, feats0 = to_sparse_coo(data_batch_0) |
|
|
coords1, feats1 = to_sparse_coo(data_batch_1) |
|
|
coords, feats = ME.utils.sparse_collate( |
|
|
coords=[coords0, coords1], feats=[feats0, feats1]) |
|
|
|
|
|
|
|
|
A = ME.SparseTensor(coordinates=coords, features=feats) |
|
|
conv = ME.MinkowskiConvolution( |
|
|
in_channels=1, out_channels=2, kernel_size=3, stride=2, dimension=2) |
|
|
B = conv(A) |
|
|
|
|
|
|
|
|
list_of_coords = B.decomposed_coordinates |
|
|
list_of_feats = B.decomposed_features |
|
|
list_of_coords, list_of_feats = B.decomposed_coordinates_and_features |
|
|
|
|
|
|
|
|
batch_index = 1 |
|
|
coords = B.coordinates_at(batch_index) |
|
|
feats = B.features_at(batch_index) |
|
|
|
|
|
|
|
|
batch_index = 3 |
|
|
print(B.coordinates_at(batch_index)) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
sparse_tensor_initialization() |
|
|
sparse_tensor_arithmetics() |
|
|
operation_mode() |
|
|
decomposition() |
|
|
|