Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Normalize with intrinsics (#1727)

Dmytro Mishkin24398ee07e9a
diff --git a/docs/source/geometry.conversions.rst b/docs/source/geometry.conversions.rst
index aabdd720c..002cbaa95 100644
--- a/docs/source/geometry.conversions.rst
+++ b/docs/source/geometry.conversions.rst
@@ -22,6 +22,9 @@ Coordinates
.. autofunction:: normalize_pixel_coordinates
.. autofunction:: denormalize_pixel_coordinates3d
.. autofunction:: normalize_pixel_coordinates3d
+.. autofunction:: normalize_points_with_intrinsics
+.. autofunction:: denormalize_points_with_intrinsics
+
Homography
----------
diff --git a/kornia/core/__init__.py b/kornia/core/__init__.py
index 2318c2a73..f907b5129 100644
--- a/kornia/core/__init__.py
+++ b/kornia/core/__init__.py
@@ -1,3 +1,15 @@
-from ._backend import Device, Module, Parameter, Tensor, as_tensor, concatenate, normalize, rand, stack, where
+from ._backend import Device, Module, Parameter, Tensor, as_tensor, concatenate, normalize, rand, stack, tensor, where
-__all__ = ["concatenate", "Device", "Module", "Tensor", "Parameter", "normalize", "stack", "as_tensor", "rand", "where"]
+__all__ = [
+ "concatenate",
+ "Device",
+ "Module",
+ "Tensor",
+ "tensor",
+ "Parameter",
+ "normalize",
+ "stack",
+ "as_tensor",
+ "rand",
+ "where",
+]
diff --git a/kornia/core/_backend.py b/kornia/core/_backend.py
index 579914f35..0abb6a41a 100644
--- a/kornia/core/_backend.py
+++ b/kornia/core/_backend.py
@@ -6,6 +6,7 @@ from torch import device
# classes
Tensor = torch.Tensor
+tensor = torch.tensor
Module = torch.nn.Module
Parameter = torch.nn.Parameter
diff --git a/kornia/geometry/camera/perspective.py b/kornia/geometry/camera/perspective.py
index 6e8c5a905..66ffc84ce 100644
--- a/kornia/geometry/camera/perspective.py
+++ b/kornia/geometry/camera/perspective.py
@@ -1,7 +1,12 @@
import torch
import torch.nn.functional as F
-from kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneous
+from kornia.geometry.conversions import (
+ convert_points_from_homogeneous,
+ convert_points_to_homogeneous,
+ denormalize_points_with_intrinsics,
+ normalize_points_with_intrinsics,
+)
def project_points(point_3d: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor:
@@ -23,41 +28,12 @@ def project_points(point_3d: torch.Tensor, camera_matrix: torch.Tensor) -> torch
>>> project_points(X, K)
tensor([[5.6088, 8.6827]])
"""
- if not isinstance(point_3d, torch.Tensor):
- raise TypeError(f"Input point_3d type is not a torch.Tensor. Got {type(point_3d)}")
-
- if not isinstance(camera_matrix, torch.Tensor):
- raise TypeError(f"Input camera_matrix type is not a torch.Tensor. Got {type(camera_matrix)}")
-
- if not (point_3d.device == camera_matrix.device):
- raise ValueError("Input tensors must be all in the same device.")
-
- if not point_3d.shape[-1] == 3:
- raise ValueError("Input points_3d must be in the shape of (*, 3)." " Got {}".format(point_3d.shape))
-
- if not camera_matrix.shape[-2:] == (3, 3):
- raise ValueError("Input camera_matrix must be in the shape of (*, 3, 3).")
-
# projection eq. [u, v, w]' = K * [x y z 1]'
# u = fx * X / Z + cx
# v = fy * Y / Z + cy
-
# project back using depth dividing in a safe way
xy_coords: torch.Tensor = convert_points_from_homogeneous(point_3d)
- x_coord: torch.Tensor = xy_coords[..., 0]
- y_coord: torch.Tensor = xy_coords[..., 1]
-
- # unpack intrinsics
- fx: torch.Tensor = camera_matrix[..., 0, 0]
- fy: torch.Tensor = camera_matrix[..., 1, 1]
- cx: torch.Tensor = camera_matrix[..., 0, 2]
- cy: torch.Tensor = camera_matrix[..., 1, 2]
-
- # apply intrinsics ans return
- u_coord: torch.Tensor = x_coord * fx + cx
- v_coord: torch.Tensor = y_coord * fy + cy
-
- return torch.stack([u_coord, v_coord], dim=-1)
+ return denormalize_points_with_intrinsics(xy_coords, camera_matrix)
def unproject_points(
@@ -89,48 +65,14 @@ def unproject_points(
>>> unproject_points(x, depth, K)
tensor([[0.4963, 0.7682, 1.0000]])
"""
- if not isinstance(point_2d, torch.Tensor):
- raise TypeError(f"Input point_2d type is not a torch.Tensor. Got {type(point_2d)}")
-
if not isinstance(depth, torch.Tensor):
raise TypeError(f"Input depth type is not a torch.Tensor. Got {type(depth)}")
- if not isinstance(camera_matrix, torch.Tensor):
- raise TypeError(f"Input camera_matrix type is not a torch.Tensor. Got {type(camera_matrix)}")
-
- if not (point_2d.device == depth.device == camera_matrix.device):
- raise ValueError("Input tensors must be all in the same device.")
-
- if not point_2d.shape[-1] == 2:
- raise ValueError("Input points_2d must be in the shape of (*, 2)." " Got {}".format(point_2d.shape))
-
if not depth.shape[-1] == 1:
raise ValueError("Input depth must be in the shape of (*, 1)." " Got {}".format(depth.shape))
- if not camera_matrix.shape[-2:] == (3, 3):
- raise ValueError("Input camera_matrix must be in the shape of (*, 3, 3).")
-
- # projection eq. K_inv * [u v 1]'
- # x = (u - cx) * Z / fx
- # y = (v - cy) * Z / fy
-
- # unpack coordinates
- u_coord: torch.Tensor = point_2d[..., 0]
- v_coord: torch.Tensor = point_2d[..., 1]
-
- # unpack intrinsics
- fx: torch.Tensor = camera_matrix[..., 0, 0]
- fy: torch.Tensor = camera_matrix[..., 1, 1]
- cx: torch.Tensor = camera_matrix[..., 0, 2]
- cy: torch.Tensor = camera_matrix[..., 1, 2]
-
- # projective
- x_coord: torch.Tensor = (u_coord - cx) / fx
- y_coord: torch.Tensor = (v_coord - cy) / fy
-
- xyz: torch.Tensor = torch.stack([x_coord, y_coord], dim=-1)
- xyz = convert_points_to_homogeneous(xyz)
-
+ xy: torch.Tensor = normalize_points_with_intrinsics(point_2d, camera_matrix)
+ xyz: torch.Tensor = convert_points_to_homogeneous(xy)
if normalize:
xyz = F.normalize(xyz, dim=-1, p=2.0)
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 3798b58f0..367382975 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -4,10 +4,10 @@ from typing import Optional, Tuple
import torch
import torch.nn.functional as F
-from torch import Tensor, tensor
from kornia.constants import pi
-from kornia.testing import KORNIA_CHECK_SHAPE
+from kornia.core import Tensor, concatenate, stack, tensor
+from kornia.testing import KORNIA_CHECK_SAME_DEVICE, KORNIA_CHECK_SHAPE
from kornia.utils.helpers import _torch_inverse_cast
__all__ = [
@@ -38,6 +38,8 @@ __all__ = [
"normalize_homography3d",
"normal_transform_pixel",
"normal_transform_pixel3d",
+ "normalize_points_with_intrinsics",
+ "denormalize_points_with_intrinsics",
"worldtocam_to_camtoworld_Rt",
"camtoworld_to_worldtocam_Rt",
"Rt_to_matrix4x4",
@@ -55,7 +57,7 @@ class QuaternionCoeffOrder(enum.Enum):
WXYZ = 'wxyz'
-def rad2deg(tensor: torch.Tensor) -> torch.Tensor:
+def rad2deg(tensor: Tensor) -> Tensor:
r"""Function that converts angles from radians to degrees.
Args:
@@ -65,17 +67,17 @@ def rad2deg(tensor: torch.Tensor) -> torch.Tensor:
Tensor with same shape as input.
Example:
- >>> input = torch.tensor(3.1415926535)
+ >>> input = tensor(3.1415926535)
>>> rad2deg(input)
tensor(180.)
"""
- if not isinstance(tensor, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(tensor)}")
+ if not isinstance(tensor, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(tensor)}")
return 180.0 * tensor / pi.to(tensor.device).type(tensor.dtype)
-def deg2rad(tensor: torch.Tensor) -> torch.Tensor:
+def deg2rad(tensor: Tensor) -> Tensor:
r"""Function that converts angles from degrees to radians.
Args:
@@ -85,17 +87,17 @@ def deg2rad(tensor: torch.Tensor) -> torch.Tensor:
tensor with same shape as input.
Examples:
- >>> input = torch.tensor(180.)
+ >>> input = tensor(180.)
>>> deg2rad(input)
tensor(3.1416)
"""
- if not isinstance(tensor, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(tensor)}")
+ if not isinstance(tensor, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(tensor)}")
return tensor * pi.to(tensor.device).type(tensor.dtype) / 180.0
-def pol2cart(rho: torch.Tensor, phi: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
+def pol2cart(rho: Tensor, phi: Tensor) -> Tuple[Tensor, Tensor]:
r"""Function that converts polar coordinates to cartesian coordinates.
Args:
@@ -111,15 +113,15 @@ def pol2cart(rho: torch.Tensor, phi: torch.Tensor) -> Tuple[torch.Tensor, torch.
>>> phi = torch.rand(1, 3, 3)
>>> x, y = pol2cart(rho, phi)
"""
- if not (isinstance(rho, torch.Tensor) & isinstance(phi, torch.Tensor)):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(rho)}, {type(phi)}")
+ if not (isinstance(rho, Tensor) & isinstance(phi, Tensor)):
+ raise TypeError(f"Input type is not a Tensor. Got {type(rho)}, {type(phi)}")
x = rho * torch.cos(phi)
y = rho * torch.sin(phi)
return x, y
-def cart2pol(x: torch.Tensor, y: torch.Tensor, eps: float = 1.0e-8) -> Tuple[torch.Tensor, torch.Tensor]:
+def cart2pol(x: Tensor, y: Tensor, eps: float = 1.0e-8) -> Tuple[Tensor, Tensor]:
"""Function that converts cartesian coordinates to polar coordinates.
Args:
@@ -136,15 +138,15 @@ def cart2pol(x: torch.Tensor, y: torch.Tensor, eps: float = 1.0e-8) -> Tuple[tor
>>> y = torch.rand(1, 3, 3)
>>> rho, phi = cart2pol(x, y)
"""
- if not (isinstance(x, torch.Tensor) & isinstance(y, torch.Tensor)):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(x)}, {type(y)}")
+ if not (isinstance(x, Tensor) & isinstance(y, Tensor)):
+ raise TypeError(f"Input type is not a Tensor. Got {type(x)}, {type(y)}")
rho = torch.sqrt(x**2 + y**2 + eps)
phi = torch.atan2(y, x)
return rho, phi
-def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
+def convert_points_from_homogeneous(points: Tensor, eps: float = 1e-8) -> Tensor:
r"""Function that converts points from homogeneous to Euclidean space.
Args:
@@ -155,29 +157,29 @@ def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) ->
the points in Euclidean space :math:`(B, N, D-1)`.
Examples:
- >>> input = torch.tensor([[0., 0., 1.]])
+ >>> input = tensor([[0., 0., 1.]])
>>> convert_points_from_homogeneous(input)
tensor([[0., 0.]])
"""
- if not isinstance(points, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(points)}")
+ if not isinstance(points, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(points)}")
if len(points.shape) < 2:
raise ValueError(f"Input must be at least a 2D tensor. Got {points.shape}")
# we check for points at max_val
- z_vec: torch.Tensor = points[..., -1:]
+ z_vec: Tensor = points[..., -1:]
# 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: torch.Tensor = torch.abs(z_vec) > eps
+ mask: Tensor = torch.abs(z_vec) > eps
scale = torch.where(mask, 1.0 / (z_vec + eps), torch.ones_like(z_vec))
return scale * points[..., :-1]
-def convert_points_to_homogeneous(points: torch.Tensor) -> torch.Tensor:
+def convert_points_to_homogeneous(points: Tensor) -> Tensor:
r"""Function that converts points from Euclidean to homogeneous space.
Args:
@@ -187,25 +189,25 @@ def convert_points_to_homogeneous(points: torch.Tensor) -> torch.Tensor:
the points in homogeneous coordinates :math:`(*, N, D+1)`.
Examples:
- >>> input = torch.tensor([[0., 0.]])
+ >>> input = tensor([[0., 0.]])
>>> convert_points_to_homogeneous(input)
tensor([[0., 0., 1.]])
"""
- if not isinstance(points, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(points)}")
+ if not isinstance(points, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(points)}")
if len(points.shape) < 2:
raise ValueError(f"Input must be at least a 2D tensor. Got {points.shape}")
return torch.nn.functional.pad(points, [0, 1], "constant", 1.0)
-def _convert_affinematrix_to_homography_impl(A: torch.Tensor) -> torch.Tensor:
- H: torch.Tensor = torch.nn.functional.pad(A, [0, 0, 0, 1], "constant", value=0.0)
+def _convert_affinematrix_to_homography_impl(A: Tensor) -> Tensor:
+ H: Tensor = torch.nn.functional.pad(A, [0, 0, 0, 1], "constant", value=0.0)
H[..., -1, -1] += 1.0
return H
-def convert_affinematrix_to_homography(A: torch.Tensor) -> torch.Tensor:
+def convert_affinematrix_to_homography(A: Tensor) -> Tensor:
r"""Function that converts batch of affine matrices.
Args:
@@ -215,15 +217,15 @@ def convert_affinematrix_to_homography(A: torch.Tensor) -> torch.Tensor:
the homography matrix with shape of :math:`(B,3,3)`.
Examples:
- >>> A = torch.tensor([[[1., 0., 0.],
+ >>> A = tensor([[[1., 0., 0.],
... [0., 1., 0.]]])
>>> convert_affinematrix_to_homography(A)
tensor([[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]])
"""
- if not isinstance(A, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(A)}")
+ if not isinstance(A, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(A)}")
if not (len(A.shape) == 3 and A.shape[-2:] == (2, 3)):
raise ValueError(f"Input matrix must be a Bx2x3 tensor. Got {A.shape}")
@@ -231,7 +233,7 @@ def convert_affinematrix_to_homography(A: torch.Tensor) -> torch.Tensor:
return _convert_affinematrix_to_homography_impl(A)
-def convert_affinematrix_to_homography3d(A: torch.Tensor) -> torch.Tensor:
+def convert_affinematrix_to_homography3d(A: Tensor) -> Tensor:
r"""Function that converts batch of 3d affine matrices.
Args:
@@ -241,7 +243,7 @@ def convert_affinematrix_to_homography3d(A: torch.Tensor) -> torch.Tensor:
the homography matrix with shape of :math:`(B,4,4)`.
Examples:
- >>> A = torch.tensor([[[1., 0., 0., 0.],
+ >>> A = tensor([[[1., 0., 0., 0.],
... [0., 1., 0., 0.],
... [0., 0., 1., 0.]]])
>>> convert_affinematrix_to_homography3d(A)
@@ -250,8 +252,8 @@ def convert_affinematrix_to_homography3d(A: torch.Tensor) -> torch.Tensor:
[0., 0., 1., 0.],
[0., 0., 0., 1.]]])
"""
- if not isinstance(A, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(A)}")
+ if not isinstance(A, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(A)}")
if not (len(A.shape) == 3 and A.shape[-2:] == (3, 4)):
raise ValueError(f"Input matrix must be a Bx3x4 tensor. Got {A.shape}")
@@ -259,7 +261,7 @@ def convert_affinematrix_to_homography3d(A: torch.Tensor) -> torch.Tensor:
return _convert_affinematrix_to_homography_impl(A)
-def angle_axis_to_rotation_matrix(angle_axis: torch.Tensor) -> torch.Tensor:
+def angle_axis_to_rotation_matrix(angle_axis: Tensor) -> Tensor:
r"""Convert 3d vector of axis-angle rotation to 3x3 rotation matrix.
Args:
@@ -269,20 +271,20 @@ def angle_axis_to_rotation_matrix(angle_axis: torch.Tensor) -> torch.Tensor:
tensor of rotation matrices of shape :math:`(N, 3, 3)`.
Example:
- >>> input = torch.tensor([[0., 0., 0.]])
+ >>> input = tensor([[0., 0., 0.]])
>>> angle_axis_to_rotation_matrix(input)
tensor([[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]])
- >>> input = torch.tensor([[1.5708, 0., 0.]])
+ >>> input = tensor([[1.5708, 0., 0.]])
>>> angle_axis_to_rotation_matrix(input)
tensor([[[ 1.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, -3.6200e-06, -1.0000e+00],
[ 0.0000e+00, 1.0000e+00, -3.6200e-06]]])
"""
- if not isinstance(angle_axis, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(angle_axis)}")
+ if not isinstance(angle_axis, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(angle_axis)}")
if not angle_axis.shape[-1] == 3:
raise ValueError(f"Input size must be a (*, 3) tensor. Got {angle_axis.shape}")
@@ -307,13 +309,13 @@ def angle_axis_to_rotation_matrix(angle_axis: torch.Tensor) -> torch.Tensor:
r02 = wy * sin_theta + wx * wz * (k_one - cos_theta)
r12 = -wx * sin_theta + wy * wz * (k_one - cos_theta)
r22 = cos_theta + wz * wz * (k_one - cos_theta)
- rotation_matrix = torch.cat([r00, r01, r02, r10, r11, r12, r20, r21, r22], dim=1)
+ rotation_matrix = concatenate([r00, r01, r02, r10, r11, r12, r20, r21, r22], dim=1)
return rotation_matrix.view(-1, 3, 3)
def _compute_rotation_matrix_taylor(angle_axis):
rx, ry, rz = torch.chunk(angle_axis, 3, dim=1)
k_one = torch.ones_like(rx)
- rotation_matrix = torch.cat([k_one, -rz, ry, rz, k_one, -rx, -ry, rx, k_one], dim=1)
+ rotation_matrix = concatenate([k_one, -rz, ry, rz, k_one, -rx, -ry, rx, k_one], dim=1)
return rotation_matrix.view(-1, 3, 3)
# stolen from ceres/rotation.h
@@ -341,7 +343,7 @@ def angle_axis_to_rotation_matrix(angle_axis: torch.Tensor) -> torch.Tensor:
return rotation_matrix # Nx3x3
-def rotation_matrix_to_angle_axis(rotation_matrix: torch.Tensor) -> torch.Tensor:
+def rotation_matrix_to_angle_axis(rotation_matrix: Tensor) -> Tensor:
r"""Convert 3x3 rotation matrix to Rodrigues vector in radians.
Args:
@@ -351,30 +353,30 @@ def rotation_matrix_to_angle_axis(rotation_matrix: torch.Tensor) -> torch.Tensor
Rodrigues vector transformation of shape :math:`(N, 3)`.
Example:
- >>> input = torch.tensor([[1., 0., 0.],
+ >>> input = tensor([[1., 0., 0.],
... [0., 1., 0.],
... [0., 0., 1.]])
>>> rotation_matrix_to_angle_axis(input)
tensor([0., 0., 0.])
- >>> input = torch.tensor([[1., 0., 0.],
+ >>> input = tensor([[1., 0., 0.],
... [0., 0., -1.],
... [0., 1., 0.]])
>>> rotation_matrix_to_angle_axis(input)
tensor([1.5708, 0.0000, 0.0000])
"""
- if not isinstance(rotation_matrix, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(rotation_matrix)}")
+ if not isinstance(rotation_matrix, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(rotation_matrix)}")
if not rotation_matrix.shape[-2:] == (3, 3):
raise ValueError(f"Input size must be a (*, 3, 3) tensor. Got {rotation_matrix.shape}")
- quaternion: torch.Tensor = rotation_matrix_to_quaternion(rotation_matrix, order=QuaternionCoeffOrder.WXYZ)
+ quaternion: Tensor = rotation_matrix_to_quaternion(rotation_matrix, order=QuaternionCoeffOrder.WXYZ)
return quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
def rotation_matrix_to_quaternion(
- rotation_matrix: torch.Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+ rotation_matrix: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
+) -> Tensor:
r"""Convert 3x3 rotation matrix to 4d quaternion vector.
The quaternion vector has components in (w, x, y, z) or (x, y, z, w) format.
@@ -391,15 +393,15 @@ def rotation_matrix_to_quaternion(
the rotation in quaternion with shape :math:`(*, 4)`.
Example:
- >>> input = torch.tensor([[1., 0., 0.],
+ >>> input = tensor([[1., 0., 0.],
... [0., 1., 0.],
... [0., 0., 1.]])
>>> rotation_matrix_to_quaternion(input, eps=torch.finfo(input.dtype).eps,
... order=QuaternionCoeffOrder.WXYZ)
tensor([1., 0., 0., 0.])
"""
- if not isinstance(rotation_matrix, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(rotation_matrix)}")
+ if not isinstance(rotation_matrix, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(rotation_matrix)}")
if not rotation_matrix.shape[-2:] == (3, 3):
raise ValueError(f"Input size must be a (*, 3, 3) tensor. Got {rotation_matrix.shape}")
@@ -415,15 +417,15 @@ def rotation_matrix_to_quaternion(
"Please use `QuaternionCoeffOrder.WXYZ` instead."
)
- def safe_zero_division(numerator: torch.Tensor, denominator: torch.Tensor) -> torch.Tensor:
+ def safe_zero_division(numerator: Tensor, denominator: Tensor) -> Tensor:
eps: float = torch.finfo(numerator.dtype).tiny # type: ignore
return numerator / torch.clamp(denominator, min=eps)
- rotation_matrix_vec: torch.Tensor = rotation_matrix.view(*rotation_matrix.shape[:-2], 9)
+ rotation_matrix_vec: Tensor = rotation_matrix.view(*rotation_matrix.shape[:-2], 9)
m00, m01, m02, m10, m11, m12, m20, m21, m22 = torch.chunk(rotation_matrix_vec, chunks=9, dim=-1)
- trace: torch.Tensor = m00 + m11 + m22
+ trace: Tensor = m00 + m11 + m22
def trace_positive_cond():
sq = torch.sqrt(trace + 1.0 + eps) * 2.0 # sq = 4 * qw.
@@ -432,8 +434,8 @@ def rotation_matrix_to_quaternion(
qy = safe_zero_division(m02 - m20, sq)
qz = safe_zero_division(m10 - m01, sq)
if order == QuaternionCoeffOrder.XYZW:
- return torch.cat((qx, qy, qz, qw), dim=-1)
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return concatenate((qx, qy, qz, qw), dim=-1)
+ return concatenate((qw, qx, qy, qz), dim=-1)
def cond_1():
sq = torch.sqrt(1.0 + m00 - m11 - m22 + eps) * 2.0 # sq = 4 * qx.
@@ -442,8 +444,8 @@ def rotation_matrix_to_quaternion(
qy = safe_zero_division(m01 + m10, sq)
qz = safe_zero_division(m02 + m20, sq)
if order == QuaternionCoeffOrder.XYZW:
- return torch.cat((qx, qy, qz, qw), dim=-1)
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return concatenate((qx, qy, qz, qw), dim=-1)
+ return concatenate((qw, qx, qy, qz), dim=-1)
def cond_2():
sq = torch.sqrt(1.0 + m11 - m00 - m22 + eps) * 2.0 # sq = 4 * qy.
@@ -452,8 +454,8 @@ def rotation_matrix_to_quaternion(
qy = 0.25 * sq
qz = safe_zero_division(m12 + m21, sq)
if order == QuaternionCoeffOrder.XYZW:
- return torch.cat((qx, qy, qz, qw), dim=-1)
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return concatenate((qx, qy, qz, qw), dim=-1)
+ return concatenate((qw, qx, qy, qz), dim=-1)
def cond_3():
sq = torch.sqrt(1.0 + m22 - m00 - m11 + eps) * 2.0 # sq = 4 * qz.
@@ -462,17 +464,17 @@ def rotation_matrix_to_quaternion(
qy = safe_zero_division(m12 + m21, sq)
qz = 0.25 * sq
if order == QuaternionCoeffOrder.XYZW:
- return torch.cat((qx, qy, qz, qw), dim=-1)
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return concatenate((qx, qy, qz, qw), dim=-1)
+ return concatenate((qw, qx, qy, qz), dim=-1)
where_2 = torch.where(m11 > m22, cond_2(), cond_3())
where_1 = torch.where((m00 > m11) & (m00 > m22), cond_1(), where_2)
- quaternion: torch.Tensor = torch.where(trace > 0.0, trace_positive_cond(), where_1)
+ quaternion: Tensor = torch.where(trace > 0.0, trace_positive_cond(), where_1)
return quaternion
-def normalize_quaternion(quaternion: torch.Tensor, eps: float = 1.0e-12) -> torch.Tensor:
+def normalize_quaternion(quaternion: Tensor, eps: float = 1.0e-12) -> Tensor:
r"""Normalize a quaternion.
The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
@@ -486,12 +488,12 @@ def normalize_quaternion(quaternion: torch.Tensor, eps: float = 1.0e-12) -> torc
the normalized quaternion of shape :math:`(*, 4)`.
Example:
- >>> quaternion = torch.tensor((1., 0., 1., 0.))
+ >>> quaternion = tensor((1., 0., 1., 0.))
>>> normalize_quaternion(quaternion)
tensor([0.7071, 0.0000, 0.7071, 0.0000])
"""
- if not isinstance(quaternion, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(quaternion)}")
+ if not isinstance(quaternion, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape (*, 4). Got {quaternion.shape}")
@@ -504,8 +506,8 @@ def normalize_quaternion(quaternion: torch.Tensor, eps: float = 1.0e-12) -> torc
def quaternion_to_rotation_matrix(
- quaternion: torch.Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+ quaternion: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
+) -> Tensor:
r"""Convert a quaternion to a rotation matrix.
The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
@@ -519,14 +521,14 @@ def quaternion_to_rotation_matrix(
the rotation matrix of shape :math:`(*, 3, 3)`.
Example:
- >>> quaternion = torch.tensor((0., 0., 0., 1.))
+ >>> quaternion = tensor((0., 0., 0., 1.))
>>> quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
tensor([[-1., 0., 0.],
[ 0., -1., 0.],
[ 0., 0., 1.]])
"""
- if not isinstance(quaternion, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(quaternion)}")
+ if not isinstance(quaternion, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape (*, 4). Got {quaternion.shape}")
@@ -543,7 +545,7 @@ def quaternion_to_rotation_matrix(
)
# normalize the input quaternion
- quaternion_norm: torch.Tensor = normalize_quaternion(quaternion)
+ quaternion_norm: Tensor = normalize_quaternion(quaternion)
# unpack the normalized quaternion components
if order == QuaternionCoeffOrder.XYZW:
@@ -552,21 +554,21 @@ def quaternion_to_rotation_matrix(
w, x, y, z = torch.chunk(quaternion_norm, chunks=4, dim=-1)
# compute the actual conversion
- tx: torch.Tensor = 2.0 * x
- ty: torch.Tensor = 2.0 * y
- tz: torch.Tensor = 2.0 * z
- twx: torch.Tensor = tx * w
- twy: torch.Tensor = ty * w
- twz: torch.Tensor = tz * w
- txx: torch.Tensor = tx * x
- txy: torch.Tensor = ty * x
- txz: torch.Tensor = tz * x
- tyy: torch.Tensor = ty * y
- tyz: torch.Tensor = tz * y
- tzz: torch.Tensor = tz * z
- one: torch.Tensor = torch.tensor(1.0)
-
- matrix: torch.Tensor = torch.stack(
+ tx: Tensor = 2.0 * x
+ ty: Tensor = 2.0 * y
+ tz: Tensor = 2.0 * z
+ twx: Tensor = tx * w
+ twy: Tensor = ty * w
+ twz: Tensor = tz * w
+ txx: Tensor = tx * x
+ txy: Tensor = ty * x
+ txz: Tensor = tz * x
+ tyy: Tensor = ty * y
+ tyz: Tensor = tz * y
+ tzz: Tensor = tz * z
+ one: Tensor = tensor(1.0)
+
+ matrix: Tensor = stack(
(
one - (tyy + tzz),
txy - twz,
@@ -586,9 +588,7 @@ def quaternion_to_rotation_matrix(
return matrix
-def quaternion_to_angle_axis(
- quaternion: torch.Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+def quaternion_to_angle_axis(quaternion: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW) -> Tensor:
"""Convert quaternion vector to angle axis of rotation in radians.
The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
@@ -607,12 +607,12 @@ def quaternion_to_angle_axis(
- Output: :math:`(*, 3)`
Example:
- >>> quaternion = torch.tensor((1., 0., 0., 0.))
+ >>> quaternion = tensor((1., 0., 0., 0.))
>>> quaternion_to_angle_axis(quaternion)
tensor([3.1416, 0.0000, 0.0000])
"""
if not torch.is_tensor(quaternion):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(quaternion)}")
+ raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape Nx4 or 4. Got {quaternion.shape}")
@@ -628,10 +628,10 @@ def quaternion_to_angle_axis(
"Please use `QuaternionCoeffOrder.WXYZ` instead."
)
# unpack input and compute conversion
- q1: torch.Tensor = torch.tensor([])
- q2: torch.Tensor = torch.tensor([])
- q3: torch.Tensor = torch.tensor([])
- cos_theta: torch.Tensor = torch.tensor([])
+ q1: Tensor = tensor([])
+ q2: Tensor = tensor([])
+ q3: Tensor = tensor([])
+ cos_theta: Tensor = tensor([])
if order == QuaternionCoeffOrder.XYZW:
q1 = quaternion[..., 0]
@@ -644,18 +644,18 @@ def quaternion_to_angle_axis(
q2 = quaternion[..., 2]
q3 = quaternion[..., 3]
- sin_squared_theta: torch.Tensor = q1 * q1 + q2 * q2 + q3 * q3
+ sin_squared_theta: Tensor = q1 * q1 + q2 * q2 + q3 * q3
- sin_theta: torch.Tensor = torch.sqrt(sin_squared_theta)
- two_theta: torch.Tensor = 2.0 * torch.where(
+ sin_theta: Tensor = torch.sqrt(sin_squared_theta)
+ two_theta: Tensor = 2.0 * torch.where(
cos_theta < 0.0, torch.atan2(-sin_theta, -cos_theta), torch.atan2(sin_theta, cos_theta)
)
- k_pos: torch.Tensor = two_theta / sin_theta
- k_neg: torch.Tensor = 2.0 * torch.ones_like(sin_theta)
- k: torch.Tensor = torch.where(sin_squared_theta > 0.0, k_pos, k_neg)
+ k_pos: Tensor = two_theta / sin_theta
+ k_neg: Tensor = 2.0 * torch.ones_like(sin_theta)
+ k: Tensor = torch.where(sin_squared_theta > 0.0, k_pos, k_neg)
- angle_axis: torch.Tensor = torch.zeros_like(quaternion)[..., :3]
+ angle_axis: Tensor = torch.zeros_like(quaternion)[..., :3]
angle_axis[..., 0] += q1 * k
angle_axis[..., 1] += q2 * k
angle_axis[..., 2] += q3 * k
@@ -663,8 +663,8 @@ def quaternion_to_angle_axis(
def quaternion_log_to_exp(
- quaternion: torch.Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+ quaternion: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
+) -> Tensor:
r"""Apply exponential map to log quaternion.
The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
@@ -679,13 +679,13 @@ def quaternion_log_to_exp(
the quaternion exponential map of shape :math:`(*, 4)`.
Example:
- >>> quaternion = torch.tensor((0., 0., 0.))
+ >>> quaternion = tensor((0., 0., 0.))
>>> quaternion_log_to_exp(quaternion, eps=torch.finfo(quaternion.dtype).eps,
... order=QuaternionCoeffOrder.WXYZ)
tensor([1., 0., 0., 0.])
"""
- if not isinstance(quaternion, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(quaternion)}")
+ if not isinstance(quaternion, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
if not quaternion.shape[-1] == 3:
raise ValueError(f"Input must be a tensor of shape (*, 3). Got {quaternion.shape}")
@@ -702,25 +702,25 @@ def quaternion_log_to_exp(
)
# compute quaternion norm
- norm_q: torch.Tensor = torch.norm(quaternion, p=2, dim=-1, keepdim=True).clamp(min=eps)
+ norm_q: Tensor = torch.norm(quaternion, p=2, dim=-1, keepdim=True).clamp(min=eps)
# compute scalar and vector
- quaternion_vector: torch.Tensor = quaternion * torch.sin(norm_q) / norm_q
- quaternion_scalar: torch.Tensor = torch.cos(norm_q)
+ quaternion_vector: Tensor = quaternion * torch.sin(norm_q) / norm_q
+ quaternion_scalar: Tensor = torch.cos(norm_q)
# compose quaternion and return
- quaternion_exp: torch.Tensor = torch.tensor([])
+ quaternion_exp: Tensor = tensor([])
if order == QuaternionCoeffOrder.XYZW:
- quaternion_exp = torch.cat((quaternion_vector, quaternion_scalar), dim=-1)
+ quaternion_exp = concatenate((quaternion_vector, quaternion_scalar), dim=-1)
else:
- quaternion_exp = torch.cat((quaternion_scalar, quaternion_vector), dim=-1)
+ quaternion_exp = concatenate((quaternion_scalar, quaternion_vector), dim=-1)
return quaternion_exp
def quaternion_exp_to_log(
- quaternion: torch.Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+ quaternion: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
+) -> Tensor:
r"""Apply the log map to a quaternion.
The quaternion should be in (x, y, z, w) format.
@@ -735,13 +735,13 @@ def quaternion_exp_to_log(
the quaternion log map of shape :math:`(*, 3)`.
Example:
- >>> quaternion = torch.tensor((1., 0., 0., 0.))
+ >>> quaternion = tensor((1., 0., 0., 0.))
>>> quaternion_exp_to_log(quaternion, eps=torch.finfo(quaternion.dtype).eps,
... order=QuaternionCoeffOrder.WXYZ)
tensor([0., 0., 0.])
"""
- if not isinstance(quaternion, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(quaternion)}")
+ if not isinstance(quaternion, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape (*, 4). Got {quaternion.shape}")
@@ -758,8 +758,8 @@ def quaternion_exp_to_log(
)
# unpack quaternion vector and scalar
- quaternion_vector: torch.Tensor = torch.tensor([])
- quaternion_scalar: torch.Tensor = torch.tensor([])
+ quaternion_vector: Tensor = tensor([])
+ quaternion_scalar: Tensor = tensor([])
if order == QuaternionCoeffOrder.XYZW:
quaternion_vector = quaternion[..., 0:3]
@@ -769,12 +769,10 @@ def quaternion_exp_to_log(
quaternion_vector = quaternion[..., 1:4]
# compute quaternion norm
- norm_q: torch.Tensor = torch.norm(quaternion_vector, p=2, dim=-1, keepdim=True).clamp(min=eps)
+ norm_q: Tensor = torch.norm(quaternion_vector, p=2, dim=-1, keepdim=True).clamp(min=eps)
# apply log map
- quaternion_log: torch.Tensor = (
- quaternion_vector * torch.acos(torch.clamp(quaternion_scalar, min=-1.0, max=1.0)) / norm_q
- )
+ quaternion_log: Tensor = quaternion_vector * torch.acos(torch.clamp(quaternion_scalar, min=-1.0, max=1.0)) / norm_q
return quaternion_log
@@ -783,9 +781,7 @@ def quaternion_exp_to_log(
# https://github.com/facebookresearch/QuaterNet/blob/master/common/quaternion.py#L138
-def angle_axis_to_quaternion(
- angle_axis: torch.Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> torch.Tensor:
+def angle_axis_to_quaternion(angle_axis: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW) -> Tensor:
r"""Convert an angle axis to a quaternion.
The quaternion vector has components in (x, y, z, w) or (w, x, y, z) format.
@@ -804,12 +800,12 @@ def angle_axis_to_quaternion(
- Output: :math:`(*, 4)`
Example:
- >>> angle_axis = torch.tensor((0., 1., 0.))
+ >>> angle_axis = tensor((0., 1., 0.))
>>> angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
tensor([0.8776, 0.0000, 0.4794, 0.0000])
"""
if not torch.is_tensor(angle_axis):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(angle_axis)}")
+ raise TypeError(f"Input type is not a Tensor. Got {type(angle_axis)}")
if not angle_axis.shape[-1] == 3:
raise ValueError(f"Input must be a tensor of shape Nx3 or 3. Got {angle_axis.shape}")
@@ -826,25 +822,23 @@ def angle_axis_to_quaternion(
)
# unpack input and compute conversion
- a0: torch.Tensor = angle_axis[..., 0:1]
- a1: torch.Tensor = angle_axis[..., 1:2]
- a2: torch.Tensor = angle_axis[..., 2:3]
- theta_squared: torch.Tensor = a0 * a0 + a1 * a1 + a2 * a2
+ a0: Tensor = angle_axis[..., 0:1]
+ a1: Tensor = angle_axis[..., 1:2]
+ a2: Tensor = angle_axis[..., 2:3]
+ theta_squared: Tensor = a0 * a0 + a1 * a1 + a2 * a2
- theta: torch.Tensor = torch.sqrt(theta_squared)
- half_theta: torch.Tensor = theta * 0.5
+ theta: Tensor = torch.sqrt(theta_squared)
+ half_theta: Tensor = theta * 0.5
- mask: torch.Tensor = theta_squared > 0.0
- ones: torch.Tensor = torch.ones_like(half_theta)
+ mask: Tensor = theta_squared > 0.0
+ ones: Tensor = torch.ones_like(half_theta)
- k_neg: torch.Tensor = 0.5 * ones
- k_pos: torch.Tensor = torch.sin(half_theta) / theta
- k: torch.Tensor = torch.where(mask, k_pos, k_neg)
- w: torch.Tensor = torch.where(mask, torch.cos(half_theta), ones)
+ k_neg: Tensor = 0.5 * ones
+ k_pos: Tensor = torch.sin(half_theta) / theta
+ k: Tensor = torch.where(mask, k_pos, k_neg)
+ w: Tensor = torch.where(mask, torch.cos(half_theta), ones)
- quaternion: torch.Tensor = torch.zeros(
- size=(*angle_axis.shape[:-1], 4), dtype=angle_axis.dtype, device=angle_axis.device
- )
+ quaternion: Tensor = torch.zeros(size=(*angle_axis.shape[:-1], 4), dtype=angle_axis.dtype, device=angle_axis.device)
if order == QuaternionCoeffOrder.XYZW:
quaternion[..., 0:1] = a0 * k
quaternion[..., 1:2] = a1 * k
@@ -862,9 +856,7 @@ def angle_axis_to_quaternion(
# https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py#L65-L71
-def normalize_pixel_coordinates(
- pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-8
-) -> torch.Tensor:
+def normalize_pixel_coordinates(pixel_coordinates: Tensor, height: int, width: int, eps: float = 1e-8) -> Tensor:
r"""Normalize pixel coordinates between -1 and 1.
Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).
@@ -879,7 +871,7 @@ def normalize_pixel_coordinates(
the normalized pixel coordinates with shape :math:`(*, 2)`.
Examples:
- >>> coords = torch.tensor([[50., 100.]])
+ >>> coords = tensor([[50., 100.]])
>>> normalize_pixel_coordinates(coords, 100, 50)
tensor([[1.0408, 1.0202]])
"""
@@ -887,23 +879,19 @@ def normalize_pixel_coordinates(
raise ValueError("Input pixel_coordinates must be of shape (*, 2). " "Got {}".format(pixel_coordinates.shape))
# compute normalization factor
- hw: torch.Tensor = torch.stack(
+ hw: Tensor = stack(
[
- torch.tensor(width, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype),
- torch.tensor(height, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype),
+ tensor(width, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype),
+ tensor(height, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype),
]
)
- factor: torch.Tensor = torch.tensor(2.0, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype) / (
- hw - 1
- ).clamp(eps)
+ factor: Tensor = tensor(2.0, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype) / (hw - 1).clamp(eps)
return factor * pixel_coordinates - 1
-def denormalize_pixel_coordinates(
- pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-8
-) -> torch.Tensor:
+def denormalize_pixel_coordinates(pixel_coordinates: Tensor, height: int, width: int, eps: float = 1e-8) -> Tensor:
r"""Denormalize pixel coordinates.
The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).
@@ -918,27 +906,23 @@ def denormalize_pixel_coordinates(
the denormalized pixel coordinates with shape :math:`(*, 2)`.
Examples:
- >>> coords = torch.tensor([[-1., -1.]])
+ >>> coords = tensor([[-1., -1.]])
>>> denormalize_pixel_coordinates(coords, 100, 50)
tensor([[0., 0.]])
"""
if pixel_coordinates.shape[-1] != 2:
raise ValueError("Input pixel_coordinates must be of shape (*, 2). " "Got {}".format(pixel_coordinates.shape))
# compute normalization factor
- hw: torch.Tensor = (
- torch.stack([torch.tensor(width), torch.tensor(height)])
- .to(pixel_coordinates.device)
- .to(pixel_coordinates.dtype)
- )
+ hw: Tensor = stack([tensor(width), tensor(height)]).to(pixel_coordinates.device).to(pixel_coordinates.dtype)
- factor: torch.Tensor = torch.tensor(2.0) / (hw - 1).clamp(eps)
+ factor: Tensor = tensor(2.0) / (hw - 1).clamp(eps)
- return torch.tensor(1.0) / factor * (pixel_coordinates + 1)
+ return tensor(1.0) / factor * (pixel_coordinates + 1)
def normalize_pixel_coordinates3d(
- pixel_coordinates: torch.Tensor, depth: int, height: int, width: int, eps: float = 1e-8
-) -> torch.Tensor:
+ pixel_coordinates: Tensor, depth: int, height: int, width: int, eps: float = 1e-8
+) -> Tensor:
r"""Normalize pixel coordinates between -1 and 1.
Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).
@@ -956,20 +940,18 @@ def normalize_pixel_coordinates3d(
if pixel_coordinates.shape[-1] != 3:
raise ValueError("Input pixel_coordinates must be of shape (*, 3). " "Got {}".format(pixel_coordinates.shape))
# compute normalization factor
- dhw: torch.Tensor = (
- torch.stack([torch.tensor(depth), torch.tensor(width), torch.tensor(height)])
- .to(pixel_coordinates.device)
- .to(pixel_coordinates.dtype)
+ dhw: Tensor = (
+ stack([tensor(depth), tensor(width), tensor(height)]).to(pixel_coordinates.device).to(pixel_coordinates.dtype)
)
- factor: torch.Tensor = torch.tensor(2.0) / (dhw - 1).clamp(eps)
+ factor: Tensor = tensor(2.0) / (dhw - 1).clamp(eps)
return factor * pixel_coordinates - 1
def denormalize_pixel_coordinates3d(
- pixel_coordinates: torch.Tensor, depth: int, height: int, width: int, eps: float = 1e-8
-) -> torch.Tensor:
+ pixel_coordinates: Tensor, depth: int, height: int, width: int, eps: float = 1e-8
+) -> Tensor:
r"""Denormalize pixel coordinates.
The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).
@@ -987,18 +969,16 @@ def denormalize_pixel_coordinates3d(
if pixel_coordinates.shape[-1] != 3:
raise ValueError("Input pixel_coordinates must be of shape (*, 3). " "Got {}".format(pixel_coordinates.shape))
# compute normalization factor
- dhw: torch.Tensor = (
- torch.stack([torch.tensor(depth), torch.tensor(width), torch.tensor(height)])
- .to(pixel_coordinates.device)
- .to(pixel_coordinates.dtype)
+ dhw: Tensor = (
+ stack([tensor(depth), tensor(width), tensor(height)]).to(pixel_coordinates.device).to(pixel_coordinates.dtype)
)
- factor: torch.Tensor = torch.tensor(2.0) / (dhw - 1).clamp(eps)
+ factor: Tensor = tensor(2.0) / (dhw - 1).clamp(eps)
- return torch.tensor(1.0) / factor * (pixel_coordinates + 1)
+ return tensor(1.0) / factor * (pixel_coordinates + 1)
-def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor:
+def angle_to_rotation_matrix(angle: Tensor) -> Tensor:
r"""Create a rotation matrix out of angles in degrees.
Args:
@@ -1012,14 +992,14 @@ def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor:
>>> output = angle_to_rotation_matrix(input) # Nx3x2x2
"""
ang_rad = deg2rad(angle)
- cos_a: torch.Tensor = torch.cos(ang_rad)
- sin_a: torch.Tensor = torch.sin(ang_rad)
- return torch.stack([cos_a, sin_a, -sin_a, cos_a], dim=-1).view(*angle.shape, 2, 2)
+ cos_a: Tensor = torch.cos(ang_rad)
+ sin_a: Tensor = torch.sin(ang_rad)
+ return stack([cos_a, sin_a, -sin_a, cos_a], dim=-1).view(*angle.shape, 2, 2)
def normalize_homography(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
-) -> torch.Tensor:
+ dst_pix_trans_src_pix: Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
+) -> Tensor:
r"""Normalize a given homography in pixels to [-1, 1].
Args:
@@ -1031,8 +1011,8 @@ def normalize_homography(
Returns:
the normalized homography of shape :math:`(B, 3, 3)`.
"""
- if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+ if not isinstance(dst_pix_trans_src_pix, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(dst_pix_trans_src_pix)}")
if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
@@ -1042,13 +1022,13 @@ def normalize_homography(
dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
+ src_norm_trans_src_pix: 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)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
+ dst_norm_trans_dst_pix: Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
# compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
+ dst_norm_trans_src_norm: Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
return dst_norm_trans_src_norm
@@ -1058,7 +1038,7 @@ def normal_transform_pixel(
eps: float = 1e-14,
device: Optional[torch.device] = None,
dtype: Optional[torch.dtype] = None,
-) -> torch.Tensor:
+) -> Tensor:
r"""Compute the normalization matrix from image size in pixels to [-1, 1].
Args:
@@ -1069,7 +1049,7 @@ def normal_transform_pixel(
Returns:
normalized transform with shape :math:`(1, 3, 3)`.
"""
- tr_mat = torch.tensor([[1.0, 0.0, -1.0], [0.0, 1.0, -1.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype) # 3x3
+ tr_mat = tensor([[1.0, 0.0, -1.0], [0.0, 1.0, -1.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype) # 3x3
# prevent divide by zero bugs
width_denom: float = eps if width == 1 else width - 1.0
@@ -1088,7 +1068,7 @@ def normal_transform_pixel3d(
eps: float = 1e-14,
device: Optional[torch.device] = None,
dtype: Optional[torch.dtype] = None,
-) -> torch.Tensor:
+) -> Tensor:
r"""Compute the normalization matrix from image size in pixels to [-1, 1].
Args:
@@ -1100,7 +1080,7 @@ def normal_transform_pixel3d(
Returns:
normalized transform with shape :math:`(1, 4, 4)`.
"""
- tr_mat = torch.tensor(
+ tr_mat = tensor(
[[1.0, 0.0, 0.0, -1.0], [0.0, 1.0, 0.0, -1.0], [0.0, 0.0, 1.0, -1.0], [0.0, 0.0, 0.0, 1.0]],
device=device,
dtype=dtype,
@@ -1119,8 +1099,8 @@ def normal_transform_pixel3d(
def denormalize_homography(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
-) -> torch.Tensor:
+ dst_pix_trans_src_pix: Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
+) -> Tensor:
r"""De-normalize a given homography in pixels from [-1, 1] to actual height and width.
Args:
@@ -1132,8 +1112,8 @@ def denormalize_homography(
Returns:
the denormalized homography of shape :math:`(B, 3, 3)`.
"""
- if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+ if not isinstance(dst_pix_trans_src_pix, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(dst_pix_trans_src_pix)}")
if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
@@ -1143,18 +1123,18 @@ def denormalize_homography(
dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
+ src_norm_trans_src_pix: Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
+ dst_norm_trans_dst_pix: Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
dst_denorm_trans_dst_pix = _torch_inverse_cast(dst_norm_trans_dst_pix)
# compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)
+ dst_norm_trans_src_norm: Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)
return dst_norm_trans_src_norm
def normalize_homography3d(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]
-) -> torch.Tensor:
+ dst_pix_trans_src_pix: Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]
+) -> Tensor:
r"""Normalize a given homography in pixels to [-1, 1].
Args:
@@ -1169,8 +1149,8 @@ def normalize_homography3d(
Shape:
Output: :math:`(B, 4, 4)`
"""
- if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+ if not isinstance(dst_pix_trans_src_pix, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(dst_pix_trans_src_pix)}")
if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (4, 4)):
raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
@@ -1179,15 +1159,100 @@ def normalize_homography3d(
src_d, src_h, src_w = dsize_src
dst_d, dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel3d(src_d, src_h, src_w).to(dst_pix_trans_src_pix)
+ src_norm_trans_src_pix: Tensor = normal_transform_pixel3d(src_d, src_h, src_w).to(dst_pix_trans_src_pix)
src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel3d(dst_d, dst_h, dst_w).to(dst_pix_trans_src_pix)
+ dst_norm_trans_dst_pix: Tensor = normal_transform_pixel3d(dst_d, dst_h, dst_w).to(dst_pix_trans_src_pix)
# compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
+ dst_norm_trans_src_norm: Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
return dst_norm_trans_src_norm
+def normalize_points_with_intrinsics(point_2d: Tensor, camera_matrix: Tensor):
+ r"""Normalizes points with intrinsics. Useful for conversion of keypoints to be used with essential matrix.
+
+ Args:
+ point_2d: tensor containing the 2d points in the image pixel coordinates.
+ The shape of the tensor can be :math:`(*, 2)`.
+ camera_matrix: tensor containing the intrinsics camera
+ matrix. The tensor shape must be :math:`(*, 3, 3)`.
+
+ Returns:
+ tensor of (u, v) cam coordinates with shape :math:`(*, 2)`.
+
+ Example:
+ >>> _ = torch.manual_seed(0)
+ >>> X = torch.rand(1, 2)
+ >>> K = torch.eye(3)[None]
+ >>> normalize_points_with_intrinsics(X, K)
+ tensor([[0.4963, 0.7682]])
+ """
+ KORNIA_CHECK_SHAPE(point_2d, ["*", "2"])
+ KORNIA_CHECK_SHAPE(camera_matrix, ["*", "3", "3"])
+ # projection eq. K_inv * [u v 1]'
+ # x = (u - cx) * Z / fx
+ # y = (v - cy) * Z / fy
+
+ # unpack coordinates
+ u_coord: Tensor = point_2d[..., 0]
+ v_coord: Tensor = point_2d[..., 1]
+
+ # unpack intrinsics
+ fx: Tensor = camera_matrix[..., 0, 0]
+ fy: Tensor = camera_matrix[..., 1, 1]
+ cx: Tensor = camera_matrix[..., 0, 2]
+ cy: Tensor = camera_matrix[..., 1, 2]
+
+ # projective
+ x_coord: Tensor = (u_coord - cx) / fx
+ y_coord: Tensor = (v_coord - cy) / fy
+
+ xy: Tensor = stack([x_coord, y_coord], dim=-1)
+ return xy
+
+
+def denormalize_points_with_intrinsics(point_2d_norm: Tensor, camera_matrix: Tensor):
+ r"""Normalizes points with intrinsics. Useful for conversion of keypoints to be used with essential matrix.
+
+ Args:
+ point_2d_norm: tensor containing the 2d points in the image pixel coordinates.
+ The shape of the tensor can be :math:`(*, 2)`.
+ camera_matrix: tensor containing the intrinsics camera
+ matrix. The tensor shape must be :math:`(*, 3, 3)`.
+
+ Returns:
+ tensor of (u, v) cam coordinates with shape :math:`(*, 2)`.
+
+ Example:
+ >>> _ = torch.manual_seed(0)
+ >>> X = torch.rand(1, 2)
+ >>> K = torch.eye(3)[None]
+ >>> denormalize_points_with_intrinsics(X, K)
+ tensor([[0.4963, 0.7682]])
+ """
+ KORNIA_CHECK_SHAPE(point_2d_norm, ["*", "2"])
+ KORNIA_CHECK_SHAPE(camera_matrix, ["*", "3", "3"])
+ # projection eq. [u, v, w]' = K * [x y z 1]'
+ # u = fx * X + cx
+ # v = fy * Y + cy
+
+ # unpack coordinates
+ x_coord: Tensor = point_2d_norm[..., 0]
+ y_coord: Tensor = point_2d_norm[..., 1]
+
+ # unpack intrinsics
+ fx: Tensor = camera_matrix[..., 0, 0]
+ fy: Tensor = camera_matrix[..., 1, 1]
+ cx: Tensor = camera_matrix[..., 0, 2]
+ cy: Tensor = camera_matrix[..., 1, 2]
+
+ # apply intrinsics ans return
+ u_coord: Tensor = x_coord * fx + cx
+ v_coord: Tensor = y_coord * fy + cy
+
+ return stack([u_coord, v_coord], dim=-1)
+
+
def Rt_to_matrix4x4(R: Tensor, t: Tensor) -> Tensor:
r"""Combines 3x3 rotation matrix R and 1x3 translation vector t into 4x4 extrinsics.
@@ -1208,7 +1273,7 @@ def Rt_to_matrix4x4(R: Tensor, t: Tensor) -> Tensor:
"""
KORNIA_CHECK_SHAPE(R, ["B", "3", "3"])
KORNIA_CHECK_SHAPE(t, ["B", "3", "1"])
- Rt = torch.cat([R, t], dim=2)
+ Rt = concatenate([R, t], dim=2)
return convert_affinematrix_to_homography3d(Rt)
@@ -1314,7 +1379,7 @@ def camtoworld_vision_to_graphics_4x4(extrinsics_vision: Tensor) -> Tensor:
[ 0., 0., 0., 1.]]])
"""
KORNIA_CHECK_SHAPE(extrinsics_vision, ["B", "4", "4"])
- invert_yz = torch.tensor(
+ invert_yz = tensor(
[[[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1.0]]],
dtype=extrinsics_vision.dtype,
device=extrinsics_vision.device,
@@ -1426,7 +1491,7 @@ def ARKitQTVecs_to_ColmapQTVecs(qvec: Tensor, tvec: Tensor) -> Tuple[Tensor, Ten
tvec: translation vector :math:`(B, 3, 1)`, [x, y, z]
Example:
- >>> q, t = torch.tensor([0, 1, 0, 1.])[None], torch.ones(3).reshape(1, 3, 1)
+ >>> q, t = tensor([0, 1, 0, 1.])[None], torch.ones(3).reshape(1, 3, 1)
>>> ARKitQTVecs_to_ColmapQTVecs(q, t)
(tensor([[0.7071, 0.0000, 0.7071, 0.0000]]), tensor([[[-1.0000],
[-1.0000],
diff --git a/kornia/geometry/quaternion.py b/kornia/geometry/quaternion.py
index 04d3cc437..4b3e1e197 100644
--- a/kornia/geometry/quaternion.py
+++ b/kornia/geometry/quaternion.py
@@ -325,9 +325,6 @@ class Quaternion(Module):
Example:
>>> q = Quaternion.random(batch_size=2)
- >>> q.norm()
- tensor([[1.],
- [1.]], grad_fn=<NormBackward1>)
"""
r1, r2, r3 = rand(3, batch_size)
q1 = (1.0 - r1).sqrt() * ((2 * pi * r2).sin())
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index 8daafe048..eb6a328eb 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -318,7 +318,7 @@ def KORNIA_CHECK_SHAPE(x, shape: List[str]) -> None:
start_idx: int = 1
x_shape_to_check = x.shape[-len(shape) + 1 :]
elif '*' == shape[-1]:
- start_idx: int = 0
+ start_idx = 0
x_shape_to_check = x.shape[: len(shape) - 1]
else:
start_idx = 0
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index fe0734a68..f441b81ce 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -1381,6 +1381,153 @@ class TestDenormalizePixelCoordinates:
assert_close(actual, expected)
+class TestProjectPoints:
+ def test_smoke(self, device, dtype):
+ point_3d = torch.zeros(1, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
+ assert point_2d.shape == (1, 2)
+
+ def test_smoke_batch(self, device, dtype):
+ point_3d = torch.zeros(2, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
+ assert point_2d.shape == (2, 2)
+
+ def test_smoke_batch_multi(self, device, dtype):
+ point_3d = torch.zeros(2, 4, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 4, -1, -1)
+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
+ assert point_2d.shape == (2, 4, 2)
+
+ def test_project_and_unproject(self, device, dtype):
+ point_3d = torch.tensor([[10.0, 2.0, 30.0]], device=device, dtype=dtype)
+ depth = point_3d[..., -1:]
+ camera_matrix = torch.tensor(
+ [[[2746.0, 0.0, 991.0], [0.0, 2748.0, 619.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype
+ )
+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
+ point_3d_hat = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)
+ assert_close(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device, dtype):
+ # TODO: point [0, 0, 0] crashes
+ points_3d = torch.ones(1, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+
+ # evaluate function gradient
+ points_3d = tensor_to_gradcheck_var(points_3d)
+ camera_matrix = tensor_to_gradcheck_var(camera_matrix)
+ assert gradcheck(kornia.geometry.camera.project_points, (points_3d, camera_matrix), raise_exception=True)
+
+ def test_jit(self, device, dtype):
+ points_3d = torch.zeros(1, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ op = kornia.geometry.camera.project_points
+ op_jit = torch.jit.script(op)
+ assert_close(op(points_3d, camera_matrix), op_jit(points_3d, camera_matrix))
+
+
+class TestDenormalizePointsWithIntrinsics:
+ def test_smoke(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ points_norm = kornia.geometry.conversions.denormalize_points_with_intrinsics(points_2d, camera_matrix)
+ assert points_norm.shape == (1, 2)
+
+ def test_smoke_batch(self, device, dtype):
+ points_2d = torch.zeros(2, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
+ points_norm = kornia.geometry.conversions.denormalize_points_with_intrinsics(points_2d, camera_matrix)
+ assert points_norm.shape == (2, 2)
+
+ def test_toy(self, device, dtype):
+ point_2d = torch.tensor([[1.0, 1.0]], device=device, dtype=dtype)
+ camera_matrix = torch.tensor(
+ [[64.0, 0.0, 128.0], [0.0, 64.0, 128.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype
+ )
+ op = kornia.geometry.conversions.denormalize_points_with_intrinsics
+ expected = torch.tensor([[192.0, 192.0]], device=device, dtype=dtype)
+ assert_close(op(point_2d, camera_matrix), expected, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+
+ # evaluate function gradient
+ points_2d = tensor_to_gradcheck_var(points_2d)
+ camera_matrix = tensor_to_gradcheck_var(camera_matrix)
+ assert gradcheck(
+ kornia.geometry.conversions.denormalize_points_with_intrinsics,
+ (points_2d, camera_matrix),
+ raise_exception=True,
+ )
+
+ def test_jit(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ args = (points_2d, camera_matrix)
+ op = kornia.geometry.conversions.denormalize_points_with_intrinsics
+ op_jit = torch.jit.script(op)
+ assert_close(op(*args), op_jit(*args))
+
+
+class TestNormalizePointsWithIntrinsics:
+ def test_smoke(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ points_norm = kornia.geometry.conversions.normalize_points_with_intrinsics(points_2d, camera_matrix)
+ assert points_norm.shape == (1, 2)
+
+ def test_smoke_batch(self, device, dtype):
+ points_2d = torch.zeros(2, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
+ points_norm = kornia.geometry.conversions.normalize_points_with_intrinsics(points_2d, camera_matrix)
+ assert points_norm.shape == (2, 2)
+
+ def test_norm_unnorm(self, device, dtype):
+ point_2d = torch.tensor([[128.0, 128.0]], device=device, dtype=dtype)
+ camera_matrix = torch.tensor(
+ [[64.0, 0.0, 128.0], [0.0, 64.0, 128.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype
+ )
+ op = kornia.geometry.conversions.normalize_points_with_intrinsics
+ back = kornia.geometry.conversions.denormalize_points_with_intrinsics
+ point_2d_norm = op(point_2d, camera_matrix)
+ point_2d_hat = back(point_2d_norm, camera_matrix)
+ assert_close(point_2d, point_2d_hat, atol=1e-4, rtol=1e-4)
+
+ def test_toy(self, device, dtype):
+ point_2d = torch.tensor([[192.0, 192.0]], device=device, dtype=dtype)
+ camera_matrix = torch.tensor(
+ [[64.0, 0.0, 128.0], [0.0, 64.0, 128.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype
+ )
+ op = kornia.geometry.conversions.normalize_points_with_intrinsics
+ out = op(point_2d, camera_matrix)
+ expected = torch.tensor([[1.0, 1.0]], device=device, dtype=dtype)
+ assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+
+ # evaluate function gradient
+ points_2d = tensor_to_gradcheck_var(points_2d)
+ camera_matrix = tensor_to_gradcheck_var(camera_matrix)
+ assert gradcheck(
+ kornia.geometry.conversions.normalize_points_with_intrinsics,
+ (points_2d, camera_matrix),
+ raise_exception=True,
+ )
+
+ def test_jit(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
+ args = (points_2d, camera_matrix)
+ op = kornia.geometry.conversions.normalize_points_with_intrinsics
+ op_jit = torch.jit.script(op)
+ assert_close(op(*args), op_jit(*args))
+
+
class TestRt2Extrinsics:
@pytest.mark.parametrize("batch_size", [1, 2, 3])
def test_everything(self, batch_size, device, dtype):