Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fixes for half precision in imgwrp (#1723)

Edgar Riba764f57931e33
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 52e8fc749..b9cfff0a7 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -578,7 +578,7 @@ def invert_affine_transform(matrix: torch.Tensor) -> torch.Tensor:
raise ValueError(f"Input matrix must be a Bx2x3 tensor. Got {matrix.shape}")
matrix_tmp: torch.Tensor = convert_affinematrix_to_homography(matrix)
- matrix_inv: torch.Tensor = torch.inverse(matrix_tmp)
+ matrix_inv: torch.Tensor = _torch_inverse_cast(matrix_tmp)
return matrix_inv[..., :2, :3]
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index d24fb0315..33a122b73 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -5,6 +5,7 @@ from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
from kornia.testing import assert_close
+from kornia.utils.helpers import _torch_inverse_cast
@pytest.mark.parametrize("batch_size", [1, 2, 5])
@@ -143,16 +144,17 @@ class TestWarpAffine:
center = torch.tensor([[w - 1, h - 1]], device=device, dtype=dtype) / 2
scale = torch.ones((1, 2), device=device, dtype=dtype)
angle = 90.0 * torch.ones(1, device=device, dtype=dtype)
- aff_ab = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
+ aff_ab_2x3 = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
# Same as opencv: cv2.getRotationMatrix2D(((w-1)/2,(h-1)/2), 90., 1.)
# warp the tensor
# Same as opencv: cv2.warpAffine(kornia.tensor_to_image(img_b), aff_ab[0].numpy(), (w, h))
- img_a = kornia.geometry.warp_affine(img_b, aff_ab, (h, w))
+ img_a = kornia.geometry.warp_affine(img_b, aff_ab_2x3, (h, w))
# invert the transform
- aff_ba = kornia.geometry.conversions.convert_affinematrix_to_homography(aff_ab).inverse()[..., :2, :]
- img_b_hat = kornia.geometry.warp_affine(img_a, aff_ba, (h, w))
+ aff_ab_3x3 = kornia.geometry.conversions.convert_affinematrix_to_homography(aff_ab_2x3)
+ aff_ba_2x3 = _torch_inverse_cast(aff_ab_3x3)[..., :2, :]
+ img_b_hat = kornia.geometry.warp_affine(img_a, aff_ba_2x3, (h, w))
assert_close(img_b_hat, img_b, atol=1e-3, rtol=1e-3)
def test_jit(self, device, dtype):
@@ -274,7 +276,7 @@ class TestWarpPerspective:
img_a = kornia.geometry.warp_perspective(img_b, H_ab, (h, w))
# invert the transform
- H_ba = torch.inverse(H_ab)
+ H_ba = _torch_inverse_cast(H_ab)
img_b_hat = kornia.geometry.warp_perspective(img_a, H_ba, (h, w))
assert_close(img_b_hat, img_b, rtol=1e-4, atol=1e-4)
diff --git a/test/geometry/transform/test_imgwarp3d.py b/test/geometry/transform/test_imgwarp3d.py
index fce049441..f17ec2a95 100644
--- a/test/geometry/transform/test_imgwarp3d.py
+++ b/test/geometry/transform/test_imgwarp3d.py
@@ -6,6 +6,7 @@ import kornia
import kornia.geometry.transform as proj
import kornia.testing as utils # test utils
from kornia.testing import assert_close
+from kornia.utils.helpers import _torch_inverse_cast
class TestWarpAffine3d:
@@ -36,7 +37,7 @@ class TestWarpAffine3d:
input = torch.rand(2, 5, 3, 4, 5, device=device, dtype=dtype)
P = torch.rand(2, 3, 4, device=device, dtype=dtype)
P = kornia.geometry.convert_affinematrix_to_homography3d(P)
- P_hat = (P.inverse() @ P)[:, :3]
+ P_hat = (_torch_inverse_cast(P) @ P)[:, :3]
output = proj.warp_affine3d(input, P_hat, out_shape, flags='nearest')
assert_close(output, input, rtol=1e-4, atol=1e-4)