Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

:rage4: Replace deprecated torch.qr (#1376)

Edgar Ribaa9e383f7114b
diff --git a/kornia/geometry/epipolar/projection.py b/kornia/geometry/epipolar/projection.py
index 595567b22..a4706d238 100644
--- a/kornia/geometry/epipolar/projection.py
+++ b/kornia/geometry/epipolar/projection.py
@@ -4,6 +4,7 @@ from typing import Tuple, Union
import torch
from kornia.geometry.epipolar import numeric
+from kornia.utils._compat import linalg_qr
def intrinsics_like(focal: float, input: torch.Tensor) -> torch.Tensor:
@@ -131,7 +132,7 @@ def KRt_from_projection(P: torch.Tensor, eps: float = 1e-6) -> Tuple[torch.Tenso
# Trick to turn QR-decomposition into RQ-decomposition
reverse = torch.tensor([[0, 0, 1], [0, 1, 0], [1, 0, 0]], device=P.device, dtype=P.dtype).unsqueeze(0)
submat_3x3 = torch.matmul(reverse, submat_3x3).permute(0, 2, 1)
- ortho_mat, upper_mat = torch.qr(submat_3x3)
+ ortho_mat, upper_mat = linalg_qr(submat_3x3)
ortho_mat = torch.matmul(reverse, ortho_mat.permute(0, 2, 1))
upper_mat = torch.matmul(reverse, torch.matmul(upper_mat.permute(0, 2, 1), reverse))
diff --git a/kornia/utils/_compat.py b/kornia/utils/_compat.py
index 3177536a7..1c6a860a3 100644
--- a/kornia/utils/_compat.py
+++ b/kornia/utils/_compat.py
@@ -19,3 +19,12 @@ else:
# 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).solution
+
+
+if version.parse(torch_version()) > version.parse("1.7.1"):
+ # TODO: remove the type: ignore once Python 3.6 is deprecated.
+ # It turns out that Pytorch has no attribute `torch.linalg` for
+ # Python 3.6 / PyTorch 1.7.0, 1.7.1
+ from torch.linalg import qr as linalg_qr # type: ignore
+else:
+ from torch import qr as linalg_qr # type: ignore