π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): closed-form 3x3 inverse in warp/normalize (cusolver-free) (#3834)
Edgar Ribad3121601d678
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex b8f0ebf83..b636a57bb 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -25,7 +25,7 @@ import torch.nn.functional as Ffrom kornia.constants import pifrom kornia.core._compat import deprecatedfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE-from kornia.core.utils import _torch_inverse_cast+from kornia.core.utils import _inverse_3x3_closed_form, _torch_inverse_cast__all__ = ["ARKitQTVecs_to_ColmapQTVecs",@@ -1088,7 +1088,9 @@ def normalize_homography(# compute the transformation pixel/norm for src/dstsrc_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)- src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)+ # Closed-form 3x3 inverse of the (well-conditioned) pixel-normalization matrix: cusolver-free,+ # so homography normalization runs on the Jetson wheel where ``torch.linalg.inv`` dlopen-fails.+ src_pix_trans_src_norm = _inverse_3x3_closed_form(src_norm_trans_src_pix)dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)# compute chain transformationsdiff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.pyindex 5faacd6cf..5f18751b1 100644--- a/kornia/geometry/transform/imgwarp.py+++ b/kornia/geometry/transform/imgwarp.py@@ -133,7 +133,11 @@ def warp_perspective(# we F.normalize the 3x3 transformation matrix and convert to 3x4dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M, (H, W), (h_out, w_out)) # Bx3x3- src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm) # Bx3x3+ # Closed-form 3x3 inverse (pure arithmetic) instead of ``torch.linalg.inv``: numerically+ # equivalent for these well-conditioned transforms, and it runs where the LAPACK/cusolver+ # backend is unavailable (e.g. the Jetson wheel, where ``linalg.inv`` dlopen-fails and would+ # otherwise crash every warp on GPU).+ src_norm_trans_dst_norm = _inverse_3x3_closed_form(dst_norm_trans_src_norm) # Bx3x3# this piece of code substitutes F.affine_grid since it does not support 3x3grid = (@@ -213,7 +217,9 @@ def warp_affine(M_3x3: torch.Tensor = convert_affinematrix_to_homography(M)dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M_3x3, (H, W), dsize)- src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm)+ # Closed-form 3x3 inverse (see warp_perspective) β cusolver-free, so affine warps run on the+ # Jetson wheel where ``torch.linalg.inv`` dlopen-fails.+ src_norm_trans_dst_norm = _inverse_3x3_closed_form(dst_norm_trans_src_norm)grid = F.affine_grid(src_norm_trans_dst_norm[:, :2, :], [B, C, dsize[0], dsize[1]], align_corners=align_corners)