π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add test_translation, test_rotation to test_homography_warper.py
edgarriba6b634718fb8b
diff --git a/test/test_homography_warper.py b/test/test_homography_warper.pyindex 4607aad4f..0aaf1bc2a 100644--- a/test/test_homography_warper.py+++ b/test/test_homography_warper.py@@ -8,11 +8,59 @@ import utils # test utilsfrom common import TEST_DEVICES-class Tester:+class TestHomographyWarper:num_tests = 10threshold = 0.05+ def test_identity(self):+ # create input data+ height, width = 4, 4+ patch_src = torch.rand(1, 1, height, width)+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3)++ # instantiate warper+ warper = tgm.HomographyWarper(height, width)++ # warp from source to destination+ patch_dst = warper(patch_src, dst_homo_src)+ assert utils.check_equal_torch(patch_src, patch_dst)++ def test_translation(self):+ # create input data+ offset = 2. # in pixel+ height, width = 4, 4+ patch_src = torch.rand(1, 1, height, width)+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3)+ dst_homo_src[..., 0, 2] = offset / (width -1) # apply offset in x++ # instantiate warper+ warper = tgm.HomographyWarper(height, width)++ # warp from source to destination+ patch_dst = warper(patch_src, dst_homo_src)+ assert utils.check_equal_torch(patch_src[..., 1:], patch_dst[..., :3])++ def test_rotation(self):+ # create input data+ height, width = 2, 2+ patch_src = torch.rand(1, 1, height, width)+ # rotation of 90deg+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3)+ dst_homo_src[..., 0, 0] = 0.0+ dst_homo_src[..., 0, 1] = 1.0+ dst_homo_src[..., 1, 0] = -1.0+ dst_homo_src[..., 1, 1] = 0.0++ # instantiate warper and warp from source to destination+ warper = tgm.HomographyWarper(height, width)+ patch_dst = warper(patch_src, dst_homo_src)++ assert torch.allclose(patch_src[..., 0, 0], patch_dst[..., 0, 1])+ assert torch.allclose(patch_src[..., 0, 1], patch_dst[..., 1, 1])+ assert torch.allclose(patch_src[..., 1, 0], patch_dst[..., 0, 0])+ assert torch.allclose(patch_src[..., 1, 1], patch_dst[..., 1, 0])+@pytest.mark.parametrize("device_type", TEST_DEVICES)@pytest.mark.parametrize("batch_size", [1, 2, 3])def test_homography_warper(self, batch_size, device_type):@@ -58,21 +106,19 @@ class Tester:patch_dst_to_src, patch_dst_to_src_functional)@pytest.mark.parametrize("device_type", TEST_DEVICES)- @pytest.mark.parametrize("batch_size", [1, 3])- @pytest.mark.parametrize("channels", [1, 3])- @pytest.mark.parametrize("height", [7, 8])- @pytest.mark.parametrize("width", [5, 16])- def test_homography_warper_gradcheck(- self, batch_size, channels, height, width, device_type):+ @pytest.mark.parametrize("batch_shape", [+ (1, 1, 7, 5), (2, 3, 8, 5), (1, 1, 7, 16),])+ def test_gradcheck(self, batch_shape, device_type):# generate input datadevice = torch.device(device_type)eye_size = 3 # identity 3x3# create checkerboard- patch_src = torch.rand(batch_size, channels, height, width).to(device)+ patch_src = torch.rand(batch_shape).to(device)patch_src = utils.tensor_to_gradcheck_var(patch_src) # to var# create base homography+ batch_size, _, height, width = patch_src.shapedst_homo_src = utils.create_eye_batch(batch_size, eye_size)dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src, requires_grad=False) # to var@@ -80,9 +126,6 @@ class Tester:# instantiate warperwarper = tgm.HomographyWarper(height, width)- patch_dst = warper(patch_src, dst_homo_src)- assert patch_dst.shape == (batch_size, channels, height, width)-# evaluate function gradientassert gradcheck(warper, (patch_src, dst_homo_src,),raise_exception=True)diff --git a/torchgeometry/homography_warper.py b/torchgeometry/homography_warper.pyindex b68853f39..58efae127 100644--- a/torchgeometry/homography_warper.py+++ b/torchgeometry/homography_warper.py@@ -22,10 +22,8 @@ class HomographyWarper(nn.Module):X_{dst} = H_{dst}^{src} * X_{src}Args:- width (int): The width of the image to warp.height (int): The height of the image to warp.- points (Tensor): Tensor[3, N] of homogeneous points in normalized image- space [-1, 1] to sample. Optional parameter.+ width (int): The width of the image to warp.padding_mode (string): Either 'zeros' to replace out of bounds withzeros or 'border' to choose the closestborder data.@@ -57,16 +55,18 @@ class HomographyWarper(nn.Module):flow = transform_points(H, grid.to(H.device).type_as(H)) # NxHxWx2return flow.view(batch_size, self.height, self.width, 2) # NxHxWx2- def forward(self, patch, dst_homo_src):+ def forward(self, patch_src, dst_homo_src):"""Warps an image or tensor from source into reference frame.Args:- patch (Tensor): The image or tensor to warp. Should be from source.- dst_homo_src (Tensor): The homography or stack of homographies- from source to destination.+ patch_src (torch.Tensor): The image or tensor to warp.+ Should be from source.+ dst_homo_src (torch.Tensor): The homography or stack of homographies+ from source to destination. The homography assumes normalized+ coordinates [-1, 1].Return:- Tensor: Patch sampled at locations from source to destination.+ torch.Tensor: Patch sampled at locations from source to destination.Shape:- Input: :math:`(N, C, H, W)` and :math:`(N, 3, 3)`@@ -78,14 +78,15 @@ class HomographyWarper(nn.Module):>>> warper = tgm.HomographyWarper(32, 32)>>> output = warper(input, homography) # NxCxHxW"""- if not dst_homo_src.device == patch.device:+ if not dst_homo_src.device == patch_src.device:raise TypeError("Patch and homography must be on the same device. \Got patch.device: {} dst_H_src.device: {}."- .format(patch.device, dst_homo_src.device))+ .format(patch_src.device, dst_homo_src.device))return torch.nn.functional.grid_sample(- patch, self.warp_grid(dst_homo_src), mode='bilinear',+ patch_src, self.warp_grid(dst_homo_src), mode='bilinear',padding_mode=self.padding_mode)+# functional apidiff --git a/torchgeometry/utils.py b/torchgeometry/utils.pyindex fd00787d6..9c920318a 100644--- a/torchgeometry/utils.py+++ b/torchgeometry/utils.py@@ -38,7 +38,7 @@ def create_meshgrid(height, width, normalized_coordinates=True):ys = torch.linspace(0, height - 1, height)# generate grid by stacking coordinatesbase_grid = torch.stack(torch.meshgrid([xs, ys])).transpose(1, 2) # 2xHxW- return torch.unsqueeze(base_grid, dim=0).permute(0, 2, 3, 1) # 1xHxwx2+ return torch.unsqueeze(base_grid, dim=0).permute(0, 2, 3, 1) # 1xHxWx2def image_to_tensor(image):