π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[Fix] Increase speed of transform_points (#687)
Adrian78c8ba046d66
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex 89828029e..b4dbc0373 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -204,15 +204,26 @@ def transform_points(trans_01: torch.Tensor,if not trans_01.shape[0] == points_1.shape[0] and trans_01.shape[0] != 1:raise ValueError("Input batch size must be the same for both tensors or 1")if not trans_01.shape[-1] == (points_1.shape[-1] + 1):- raise ValueError("Last input dimensions must differe by one unit")+ raise ValueError("Last input dimensions must differ by one unit")++ # We reshape to BxNxD in case we get more dimensions, e.g., MxBxNxD+ shape_inp = list(points_1.shape)+ points_1 = points_1.reshape(-1, points_1.shape[-2], points_1.shape[-1])+ trans_01 = trans_01.reshape(-1, trans_01.shape[-2], trans_01.shape[-1])+ # We expand trans_01 to match the dimensions needed for bmm+ trans_01 = torch.repeat_interleave(trans_01, repeats=points_1.shape[0] // trans_01.shape[0], dim=0)# to homogeneouspoints_1_h = convert_points_to_homogeneous(points_1) # BxNxD+1# transform coordinates- points_0_h = torch.matmul(- trans_01.unsqueeze(1), points_1_h.unsqueeze(-1))+ points_0_h = torch.bmm(points_1_h,+ trans_01.permute(0, 2, 1))points_0_h = torch.squeeze(points_0_h, dim=-1)# to euclideanpoints_0 = convert_points_from_homogeneous(points_0_h) # BxNxD+ # reshape to the input shape+ shape_inp[-2] = points_0.shape[-2]+ shape_inp[-1] = points_0.shape[-1]+ points_0 = points_0.reshape(shape_inp)return points_0diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.pyindex 1ead7cb64..3a4d81c29 100644--- a/test/augmentation/test_augmentation_mix.py+++ b/test/augmentation/test_augmentation_mix.py@@ -174,9 +174,9 @@ class TestRandomCutMix:assert_allclose(out_image, expected, rtol=1e-4, atol=1e-4)assert (out_label[0, :, 0] == label).all()- assert (out_label[0, :, 1] == torch.tensor([0, 1])).all()+ assert (out_label[0, :, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()# cut area = 4 / 12- assert_allclose(out_label[0, :, 2], torch.tensor([0.33333, 0.33333], dtype=dtype))+ assert_allclose(out_label[0, :, 2], torch.tensor([0.33333, 0.33333], device=device, dtype=dtype))def test_random_mixup_num2(self, device, dtype):torch.manual_seed(76)diff --git a/test/performance/test_project_points_speed.py b/test/performance/test_project_points_speed.pynew file mode 100644index 000000000..c1c3b5027--- /dev/null+++ b/test/performance/test_project_points_speed.py@@ -0,0 +1,24 @@+import pytest+from time import time++import torch+import kornia as kornia+++points_shapes = [(64, 1024**2, 3), (8192, 8192, 3), (1024**2, 64, 3)]+++def test_performance_speed(device, dtype):+ if device.type != 'cuda' or not torch.cuda.is_available():+ pytest.skip("Cuda not available in system,")++ print("Benchmarking project_points")+ for input_shape in points_shapes:+ BS = input_shape[0]+ inpt = torch.rand(input_shape).to(device)+ pose = torch.rand((1, 4, 4)).to(device)+ torch.cuda.synchronize(device)+ t = time()+ kornia.geometry.transform_points(pose, inpt)+ torch.cuda.synchronize(device)+ print(f"inp={input_shape}, dev={device}, {time() - t}, sec")