Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

improve from homogeneous conversion and add integration test

edgarriba6908f4554da1
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 2660ad439..cea1df356 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -73,7 +73,7 @@ def deg2rad(tensor: torch.Tensor) -> torch.Tensor:
def convert_points_from_homogeneous(
- points: torch.Tensor, eps: float = 1e-5) -> torch.Tensor:
+ points: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
r"""Function that converts points from homogeneous to Euclidean space.
Examples::
@@ -95,11 +95,9 @@ def convert_points_from_homogeneous(
# set the results of division by zeror/near-zero to 1.0
# follow the convention of opencv:
# https://github.com/opencv/opencv/pull/14411/files
- mask_valid_points = torch.abs(z_vec) > eps
- scale: torch.Tensor = torch.where(
- mask_valid_points,
- torch.tensor(1.) / z_vec.masked_fill(~mask_valid_points, eps),
- torch.ones_like(z_vec))
+ mask: torch.Tensor = torch.abs(z_vec) > eps
+ scale: torch.Tensor = torch.ones_like(z_vec).masked_scatter_(
+ mask, torch.tensor(1.0) / z_vec[mask])
return scale * points[..., :-1]
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index 1e26f9ef4..472e75ec7 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -491,15 +491,6 @@ class TestConvertPointsFromHomogeneous:
assert gradcheck(kornia.convert_points_from_homogeneous, (points_h,),
raise_exception=True)
- def test_gradcheck_zero_division(self, device):
- points_h = torch.tensor([[1., 2., 3., 4., 5.],
- [4., 6., 0., -3., 1e-9]]).t().to(device)
-
- # evaluate function gradient
- points_h = tensor_to_gradcheck_var(points_h) # to var
- assert gradcheck(kornia.convert_points_from_homogeneous, (points_h,),
- raise_exception=True)
-
@pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device):
op = kornia.convert_points_from_homogeneous
diff --git a/test/integration/test_warp.py b/test/integration/test_warp.py
new file mode 100644
index 000000000..40bfcc46e
--- /dev/null
+++ b/test/integration/test_warp.py
@@ -0,0 +1,59 @@
+import pytest
+
+import kornia
+import numpy as np
+
+import torch
+import torch.nn as nn
+import torch.nn.functional as F
+import torch.optim as optim
+from test.common import device
+
+
+class MyHomography(nn.Module):
+
+ def __init__(self, init_homo: torch.Tensor) -> None:
+ super().__init__()
+ self.homo = nn.Parameter(init_homo.clone().detach())
+
+ def forward(self) -> torch.Tensor:
+ return torch.unsqueeze(self.homo, dim=0)
+
+
+class TestWarping:
+ # optimization
+ lr = 1e-3
+ num_iterations = 100
+
+ def test_smoke(self, device):
+
+ img_src_t: torch.Tensor = torch.rand(1, 3, 120, 120).to(device)
+ img_dst_t: torch.Tensor = torch.rand(1, 3, 120, 120).to(device)
+
+ init_homo: torch.Tensor = torch.from_numpy(
+ np.array([
+ [0.0415, 1.2731, -1.1731],
+ [-0.9094, 0.5072, 0.4272],
+ [0.0762, 1.3981, 1.0646]
+ ])
+ ).float()
+
+ height, width = img_dst_t.shape[-2:]
+ warper = kornia.HomographyWarper(height, width)
+ dst_homo_src = MyHomography(init_homo=init_homo).to(device)
+
+ learning_rate = self.lr
+ optimizer = optim.Adam(dst_homo_src.parameters(), lr=learning_rate)
+
+ for iter_idx in range(self.num_iterations):
+ # warp the reference image to the destiny with current homography
+ img_src_to_dst = warper(img_src_t, dst_homo_src())
+
+ # compute the photometric loss
+ loss = F.l1_loss(img_src_to_dst, img_dst_t)
+
+ optimizer.zero_grad()
+ loss.backward()
+ optimizer.step()
+
+ assert not bool(torch.isnan(dst_homo_src.homo.grad).any())