π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
refactor jit tests
edgarribae6600082800d
diff --git a/Makefile b/Makefileindex f1d74039a..cb4620847 100644--- a/Makefile+++ b/Makefile@@ -1,6 +1,6 @@.PHONY: test test-cpu test-gpu lint mypy build-docs install uninstall FORCE-test: test_cpu test_gpu+test: mypy lint test-cputest-cpu: FORCEpytest --typetest cpu -vxdiff --git a/test/test_imgwarp.py b/test/test_imgwarp.pyindex 30e691e1f..e6c0b36cd 100644--- a/test/test_imgwarp.py+++ b/test/test_imgwarp.py@@ -364,6 +364,9 @@ class TestRemap:raise_exception=True)def test_jit(self):+ @torch.jit.script+ def op_script(input, map1, map2):+ return tgm.remap(input, map1, map2)batch_size, channels, height, width = 1, 1, 3, 4img = torch.ones(batch_size, channels, height, width)@@ -372,9 +375,35 @@ class TestRemap:grid += 1. # apply some shiftinput = (img, grid[..., 0], grid[..., 1],)- remap_traced = torch.jit.trace(tgm.remap, input)+ actual = op_script(*input)+ expected = tgm.remap(*input)+ assert_allclose(actual, expected)++ def test_jit_trace(self):+ @torch.jit.script+ def op_script(input, map1, map2):+ return tgm.remap(input, map1, map2)+ # 1. Trace op+ batch_size, channels, height, width = 1, 1, 3, 4+ img = torch.ones(batch_size, channels, height, width)+ grid = tgm.utils.create_meshgrid(+ height, width, normalized_coordinates=False)+ grid += 1. # apply some shift+ input_tuple = (img, grid[..., 0], grid[..., 1])+ op_traced = torch.jit.trace(op_script, input_tuple)- assert_allclose(tgm.remap(*input), remap_traced(*input))+ # 2. Generate different input+ batch_size, channels, height, width = 2, 2, 2, 5+ img = torch.ones(batch_size, channels, height, width)+ grid = tgm.utils.create_meshgrid(+ height, width, normalized_coordinates=False)+ grid += 2. # apply some shift++ # 3. Apply to different input+ input_tuple = (img, grid[..., 0], grid[..., 1])+ actual = op_script(*input_tuple)+ expected = tgm.remap(*input_tuple)+ assert_allclose(actual, expected)class TestInvertAffineTransform:@@ -415,9 +444,22 @@ class TestInvertAffineTransform:raise_exception=True)def test_jit(self):+ @torch.jit.script+ def op_script(input):+ return tgm.invert_affine_transform(input)+ matrix = torch.eye(2, 3)+ op_traced = torch.jit.trace(op_script, matrix)+ actual = op_traced(matrix)+ expected = tgm.invert_affine_transform(matrix)+ assert_allclose(actual, expected)++ def test_jit_trace(self):+ @torch.jit.script+ def op_script(input):+ return tgm.invert_affine_transform(input)matrix = torch.eye(2, 3)- op_traced = torch.jit.trace(- tgm.invert_affine_transform, matrix)- actual = tgm.invert_affine_transform(matrix)- actual_traced = op_traced(matrix)- assert_allclose(actual, actual_traced)+ matrix_2 = torch.eye(2, 3).repeat(2, 1, 1)+ op_traced = torch.jit.trace(op_script, matrix)+ actual = op_traced(matrix_2)+ expected = tgm.invert_affine_transform(matrix_2)+ assert_allclose(actual, expected)diff --git a/test/test_pinhole.py b/test/test_pinhole.pyindex a02d06669..630a94d34 100644--- a/test/test_pinhole.py+++ b/test/test_pinhole.py@@ -401,17 +401,41 @@ class TestNormalizePixelCoordinates:assert_allclose(grid_norm, expected)def test_jit(self):- height = torch.tensor(3)- width = torch.tensor(4)+ @torch.jit.script+ def op_script(input: torch.Tensor, height: int,+ width: int) -> torch.Tensor:+ return tgm.normalize_pixel_coordinates(input, height, width)+ height, width = 3, 4grid = tgm.utils.create_meshgrid(height, width, normalized_coordinates=False)+ actual = op_script(grid, height, width)+ expected = tgm.normalize_pixel_coordinates(+ grid, height, width)++ assert_allclose(actual, expected)++ def test_jit_trace(self):+ @torch.jit.script+ def op_script(input, height, width):+ return tgm.normalize_pixel_coordinates(input, height, width)+ # 1. Trace op+ height, width = 3, 4+ grid = tgm.utils.create_meshgrid(+ height, width, normalized_coordinates=False)op_traced = torch.jit.trace(- tgm.normalize_pixel_coordinates,- (grid, height, width,))+ op_script,+ (grid, torch.tensor(height), torch.tensor(width),))++ # 2. Generate new input+ height, width = 2, 5+ grid = tgm.utils.create_meshgrid(+ height, width, normalized_coordinates=False).repeat(2, 1, 1, 1)- actual = tgm.normalize_pixel_coordinates(+ # 3. Evaluate+ actual = op_traced(+ grid, torch.tensor(height), torch.tensor(width))+ expected = tgm.normalize_pixel_coordinates(grid, height, width)- expected = op_traced(grid, height, width)assert_allclose(actual, expected)diff --git a/torchgeometry/core/pinhole.py b/torchgeometry/core/pinhole.pyindex aef9dc226..25dedfb72 100644--- a/torchgeometry/core/pinhole.py+++ b/torchgeometry/core/pinhole.py@@ -1,4 +1,4 @@-from typing import Iterable, Optional+from typing import Iterable, Optional, Unionimport warningsimport torch@@ -602,8 +602,8 @@ def pixel2cam(depth: torch.Tensor, intrinsics_inv: torch.Tensor,def normalize_pixel_coordinates(pixel_coordinates: torch.Tensor,- height: float,- width: float) -> torch.Tensor:+ height: int,+ width: int) -> torch.Tensor:r"""Normalize pixel coordinates between -1 and 1.Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).@@ -611,8 +611,8 @@ def normalize_pixel_coordinates(Args:pixel_coordinate (torch.Tensor): the grid with pixel coordinates.Shape must be :math:`(B, H, W, 2)`.- width (float): the maximum width in the x-axis.- height (float): the maximum height in the y-axis.+ width (int): the maximum width in the x-axis.+ height (int): the maximum height in the y-axis.Return:torch.Tensor: the nornmalized pixel coordinates.@@ -620,11 +620,20 @@ def normalize_pixel_coordinates(if pixel_coordinates.shape[-1] != 2:raise ValueError("Input pixel_coordinates must be of shape (*, 2). ""Got {}".format(pixel_coordinates.shape))+ height_t: torch.Tensor = torch.tensor([])+ width_t: torch.Tensor = torch.tensor([])+ if isinstance(height, int):+ height_t = torch.tensor(height)+ else:+ height_t = height+ if isinstance(width, int):+ width_t = torch.tensor(width)+ else:+ width_t = width# compute normalization factor- hw: torch.Tensor = torch.stack([- torch.tensor(width), torch.tensor(height)]- ).to(pixel_coordinates.device).to(pixel_coordinates.dtype)+ hw: torch.Tensor = torch.stack([width_t, height_t]).to(+ pixel_coordinates.device).to(pixel_coordinates.dtype)factor: torch.Tensor = torch.tensor(2.) / (hw - torch.tensor(1.))