π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fixes warnings and add compatibility stub in torch solve (#1235)
Edgar Ribaac597446adcd
diff --git a/docs/source/_static/css/copybtn.css b/docs/source/_static/css/copybtn.cssindex d7bb59738..fef76c3d5 100644--- a/docs/source/_static/css/copybtn.css+++ b/docs/source/_static/css/copybtn.css@@ -20,4 +20,4 @@.copybtn:hover {background-color: rgba(100, 30, 255, 0.1);opacity: 0.9;-}\ No newline at end of file+}diff --git a/kornia/geometry/subpix/spatial_soft_argmax.py b/kornia/geometry/subpix/spatial_soft_argmax.pyindex 6770761e3..73d34355e 100644--- a/kornia/geometry/subpix/spatial_soft_argmax.py+++ b/kornia/geometry/subpix/spatial_soft_argmax.py@@ -8,6 +8,7 @@ import korniafrom kornia.geometry import normalize_pixel_coordinates, normalize_pixel_coordinates3dfrom kornia.geometry.subpix import dsntfrom kornia.utils import create_meshgrid, create_meshgrid3d+from kornia.utils.helpers import _torch_solve_castdef _get_window_grid_kernel2d(h: int, w: int, device: torch.device = torch.device('cpu')) -> torch.Tensor:@@ -626,7 +627,7 @@ def conv_quad_interp3d(nms_mask: torch.Tensor = kornia.feature.nms3d(input, (3, 3, 3), True)x_solved: torch.Tensor = torch.zeros_like(b)- x_solved_masked, _ = torch.solve(b[nms_mask.view(-1)], Hes[nms_mask.view(-1)])+ x_solved_masked, _ = _torch_solve_cast(b[nms_mask.view(-1)], Hes[nms_mask.view(-1)])x_solved.masked_scatter_(nms_mask.view(-1, 1, 1), x_solved_masked)dx: torch.Tensor = -x_solveddiff --git a/kornia/geometry/transform/thin_plate_spline.py b/kornia/geometry/transform/thin_plate_spline.pyindex a85f89d03..b90537bb8 100644--- a/kornia/geometry/transform/thin_plate_spline.py+++ b/kornia/geometry/transform/thin_plate_spline.py@@ -4,6 +4,7 @@ import torchimport torch.nn as nnfrom kornia.utils import create_meshgrid+from kornia.utils.helpers import _torch_solve_cast__all__ = ["get_tps_transform", "warp_points_tps", "warp_image_tps"]@@ -82,7 +83,7 @@ def get_tps_transform(points_src: torch.Tensor, points_dst: torch.Tensor) -> Tupl_matrix: torch.Tensor = torch.cat((k_matrix, p_matrix), -1)l_matrix = torch.cat((l_matrix, p_matrix_t), 1)- weights, _ = torch.solve(dest_with_zeros, l_matrix)+ weights, _ = _torch_solve_cast(dest_with_zeros, l_matrix)kernel_weights: torch.Tensor = weights[:, :-3]affine_weights: torch.Tensor = weights[:, -3:]diff --git a/kornia/utils/_compat.py b/kornia/utils/_compat.pynew file mode 100644index 000000000..1481eeb2e--- /dev/null+++ b/kornia/utils/_compat.py@@ -0,0 +1,12 @@+import torch++from packaging import version++if version.parse(torch.__version__) > version.parse("1.7.1"):+ from torch.linalg import solve+else:+ from torch import solve as _solve++ # NOTE: in previous versions `torch.solve` accepted arguments in another order.+ def solve(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:+ return _solve(B, A).solutiondiff --git a/kornia/utils/helpers.py b/kornia/utils/helpers.pyindex 8f8e42fd2..5aee37289 100644--- a/kornia/utils/helpers.py+++ b/kornia/utils/helpers.py@@ -2,6 +2,8 @@ from typing import Any, List, Optional, Tupleimport torch+from kornia.utils._compat import solve+def _extract_device_dtype(tensor_list: List[Optional[Any]]) -> Tuple[torch.device, torch.dtype]:"""Check if all the input are in the same device (only if when they are torch.Tensor).@@ -84,6 +86,7 @@ def _torch_svd_cast(input: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, toreturn (out1.to(input.dtype), out2.to(input.dtype), out3.to(input.dtype))+# TODO: return only `torch.Tensor` and review all the calls to adjustdef _torch_solve_cast(input: torch.Tensor, A: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:"""Helper function to make torch.solve work with other than fp32/64.@@ -97,6 +100,6 @@ def _torch_solve_cast(input: torch.Tensor, A: torch.Tensor) -> Tuple[torch.Tensoif dtype not in (torch.float32, torch.float64):dtype = torch.float32- out1, out2 = torch.solve(input.to(dtype), A.to(dtype))+ out = solve(A.to(dtype), input.to(dtype))- return (out1.to(input.dtype), out2.to(input.dtype))+ return (out.to(input.dtype), out)