Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add test_scale_pinhole and fix scale input shape

edgarriba24e788f64d7d
diff --git a/test/test_depth_warper.py b/test/test_depth_warper.py
index 3fafdb47e..e4adde536 100644
--- a/test/test_depth_warper.py
+++ b/test/test_depth_warper.py
@@ -47,7 +47,7 @@ class Tester(unittest.TestCase):
warper = tgm.DepthWarper(pinhole_i)
warper.compute_homographies(
pinhole_ref, scale=torch.ones(
- batch_size, 1))
+ batch_size))
# generate synthetic inverse depth
inv_depth_ref = torch.ones(batch_size, 1, height, width)
diff --git a/test/test_functional.py b/test/test_functional.py
index 298072c98..2f60e22f0 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -30,6 +30,25 @@ def test_inverse_pose(batch_size, device_type):
assert gradcheck(tgm.inverse_pose, (dst_pose_src,),
raise_exception=True)
[email protected]("device_type", TEST_DEVICES)
+#@pytest.mark.parametrize("batch_size", [1, 2, 5, 6])
[email protected]("batch_size", [6])
+def test_scale_pinhole(batch_size, device_type):
+ # generate input data
+ device = torch.device(device_type)
+ pinholes = torch.rand(batch_size, 12).to(device)
+ scales = torch.rand(batch_size).to(device)
+
+ pinholes_scale = tgm.scale_pinhole(pinholes, scales)
+ assert utils.check_equal_torch(
+ pinholes_scale[..., :6] / scales.unsqueeze(-1), pinholes[..., :6])
+
+ # evaluate function gradient
+ pinholes = utils.tensor_to_gradcheck_var(pinholes) # to var
+ scales = utils.tensor_to_gradcheck_var(scales) # to var
+ assert gradcheck(tgm.scale_pinhole, (pinholes, scales,),
+ raise_exception=True)
+
@pytest.mark.parametrize("device_type", TEST_DEVICES)
@pytest.mark.parametrize("batch_size", [1, 2, 5, 6])
def test_pinhole_matrix(batch_size, device_type):
diff --git a/torchgeometry/depth_warper.py b/torchgeometry/depth_warper.py
index f525b0e6d..82c8fa351 100644
--- a/torchgeometry/depth_warper.py
+++ b/torchgeometry/depth_warper.py
@@ -39,7 +39,7 @@ class DepthWarper(nn.Module):
if scale is None:
batch_size = pinhole.shape[0]
scale = torch.ones(
- batch_size, 1).to(
+ batch_size).to(
pinhole.device).type_as(pinhole)
# TODO: add type and value checkings
pinhole_ref = scale_pinhole(pinhole, scale)
diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.py
index 42c560479..519e08b98 100644
--- a/torchgeometry/pinhole.py
+++ b/torchgeometry/pinhole.py
@@ -15,8 +15,6 @@ __all__ = [
"InversePose",
"PinholeMatrix",
"InversePinholeMatrix",
- "ScalePinhole",
- "Homography_i_H_ref",
]
@@ -132,14 +130,14 @@ def scale_pinhole(pinholes, scale):
- Output: :math:`(N, 12)`
Example:
- >>> pinhole_i = torch.rand(1, 12) # Nx12
- >>> scales = 2.0 * torch.ones(1, 1) # Nx1
+ >>> pinhole_i = torch.rand(1, 12) # Nx12
+ >>> scales = 2.0 * torch.ones(1) # N
>>> pinhole_i_scaled = tgm.scale_pinhole(pinhole_i) # Nx12
"""
assert len(pinholes.shape) == 2 and pinholes.shape[1] == 12, pinholes.shape
- assert len(scale.shape) == 2 and scale.shape[1] == 1, scale.shape
+ assert len(scale.shape) == 1, scale.shape
pinholes_scaled = pinholes.clone()
- pinholes_scaled[..., :6] = pinholes[..., :6] * scale
+ pinholes_scaled[..., :6] = pinholes[..., :6] * scale.unsqueeze(-1)
return pinholes_scaled
@@ -270,19 +268,3 @@ class InversePinholeMatrix(nn.Module):
def forward(self, input):
return inverse_pinhole_matrix(input)
-
-
-class ScalePinhole(nn.Module):
- def __init__(self):
- super(ScalePinhole, self).__init__()
-
- def forward(self, input, scale):
- return scale_pinhole(input, scale)
-
-
-class Homography_i_H_ref(nn.Module):
- def __init__(self):
- super(Homography_i_H_ref, self).__init__()
-
- def forward(self, pinhole_i, pinhole_ref):
- return homography_i_H_ref(pinhole_i, pinhole_ref)