Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor normalize_pixel_coordinates for jit

edgarribade55439801f3
diff --git a/test/test_pinhole.py b/test/test_pinhole.py
index ed38de9df..a02d06669 100644
--- a/test/test_pinhole.py
+++ b/test/test_pinhole.py
@@ -3,6 +3,7 @@ import pytest
import torch
import torchgeometry as tgm
from torch.autograd import gradcheck
+from torch.testing import assert_allclose
import utils # test utilities
from common import device_type
@@ -386,3 +387,31 @@ def test_homography_i_H_ref(batch_size, device_type):
(utils.tensor_to_gradcheck_var(pinhole_ref) + eps,
utils.tensor_to_gradcheck_var(pinhole_i) + eps,),
raise_exception=True)
+
+
+class TestNormalizePixelCoordinates:
+ def test_small(self):
+ height, width = 3, 4
+ grid = tgm.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+ expected = tgm.utils.create_meshgrid(
+ height, width, normalized_coordinates=True)
+ grid_norm = tgm.normalize_pixel_coordinates(
+ grid, height, width)
+ assert_allclose(grid_norm, expected)
+
+ def test_jit(self):
+ height = torch.tensor(3)
+ width = torch.tensor(4)
+ grid = tgm.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+
+ op_traced = torch.jit.trace(
+ tgm.normalize_pixel_coordinates,
+ (grid, height, width,))
+
+ actual = 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.py
index 0594c47b3..1a5270f5e 100644
--- a/torchgeometry/core/pinhole.py
+++ b/torchgeometry/core/pinhole.py
@@ -605,37 +605,31 @@ def normalize_pixel_coordinates(
height: float,
width: float) -> torch.Tensor:
r"""Normalize pixel coordinates between -1 and 1.
-
Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).
-
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.
-
Return:
torch.Tensor: the nornmalized pixel coordinates.
"""
- if len(pixel_coordinates.shape) != 4 and pixel_coordinates.shape[-1] != 2:
- raise ValueError("Input pixel_coordinates must be of shape BxHxWx2. "
+ if pixel_coordinates.shape[-1] != 2:
+ raise ValueError("Input pixel_coordinates must be of shape (*, 2). "
"Got {}".format(pixel_coordinates.shape))
- # unpack pixel coordinates
- u_coord, v_coord = torch.chunk(pixel_coordinates, dim=-1, chunks=2)
+ # compute normalization factor
+ hw: torch.Tensor = torch.stack([
+ torch.tensor(width), torch.tensor(height)]
+ ).to(pixel_coordinates.device).to(pixel_coordinates.dtype)
- # apply actual normalization
- factor_u: float = 2. / (width - 1)
- factor_v: float = 2. / (height - 1)
- u_coord_norm: torch.Tensor = factor_u * u_coord - 1.
- v_coord_norm: torch.Tensor = factor_v * v_coord - 1.
+ factor: torch.Tensor = torch.tensor(2.) / (hw - torch.tensor(1.))
- # stack normalized coordinates and return
- pixel_coordinates_norm: torch.Tensor = torch.cat(
- [u_coord_norm, v_coord_norm], dim=-1)
+ # normalize coordinates and return
+ pixel_coordinates_norm: torch.Tensor = \
+ factor * pixel_coordinates - torch.tensor(1.)
return pixel_coordinates_norm
-
# based on
# https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py#L43