Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Enhance] Added dtype flag to geometry module (#820)

shijianjian670624025046
diff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.py
index 323c823b8..eab82c984 100644
--- a/kornia/augmentation/__init__.py
+++ b/kornia/augmentation/__init__.py
@@ -71,5 +71,5 @@ __all__ = [
"RandomAffine3D",
"RandomCrop3D",
"CenterCrop3D",
- "RandomEqualize3D"
+ "RandomEqualize3D",
]
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index b69d4ad41..c3244f5d7 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -291,9 +291,11 @@ class PinholeCamera:
tx,
ty,
tz,
- batch_size=1):
+ batch_size=1,
+ device: Optional[torch.device] = None,
+ dtype: Optional[torch.dtype] = None):
# create the camera matrix
- intrinsics = torch.zeros(batch_size, 4, 4)
+ intrinsics = torch.zeros(batch_size, 4, 4, device=device, dtype=dtype)
intrinsics[..., 0, 0] += fx
intrinsics[..., 1, 1] += fy
intrinsics[..., 0, 2] += cx
@@ -301,14 +303,14 @@ class PinholeCamera:
intrinsics[..., 2, 2] += 1.0
intrinsics[..., 3, 3] += 1.0
# create the pose matrix
- extrinsics = torch.eye(4).repeat(batch_size, 1, 1)
+ extrinsics = torch.eye(4, device=device, dtype=dtype).repeat(batch_size, 1, 1)
extrinsics[..., 0, -1] += tx
extrinsics[..., 1, -1] += ty
extrinsics[..., 2, -1] += tz
# create image hegith and width
- height_tmp = torch.zeros(batch_size)
+ height_tmp = torch.zeros(batch_size, device=device, dtype=dtype)
height_tmp[..., 0] += height
- width_tmp = torch.zeros(batch_size)
+ width_tmp = torch.zeros(batch_size, device=device, dtype=dtype)
width_tmp[..., 0] += width
return self(intrinsics, extrinsics, height_tmp, width_tmp)
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 163f6c96d..b75f3f947 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -658,10 +658,12 @@ def normalize_pixel_coordinates(
"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)
+ torch.tensor(width, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype),
+ torch.tensor(height, device=pixel_coordinates.device, dtype=pixel_coordinates.dtype)
+ ])
- factor: torch.Tensor = torch.tensor(2.) / (hw - 1).clamp(eps)
+ factor: torch.Tensor = torch.tensor(
+ 2., device=pixel_coordinates.device, dtype=pixel_coordinates.dtype) / (hw - 1).clamp(eps)
return factor * pixel_coordinates - 1
diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.py
index d9983636e..e523d32a7 100644
--- a/kornia/geometry/homography.py
+++ b/kornia/geometry/homography.py
@@ -6,6 +6,8 @@ from typing import Tuple, Optional
import kornia
from kornia.geometry.epipolar import normalize_points
+from kornia.utils import _extract_device_dtype
+
TupleTensor = Tuple[torch.Tensor, torch.Tensor]
@@ -28,6 +30,8 @@ def find_homography_dlt(
assert len(points1.shape) >= 1 and points1.shape[-1] == 2, points1.shape
assert points1.shape[1] >= 4, points1.shape
+ device, dtype = _extract_device_dtype([points1, points2])
+
eps: float = 1e-8
points1_norm, transform1 = normalize_points(points1)
points2_norm, transform2 = normalize_points(points2)
@@ -54,7 +58,7 @@ def find_homography_dlt(
U, S, V = torch.svd(A)
except:
warnings.warn('SVD did not converge', RuntimeWarning)
- return torch.empty((points1_norm.size(0), 3, 3), device=points1.device)
+ return torch.empty((points1_norm.size(0), 3, 3), device=device, dtype=dtype)
H = V[..., -1].view(-1, 3, 3)
H = transform2.inverse() @ (H @ transform1)
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index 33b81eaf4..3a9d99470 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -199,8 +199,11 @@ def transform_points(trans_01: torch.Tensor,
"""
check_is_tensor(trans_01)
check_is_tensor(points_1)
- if not trans_01.device == points_1.device:
- raise TypeError("Tensor must be in the same device")
+ if not (trans_01.device == points_1.device and trans_01.dtype == points_1.dtype):
+ raise TypeError(
+ "Tensor must be in the same device and dtype. "
+ f"Got trans_01 with ({trans_01.dtype}, {points_1.dtype}) and "
+ f"points_1 with ({points_1.dtype}, {points_1.dtype})")
if not trans_01.shape[0] == points_1.shape[0] and trans_01.shape[0] != 1:
raise ValueError("Input batch size must be the same for both tensors or 1")
if not trans_01.shape[-1] == (points_1.shape[-1] + 1):
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 7458daa16..385fb328d 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -9,6 +9,7 @@ from kornia.geometry.transform.imgwarp import (
from kornia.geometry.transform.projwarp import (
warp_affine3d, get_projective_transform
)
+from kornia.utils import _extract_device_dtype
__all__ = [
"affine",
@@ -465,17 +466,18 @@ class Affine(nn.Module):
self._batch_size = batch_size
super().__init__()
+ device, dtype = _extract_device_dtype([angle, translation, scale_factor])
if angle is None:
- angle = torch.zeros(batch_size)
+ angle = torch.zeros(batch_size, device=device, dtype=dtype)
self.angle = angle
if translation is None:
- translation = torch.zeros(batch_size, 2)
+ translation = torch.zeros(batch_size, 2, device=device, dtype=dtype)
self.translation = translation
if scale_factor is None:
- scale_factor = torch.ones(batch_size, 2)
+ scale_factor = torch.ones(batch_size, 2, device=device, dtype=dtype)
self.scale_factor = scale_factor
self.shear = shear
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 0ada14cc8..e7fe0752c 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -342,6 +342,9 @@ def get_rotation_matrix2d(
if not (center.shape[0] == angle.shape[0] == scale.shape[0]):
raise ValueError("Inputs must have same batch size dimension. Got center {}, angle {} and scale {}"
.format(center.shape, angle.shape, scale.shape))
+ if not (center.device == angle.device == scale.device) or not (center.dtype == angle.dtype == scale.dtype):
+ raise ValueError("Inputs must have same device Got center ({}, {}), angle ({}, {}) and scale ({}, {})"
+ .format(center.device, center.dtype, angle.device, angle.dtype, scale.device, scale.dtype))
# convert angle and apply scale
rotation_matrix: torch.Tensor = angle_to_rotation_matrix(angle)
scaling_matrix: torch.Tensor = torch.zeros(
@@ -358,7 +361,7 @@ def get_rotation_matrix2d(
# create output tensor
batch_size: int = center.shape[0]
- one = torch.tensor(1.).to(center.device)
+ one = torch.tensor(1., device=center.device, dtype=center.dtype)
M: torch.Tensor = torch.zeros(
batch_size, 2, 3, device=center.device, dtype=center.dtype)
M[..., 0:2, 0:2] = scaled_rotation
diff --git a/kornia/geometry/warp/depth_warper.py b/kornia/geometry/warp/depth_warper.py
index e8b31d3ad..419a6409d 100644
--- a/kornia/geometry/warp/depth_warper.py
+++ b/kornia/geometry/warp/depth_warper.py
@@ -95,9 +95,9 @@ class DepthWarper(nn.Module):
return self
def _compute_projection(self, x, y, invd):
- point = torch.FloatTensor([[[x], [y], [1.0], [invd]]])
- flow = torch.matmul(
- self._dst_proj_src, point.to(self._dst_proj_src.device))
+ point = torch.tensor(
+ [[[x], [y], [1.0], [invd]]], device=self._dst_proj_src.device, dtype=self._dst_proj_src.dtype)
+ flow = torch.matmul(self._dst_proj_src, point)
z = 1. / flow[:, 2]
x = (flow[:, 0] * z)
y = (flow[:, 1] * z)
@@ -144,18 +144,18 @@ class DepthWarper(nn.Module):
dtype: torch.dtype = depth_src.dtype
# expand the base coordinate grid according to the input batch size
- pixel_coords: torch.Tensor = self.grid.to(device).to(dtype).expand(
+ pixel_coords: torch.Tensor = self.grid.to(device=device, dtype=dtype).expand(
batch_size, -1, -1, -1) # BxHxWx3
# reproject the pixel coordinates to the camera frame
cam_coords_src: torch.Tensor = pixel2cam(
depth_src,
- self._pinhole_src.intrinsics_inverse().to(dtype),
+ self._pinhole_src.intrinsics_inverse().to(device=device, dtype=dtype),
pixel_coords) # BxHxWx3
# reproject the camera coordinates to the pixel
pixel_coords_src: torch.Tensor = cam2pixel(
- cam_coords_src, self._dst_proj_src.to(dtype)) # (B*N)xHxWx2
+ cam_coords_src, self._dst_proj_src.to(device=device, dtype=dtype)) # (B*N)xHxWx2
# normalize between -1 and 1 the coordinates
pixel_coords_src_norm: torch.Tensor = normalize_pixel_coordinates(
diff --git a/kornia/geometry/warp/homography_warper.py b/kornia/geometry/warp/homography_warper.py
index d2164ff46..27971b0e1 100644
--- a/kornia/geometry/warp/homography_warper.py
+++ b/kornia/geometry/warp/homography_warper.py
@@ -277,7 +277,10 @@ class HomographyWarper(nn.Module):
return warped_patch
-def normal_transform_pixel(height: int, width: int, eps: float = 1e-14) -> torch.Tensor:
+def normal_transform_pixel(
+ height: int, width: int, eps: float = 1e-14,
+ device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None
+) -> torch.Tensor:
r"""Compute the normalization matrix from image size in pixels to [-1, 1].
Args:
@@ -290,7 +293,7 @@ def normal_transform_pixel(height: int, width: int, eps: float = 1e-14) -> torch
"""
tr_mat = torch.tensor([[1.0, 0.0, -1.0],
[0.0, 1.0, -1.0],
- [0.0, 0.0, 1.0]]) # 3x3
+ [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
@@ -302,7 +305,10 @@ def normal_transform_pixel(height: int, width: int, eps: float = 1e-14) -> torch
return tr_mat.unsqueeze(0) # 1x3x3
-def normal_transform_pixel3d(depth: int, height: int, width: int, eps: float = 1e-14) -> torch.Tensor:
+def normal_transform_pixel3d(
+ depth: int, height: int, width: int, eps: float = 1e-14,
+ device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None
+) -> torch.Tensor:
r"""Compute the normalization matrix from image size in pixels to [-1, 1].
Args:
@@ -317,7 +323,7 @@ def normal_transform_pixel3d(depth: int, height: int, width: int, eps: float = 1
tr_mat = torch.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]]) # 4x4
+ [0.0, 0.0, 0.0, 1.0]], device=device, dtype=dtype) # 4x4
# prevent divide by zero bugs
width_denom: float = eps if width == 1 else width - 1.0
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index fc86bfe62..58100037d 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -30,10 +30,10 @@ def create_checkerboard(h, w, nw):
# TODO: Isn't this function duplicated with eye_like?
-def create_eye_batch(batch_size, eye_size):
+def create_eye_batch(batch_size, eye_size, device=None, dtype=None):
"""Creates a batch of identity matrices of shape Bx3x3
"""
- return torch.eye(eye_size).view(
+ return torch.eye(eye_size, device=device, dtype=dtype).view(
1, eye_size, eye_size).expand(batch_size, -1, -1)
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 9d43ed804..a8df50ec0 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -155,7 +155,7 @@ class CommonTests(BaseTester):
(test_input, test_transform), params=generated_params, return_transform=True)
assert output.shape[0] == batch_shape[0]
assert final_transformation.shape == expected_transformation_shape
- assert_allclose(final_transformation, transformation @ test_transform)
+ assert_allclose(final_transformation, transformation @ test_transform, atol=1e-4, rtol=1e-4)
output, transformation = augmentation((test_input, test_transform), params=generated_params)
assert output.shape[0] == batch_shape[0]
@@ -177,13 +177,13 @@ class CommonTests(BaseTester):
output, final_transformation = augmentation((test_input, test_transform), params=generated_params)
assert output.shape[0] == batch_shape[0]
assert final_transformation.shape == expected_transformation_shape
- assert_allclose(final_transformation, transformation @ test_transform)
+ assert_allclose(final_transformation, transformation @ test_transform, atol=1e-4, rtol=1e-4)
output, final_transformation = augmentation(
(test_input, test_transform), params=generated_params, return_transform=True)
assert output.shape[0] == batch_shape[0]
assert final_transformation.shape == expected_transformation_shape
- assert_allclose(final_transformation, transformation @ test_transform)
+ assert_allclose(final_transformation, transformation @ test_transform, atol=1e-4, rtol=1e-4)
def _test_cardinality_implementation(self, input_shape, expected_output_shape, params):
@@ -256,8 +256,8 @@ class CommonTests(BaseTester):
assert out2.shape == out_sequence.shape
assert transform.shape == transform_sequence.shape
- assert_allclose(out2, out_sequence)
- assert_allclose(transform, transform_sequence)
+ assert_allclose(out2, out_sequence, atol=1e-4, rtol=1e-4)
+ assert_allclose(transform, transform_sequence, atol=1e-4, rtol=1e-4)
def _test_inverse_coordinate_check_implementation(self, params):
torch.manual_seed(42)
@@ -286,7 +286,7 @@ class CommonTests(BaseTester):
output_values = output[0, :, output_indices[:, 1][value_mask], output_indices[:, 0][value_mask]]
input_values = input_tensor[0, :, input_indices[:, 1][value_mask], input_indices[:, 0][value_mask]]
- assert_allclose(output_values, input_values)
+ assert_allclose(output_values, input_values, atol=1e-4, rtol=1e-4)
def _test_gradcheck_implementation(self, params):
input_tensor = torch.rand((3, 5, 5), device=self.device, dtype=self.dtype) # 3 x 3
@@ -602,7 +602,7 @@ class TestRandomVerticalFlipAlternative(CommonTests):
class TestRandomRotationAlternative(CommonTests):
possible_params: Dict["str", Tuple] = {
- "degrees": (0., (-360., 360.), [0., 0.], torch.Tensor((-180., 180))),
+ "degrees": (0., (-360., 360.), [0., 0.], torch.tensor((-180., 180))),
"interpolation": (0, Resample.BILINEAR.name, Resample.BILINEAR, None),
"resample": (0, Resample.BILINEAR.name, Resample.BILINEAR),
"align_corners": (False, True),
@@ -832,26 +832,21 @@ class TestRandomHorizontalFlip:
input = torch.tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
- [0., 0., 1., 2.]]) # 3 x 4
-
- input = input.to(device)
+ [0., 0., 1., 2.]], device=device, dtype=dtype) # 3 x 4
expected = torch.tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
- [2., 1., 0., 0.]]) # 3 x 4
+ [2., 1., 0., 0.]], device=device, dtype=dtype) # 3 x 4
expected = expected.to(device)
expected_transform = torch.tensor([[-1., 0., 3.],
[0., 1., 0.],
- [0., 0., 1.]]) # 3 x 3
-
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]], device=device, dtype=dtype) # 3 x 3
identity = torch.tensor([[1., 0., 0.],
[0., 1., 0.],
- [0., 0., 1.]]) # 3 x 3
- identity = identity.to(device)
+ [0., 0., 1.]], device=device, dtype=dtype) # 3 x 3
assert (f(input)[0] == expected).all()
assert (f(input)[1] == expected_transform).all()
@@ -867,23 +862,19 @@ class TestRandomHorizontalFlip:
input = torch.tensor([[[[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
- input = input.to(device)
+ [0., 1., 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected = torch.tensor([[[[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]]]) # 1 x 1 x 3 x 3
- expected = expected.to(device)
+ [1., 1., 0.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected_transform = torch.tensor([[[-1., 0., 2.],
[0., 1., 0.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
identity = torch.tensor([[[1., 0., 0.],
[0., 1., 0.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- identity = identity.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
input = input.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
expected = expected.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
@@ -914,16 +905,13 @@ class TestRandomHorizontalFlip:
input = torch.tensor([[[[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
- input = input.to(device)
+ [0., 1., 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected_transform = torch.tensor([[[-1., 0., 2.],
[0., 1., 0.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
expected_transform_1 = expected_transform @ expected_transform
- expected_transform_1 = expected_transform_1.to(device)
assert(f(input)[0] == input).all()
assert(f(input)[1] == expected_transform_1).all()
@@ -977,7 +965,7 @@ class TestRandomHorizontalFlip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -985,19 +973,19 @@ class TestRandomHorizontalFlip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 0., 0.],
[0., 5., 5.],
- [0., 0., 0.]]]) # 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1)
actual = op_trace(input)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
input = torch.rand((3, 3), device=device, dtype=dtype) # 3 x 3
@@ -1024,30 +1012,26 @@ class TestRandomVerticalFlip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
- input = input.to(device)
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor([[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]) # 3 x 3
- expected = expected.to(device)
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
expected_transform = torch.tensor([[1., 0., 0.],
[0., -1., 2.],
- [0., 0., 1.]]) # 3 x 3
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]], device=device, dtype=dtype) # 3 x 3
identity = torch.tensor([[1., 0., 0.],
[0., 1., 0.],
- [0., 0., 1.]]) # 3 x 3
- identity = identity.to(device)
+ [0., 0., 1.]], device=device, dtype=dtype) # 3 x 3
- assert_allclose(f(input)[0], expected)
- assert_allclose(f(input)[1], expected_transform)
- assert_allclose(f1(input)[0], input)
- assert_allclose(f1(input)[1], identity)
- assert_allclose(f2(input), expected)
- assert_allclose(f3(input), input)
+ assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[0], input, atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[1], identity, atol=1e-4, rtol=1e-4)
+ assert_allclose(f2(input), expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(f3(input), input, atol=1e-4, rtol=1e-4)
def test_batch_random_vflip(self, device, dtype):
@@ -1056,33 +1040,29 @@ class TestRandomVerticalFlip:
input = torch.tensor([[[[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
- input = input.to(device)
+ [0., 1., 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected = torch.tensor([[[[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]]]) # 1 x 1 x 3 x 3
- expected = expected.to(device)
+ [0., 0., 0.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected_transform = torch.tensor([[[1., 0., 0.],
[0., -1., 2.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
identity = torch.tensor([[[1., 0., 0.],
[0., 1., 0.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- identity = identity.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
input = input.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
expected = expected.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 3 x 3
identity = identity.repeat(5, 1, 1) # 5 x 3 x 3
- assert_allclose(f(input)[0], expected)
- assert_allclose(f(input)[1], expected_transform)
- assert_allclose(f1(input)[0], input)
- assert_allclose(f1(input)[1], identity)
+ assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[0], input, atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[1], identity, atol=1e-4, rtol=1e-4)
def test_same_on_batch(self, device, dtype):
f = RandomVerticalFlip(p=0.5, same_on_batch=True)
@@ -1103,20 +1083,18 @@ class TestRandomVerticalFlip:
input = torch.tensor([[[[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
- input = input.to(device)
+ [0., 1., 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3
expected_transform = torch.tensor([[[1., 0., 0.],
[0., -1., 2.],
- [0., 0., 1.]]]) # 1 x 3 x 3
- expected_transform = expected_transform.to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype) # 1 x 3 x 3
expected_transform_1 = expected_transform @ expected_transform
- assert_allclose(f(input)[0], input.squeeze())
- assert_allclose(f(input)[1], expected_transform_1)
- assert_allclose(f1(input)[0], input.squeeze())
- assert_allclose(f1(input)[1], expected_transform)
+ assert_allclose(f(input)[0], input.squeeze(), atol=1e-4, rtol=1e-4)
+ assert_allclose(f(input)[1], expected_transform_1, atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[0], input.squeeze(), atol=1e-4, rtol=1e-4)
+ assert_allclose(f1(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
def test_random_vflip_coord_check(self, device, dtype):
@@ -1164,7 +1142,7 @@ class TestRandomVerticalFlip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -1172,19 +1150,19 @@ class TestRandomVerticalFlip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]]) # 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1)
actual = op_trace(input)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
input = torch.rand((3, 3), device=device, dtype=dtype) # 3 x 3
@@ -1216,7 +1194,7 @@ class TestColorJitter:
assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)
- assert_allclose(f1(input)[1], expected_transform)
+ assert_allclose(f1(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
def test_color_jitter_batch(self, device, dtype):
f = ColorJitter()
@@ -1229,7 +1207,7 @@ class TestColorJitter:
assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)
- assert_allclose(f1(input)[1], expected_transform)
+ assert_allclose(f1(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
def test_same_on_batch(self, device, dtype):
f = ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1, same_on_batch=True)
@@ -1593,7 +1571,7 @@ class TestRectangleRandomErasing:
1.0, (.2, .4), (.3, .5)
)(img)
actual = op_script(img)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestRandomGrayscale:
@@ -1613,9 +1591,8 @@ class TestRandomGrayscale:
input = torch.rand(3, 5, 5, device=device, dtype=dtype) # 3 x 5 x 5
expected_transform = torch.eye(3, device=device, dtype=dtype).unsqueeze(0) # 3 x 3
- expected_transform = expected_transform.to(device)
- assert_allclose(f(input)[1], expected_transform)
+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
def test_same_on_batch(self, device, dtype):
f = RandomGrayscale(p=0.5, same_on_batch=True)
@@ -1662,7 +1639,7 @@ class TestRandomGrayscale:
[0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]], device=device, dtype=dtype)
img_gray = kornia.augmentation.RandomGrayscale(p=1.)(data)
- assert_allclose(img_gray, expected)
+ assert_allclose(img_gray, expected, atol=1e-4, rtol=1e-4)
def test_opencv_false(self, device, dtype):
data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
@@ -1686,7 +1663,7 @@ class TestRandomGrayscale:
expected = data
img_gray = kornia.augmentation.RandomGrayscale(p=0.)(data)
- assert_allclose(img_gray, expected)
+ assert_allclose(img_gray, expected, atol=1e-4, rtol=1e-4)
def test_opencv_true_batch(self, device, dtype):
data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
@@ -1729,7 +1706,7 @@ class TestRandomGrayscale:
expected = expected.unsqueeze(0).repeat(4, 1, 1, 1)
img_gray = kornia.augmentation.RandomGrayscale(p=1.)(data)
- assert_allclose(img_gray, expected)
+ assert_allclose(img_gray, expected, atol=1e-4, rtol=1e-4)
def test_opencv_false_batch(self, device, dtype):
data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
@@ -1754,7 +1731,7 @@ class TestRandomGrayscale:
expected = data
img_gray = kornia.augmentation.RandomGrayscale(p=0.)(data)
- assert_allclose(img_gray, expected)
+ assert_allclose(img_gray, expected, atol=1e-4, rtol=1e-4)
def test_random_grayscale_sequential_batch(self, device, dtype):
f = nn.Sequential(
@@ -1768,8 +1745,8 @@ class TestRandomGrayscale:
expected_transform = torch.eye(3, device=device, dtype=dtype).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3
expected_transform = expected_transform.to(device)
- assert_allclose(f(input)[0], expected)
- assert_allclose(f(input)[1], expected_transform)
+ assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
input = torch.rand((3, 5, 5), device=device, dtype=dtype) # 3 x 3
@@ -1935,7 +1912,7 @@ class TestRandomRotation:
input = torch.tensor([[1., 0., 0., 2.],
[0., 0., 0., 0.],
[0., 1., 2., 0.],
- [0., 0., 1., 2.]]) # 4 x 4
+ [0., 0., 1., 2.]], device=device, dtype=dtype) # 4 x 4
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -1943,11 +1920,11 @@ class TestRandomRotation:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor([[[0.0000, 0.2584, 0.0000],
[2.9552, 5.0000, 0.2584],
- [1.6841, 0.4373, 0.0000]]])
+ [1.6841, 0.4373, 0.0000]]], device=device, dtype=dtype)
actual = op_trace(input)
@@ -1986,7 +1963,7 @@ class TestRandomCrop:
rc = RandomCrop(size=(2, 3), padding=None, align_corners=True, p=1.)
out = rc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_no_padding_batch(self, device, dtype):
torch.manual_seed(42)
@@ -2004,7 +1981,7 @@ class TestRandomCrop:
rc = RandomCrop(size=(2, 3), padding=None, align_corners=True, p=1.)
out = rc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_same_on_batch(self, device, dtype):
f = RandomCrop(size=(2, 3), padding=1, same_on_batch=True, align_corners=True, p=1.)
@@ -2030,7 +2007,7 @@ class TestRandomCrop:
rc = RandomCrop(size=(2, 3), padding=1, align_corners=True, p=1.)
out = rc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_padding_batch_2(self, device, dtype):
torch.manual_seed(42)
@@ -2050,7 +2027,7 @@ class TestRandomCrop:
rc = RandomCrop(size=(2, 3), padding=(0, 1), fill=10, align_corners=True, p=1.)
out = rc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_padding_batch_3(self, device, dtype):
torch.manual_seed(0)
@@ -2086,7 +2063,7 @@ class TestRandomCrop:
rc = RandomCrop(size=(2, 3), pad_if_needed=True, fill=9, align_corners=True, p=1.)
out = rc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
torch.manual_seed(0) # for random reproductibility
@@ -2103,7 +2080,7 @@ class TestRandomCrop:
actual = op_script(img)
expected = kornia.center_crop3d(img)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.skip("Need to fix Union type")
def test_jit_trace(self, device, dtype):
@@ -2121,7 +2098,7 @@ class TestRandomCrop:
# 3. Evaluate
actual = op_trace(img)
expected = op(img)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestRandomResizedCrop:
@@ -2178,7 +2155,7 @@ class TestRandomResizedCrop:
rrc = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.))
# It will crop a size of (2, 2) from the aspect ratio implementation of torch
out = rrc(inp)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
def test_crop_scale_ratio_batch(self, device, dtype):
torch.manual_seed(0)
diff --git a/test/geometry/epipolar/test_fundamental.py b/test/geometry/epipolar/test_fundamental.py
index 03746ade8..0e5b94e20 100644
--- a/test/geometry/epipolar/test_fundamental.py
+++ b/test/geometry/epipolar/test_fundamental.py
@@ -73,7 +73,7 @@ class TestNormalizeTransformation:
]], device=device, dtype=dtype)
trans_norm = epi.normalize_transformation(trans)
- assert_allclose(trans_norm, trans_expected)
+ assert_allclose(trans_norm, trans_expected, atol=1e-4, rtol=1e-4)
def test_check_corner_case(self, device, dtype):
trans = torch.tensor([[
@@ -85,7 +85,7 @@ class TestNormalizeTransformation:
trans_expected = trans.clone()
trans_norm = epi.normalize_transformation(trans)
- assert_allclose(trans_norm, trans_expected)
+ assert_allclose(trans_norm, trans_expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
trans = torch.rand(2, 3, 3,
@@ -261,7 +261,7 @@ class TestFundamentlFromEssential:
F_mat_norm = epi.normalize_transformation(F_mat)
F_hat_norm = epi.normalize_transformation(F_hat)
- assert_allclose(F_mat_norm, F_hat_norm)
+ assert_allclose(F_mat_norm, F_hat_norm, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
E_mat = torch.rand(1, 3, 3, device=device, dtype=torch.float64, requires_grad=True)
@@ -311,7 +311,7 @@ class TestFundamentalFromProjections:
F_mat_norm = epi.normalize_transformation(F_mat)
F_hat_norm = epi.normalize_transformation(F_hat)
- assert_allclose(F_mat_norm, F_hat_norm)
+ assert_allclose(F_mat_norm, F_hat_norm, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
P1 = torch.rand(1, 3, 4, device=device, dtype=torch.float64, requires_grad=True)
diff --git a/test/geometry/epipolar/test_projection.py b/test/geometry/epipolar/test_projection.py
index 51d534f17..889bd9b86 100644
--- a/test/geometry/epipolar/test_projection.py
+++ b/test/geometry/epipolar/test_projection.py
@@ -61,7 +61,7 @@ class TestScaleIntrinsics:
]], device=device, dtype=dtype)
camera_matrix_scale = epi.scale_intrinsics(camera_matrix, scale_factor)
- assert_allclose(camera_matrix_scale, camera_matrix_expected)
+ assert_allclose(camera_matrix_scale, camera_matrix_expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
scale_factor = torch.ones(1, device=device, dtype=torch.float64, requires_grad=True)
@@ -111,7 +111,7 @@ class TestProjectionFromKRt:
]], device=device, dtype=dtype)
P_estimated = epi.projection_from_KRt(K, R, t)
- assert_allclose(P_estimated, P_expected)
+ assert_allclose(P_estimated, P_expected, atol=1e-4, rtol=1e-4)
def test_krt_from_projection(self, device, dtype):
P = torch.tensor([[
@@ -137,9 +137,9 @@ class TestProjectionFromKRt:
], device=device, dtype=dtype)
K_estimated, R_estimated, t_estimated = epi.KRt_from_projection(P)
- assert_allclose(K_estimated, K_expected)
- assert_allclose(R_estimated, R_expected)
- assert_allclose(t_estimated, t_expected)
+ assert_allclose(K_estimated, K_expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(R_estimated, R_expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(t_estimated, t_expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
K = torch.rand(1, 3, 3, device=device, dtype=torch.float64, requires_grad=True)
@@ -210,9 +210,9 @@ class TestKRtFromProjection:
], device=device, dtype=dtype)
K_estimated, R_estimated, t_estimated = epi.KRt_from_projection(P)
- assert_allclose(K_estimated, K_expected)
- assert_allclose(R_estimated, R_expected)
- assert_allclose(t_estimated, t_expected)
+ assert_allclose(K_estimated, K_expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(R_estimated, R_expected, atol=1e-4, rtol=1e-4)
+ assert_allclose(t_estimated, t_expected, atol=1e-4, rtol=1e-4)
def test_projection_from_krt(self, device, dtype):
K = torch.tensor([[
@@ -238,7 +238,7 @@ class TestKRtFromProjection:
]], device=device, dtype=dtype)
P_estimated = epi.projection_from_KRt(K, R, t)
- assert_allclose(P_estimated, P_expected)
+ assert_allclose(P_estimated, P_expected, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device):
P_mat = torch.rand(1, 3, 4, device=device, dtype=torch.float64, requires_grad=True)
diff --git a/test/geometry/subpix/test_spatial_softargmax.py b/test/geometry/subpix/test_spatial_softargmax.py
index 9f1a0c029..5a5254768 100644
--- a/test/geometry/subpix/test_spatial_softargmax.py
+++ b/test/geometry/subpix/test_spatial_softargmax.py
@@ -11,12 +11,12 @@ from kornia.geometry.subpix.spatial_soft_argmax import _get_center_kernel2d, _ge
class TestCenterKernel2d:
- def test_smoke(self, device):
- kernel = _get_center_kernel2d(3, 4).to(device)
+ def test_smoke(self, device, dtype):
+ kernel = _get_center_kernel2d(3, 4, device=device).to(dtype=dtype)
assert kernel.shape == (2, 2, 3, 4)
- def test_odd(self, device):
- kernel = _get_center_kernel2d(3, 3).to(device)
+ def test_odd(self, device, dtype):
+ kernel = _get_center_kernel2d(3, 3, device=device).to(dtype=dtype)
expected = torch.tensor([
[[[0., 0., 0.],
[0., 1., 0.],
@@ -29,126 +29,127 @@ class TestCenterKernel2d:
[0., 0., 0.]],
[[0., 0., 0.],
[0., 1., 0.],
- [0., 0., 0.]]]]).to(device)
- assert_allclose(kernel, expected)
+ [0., 0., 0.]]]], device=device, dtype=dtype)
+ assert_allclose(kernel, expected, atol=1e-4, rtol=1e-4)
- def test_even(self, device):
- kernel = _get_center_kernel2d(2, 2).to(device)
- expected = torch.ones(2, 2, 2, 2).to(device) * 0.25
+ def test_even(self, device, dtype):
+ kernel = _get_center_kernel2d(2, 2, device=device).to(dtype=dtype)
+ expected = torch.ones(2, 2, 2, 2, device=device, dtype=dtype) * 0.25
expected[0, 1] = 0
expected[1, 0] = 0
- assert_allclose(kernel, expected)
+ assert_allclose(kernel, expected, atol=1e-4, rtol=1e-4)
class TestCenterKernel3d:
- def test_smoke(self, device):
- kernel = _get_center_kernel3d(6, 3, 4).to(device)
+ def test_smoke(self, device, dtype):
+ kernel = _get_center_kernel3d(6, 3, 4, device=device).to(dtype=dtype)
assert kernel.shape == (3, 3, 6, 3, 4)
- def test_odd(self, device):
- kernel = _get_center_kernel3d(3, 5, 7).to(device)
- expected = torch.zeros(3, 3, 3, 5, 7).to(device)
+ def test_odd(self, device, dtype):
+ kernel = _get_center_kernel3d(3, 5, 7, device=device).to(dtype=dtype)
+ expected = torch.zeros(3, 3, 3, 5, 7, device=device, dtype=dtype)
expected[0, 0, 1, 2, 3] = 1.
expected[1, 1, 1, 2, 3] = 1.
expected[2, 2, 1, 2, 3] = 1.
- assert_allclose(kernel, expected)
+ assert_allclose(kernel, expected, atol=1e-4, rtol=1e-4)
- def test_even(self, device):
- kernel = _get_center_kernel3d(2, 4, 3).to(device)
- expected = torch.zeros(3, 3, 2, 4, 3).to(device)
+ def test_even(self, device, dtype):
+ kernel = _get_center_kernel3d(2, 4, 3, device=device).to(dtype=dtype)
+ expected = torch.zeros(3, 3, 2, 4, 3, device=device, dtype=dtype)
expected[0, 0, :, 1:3, 1] = 0.25
expected[1, 1, :, 1:3, 1] = 0.25
expected[2, 2, :, 1:3, 1] = 0.25
- assert_allclose(kernel, expected)
+ assert_allclose(kernel, expected, atol=1e-4, rtol=1e-4)
class TestSpatialSoftArgmax2d:
- def test_smoke(self, device):
- input = torch.zeros(1, 1, 2, 3).to(device)
+ def test_smoke(self, device, dtype):
+ input = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)
m = kornia.SpatialSoftArgmax2d()
assert m(input).shape == (1, 1, 2)
- def test_smoke_batch(self, device):
- input = torch.zeros(2, 1, 2, 3).to(device)
+ def test_smoke_batch(self, device, dtype):
+ input = torch.zeros(2, 1, 2, 3, device=device, dtype=dtype)
m = kornia.SpatialSoftArgmax2d()
assert m(input).shape == (2, 1, 2)
- def test_top_left_normalized(self, device):
- input = torch.zeros(1, 1, 2, 3).to(device)
+ def test_top_left_normalized(self, device, dtype):
+ input = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)
input[..., 0, 0] = 1e16
coord = kornia.spatial_soft_argmax2d(input, normalized_coordinates=True)
- assert_allclose(coord[..., 0].item(), -1.0)
- assert_allclose(coord[..., 1].item(), -1.0)
+ assert_allclose(coord[..., 0].item(), -1.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[..., 1].item(), -1.0, atol=1e-4, rtol=1e-4)
- def test_top_left(self, device):
- input = torch.zeros(1, 1, 2, 3).to(device)
+ def test_top_left(self, device, dtype):
+ input = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)
input[..., 0, 0] = 1e16
coord = kornia.spatial_soft_argmax2d(input, normalized_coordinates=False)
- assert_allclose(coord[..., 0].item(), 0.0)
- assert_allclose(coord[..., 1].item(), 0.0)
+ assert_allclose(coord[..., 0].item(), 0.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[..., 1].item(), 0.0, atol=1e-4, rtol=1e-4)
- def test_bottom_right_normalized(self, device):
- input = torch.zeros(1, 1, 2, 3).to(device)
+ def test_bottom_right_normalized(self, device, dtype):
+ input = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)
input[..., -1, -1] = 1e16
coord = kornia.spatial_soft_argmax2d(input, normalized_coordinates=True)
- assert_allclose(coord[..., 0].item(), 1.0)
- assert_allclose(coord[..., 1].item(), 1.0)
+ assert_allclose(coord[..., 0].item(), 1.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[..., 1].item(), 1.0, atol=1e-4, rtol=1e-4)
- def test_bottom_right(self, device):
- input = torch.zeros(1, 1, 2, 3).to(device)
+ def test_bottom_right(self, device, dtype):
+ input = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)
input[..., -1, -1] = 1e16
coord = kornia.spatial_soft_argmax2d(input, normalized_coordinates=False)
- assert_allclose(coord[..., 0].item(), 2.0)
- assert_allclose(coord[..., 1].item(), 1.0)
+ assert_allclose(coord[..., 0].item(), 2.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[..., 1].item(), 1.0, atol=1e-4, rtol=1e-4)
- def test_batch2_n2(self, device):
- input = torch.zeros(2, 2, 2, 3).to(device)
+ def test_batch2_n2(self, device, dtype):
+ input = torch.zeros(2, 2, 2, 3, device=device, dtype=dtype)
input[0, 0, 0, 0] = 1e16 # top-left
input[0, 1, 0, -1] = 1e16 # top-right
input[1, 0, -1, 0] = 1e16 # bottom-left
input[1, 1, -1, -1] = 1e16 # bottom-right
coord = kornia.spatial_soft_argmax2d(input)
- assert_allclose(coord[0, 0, 0].item(), -1.0) # top-left
- assert_allclose(coord[0, 0, 1].item(), -1.0)
- assert_allclose(coord[0, 1, 0].item(), 1.0) # top-right
- assert_allclose(coord[0, 1, 1].item(), -1.0)
- assert_allclose(coord[1, 0, 0].item(), -1.0) # bottom-left
- assert_allclose(coord[1, 0, 1].item(), 1.0)
- assert_allclose(coord[1, 1, 0].item(), 1.0) # bottom-right
- assert_allclose(coord[1, 1, 1].item(), 1.0)
-
- def test_gradcheck(self, device):
- input = torch.rand(2, 3, 3, 2).to(device)
+ assert_allclose(coord[0, 0, 0].item(), -1.0, atol=1e-4, rtol=1e-4) # top-left
+ assert_allclose(coord[0, 0, 1].item(), -1.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[0, 1, 0].item(), 1.0, atol=1e-4, rtol=1e-4) # top-right
+ assert_allclose(coord[0, 1, 1].item(), -1.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[1, 0, 0].item(), -1.0, atol=1e-4, rtol=1e-4) # bottom-left
+ assert_allclose(coord[1, 0, 1].item(), 1.0, atol=1e-4, rtol=1e-4)
+ assert_allclose(coord[1, 1, 0].item(), 1.0, atol=1e-4, rtol=1e-4) # bottom-right
+ assert_allclose(coord[1, 1, 1].item(), 1.0, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device, dtype):
+ input = torch.rand(2, 3, 3, 2, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.spatial_soft_argmax2d,
(input), raise_exception=True)
- def test_end_to_end(self, device):
- input = torch.full((1, 2, 7, 7), 1.0, requires_grad=True).to(device)
- target = torch.as_tensor([[[0.0, 0.0], [1.0, 1.0]]]).to(device)
- std = torch.tensor([1.0, 1.0]).to(device)
+ def test_end_to_end(self, device, dtype):
+ input = torch.full((1, 2, 7, 7), 1.0, requires_grad=True, device=device, dtype=dtype)
+ target = torch.as_tensor([[[0.0, 0.0], [1.0, 1.0]]], device=device, dtype=dtype)
+ std = torch.tensor([1.0, 1.0], device=device, dtype=dtype)
hm = kornia.geometry.dsnt.spatial_softmax2d(input)
- assert_allclose(hm.sum(-1).sum(-1), torch.tensor(1.0).to(device))
+ assert_allclose(hm.sum(-1).sum(-1), torch.tensor(1.0, device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
pred = kornia.geometry.dsnt.spatial_expectation2d(hm)
- assert_allclose(pred, torch.as_tensor([[[0.0, 0.0], [0.0, 0.0]]]).to(device))
+ assert_allclose(
+ pred, torch.as_tensor([[[0.0, 0.0], [0.0, 0.0]]], device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
loss1 = mse_loss(pred, target, size_average=None, reduce=None,
reduction='none').mean(-1, keepdim=False)
- expected_loss1 = torch.as_tensor([[0.0, 1.0]]).to(device)
- assert_allclose(loss1, expected_loss1)
+ expected_loss1 = torch.as_tensor([[0.0, 1.0]], device=device, dtype=dtype)
+ assert_allclose(loss1, expected_loss1, atol=1e-4, rtol=1e-4)
target_hm = kornia.geometry.dsnt.render_gaussian2d(
target, std, input.shape[-2:]).contiguous()
loss2 = kornia.losses.js_div_loss_2d(hm, target_hm, reduction='none')
- expected_loss2 = torch.as_tensor([[0.0087, 0.0818]]).to(device)
+ expected_loss2 = torch.as_tensor([[0.0087, 0.0818]], device=device, dtype=dtype)
assert_allclose(loss2, expected_loss2, rtol=0, atol=1e-3)
loss = (loss1 + loss2).mean()
@@ -168,261 +169,261 @@ class TestSpatialSoftArgmax2d:
class TestConvSoftArgmax2d:
- def test_smoke(self, device):
- input = torch.zeros(1, 1, 3, 3).to(device)
+ def test_smoke(self, device, dtype):
+ input = torch.zeros(1, 1, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax2d((3, 3))
assert m(input).shape == (1, 1, 2, 3, 3)
- def test_smoke_batch(self, device):
- input = torch.zeros(2, 5, 3, 3).to(device)
+ def test_smoke_batch(self, device, dtype):
+ input = torch.zeros(2, 5, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax2d()
assert m(input).shape == (2, 5, 2, 3, 3)
- def test_smoke_with_val(self, device):
- input = torch.zeros(1, 1, 3, 3).to(device)
+ def test_smoke_with_val(self, device, dtype):
+ input = torch.zeros(1, 1, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax2d((3, 3), output_value=True)
coords, val = m(input)
assert coords.shape == (1, 1, 2, 3, 3)
assert val.shape == (1, 1, 3, 3)
- def test_smoke_batch_with_val(self, device):
- input = torch.zeros(2, 5, 3, 3).to(device)
+ def test_smoke_batch_with_val(self, device, dtype):
+ input = torch.zeros(2, 5, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax2d((3, 3), output_value=True)
coords, val = m(input)
assert coords.shape == (2, 5, 2, 3, 3)
assert val.shape == (2, 5, 3, 3)
- def test_gradcheck(self, device):
- input = torch.rand(2, 3, 5, 5).to(device)
+ def test_gradcheck(self, device, dtype):
+ input = torch.rand(2, 3, 5, 5, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.conv_soft_argmax2d,
(input), raise_exception=True)
- def test_cold_diag(self, device):
+ def test_cold_diag(self, device, dtype):
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),
temperature=0.05,
normalized_coordinates=False,
output_value=True)
expected_val = torch.tensor([[[[1., 0.],
- [0., 1.]]]]).to(device)
+ [0., 1.]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[[[1., 3.],
[1., 3.]],
[[1., 1.],
- [3., 3.]]]]]).to(device)
+ [3., 3.]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_hot_diag(self, device):
+ def test_hot_diag(self, device, dtype):
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),
temperature=10.,
normalized_coordinates=False,
output_value=True)
expected_val = torch.tensor([[[[0.1214, 0.],
- [0., 0.1214]]]]).to(device)
+ [0., 0.1214]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[[[1., 3.],
[1., 3.]],
[[1., 1.],
- [3., 3.]]]]]).to(device)
+ [3., 3.]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_cold_diag_norm(self, device):
+ def test_cold_diag_norm(self, device, dtype):
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),
temperature=0.05,
normalized_coordinates=True,
output_value=True)
expected_val = torch.tensor([[[[1., 0.],
- [0., 1.]]]]).to(device)
+ [0., 1.]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[[[-0.5, 0.5],
[-0.5, 0.5]],
[[-0.5, -0.5],
- [0.5, 0.5]]]]]).to(device)
+ [0.5, 0.5]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_hot_diag_norm(self, device):
+ def test_hot_diag_norm(self, device, dtype):
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),
temperature=10.,
normalized_coordinates=True,
output_value=True)
expected_val = torch.tensor([[[[0.1214, 0.],
- [0., 0.1214]]]]).to(device)
+ [0., 0.1214]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[[[-0.5, 0.5],
[-0.5, 0.5]],
[[-0.5, -0.5],
- [0.5, 0.5]]]]]).to(device)
+ [0.5, 0.5]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
class TestConvSoftArgmax3d:
- def test_smoke(self, device):
- input = torch.zeros(1, 1, 3, 3, 3).to(device)
+ def test_smoke(self, device, dtype):
+ input = torch.zeros(1, 1, 3, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax3d((3, 3, 3), output_value=False)
assert m(input).shape == (1, 1, 3, 3, 3, 3)
- def test_smoke_with_val(self, device):
- input = torch.zeros(1, 1, 3, 3, 3).to(device)
+ def test_smoke_with_val(self, device, dtype):
+ input = torch.zeros(1, 1, 3, 3, 3, device=device, dtype=dtype)
m = kornia.ConvSoftArgmax3d((3, 3, 3), output_value=True)
coords, val = m(input)
assert coords.shape == (1, 1, 3, 3, 3, 3)
assert val.shape == (1, 1, 3, 3, 3)
- def test_gradcheck(self, device):
- input = torch.rand(1, 2, 3, 5, 5).to(device)
+ def test_gradcheck(self, device, dtype):
+ input = torch.rand(1, 2, 3, 5, 5, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.conv_soft_argmax3d,
(input), raise_exception=True)
- def test_cold_diag(self, device):
+ def test_cold_diag(self, device, dtype):
input = torch.tensor([[[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]]).to(device)
+ ]]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),
temperature=0.05,
normalized_coordinates=False,
output_value=True)
expected_val = torch.tensor([[[[[1., 0.],
- [0., 1.]]]]]).to(device)
+ [0., 1.]]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[
[[[0., 0.],
[0., 0.]]],
[[[1., 3.],
[1., 3.]]],
[[[1., 1.],
- [3., 3.]]]]]]).to(device)
+ [3., 3.]]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_hot_diag(self, device):
+ def test_hot_diag(self, device, dtype):
input = torch.tensor([[[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]]).to(device)
+ ]]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),
temperature=10.,
normalized_coordinates=False,
output_value=True)
expected_val = torch.tensor([[[[[0.1214, 0.],
- [0., 0.1214]]]]]).to(device)
+ [0., 0.1214]]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[
[[[0., 0.],
[0., 0.]]],
[[[1., 3.],
[1., 3.]]],
[[[1., 1.],
- [3., 3.]]]]]]).to(device)
+ [3., 3.]]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_cold_diag_norm(self, device):
+ def test_cold_diag_norm(self, device, dtype):
input = torch.tensor([[[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]]).to(device)
+ ]]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),
temperature=0.05,
normalized_coordinates=True,
output_value=True)
expected_val = torch.tensor([[[[[1., 0.],
- [0., 1.]]]]]).to(device)
+ [0., 1.]]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[
[[[-1., -1.],
[-1., -1.]]],
[[[-0.5, 0.5],
[-0.5, 0.5]]],
[[[-0.5, -0.5],
- [0.5, 0.5]]]]]]).to(device)
+ [0.5, 0.5]]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
- def test_hot_diag_norm(self, device):
+ def test_hot_diag_norm(self, device, dtype):
input = torch.tensor([[[[
[0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0.],
- ]]]]).to(device)
+ ]]]], device=device, dtype=dtype)
softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),
temperature=10.,
normalized_coordinates=True,
output_value=True)
expected_val = torch.tensor([[[[[0.1214, 0.],
- [0., 0.1214]]]]]).to(device)
+ [0., 0.1214]]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[
[[[-1., -1.],
[-1., -1.]]],
[[[-0.5, 0.5],
[-0.5, 0.5]]],
[[[-0.5, -0.5],
- [0.5, 0.5]]]]]]).to(device)
+ [0.5, 0.5]]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
class TestConvQuadInterp3d:
- def test_smoke(self, device):
- input = torch.randn(2, 3, 3, 4, 4).to(device)
+ def test_smoke(self, device, dtype):
+ input = torch.randn(2, 3, 3, 4, 4, device=device, dtype=dtype)
nms = kornia.geometry.ConvQuadInterp3d(1)
coord, val = nms(input)
assert coord.shape == (2, 3, 3, 3, 4, 4)
assert val.shape == (2, 3, 3, 4, 4)
- def test_gradcheck(self, device):
- input = torch.rand(1, 1, 3, 5, 5).to(device)
+ def test_gradcheck(self, device, dtype):
+ input = torch.rand(1, 1, 3, 5, 5, device=device, dtype=dtype)
input[0, 0, 1, 2, 2] += 20.
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.geometry.ConvQuadInterp3d(strict_maxima_bonus=0),
(input), raise_exception=True, atol=1e-3, rtol=1e-3)
- def test_diag(self, device):
+ def test_diag(self, device, dtype):
input = torch.tensor([[
[[0., 0., 0., 0, 0],
[0., 0., 0.0, 0, 0.],
@@ -441,7 +442,7 @@ class TestConvQuadInterp3d:
[0., 0, 0., 0, 0.],
[0., 0., 0, 0, 0.],
[0., 0., 0., 0, 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
input = kornia.gaussian_blur2d(input, (5, 5), (0.5, 0.5)).unsqueeze(0)
softargmax = kornia.geometry.ConvQuadInterp3d(10)
expected_val = torch.tensor([[[
@@ -459,7 +460,7 @@ class TestConvQuadInterp3d:
[0., 0., 0.0, 0, 0.],
[0., 0, 0., 0, 0.],
[0., 0., 0, 0, 0.],
- [0., 0., 0., 0, 0.]]]]]).to(device)
+ [0., 0., 0., 0, 0.]]]]], device=device, dtype=dtype)
expected_coord = torch.tensor([[[[[[0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0],
@@ -514,7 +515,7 @@ class TestConvQuadInterp3d:
[1.0, 1.0, 1.0, 1.0, 1.0],
[2.0, 2.0, 2.0, 2.0, 2.0],
[3.0, 3.0, 3.0, 3.0, 3.0],
- [4.0, 4.0, 4.0, 4.0, 4.0]]]]]]).to(device)
+ [4.0, 4.0, 4.0, 4.0, 4.0]]]]]], device=device, dtype=dtype)
coords, val = softargmax(input)
- assert_allclose(val, expected_val)
- assert_allclose(coords, expected_coord)
+ assert_allclose(val, expected_val, atol=1e-4, rtol=1e-4)
+ assert_allclose(coords, expected_coord, atol=1e-4, rtol=1e-4)
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index cac86fbb7..080af50aa 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -16,40 +16,40 @@ from torch.testing import assert_allclose
class TestAngleAxisToQuaternion:
- def test_smoke(self, device):
+ def test_smoke(self, device, dtype):
angle_axis = torch.zeros(3)
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
assert quaternion.shape == (4,)
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- angle_axis = torch.zeros(batch_size, 3).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ angle_axis = torch.zeros(batch_size, 3, device=device, dtype=dtype)
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
assert quaternion.shape == (batch_size, 4)
- def test_zero_angle(self, device):
- angle_axis = torch.tensor([0., 0., 0.]).to(device)
- expected = torch.tensor([1., 0., 0., 0.]).to(device)
+ def test_zero_angle(self, device, dtype):
+ angle_axis = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert_allclose(quaternion, expected)
+ assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
def test_small_angle(self, device, dtype):
theta = 1e-2
- angle_axis = torch.tensor([theta, 0., 0.]).to(device, dtype)
- expected = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device, dtype)
+ angle_axis = torch.tensor([theta, 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.], device=device, dtype=dtype)
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert_allclose(quaternion, expected)
+ assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
def test_x_rotation(self, device, dtype):
half_sqrt2 = 0.5 * np.sqrt(2)
- angle_axis = torch.tensor([kornia.pi / 2, 0., 0.]).to(device, dtype)
- expected = torch.tensor([half_sqrt2, half_sqrt2, 0., 0.]).to(device, dtype)
+ angle_axis = torch.tensor([kornia.pi / 2, 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([half_sqrt2, half_sqrt2, 0., 0.], device=device, dtype=dtype)
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert_allclose(quaternion, expected)
+ assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
eps = 1e-12
- angle_axis = torch.tensor([0., 0., 0.]).to(device) + eps
+ angle_axis = torch.tensor([0., 0., 0.], device=device, dtype=dtype) + eps
angle_axis = tensor_to_gradcheck_var(angle_axis)
# evaluate function gradient
assert gradcheck(kornia.angle_axis_to_quaternion, (angle_axis,),
@@ -59,269 +59,267 @@ class TestAngleAxisToQuaternion:
class TestRotationMatrixToQuaternion:
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- matrix = torch.zeros(batch_size, 3, 3).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ matrix = torch.zeros(batch_size, 3, 3, device=device, dtype=dtype)
quaternion = kornia.rotation_matrix_to_quaternion(matrix)
assert quaternion.shape == (batch_size, 4)
- def test_identity(self, device):
+ def test_identity(self, device, dtype):
matrix = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
expected = torch.tensor(
- [0., 0., 0., 1.],
- ).to(device)
+ [0., 0., 0., 1.], device=device, dtype=dtype)
quaternion = kornia.rotation_matrix_to_quaternion(matrix)
- assert_allclose(quaternion, expected)
+ assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
- def test_rot_x_45(self, device):
+ def test_rot_x_45(self, device, dtype):
matrix = torch.tensor([
[1., 0., 0.],
[0., 0., -1.],
[0., 1., 0.],
- ]).to(device)
- pi_half2 = torch.cos(kornia.pi / 4).to(device)
+ ], device=device, dtype=dtype)
+ pi_half2 = torch.cos(kornia.pi / 4).to(device=device, dtype=dtype)
expected = torch.tensor(
- [pi_half2, 0., 0., pi_half2],
- ).to(device)
+ [pi_half2, 0., 0., pi_half2], device=device, dtype=dtype)
quaternion = kornia.rotation_matrix_to_quaternion(matrix)
- assert_allclose(quaternion, expected)
+ assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
- def test_back_and_forth(self, device):
+ def test_back_and_forth(self, device, dtype):
matrix = torch.tensor([
[1., 0., 0.],
[0., 0., -1.],
[0., 1., 0.],
- ]).to(device)
+ ], device=device, dtype=dtype)
quaternion = kornia.rotation_matrix_to_quaternion(matrix)
matrix_hat = kornia.quaternion_to_rotation_matrix(quaternion)
assert_allclose(matrix, matrix_hat)
- def test_corner_case(self, device):
+ def test_corner_case(self, device, dtype):
matrix = torch.tensor([
[-0.7799533010, -0.5432914495, 0.3106555045],
[0.0492402576, -0.5481169224, -0.8349509239],
[0.6238971353, -0.6359263659, 0.4542570710]
- ]).to(device)
+ ], device=device, dtype=dtype)
quaternion_true = torch.tensor([0.280136495828629, -0.440902262926102,
- 0.834015488624573, 0.177614107728004]).to(device)
+ 0.834015488624573, 0.177614107728004], device=device, dtype=dtype)
quaternion = kornia.rotation_matrix_to_quaternion(matrix)
torch.set_printoptions(precision=10)
assert_allclose(quaternion_true, quaternion)
- def test_gradcheck(self, device):
- matrix = torch.eye(3).to(device)
+ def test_gradcheck(self, device, dtype):
+ matrix = torch.eye(3, device=device, dtype=dtype)
matrix = tensor_to_gradcheck_var(matrix)
# evaluate function gradient
assert gradcheck(kornia.rotation_matrix_to_quaternion, (matrix,),
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.quaternion_log_to_exp
op_script = torch.jit.script(op)
- quaternion = torch.tensor([0., 0., 1.]).to(device)
+ quaternion = torch.tensor([0., 0., 1.], device=device, dtype=dtype)
actual = op_script(quaternion)
expected = op(quaternion)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestQuaternionToRotationMatrix:
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- quaternion = torch.zeros(batch_size, 4).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)
matrix = kornia.quaternion_to_rotation_matrix(quaternion)
assert matrix.shape == (batch_size, 3, 3)
- def test_unit_quaternion(self, device):
- quaternion = torch.tensor([0., 0., 0., 1.]).to(device)
+ def test_unit_quaternion(self, device, dtype):
+ quaternion = torch.tensor([0., 0., 0., 1.], device=device, dtype=dtype)
expected = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
matrix = kornia.quaternion_to_rotation_matrix(quaternion)
- assert_allclose(matrix, expected)
+ assert_allclose(matrix, expected, atol=1e-4, rtol=1e-4)
- def test_x_rotation(self, device):
- quaternion = torch.tensor([1., 0., 0., 0.]).to(device)
+ def test_x_rotation(self, device, dtype):
+ quaternion = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
expected = torch.tensor([
[1., 0., 0.],
[0., -1., 0.],
[0., 0., -1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
matrix = kornia.quaternion_to_rotation_matrix(quaternion)
- assert_allclose(matrix, expected)
+ assert_allclose(matrix, expected, atol=1e-4, rtol=1e-4)
- def test_y_rotation(self, device):
- quaternion = torch.tensor([0., 1., 0., 0.]).to(device)
+ def test_y_rotation(self, device, dtype):
+ quaternion = torch.tensor([0., 1., 0., 0.], device=device, dtype=dtype)
expected = torch.tensor([
[-1., 0., 0.],
[0., 1., 0.],
[0., 0., -1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
matrix = kornia.quaternion_to_rotation_matrix(quaternion)
- assert_allclose(matrix, expected)
+ assert_allclose(matrix, expected, atol=1e-4, rtol=1e-4)
- def test_z_rotation(self, device):
- quaternion = torch.tensor([0., 0., 1., 0.]).to(device)
+ def test_z_rotation(self, device, dtype):
+ quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)
expected = torch.tensor([
[-1., 0., 0.],
[0., -1., 0.],
[0., 0., 1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
matrix = kornia.quaternion_to_rotation_matrix(quaternion)
- assert_allclose(matrix, expected)
+ assert_allclose(matrix, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
- quaternion = torch.tensor([0., 0., 0., 1.]).to(device)
+ def test_gradcheck(self, device, dtype):
+ quaternion = torch.tensor([0., 0., 0., 1.], device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_to_rotation_matrix, (quaternion,),
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(input):
return kornia.quaternion_to_rotation_matrix(input)
- quaternion = torch.tensor([0., 0., 1., 0.]).to(device)
+ quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)
actual = op_script(quaternion)
expected = kornia.quaternion_to_rotation_matrix(quaternion)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestQuaternionLogToExp:
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- quaternion_log = torch.zeros(batch_size, 3).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ quaternion_log = torch.zeros(batch_size, 3, device=device, dtype=dtype)
quaternion_exp = kornia.quaternion_log_to_exp(quaternion_log)
assert quaternion_exp.shape == (batch_size, 4)
- def test_unit_quaternion(self, device):
- quaternion_log = torch.tensor([0., 0., 0.]).to(device)
- expected = torch.tensor([0., 0., 0., 1.]).to(device)
+ def test_unit_quaternion(self, device, dtype):
+ quaternion_log = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([0., 0., 0., 1.], device=device, dtype=dtype)
assert_allclose(kornia.quaternion_log_to_exp(quaternion_log), expected)
- def test_pi_quaternion(self, device):
- one = torch.tensor(1.).to(device)
- quaternion_log = torch.tensor([1., 0., 0.]).to(device)
- expected = torch.tensor([torch.sin(one), 0., 0., torch.cos(one)]).to(device)
+ def test_pi_quaternion(self, device, dtype):
+ one = torch.tensor(1., device=device, dtype=dtype)
+ quaternion_log = torch.tensor([1., 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([torch.sin(one), 0., 0., torch.cos(one)], device=device, dtype=dtype)
assert_allclose(kornia.quaternion_log_to_exp(quaternion_log), expected)
- def test_back_and_forth(self, device):
- quaternion_log = torch.tensor([0., 0., 0.]).to(device)
+ def test_back_and_forth(self, device, dtype):
+ quaternion_log = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
quaternion_exp = kornia.quaternion_log_to_exp(quaternion_log)
quaternion_log_hat = kornia.quaternion_exp_to_log(quaternion_exp)
assert_allclose(quaternion_log, quaternion_log_hat)
- def test_gradcheck(self, device):
- quaternion = torch.tensor([0., 0., 1.]).to(device)
+ def test_gradcheck(self, device, dtype):
+ quaternion = torch.tensor([0., 0., 1.], device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_log_to_exp, (quaternion,),
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.quaternion_log_to_exp
op_script = torch.jit.script(op)
- quaternion = torch.tensor([0., 0., 1.]).to(device)
+ quaternion = torch.tensor([0., 0., 1.], device=device, dtype=dtype)
actual = op_script(quaternion)
expected = op(quaternion)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestQuaternionExpToLog:
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- quaternion_exp = torch.zeros(batch_size, 4).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ quaternion_exp = torch.zeros(batch_size, 4, device=device, dtype=dtype)
quaternion_log = kornia.quaternion_exp_to_log(quaternion_exp)
assert quaternion_log.shape == (batch_size, 3)
- def test_unit_quaternion(self, device):
- quaternion_exp = torch.tensor([0., 0., 0., 1.]).to(device)
- expected = torch.tensor([0., 0., 0.]).to(device)
- assert_allclose(kornia.quaternion_exp_to_log(quaternion_exp), expected)
+ def test_unit_quaternion(self, device, dtype):
+ quaternion_exp = torch.tensor([0., 0., 0., 1.], device=device, dtype=dtype)
+ expected = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
+ assert_allclose(kornia.quaternion_exp_to_log(quaternion_exp), expected, atol=1e-4, rtol=1e-4)
- def test_pi_quaternion(self, device):
- quaternion_exp = torch.tensor([1., 0., 0., 0.]).to(device)
- expected = torch.tensor([kornia.pi / 2, 0., 0.]).to(device)
- assert_allclose(kornia.quaternion_exp_to_log(quaternion_exp), expected)
+ def test_pi_quaternion(self, device, dtype):
+ quaternion_exp = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([kornia.pi / 2, 0., 0.], device=device, dtype=dtype)
+ assert_allclose(kornia.quaternion_exp_to_log(quaternion_exp), expected, atol=1e-4, rtol=1e-4)
- def test_back_and_forth(self, device):
- quaternion_exp = torch.tensor([1., 0., 0., 0.]).to(device)
+ def test_back_and_forth(self, device, dtype):
+ quaternion_exp = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
quaternion_log = kornia.quaternion_exp_to_log(quaternion_exp)
quaternion_exp_hat = kornia.quaternion_log_to_exp(quaternion_log)
- assert_allclose(quaternion_exp, quaternion_exp_hat)
+ assert_allclose(quaternion_exp, quaternion_exp_hat, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
- quaternion = torch.tensor([1., 0., 0., 0.]).to(device)
+ def test_gradcheck(self, device, dtype):
+ quaternion = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_exp_to_log, (quaternion,),
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.quaternion_exp_to_log
op_script = torch.jit.script(op)
- quaternion = torch.tensor([0., 0., 1., 0.]).to(device)
+ quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)
actual = op_script(quaternion)
expected = op(quaternion)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestQuaternionToAngleAxis:
- def test_smoke(self, device):
- quaternion = torch.zeros(4).to(device)
+ def test_smoke(self, device, dtype):
+ quaternion = torch.zeros(4, device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
assert angle_axis.shape == (3,)
@pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch(self, device, batch_size):
- quaternion = torch.zeros(batch_size, 4).to(device)
+ def test_smoke_batch(self, batch_size, device, dtype):
+ quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
assert angle_axis.shape == (batch_size, 3)
- def test_unit_quaternion(self, device):
- quaternion = torch.tensor([1., 0., 0., 0.]).to(device)
- expected = torch.tensor([0., 0., 0.]).to(device)
+ def test_unit_quaternion(self, device, dtype):
+ quaternion = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert_allclose(angle_axis, expected)
+ assert_allclose(angle_axis, expected, atol=1e-4, rtol=1e-4)
- def test_y_rotation(self, device):
- quaternion = torch.tensor([0., 0., 1., 0.]).to(device)
- expected = torch.tensor([0., kornia.pi, 0.]).to(device)
+ def test_y_rotation(self, device, dtype):
+ quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([0., kornia.pi, 0.], device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert_allclose(angle_axis, expected)
+ assert_allclose(angle_axis, expected, atol=1e-4, rtol=1e-4)
def test_z_rotation(self, device, dtype):
- quaternion = torch.tensor([np.sqrt(3) / 2, 0., 0., 0.5]).to(device, dtype)
- expected = torch.tensor([0., 0., kornia.pi / 3]).to(device, dtype)
+ quaternion = torch.tensor([np.sqrt(3) / 2, 0., 0., 0.5], device=device, dtype=dtype)
+ expected = torch.tensor([0., 0., kornia.pi / 3], device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert_allclose(angle_axis, expected)
+ assert_allclose(angle_axis, expected, atol=1e-4, rtol=1e-4)
def test_small_angle(self, device, dtype):
theta = 1e-2
- quaternion = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device, dtype)
- expected = torch.tensor([theta, 0., 0.]).to(device, dtype)
+ quaternion = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.], device=device, dtype=dtype)
+ expected = torch.tensor([theta, 0., 0.], device=device, dtype=dtype)
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert_allclose(angle_axis, expected)
+ assert_allclose(angle_axis, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
eps = 1e-12
- quaternion = torch.tensor([1., 0., 0., 0.]).to(device) + eps
+ quaternion = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype) + eps
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_to_angle_axis, (quaternion,),
@@ -334,10 +332,9 @@ def test_pi():
@pytest.mark.parametrize("batch_shape", [
(2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
-def test_rad2deg(batch_shape, device):
+def test_rad2deg(batch_shape, device, dtype):
# generate input data
- x_rad = kornia.pi * torch.rand(batch_shape)
- x_rad = x_rad.to(device)
+ x_rad = kornia.pi * torch.rand(batch_shape, device=device, dtype=dtype)
# convert radians/degrees
x_deg = kornia.rad2deg(x_rad)
@@ -353,16 +350,15 @@ def test_rad2deg(batch_shape, device):
@pytest.mark.parametrize("batch_shape", [
(2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
-def test_deg2rad(batch_shape, device):
+def test_deg2rad(batch_shape, device, dtype):
# generate input data
- x_deg = 180. * torch.rand(batch_shape)
- x_deg = x_deg.to(device)
+ x_deg = 180. * torch.rand(batch_shape, device=device, dtype=dtype)
# convert radians/degrees
x_rad = kornia.deg2rad(x_deg)
x_rad_to_deg = kornia.rad2deg(x_rad)
- assert_allclose(x_deg, x_rad_to_deg)
+ assert_allclose(x_deg, x_rad_to_deg, atol=1e-4, rtol=1e-4)
assert gradcheck(kornia.deg2rad, (tensor_to_gradcheck_var(x_deg),),
raise_exception=True)
@@ -389,7 +385,7 @@ class TestConvertPointsToHomogeneous:
# to euclidean
points = kornia.convert_points_to_homogeneous(points_h)
- assert_allclose(points, expected)
+ assert_allclose(points, expected, atol=1e-4, rtol=1e-4)
def test_convert_points_batch(self, device, dtype):
# generate input data
@@ -411,12 +407,12 @@ class TestConvertPointsToHomogeneous:
# to euclidean
points = kornia.convert_points_to_homogeneous(points_h)
- assert_allclose(points, expected)
+ assert_allclose(points, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [
(2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
- def test_gradcheck(self, device, batch_shape):
- points_h = torch.rand(batch_shape).to(device)
+ def test_gradcheck(self, batch_shape, device, dtype):
+ points_h = torch.rand(batch_shape, device=device, dtype=dtype)
# evaluate function gradient
points_h = tensor_to_gradcheck_var(points_h) # to var
@@ -424,30 +420,30 @@ class TestConvertPointsToHomogeneous:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.convert_points_to_homogeneous
op_script = torch.jit.script(op)
- points_h = torch.zeros(1, 2, 3).to(device)
+ points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)
actual = op_script(points_h)
expected = op(points_h)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestConvertAtoH:
- def test_convert_points(self, device):
+ def test_convert_points(self, device, dtype):
# generate input data
A = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
- ]).to(device).view(1, 2, 3)
+ ], device=device, dtype=dtype).view(1, 2, 3)
expected = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device).view(1, 3, 3)
+ ], device=device, dtype=dtype).view(1, 3, 3)
# to euclidean
H = kornia.geometry.conversions.convert_affinematrix_to_homography(A)
@@ -455,8 +451,8 @@ class TestConvertAtoH:
@pytest.mark.parametrize("batch_shape", [
(10, 2, 3), (16, 2, 3)])
- def test_gradcheck(self, device, batch_shape):
- points_h = torch.rand(batch_shape).to(device)
+ def test_gradcheck(self, batch_shape, device, dtype):
+ points_h = torch.rand(batch_shape, device=device, dtype=dtype)
# evaluate function gradient
points_h = tensor_to_gradcheck_var(points_h) # to var
@@ -464,19 +460,19 @@ class TestConvertAtoH:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.convert_affinematrix_to_homography
op_script = torch.jit.script(op)
- points_h = torch.zeros(1, 2, 3).to(device)
+ points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)
actual = op_script(points_h)
expected = op(points_h)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestConvertPointsFromHomogeneous:
- def test_convert_points(self, device):
+ def test_convert_points(self, device, dtype):
# generate input data
points_h = torch.tensor([
[1., 2., 1.],
@@ -484,7 +480,7 @@ class TestConvertPointsFromHomogeneous:
[2., 1., 0.],
[-1., -2., -1.],
[0., 1., -2.],
- ]).to(device)
+ ], device=device, dtype=dtype)
expected = torch.tensor([
[1., 2.],
@@ -492,13 +488,13 @@ class TestConvertPointsFromHomogeneous:
[2., 1.],
[1., 2.],
[0., -0.5],
- ]).to(device)
+ ], device=device, dtype=dtype)
# to euclidean
points = kornia.convert_points_from_homogeneous(points_h)
- assert_allclose(points, expected)
+ assert_allclose(points, expected, atol=1e-4, rtol=1e-4)
- def test_convert_points_batch(self, device):
+ def test_convert_points_batch(self, device, dtype):
# generate input data
points_h = torch.tensor([[
[2., 1., 0.],
@@ -506,7 +502,7 @@ class TestConvertPointsFromHomogeneous:
[0., 1., 2.],
], [
[0., 1., -2.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor([[
[2., 1.],
@@ -514,16 +510,16 @@ class TestConvertPointsFromHomogeneous:
[0., 0.5],
], [
[0., -0.5],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# to euclidean
points = kornia.convert_points_from_homogeneous(points_h)
- assert_allclose(points, expected)
+ assert_allclose(points, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [
(2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
- def test_gradcheck(self, device, batch_shape):
- points_h = torch.rand(batch_shape).to(device)
+ def test_gradcheck(self, batch_shape, device, dtype):
+ points_h = torch.rand(batch_shape, device=device, dtype=dtype)
# evaluate function gradient
points_h = tensor_to_gradcheck_var(points_h) # to var
@@ -531,29 +527,29 @@ class TestConvertPointsFromHomogeneous:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.convert_points_from_homogeneous
op_script = torch.jit.script(op)
- points_h = torch.zeros(1, 2, 3).to(device)
+ points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)
actual = op_script(points_h)
expected = op(points_h)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
-def test_angle_axis_to_rotation_matrix(batch_size, device):
+def test_angle_axis_to_rotation_matrix(batch_size, device, dtype):
# generate input data
- angle_axis = torch.rand(batch_size, 3).to(device)
- eye_batch = create_eye_batch(batch_size, 3).to(device)
+ angle_axis = torch.rand(batch_size, 3, device=device, dtype=dtype)
+ eye_batch = create_eye_batch(batch_size, 3, device=device, dtype=dtype)
# apply transform
rotation_matrix = kornia.angle_axis_to_rotation_matrix(angle_axis)
rotation_matrix_eye = torch.matmul(
rotation_matrix, rotation_matrix.transpose(1, 2))
- assert_allclose(rotation_matrix_eye, eye_batch)
+ assert_allclose(rotation_matrix_eye, eye_batch, atol=1e-4, rtol=1e-4)
# evaluate function gradient
angle_axis = tensor_to_gradcheck_var(angle_axis) # to var
@@ -583,95 +579,95 @@ def test_rotation_matrix_to_angle_axis_gradcheck(batch_size, device_type):
[-0.67915707, -0.56931658, 0.46327563],
[-0.41881476, -0.21775548, -0.88157628]])
rvec_2 = torch.tensor([-2.44916812, 1.18053411, 0.4085298])
- rmat = torch.stack([rmat_2, rmat_1], dim=0).to(device)
- rvec = torch.stack([rvec_2, rvec_1], dim=0).to(device)
+ rmat = torch.stack([rmat_2, rmat_1], dim=0, device=device, dtype=dtype)
+ rvec = torch.stack([rvec_2, rvec_1], dim=0, device=device, dtype=dtype)
assert_allclose(kornia.rotation_matrix_to_angle_axis(rmat), rvec)'''
class TestNormalizePixelCoordinates:
- def test_tensor_bhw2(self, device):
+ def test_tensor_bhw2(self, device, dtype):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype=dtype)
expected = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
grid_norm = kornia.normalize_pixel_coordinates(
grid, height, width)
- assert_allclose(grid_norm, expected)
+ assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)
- def test_list(self, device):
+ def test_list(self, device, dtype):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype=dtype)
grid = grid.contiguous().view(-1, 2)
expected = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
expected = expected.contiguous().view(-1, 2)
grid_norm = kornia.normalize_pixel_coordinates(
grid, height, width)
- assert_allclose(grid_norm, expected)
+ assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.normalize_pixel_coordinates
op_script = torch.jit.script(op)
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
actual = op_script(grid, height, width)
expected = op(grid, height, width)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestDenormalizePixelCoordinates:
- def test_tensor_bhw2(self, device):
+ def test_tensor_bhw2(self, device, dtype):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
expected = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype=dtype)
grid_norm = kornia.denormalize_pixel_coordinates(
grid, height, width)
- assert_allclose(grid_norm, expected)
+ assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)
- def test_list(self, device):
+ def test_list(self, device, dtype):
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
grid = grid.contiguous().view(-1, 2)
expected = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype=dtype)
expected = expected.contiguous().view(-1, 2)
grid_norm = kornia.denormalize_pixel_coordinates(
grid, height, width)
- assert_allclose(grid_norm, expected)
+ assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
op = kornia.denormalize_pixel_coordinates
op_script = torch.jit.script(op)
height, width = 3, 4
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=True).to(device)
+ height, width, normalized_coordinates=True, device=device).to(dtype=dtype)
actual = op_script(grid, height, width)
expected = op(grid, height, width)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
diff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.py
index a3b6c2bf3..99f312bb3 100644
--- a/test/geometry/test_depth.py
+++ b/test/geometry/test_depth.py
@@ -9,43 +9,43 @@ from torch.testing import assert_allclose
class TestDepthTo3d:
- def test_smoke(self, device):
- depth = torch.rand(1, 1, 3, 4).to(device)
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ def test_smoke(self, device, dtype):
+ depth = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix)
assert points3d.shape == (1, 3, 3, 4)
@pytest.mark.parametrize("batch_size", [2, 4, 5])
- def test_shapes(self, device, batch_size):
- depth = torch.rand(batch_size, 1, 3, 4).to(device)
- camera_matrix = torch.rand(batch_size, 3, 3).to(device)
+ def test_shapes(self, batch_size, device, dtype):
+ depth = torch.rand(batch_size, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(batch_size, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix)
assert points3d.shape == (batch_size, 3, 3, 4)
@pytest.mark.parametrize("batch_size", [1, 2, 4, 5])
- def test_shapes_broadcast(self, device, batch_size):
- depth = torch.rand(batch_size, 1, 3, 4).to(device)
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ def test_shapes_broadcast(self, batch_size, device, dtype):
+ depth = torch.rand(batch_size, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix)
assert points3d.shape == (batch_size, 3, 3, 4)
- def test_unproject_denormalized(self, device):
+ def test_unproject_denormalized(self, device, dtype):
# this is for default normalize_points=False
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
points3d_expected = torch.tensor([[[
[0., 2., 4.],
@@ -62,25 +62,25 @@ class TestDepthTo3d:
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix) # default is normalize_points=False
- assert_allclose(points3d, points3d_expected)
+ assert_allclose(points3d, points3d_expected, atol=1e-4, rtol=1e-4)
- def test_unproject_normalized(self, device):
+ def test_unproject_normalized(self, device, dtype):
# this is for normalize_points=True
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
points3d_expected = torch.tensor([[[
[0.0000, 1.4142, 1.7889],
@@ -97,39 +97,39 @@ class TestDepthTo3d:
[1.4142, 1.1547, 0.8165],
[0.8944, 0.8165, 0.6667],
[0.6325, 0.6030, 0.5345],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix, normalize_points=True)
- assert_allclose(points3d, points3d_expected)
+ assert_allclose(points3d, points3d_expected, atol=1e-4, rtol=1e-4)
- def test_unproject_and_project(self, device):
+ def test_unproject_and_project(self, device, dtype):
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
points3d = kornia.depth_to_3d(depth, camera_matrix)
points2d = kornia.project_points(
points3d.permute(0, 2, 3, 1),
camera_matrix[:, None, None]
)
- points2d_expected = kornia.create_meshgrid(4, 3, False).to(device)
- assert_allclose(points2d, points2d_expected)
+ points2d_expected = kornia.create_meshgrid(4, 3, False, device=device).to(dtype=dtype)
+ assert_allclose(points2d, points2d_expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# generate input data
- depth = torch.rand(1, 1, 3, 4).to(device)
+ depth = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
depth = utils.tensor_to_gradcheck_var(depth) # to var
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var
# evaluate function gradient
@@ -138,43 +138,43 @@ class TestDepthTo3d:
class TestDepthToNormals:
- def test_smoke(self, device):
- depth = torch.rand(1, 1, 3, 4).to(device)
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ def test_smoke(self, device, dtype):
+ depth = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_normals(depth, camera_matrix)
assert points3d.shape == (1, 3, 3, 4)
@pytest.mark.parametrize("batch_size", [2, 4, 5])
- def test_shapes(self, device, batch_size):
- depth = torch.rand(batch_size, 1, 3, 4).to(device)
- camera_matrix = torch.rand(batch_size, 3, 3).to(device)
+ def test_shapes(self, batch_size, device, dtype):
+ depth = torch.rand(batch_size, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(batch_size, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_normals(depth, camera_matrix)
assert points3d.shape == (batch_size, 3, 3, 4)
@pytest.mark.parametrize("batch_size", [2, 4, 5])
- def test_shapes_broadcast(self, device, batch_size):
- depth = torch.rand(batch_size, 1, 3, 4).to(device)
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ def test_shapes_broadcast(self, batch_size, device, dtype):
+ depth = torch.rand(batch_size, 1, 3, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
points3d = kornia.depth_to_normals(depth, camera_matrix)
assert points3d.shape == (batch_size, 3, 3, 4)
- def test_simple(self, device):
+ def test_simple(self, device, dtype):
# this is for default normalize_points=False
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
normals_expected = torch.tensor([[[
[0., 0., 0.],
@@ -191,25 +191,25 @@ class TestDepthToNormals:
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
normals = kornia.depth_to_normals(depth, camera_matrix) # default is normalize_points=False
assert_allclose(normals, normals_expected, 1e-3, 1e-3)
- def test_simple_normalized(self, device):
+ def test_simple_normalized(self, device, dtype):
# this is for default normalize_points=False
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
normals_expected = torch.tensor([[[
[0.3432, 0.4861, 0.7628],
@@ -226,17 +226,17 @@ class TestDepthToNormals:
[0.8253, 0.7981, 0.6415],
[0.5432, 0.5807, 0.5105],
[0.4129, 0.4824, 0.4784],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
normals = kornia.depth_to_normals(depth, camera_matrix, normalize_points=True)
assert_allclose(normals, normals_expected, 1e-3, 1e-3)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# generate input data
- depth = torch.rand(1, 1, 3, 4).to(device)
+ depth = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
depth = utils.tensor_to_gradcheck_var(depth) # to var
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var
# evaluate function gradient
@@ -245,119 +245,119 @@ class TestDepthToNormals:
class TestWarpFrameDepth:
- def test_smoke(self, device):
- image_src = torch.rand(1, 3, 3, 4).to(device)
- depth_dst = torch.rand(1, 1, 3, 4).to(device)
- src_trans_dst = torch.rand(1, 4, 4).to(device)
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ def test_smoke(self, device, dtype):
+ image_src = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
+ depth_dst = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
+ src_trans_dst = torch.rand(1, 4, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)
assert image_dst.shape == (1, 3, 3, 4)
@pytest.mark.parametrize("batch_size", [2, 4, 5])
@pytest.mark.parametrize("num_features", [1, 3, 5])
- def test_shape(self, device, batch_size, num_features):
- image_src = torch.rand(batch_size, num_features, 3, 4).to(device)
- depth_dst = torch.rand(batch_size, 1, 3, 4).to(device)
- src_trans_dst = torch.rand(batch_size, 4, 4).to(device)
- camera_matrix = torch.rand(batch_size, 3, 3).to(device)
+ def test_shape(self, batch_size, num_features, device, dtype):
+ image_src = torch.rand(batch_size, num_features, 3, 4, device=device, dtype=dtype)
+ depth_dst = torch.rand(batch_size, 1, 3, 4, device=device, dtype=dtype)
+ src_trans_dst = torch.rand(batch_size, 4, 4, device=device, dtype=dtype)
+ camera_matrix = torch.rand(batch_size, 3, 3, device=device, dtype=dtype)
image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)
assert image_dst.shape == (batch_size, num_features, 3, 4)
- def test_translation(self, device):
+ def test_translation(self, device, dtype):
# this is for normalize_points=False
image_src = torch.tensor([[[
[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
depth_dst = torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
src_trans_dst = torch.tensor([[
[1., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
h, w = image_src.shape[-2:]
camera_matrix = torch.tensor([[
[1., 0., w / 2],
[0., 1., h / 2],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
image_dst_expected = torch.tensor([[[
[2., 3., 0.],
[2., 3., 0.],
[2., 3., 0.],
[2., 3., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
image_dst = kornia.warp_frame_depth(
image_src, depth_dst, src_trans_dst, camera_matrix) # default is normalize_points=False
assert_allclose(image_dst, image_dst_expected, 1e-3, 1e-3)
- def test_translation_normalized(self, device):
+ def test_translation_normalized(self, device, dtype):
# this is for normalize_points=True
image_src = torch.tensor([[[
[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
depth_dst = torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
src_trans_dst = torch.tensor([[
[1., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
h, w = image_src.shape[-2:]
camera_matrix = torch.tensor([[
[1., 0., w / 2],
[0., 1., h / 2],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
image_dst_expected = torch.tensor([[[
[0.9223, 0.0000, 0.0000],
[2.8153, 1.5000, 0.0000],
[2.8028, 2.6459, 0.0000],
[2.8153, 1.5000, 0.0000],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
image_dst = kornia.warp_frame_depth(
image_src, depth_dst, src_trans_dst, camera_matrix, normalize_points=True)
assert_allclose(image_dst, image_dst_expected, 1e-3, 1e-3)
- def test_gradcheck(self, device):
- image_src = torch.rand(1, 3, 3, 4).to(device)
+ def test_gradcheck(self, device, dtype):
+ image_src = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
image_src = utils.tensor_to_gradcheck_var(image_src) # to var
- depth_dst = torch.rand(1, 1, 3, 4).to(device)
+ depth_dst = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
depth_dst = utils.tensor_to_gradcheck_var(depth_dst) # to var
- src_trans_dst = torch.rand(1, 4, 4).to(device)
+ src_trans_dst = torch.rand(1, 4, 4, device=device, dtype=dtype)
src_trans_dst = utils.tensor_to_gradcheck_var(src_trans_dst) # to var
- camera_matrix = torch.rand(1, 3, 3).to(device)
+ camera_matrix = torch.rand(1, 3, 3, device=device, dtype=dtype)
camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var
# evaluate function gradient
diff --git a/test/geometry/test_homography.py b/test/geometry/test_homography.py
index 7a85fa738..3cb0f926f 100644
--- a/test/geometry/test_homography.py
+++ b/test/geometry/test_homography.py
@@ -45,7 +45,7 @@ class TestFindHomographyDLT:
H_noweights = find_homography_dlt(points1, points2, None)
H_withweights = find_homography_dlt(points1, points2, weights)
assert H_noweights.shape == (B, 3, 3) and H_withweights.shape == (B, 3, 3)
- assert_allclose(H_noweights, H_withweights)
+ assert_allclose(H_noweights, H_withweights, rtol=1e-3, atol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_clean_points(self, batch_size, device, dtype):
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index a135448b7..5a68fb1b2 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -8,9 +8,9 @@ from torch.autograd import gradcheck
from torch.testing import assert_allclose
-def identity_matrix(batch_size):
+def identity_matrix(batch_size, device, dtype):
r"""Creates a batched homogeneous identity matrix"""
- return torch.eye(4).repeat(batch_size, 1, 1) # Nx4x4
+ return torch.eye(4, device=device, dtype=dtype).repeat(batch_size, 1, 1) # Nx4x4
def euler_angles_to_rotation_matrix(x, y, z):
@@ -48,13 +48,12 @@ class TestTransformPoints:
@pytest.mark.parametrize("num_points", [2, 3, 5])
@pytest.mark.parametrize("num_dims", [2, 3])
def test_transform_points(
- self, device, batch_size, num_points, num_dims):
+ self, batch_size, num_points, num_dims, device, dtype):
# generate input data
eye_size = num_dims + 1
- points_src = torch.rand(batch_size, num_points, num_dims)
- points_src = points_src.to(device)
+ points_src = torch.rand(batch_size, num_points, num_dims, device=device, dtype=dtype)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size)
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device=device, dtype=dtype)
dst_homo_src = dst_homo_src.to(device)
# transform the points from dst to ref
@@ -65,69 +64,69 @@ class TestTransformPoints:
points_dst_to_src = kornia.transform_points(src_homo_dst, points_dst)
# projected should be equal as initial
- assert_allclose(points_src, points_dst_to_src)
+ assert_allclose(points_src, points_dst_to_src, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# generate input data
batch_size, num_points, num_dims = 2, 3, 2
eye_size = num_dims + 1
- points_src = torch.rand(batch_size, num_points, num_dims).to(device)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device)
+ points_src = torch.rand(batch_size, num_points, num_dims, device=device, dtype=dtype)
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device=device, dtype=dtype)
# evaluate function gradient
points_src = utils.tensor_to_gradcheck_var(points_src) # to var
dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src) # to var
assert gradcheck(kornia.transform_points, (dst_homo_src, points_src,),
raise_exception=True)
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(transform, points):
return kornia.transform_points(transform, points)
- points = torch.ones(1, 2, 2, device=device)
+ points = torch.ones(1, 2, 2, device=device, dtype=dtype)
transform = kornia.eye_like(3, points)
op = kornia.transform_points
op_script = torch.jit.script(op)
actual = op_script(transform, points)
expected = op(transform, points)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit_trace(self, device):
+ def test_jit_trace(self, device, dtype):
@torch.jit.script
def op_script(transform, points):
return kornia.transform_points(transform, points)
- points = torch.ones(1, 2, 2).to(device)
+ points = torch.ones(1, 2, 2, device=device, dtype=dtype)
transform = torch.eye(3)[None].to(device)
op_script_trace = torch.jit.trace(op_script, (transform, points,))
actual = op_script_trace(transform, points)
expected = kornia.transform_points(transform, points)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestTransformBoxes:
- def test_transform_boxes(self, device):
+ def test_transform_boxes(self, device, dtype):
- boxes = torch.tensor([[139.2640, 103.0150, 397.3120, 410.5225]]).to(device)
+ boxes = torch.tensor([[139.2640, 103.0150, 397.3120, 410.5225]], device=device, dtype=dtype)
- expected = torch.tensor([372.7360, 103.0150, 114.6880, 410.5225]).to(device)
+ expected = torch.tensor([372.7360, 103.0150, 114.6880, 410.5225], device=device, dtype=dtype)
trans_mat = torch.tensor([[[-1., 0., 512.],
[0., 1., 0.],
- [0., 0., 1.]]]).to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype)
out = kornia.transform_boxes(trans_mat, boxes)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
- def test_transform_multiple_boxes(self, device):
+ def test_transform_multiple_boxes(self, device, dtype):
boxes = torch.tensor([[139.2640, 103.0150, 397.3120, 410.5225],
[1.0240, 80.5547, 512.0000, 512.0000],
[165.2053, 262.1440, 510.6347, 508.9280],
- [119.8080, 144.2067, 257.0240, 410.1292]]).to(device)
+ [119.8080, 144.2067, 257.0240, 410.1292]], device=device, dtype=dtype)
boxes = boxes.repeat(2, 1, 1) # 2 x 4 x 4 two images 4 boxes each
@@ -139,7 +138,7 @@ class TestTransformBoxes:
[[139.2640, 103.0150, 397.3120, 410.5225],
[1.0240, 80.5547, 512.0000, 512.0000],
[165.2053, 262.1440, 510.6347, 508.9280],
- [119.8080, 144.2067, 257.0240, 410.1292]]]).to(device)
+ [119.8080, 144.2067, 257.0240, 410.1292]]], device=device, dtype=dtype)
trans_mat = torch.tensor([[[-1., 0., 512.],
[0., 1., 0.],
@@ -147,40 +146,40 @@ class TestTransformBoxes:
[[1., 0., 0.],
[0., 1., 0.],
- [0., 0., 1.]]]).to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype)
out = kornia.transform_boxes(trans_mat, boxes)
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
- def test_transform_boxes_wh(self, device):
+ def test_transform_boxes_wh(self, device, dtype):
boxes = torch.tensor([[139.2640, 103.0150, 258.0480, 307.5075],
[1.0240, 80.5547, 510.9760, 431.4453],
[165.2053, 262.1440, 345.4293, 246.7840],
- [119.8080, 144.2067, 137.2160, 265.9225]]).to(device)
+ [119.8080, 144.2067, 137.2160, 265.9225]], device=device, dtype=dtype)
expected = torch.tensor([[372.7360, 103.0150, -258.0480, 307.5075],
[510.9760, 80.5547, -510.9760, 431.4453],
[346.7947, 262.1440, -345.4293, 246.7840],
- [392.1920, 144.2067, -137.2160, 265.9225]]).to(device)
+ [392.1920, 144.2067, -137.2160, 265.9225]], device=device, dtype=dtype)
trans_mat = torch.tensor([[[-1., 0., 512.],
[0., 1., 0.],
- [0., 0., 1.]]]).to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype)
out = kornia.transform_boxes(trans_mat, boxes, mode='xywh')
- assert_allclose(out, expected)
+ assert_allclose(out, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
boxes = torch.tensor([[139.2640, 103.0150, 258.0480, 307.5075],
[1.0240, 80.5547, 510.9760, 431.4453],
[165.2053, 262.1440, 345.4293, 246.7840],
- [119.8080, 144.2067, 137.2160, 265.9225]]).to(device)
+ [119.8080, 144.2067, 137.2160, 265.9225]], device=device, dtype=dtype)
trans_mat = torch.tensor([[[-1., 0., 512.],
[0., 1., 0.],
- [0., 0., 1.]]]).to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype)
trans_mat = utils.tensor_to_gradcheck_var(trans_mat)
boxes = utils.tensor_to_gradcheck_var(boxes)
@@ -188,48 +187,48 @@ class TestTransformBoxes:
assert gradcheck(kornia.transform_boxes, (trans_mat, boxes), raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(transform, boxes):
return kornia.transform_boxes(transform, boxes)
- boxes = torch.tensor([139.2640, 103.0150, 258.0480, 307.5075]).to(device)
+ boxes = torch.tensor([139.2640, 103.0150, 258.0480, 307.5075], device=device, dtype=dtype)
trans_mat = torch.tensor([[[-1., 0., 512.],
[0., 1., 0.],
- [0., 0., 1.]]]).to(device)
+ [0., 0., 1.]]], device=device, dtype=dtype)
actual = op_script(trans_mat, boxes)
expected = kornia.transform_points(trans_mat, boxes)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestComposeTransforms:
- def test_translation_4x4(self, device):
+ def test_translation_4x4(self, device, dtype):
offset = 10
- trans_01 = identity_matrix(batch_size=1)[0].to(device)
- trans_12 = identity_matrix(batch_size=1)[0].to(device)
+ trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]
+ trans_12 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]
trans_12[..., :3, -1] += offset # add offset to translation vector
trans_02 = kornia.compose_transformations(trans_01, trans_12)
- assert_allclose(trans_02, trans_12)
+ assert_allclose(trans_02, trans_12, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_translation_Bx4x4(self, device, batch_size):
+ def test_translation_Bx4x4(self, batch_size, device, dtype):
offset = 10
- trans_01 = identity_matrix(batch_size)
- trans_12 = identity_matrix(batch_size)
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
+ trans_12 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_12[..., :3, -1] += offset # add offset to translation vector
trans_02 = kornia.compose_transformations(trans_01, trans_12)
- assert_allclose(trans_02, trans_12)
+ assert_allclose(trans_02, trans_12, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_gradcheck(self, device, batch_size):
- trans_01 = identity_matrix(batch_size).to(device)
- trans_12 = identity_matrix(batch_size).to(device)
+ def test_gradcheck(self, batch_size, device, dtype):
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
+ trans_12 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_01 = utils.tensor_to_gradcheck_var(trans_01) # to var
trans_12 = utils.tensor_to_gradcheck_var(trans_12) # to var
@@ -239,43 +238,43 @@ class TestComposeTransforms:
class TestInverseTransformation:
- def test_translation_4x4(self, device):
+ def test_translation_4x4(self, device, dtype):
offset = 10
- trans_01 = identity_matrix(batch_size=1)[0].to(device)
+ trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]
trans_01[..., :3, -1] += offset # add offset to translation vector
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert_allclose(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_translation_Bx4x4(self, device, batch_size):
+ def test_translation_Bx4x4(self, batch_size, device, dtype):
offset = 10
- trans_01 = identity_matrix(batch_size).to(device)
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_01[..., :3, -1] += offset # add offset to translation vector
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert_allclose(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_rotation_translation_Bx4x4(self, device, batch_size):
+ def test_rotation_translation_Bx4x4(self, batch_size, device, dtype):
offset = 10
x, y, z = 0, 0, kornia.pi
- ones = torch.ones(batch_size).to(device)
+ ones = torch.ones(batch_size, device=device, dtype=dtype)
rmat_01 = euler_angles_to_rotation_matrix(x * ones, y * ones, z * ones)
- trans_01 = identity_matrix(batch_size).to(device)
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_01[..., :3, -1] += offset # add offset to translation vector
trans_01[..., :3, :3] = rmat_01[..., :3, :3]
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert_allclose(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_gradcheck(self, device, batch_size):
- trans_01 = identity_matrix(batch_size).to(device)
+ def test_gradcheck(self, batch_size, device, dtype):
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_01 = utils.tensor_to_gradcheck_var(trans_01) # to var
assert gradcheck(kornia.inverse_transformation, (trans_01,),
raise_exception=True)
@@ -283,36 +282,36 @@ class TestInverseTransformation:
class TestRelativeTransformation:
- def test_translation_4x4(self, device):
+ def test_translation_4x4(self, device, dtype):
offset = 10.
- trans_01 = identity_matrix(batch_size=1)[0].to(device)
- trans_02 = identity_matrix(batch_size=1)[0].to(device)
+ trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]
+ trans_02 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]
trans_02[..., :3, -1] += offset # add offset to translation vector
trans_12 = kornia.relative_transformation(trans_01, trans_02)
trans_02_hat = kornia.compose_transformations(trans_01, trans_12)
- assert_allclose(trans_02_hat, trans_02)
+ assert_allclose(trans_02_hat, trans_02, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_rotation_translation_Bx4x4(self, device, batch_size):
+ def test_rotation_translation_Bx4x4(self, batch_size, device, dtype):
offset = 10.
x, y, z = 0., 0., kornia.pi
- ones = torch.ones(batch_size).to(device)
+ ones = torch.ones(batch_size, device=device, dtype=dtype)
rmat_02 = euler_angles_to_rotation_matrix(x * ones, y * ones, z * ones)
- trans_01 = identity_matrix(batch_size).to(device)
- trans_02 = identity_matrix(batch_size).to(device)
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
+ trans_02 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_02[..., :3, -1] += offset # add offset to translation vector
trans_02[..., :3, :3] = rmat_02[..., :3, :3]
trans_12 = kornia.relative_transformation(trans_01, trans_02)
trans_02_hat = kornia.compose_transformations(trans_01, trans_12)
- assert_allclose(trans_02_hat, trans_02)
+ assert_allclose(trans_02_hat, trans_02, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_gradcheck(self, device, batch_size):
- trans_01 = identity_matrix(batch_size).to(device)
- trans_02 = identity_matrix(batch_size).to(device)
+ def test_gradcheck(self, batch_size, device, dtype):
+ trans_01 = identity_matrix(batch_size, device=device, dtype=dtype)
+ trans_02 = identity_matrix(batch_size, device=device, dtype=dtype)
trans_01 = utils.tensor_to_gradcheck_var(trans_01) # to var
trans_02 = utils.tensor_to_gradcheck_var(trans_02) # to var
@@ -325,13 +324,12 @@ class TestTransformLAFs:
@pytest.mark.parametrize("batch_size", [1, 2, 5])
@pytest.mark.parametrize("num_points", [2, 3, 5])
def test_transform_points(
- self, batch_size, num_points, device):
+ self, batch_size, num_points, device, dtype):
# generate input data
eye_size = 3
- lafs_src = torch.rand(batch_size, num_points, 2, 3).to(device)
+ lafs_src = torch.rand(batch_size, num_points, 2, 3, device=device, dtype=dtype)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size)
- dst_homo_src = dst_homo_src.to(device)
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device=device, dtype=dtype)
# transform the points from dst to ref
lafs_dst = kornia.perspective_transform_lafs(dst_homo_src, lafs_src)
@@ -343,12 +341,12 @@ class TestTransformLAFs:
# projected should be equal as initial
assert_allclose(lafs_src, lafs_dst_to_src)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# generate input data
batch_size, num_points, num_dims = 2, 3, 2
eye_size = 3
- points_src = torch.rand(batch_size, num_points, 2, 3).to(device)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device)
+ points_src = torch.rand(batch_size, num_points, 2, 3, device=device, dtype=dtype)
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size).to(device=device, dtype=dtype)
# evaluate function gradient
points_src = utils.tensor_to_gradcheck_var(points_src) # to var
dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src) # to var
diff --git a/test/geometry/test_perspective.py b/test/geometry/test_perspective.py
index 0621d5d55..42b885255 100644
--- a/test/geometry/test_perspective.py
+++ b/test/geometry/test_perspective.py
@@ -9,40 +9,40 @@ from torch.testing import assert_allclose
class TestProjectPoints:
- def test_smoke(self, device):
- point_3d = torch.zeros(1, 3).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ 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.project_points(point_3d, camera_matrix)
assert point_2d.shape == (1, 2)
- def test_smoke_batch(self, device):
- point_3d = torch.zeros(2, 3).to(device)
- camera_matrix = torch.eye(3).expand(2, -1, -1).to(device)
+ 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.project_points(point_3d, camera_matrix)
assert point_2d.shape == (2, 2)
- def test_smoke_batch_multi(self, device):
- point_3d = torch.zeros(2, 4, 3).to(device)
- camera_matrix = torch.eye(3).expand(2, 4, -1, -1).to(device)
+ 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.project_points(point_3d, camera_matrix)
assert point_2d.shape == (2, 4, 2)
- def test_project_and_unproject(self, device):
- point_3d = torch.tensor([[10., 2., 30.]]).to(device)
+ def test_project_and_unproject(self, device, dtype):
+ point_3d = torch.tensor([[10., 2., 30.]], device=device, dtype=dtype)
depth = point_3d[..., -1:]
camera_matrix = torch.tensor([[
[2746., 0., 991.],
[0., 2748., 619.],
[0., 0., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
point_2d = kornia.project_points(point_3d, camera_matrix)
point_3d_hat = kornia.unproject_points(point_2d, depth, camera_matrix)
- assert_allclose(point_3d, point_3d_hat)
+ assert_allclose(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# TODO: point [0, 0, 0] crashes
- points_3d = torch.ones(1, 3).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ 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)
@@ -52,81 +52,81 @@ class TestProjectPoints:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(points_3d, camera_matrix):
return kornia.project_points(points_3d, camera_matrix)
- points_3d = torch.zeros(1, 3).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ points_3d = torch.zeros(1, 3, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
actual = op_script(points_3d, camera_matrix)
expected = kornia.project_points(points_3d, camera_matrix)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
class TestUnprojectPoints:
- def test_smoke(self, device):
- points_2d = torch.zeros(1, 2).to(device)
- depth = torch.ones(1, 1).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ def test_smoke(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ depth = torch.ones(1, 1, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (1, 3)
- def test_smoke_batch(self, device):
- points_2d = torch.zeros(2, 2).to(device)
- depth = torch.ones(2, 1).to(device)
- camera_matrix = torch.eye(3).expand(2, -1, -1).to(device)
+ def test_smoke_batch(self, device, dtype):
+ points_2d = torch.zeros(2, 2, device=device, dtype=dtype)
+ depth = torch.ones(2, 1, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (2, 3)
- def test_smoke_multi_batch(self, device):
- points_2d = torch.zeros(2, 3, 2).to(device)
- depth = torch.ones(2, 3, 1).to(device)
- camera_matrix = torch.eye(3).expand(2, 3, -1, -1).to(device)
+ def test_smoke_multi_batch(self, device, dtype):
+ points_2d = torch.zeros(2, 3, 2, device=device, dtype=dtype)
+ depth = torch.ones(2, 3, 1, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 3, -1, -1)
point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (2, 3, 3)
- def test_unproject_center(self, device):
- point_2d = torch.tensor([[0., 0.]]).to(device)
- depth = torch.tensor([[2.]]).to(device)
+ def test_unproject_center(self, device, dtype):
+ point_2d = torch.tensor([[0., 0.]], device=device, dtype=dtype)
+ depth = torch.tensor([[2.]], device=device, dtype=dtype)
camera_matrix = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device)
- expected = torch.tensor([[0., 0., 2.]]).to(device)
+ ], device=device, dtype=dtype)
+ expected = torch.tensor([[0., 0., 2.]], device=device, dtype=dtype)
actual = kornia.unproject_points(point_2d, depth, camera_matrix)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_unproject_center_normalize(self, device):
- point_2d = torch.tensor([[0., 0.]]).to(device)
- depth = torch.tensor([[2.]]).to(device)
+ def test_unproject_center_normalize(self, device, dtype):
+ point_2d = torch.tensor([[0., 0.]], device=device, dtype=dtype)
+ depth = torch.tensor([[2.]], device=device, dtype=dtype)
camera_matrix = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device)
- expected = torch.tensor([[0., 0., 2.]]).to(device)
+ ], device=device, dtype=dtype)
+ expected = torch.tensor([[0., 0., 2.]], device=device, dtype=dtype)
actual = kornia.unproject_points(point_2d, depth, camera_matrix, True)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_unproject_and_project(self, device):
- point_2d = torch.tensor([[0., 0.]]).to(device)
- depth = torch.tensor([[2.]]).to(device)
+ def test_unproject_and_project(self, device, dtype):
+ point_2d = torch.tensor([[0., 0.]], device=device, dtype=dtype)
+ depth = torch.tensor([[2.]], device=device, dtype=dtype)
camera_matrix = torch.tensor([
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
point_3d = kornia.unproject_points(point_2d, depth, camera_matrix)
point_2d_hat = kornia.project_points(point_3d, camera_matrix)
- assert_allclose(point_2d, point_2d_hat)
+ assert_allclose(point_2d, point_2d_hat, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
- points_2d = torch.zeros(1, 2).to(device)
- depth = torch.ones(1, 1).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ def test_gradcheck(self, device, dtype):
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ depth = torch.ones(1, 1, 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)
@@ -137,16 +137,16 @@ class TestUnprojectPoints:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(points_2d, depth, camera_matrix):
return kornia.unproject_points(
points_2d, depth, camera_matrix, False)
- points_2d = torch.zeros(1, 2).to(device)
- depth = torch.ones(1, 1).to(device)
- camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
+ points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
+ depth = torch.ones(1, 1, device=device, dtype=dtype)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
actual = op_script(points_2d, depth, camera_matrix)
expected = kornia.unproject_points(points_2d, depth, camera_matrix)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
diff --git a/test/geometry/test_pinhole.py b/test/geometry/test_pinhole.py
index eb7448e3d..7432fa719 100644
--- a/test/geometry/test_pinhole.py
+++ b/test/geometry/test_pinhole.py
@@ -9,39 +9,39 @@ from torch.testing import assert_allclose
class TestPinholeCamera:
- def _create_intrinsics(self, batch_size, fx, fy, cx, cy):
- intrinsics = torch.eye(4)
+ def _create_intrinsics(self, batch_size, fx, fy, cx, cy, device, dtype):
+ intrinsics = torch.eye(4, device=device, dtype=dtype)
intrinsics[..., 0, 0] = fx
intrinsics[..., 1, 1] = fy
intrinsics[..., 0, 2] = cx
intrinsics[..., 1, 2] = cy
return intrinsics.expand(batch_size, -1, -1)
- def _create_extrinsics(self, batch_size, tx, ty, tz):
- extrinsics = torch.eye(4)
+ def _create_extrinsics(self, batch_size, tx, ty, tz, device, dtype):
+ extrinsics = torch.eye(4, device=device, dtype=dtype)
extrinsics[..., 0, -1] = tx
extrinsics[..., 1, -1] = ty
extrinsics[..., 2, -1] = tz
return extrinsics.expand(batch_size, -1, -1)
- def test_smoke(self, device):
- intrinsics = torch.eye(4)[None].to(device)
- extrinsics = torch.eye(4)[None].to(device)
- height = torch.ones(1).to(device)
- width = torch.ones(1).to(device)
+ def test_smoke(self, device, dtype):
+ intrinsics = torch.eye(4, device=device, dtype=dtype)[None]
+ extrinsics = torch.eye(4, device=device, dtype=dtype)[None]
+ height = torch.ones(1, device=device, dtype=dtype)
+ width = torch.ones(1, device=device, dtype=dtype)
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
assert isinstance(pinhole, kornia.PinholeCamera)
- def test_pinhole_camera_attributes(self, device):
+ def test_pinhole_camera_attributes(self, device, dtype):
batch_size = 1
height, width = 4, 6
fx, fy, cx, cy = 1, 2, width / 2, height / 2
tx, ty, tz = 1, 2, 3
- intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy).to(device)
- extrinsics = self._create_extrinsics(batch_size, tx, ty, tz).to(device)
- height = torch.ones(batch_size).to(device) * height
- width = torch.ones(batch_size).to(device) * width
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype)
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
@@ -60,16 +60,16 @@ class TestPinholeCamera:
assert pinhole.rotation_matrix.shape == (batch_size, 3, 3)
assert pinhole.translation_vector.shape == (batch_size, 3, 1)
- def test_pinhole_camera_translation_setters(self, device):
+ def test_pinhole_camera_translation_setters(self, device, dtype):
batch_size = 1
height, width = 4, 6
fx, fy, cx, cy = 1, 2, width / 2, height / 2
tx, ty, tz = 1, 2, 3
- intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy).to(device)
- extrinsics = self._create_extrinsics(batch_size, tx, ty, tz).to(device)
- height = torch.ones(batch_size).to(device) * height
- width = torch.ones(batch_size).to(device) * width
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype)
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
@@ -95,16 +95,16 @@ class TestPinholeCamera:
assert pinhole.ty.item() == 0.
assert pinhole.tz.item() == 0.
- def test_pinhole_camera_attributes_batch2(self, device):
+ def test_pinhole_camera_attributes_batch2(self, device, dtype):
batch_size = 2
height, width = 4, 6
fx, fy, cx, cy = 1, 2, width / 2, height / 2
tx, ty, tz = 1, 2, 3
- intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy).to(device)
- extrinsics = self._create_extrinsics(batch_size, tx, ty, tz).to(device)
- height = torch.ones(batch_size).to(device) * height
- width = torch.ones(batch_size).to(device) * width
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype)
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
@@ -123,53 +123,53 @@ class TestPinholeCamera:
assert pinhole.rotation_matrix.shape == (batch_size, 3, 3)
assert pinhole.translation_vector.shape == (batch_size, 3, 1)
- def test_pinhole_camera_scale(self, device):
+ def test_pinhole_camera_scale(self, device, dtype):
batch_size = 2
height, width = 4, 6
fx, fy, cx, cy = 1, 2, width / 2, height / 2
tx, ty, tz = 1, 2, 3
scale_val = 2.0
- intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy).to(device)
- extrinsics = self._create_extrinsics(batch_size, tx, ty, tz).to(device)
- height = torch.ones(batch_size).to(device) * height
- width = torch.ones(batch_size).to(device) * width
- scale_factor = torch.ones(batch_size).to(device) * scale_val
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype)
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
+ scale_factor = torch.ones(batch_size, device=device, dtype=dtype) * scale_val
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
pinhole_scale = pinhole.scale(scale_factor)
assert_allclose(
pinhole_scale.intrinsics[..., 0, 0],
- pinhole.intrinsics[..., 0, 0] * scale_val) # fx
+ pinhole.intrinsics[..., 0, 0] * scale_val, atol=1e-4, rtol=1e-4) # fx
assert_allclose(
pinhole_scale.intrinsics[..., 1, 1],
- pinhole.intrinsics[..., 1, 1] * scale_val) # fy
+ pinhole.intrinsics[..., 1, 1] * scale_val, atol=1e-4, rtol=1e-4) # fy
assert_allclose(
pinhole_scale.intrinsics[..., 0, 2],
- pinhole.intrinsics[..., 0, 2] * scale_val) # cx
+ pinhole.intrinsics[..., 0, 2] * scale_val, atol=1e-4, rtol=1e-4) # cx
assert_allclose(
pinhole_scale.intrinsics[..., 1, 2],
- pinhole.intrinsics[..., 1, 2] * scale_val) # cy
+ pinhole.intrinsics[..., 1, 2] * scale_val, atol=1e-4, rtol=1e-4) # cy
assert_allclose(
pinhole_scale.height,
- pinhole.height * scale_val)
+ pinhole.height * scale_val, atol=1e-4, rtol=1e-4)
assert_allclose(
pinhole_scale.width,
- pinhole.width * scale_val)
+ pinhole.width * scale_val, atol=1e-4, rtol=1e-4)
- def test_pinhole_camera_scale_inplace(self, device):
+ def test_pinhole_camera_scale_inplace(self, device, dtype):
batch_size = 2
height, width = 4, 6
fx, fy, cx, cy = 1, 2, width / 2, height / 2
tx, ty, tz = 1, 2, 3
scale_val = 2.0
- intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy).to(device)
- extrinsics = self._create_extrinsics(batch_size, tx, ty, tz).to(device)
- height = torch.ones(batch_size).to(device) * height
- width = torch.ones(batch_size).to(device) * width
- scale_factor = torch.ones(batch_size).to(device) * scale_val
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics(batch_size, tx, ty, tz, device=device, dtype=dtype)
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
+ scale_factor = torch.ones(batch_size, device=device, dtype=dtype) * scale_val
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
pinhole_scale = pinhole.clone()
@@ -177,28 +177,28 @@ class TestPinholeCamera:
assert_allclose(
pinhole_scale.intrinsics[..., 0, 0],
- pinhole.intrinsics[..., 0, 0] * scale_val) # fx
+ pinhole.intrinsics[..., 0, 0] * scale_val, atol=1e-4, rtol=1e-4) # fx
assert_allclose(
pinhole_scale.intrinsics[..., 1, 1],
- pinhole.intrinsics[..., 1, 1] * scale_val) # fy
+ pinhole.intrinsics[..., 1, 1] * scale_val, atol=1e-4, rtol=1e-4) # fy
assert_allclose(
pinhole_scale.intrinsics[..., 0, 2],
- pinhole.intrinsics[..., 0, 2] * scale_val) # cx
+ pinhole.intrinsics[..., 0, 2] * scale_val, atol=1e-4, rtol=1e-4) # cx
assert_allclose(
pinhole_scale.intrinsics[..., 1, 2],
- pinhole.intrinsics[..., 1, 2] * scale_val) # cy
+ pinhole.intrinsics[..., 1, 2] * scale_val, atol=1e-4, rtol=1e-4) # cy
assert_allclose(
- pinhole_scale.height, pinhole.height * scale_val)
+ pinhole_scale.height, pinhole.height * scale_val, atol=1e-4, rtol=1e-4)
assert_allclose(
- pinhole_scale.width, pinhole.width * scale_val)
+ pinhole_scale.width, pinhole.width * scale_val, atol=1e-4, rtol=1e-4)
'''@pytest.mark.parametrize("batch_size", [1, 2, 5, 6])
def test_scale_pinhole(batch_size, device_type):
# generate input data
device = torch.device(device_type)
- pinholes = torch.rand(batch_size, 12).to(device)
- scales = torch.rand(batch_size).to(device)
+ pinholes = torch.rand(batch_size, 12, device=device, dtype=dtype)
+ scales = torch.rand(batch_size, device=device, dtype=dtype)
pinholes_scale = kornia.scale_pinhole(pinholes, scales)
assert_allclose(
@@ -291,7 +291,7 @@ def test_homography_i_H_ref(batch_size, device_type):
pinhole_ref = utils.create_pinhole(
fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
- pinhole_ref = pinhole_ref.repeat(batch_size, 1).to(device)
+ pinhole_ref = pinhole_ref.repeat(batch_size, 1, device=device, dtype=dtype)
pinhole_i = utils.create_pinhole(
fx,
@@ -306,7 +306,7 @@ def test_homography_i_H_ref(batch_size, device_type):
tx + offset_x,
ty,
tz)
- pinhole_i = pinhole_i.repeat(batch_size, 1).to(device)
+ pinhole_i = pinhole_i.repeat(batch_size, 1, device=device, dtype=dtype)
# compute homography from ref to i
i_H_ref = kornia.homography_i_H_ref(pinhole_i, pinhole_ref) + eps
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index 244453056..3ad72ad1c 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -8,104 +8,104 @@ from torch.testing import assert_allclose
class TestResize:
- def test_smoke(self, device):
- inp = torch.rand(1, 3, 3, 4).to(device)
+ def test_smoke(self, device, dtype):
+ inp = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
out = kornia.resize(inp, (3, 4))
- assert_allclose(inp, out)
+ assert_allclose(inp, out, atol=1e-4, rtol=1e-4)
- def test_upsize(self, device):
- inp = torch.rand(1, 3, 3, 4).to(device)
+ def test_upsize(self, device, dtype):
+ inp = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
out = kornia.resize(inp, (6, 8))
assert out.shape == (1, 3, 6, 8)
- def test_downsize(self, device):
- inp = torch.rand(1, 3, 5, 2).to(device)
+ def test_downsize(self, device, dtype):
+ inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
out = kornia.resize(inp, (3, 1))
assert out.shape == (1, 3, 3, 1)
- def test_one_param(self, device):
- inp = torch.rand(1, 3, 5, 2).to(device)
+ def test_one_param(self, device, dtype):
+ inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
out = kornia.resize(inp, 10)
assert out.shape == (1, 3, 25, 10)
- def test_one_param_long(self, device):
- inp = torch.rand(1, 3, 5, 2).to(device)
+ def test_one_param_long(self, device, dtype):
+ inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
out = kornia.resize(inp, 10, side="long")
assert out.shape == (1, 3, 10, 4)
- def test_one_param_vert(self, device):
- inp = torch.rand(1, 3, 5, 2).to(device)
+ def test_one_param_vert(self, device, dtype):
+ inp = torch.rand(1, 3, 5, 2, device=device, dtype=dtype)
out = kornia.resize(inp, 10, side="vert")
assert out.shape == (1, 3, 10, 4)
- def test_one_param_horz(self, device):
- inp = torch.rand(1, 3, 2, 5).to(device)
+ def test_one_param_horz(self, device, dtype):
+ inp = torch.rand(1, 3, 2, 5, device=device, dtype=dtype)
out = kornia.resize(inp, 10, side="horz")
assert out.shape == (1, 3, 4, 10)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# test parameters
new_size = 4
- input = torch.rand(1, 2, 3, 4).to(device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.Resize(new_size), (input, ), raise_exception=True)
class TestRescale:
- def test_smoke(self, device):
- input = torch.rand(1, 3, 3, 4, device=device)
+ def test_smoke(self, device, dtype):
+ input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
output = kornia.rescale(input, (1.0, 1.0))
- assert_allclose(input, output)
+ assert_allclose(input, output, atol=1e-4, rtol=1e-4)
- def test_upsize(self, device):
- input = torch.rand(1, 3, 3, 4, device=device)
+ def test_upsize(self, device, dtype):
+ input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
output = kornia.rescale(input, (3.0, 2.0))
assert output.shape == (1, 3, 9, 8)
- def test_downsize(self, device):
- input = torch.rand(1, 3, 9, 8, device=device)
+ def test_downsize(self, device, dtype):
+ input = torch.rand(1, 3, 9, 8, device=device, dtype=dtype)
output = kornia.rescale(input, (1.0 / 3.0, 1.0 / 2.0))
assert output.shape == (1, 3, 3, 4)
- def test_one_param(self, device):
- input = torch.rand(1, 3, 3, 4, device=device)
+ def test_one_param(self, device, dtype):
+ input = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
output = kornia.rescale(input, 2.0)
assert output.shape == (1, 3, 6, 8)
- def test_gradcheck(self, device):
- input = torch.rand(1, 2, 3, 4).to(device)
+ def test_gradcheck(self, device, dtype):
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input)
assert gradcheck(kornia.Rescale(2.0), (input, ), raise_exception=True)
class TestRotate:
- def test_angle90(self, device):
+ def test_angle90(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor([[
[0., 0.],
[4., 6.],
[3., 5.],
[0., 0.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- angle = torch.tensor([90.]).to(device)
+ angle = torch.tensor([90.], device=device, dtype=dtype)
transform = kornia.Rotate(angle, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_angle90_batch2(self, device):
+ def test_angle90_batch2(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[0., 0.],
[4., 6.],
@@ -116,20 +116,20 @@ class TestRotate:
[5., 3.],
[6., 4.],
[0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
# prepare transformation
- angle = torch.tensor([90., -90.]).to(device)
+ angle = torch.tensor([90., -90.], device=device, dtype=dtype)
transform = kornia.Rotate(angle, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_angle90_batch2_broadcast(self, device):
+ def test_angle90_batch2_broadcast(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[0., 0.],
[4., 6.],
@@ -140,62 +140,62 @@ class TestRotate:
[4., 6.],
[3., 5.],
[0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
# prepare transformation
- angle = torch.tensor([90.]).to(device)
+ angle = torch.tensor([90.], device=device, dtype=dtype)
transform = kornia.Rotate(angle, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# test parameters
- angle = torch.tensor([90.]).to(device)
+ angle = torch.tensor([90.], device=device, dtype=dtype)
angle = utils.tensor_to_gradcheck_var(
angle, requires_grad=False) # to var
# evaluate function gradient
- input = torch.rand(1, 2, 3, 4).to(device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.rotate, (input, angle,), raise_exception=True)
@pytest.mark.skip('Need deep look into it since crashes everywhere.')
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- angle = torch.tensor([90.]).to(device)
+ def test_jit(self, device, dtype):
+ angle = torch.tensor([90.], device=device, dtype=dtype)
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
rot = kornia.Rotate(angle)
rot_traced = torch.jit.trace(kornia.Rotate(angle), img)
assert_allclose(rot(img), rot_traced(img))
class TestTranslate:
- def test_dxdy(self, device):
+ def test_dxdy(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor([[
[0., 1.],
[0., 3.],
[0., 5.],
[0., 7.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- translation = torch.tensor([[1., 0.]]).to(device)
+ translation = torch.tensor([[1., 0.]], device=device, dtype=dtype)
transform = kornia.Translate(translation, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_dxdy_batch(self, device):
+ def test_dxdy_batch(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[0., 1.],
[0., 3.],
@@ -206,20 +206,20 @@ class TestTranslate:
[0., 1.],
[0., 3.],
[0., 5.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
# prepare transformation
- translation = torch.tensor([[1., 0.], [1., 1.]]).to(device)
+ translation = torch.tensor([[1., 0.], [1., 1.]], device=device, dtype=dtype)
transform = kornia.Translate(translation, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_dxdy_batch_broadcast(self, device):
+ def test_dxdy_batch_broadcast(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 2.],
[3., 4.],
[5., 6.],
[7., 8.],
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[0., 1.],
[0., 3.],
@@ -230,176 +230,176 @@ class TestTranslate:
[0., 3.],
[0., 5.],
[0., 7.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
# prepare transformation
- translation = torch.tensor([[1., 0.]]).to(device)
+ translation = torch.tensor([[1., 0.]], device=device, dtype=dtype)
transform = kornia.Translate(translation, align_corners=True)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# test parameters
- translation = torch.tensor([[1., 0.]]).to(device)
+ translation = torch.tensor([[1., 0.]], device=device, dtype=dtype)
translation = utils.tensor_to_gradcheck_var(
translation, requires_grad=False) # to var
# evaluate function gradient
- input = torch.rand(1, 2, 3, 4).to(device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.translate, (input, translation,),
raise_exception=True)
@pytest.mark.skip('Need deep look into it since crashes everywhere.')
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- translation = torch.tensor([[1., 0.]]).to(device)
+ def test_jit(self, device, dtype):
+ translation = torch.tensor([[1., 0.]], device=device, dtype=dtype)
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
trans = kornia.Translate(translation)
trans_traced = torch.jit.trace(kornia.Translate(translation), img)
- assert_allclose(trans(img), trans_traced(img))
+ assert_allclose(trans(img), trans_traced(img), atol=1e-4, rtol=1e-4)
class TestScale:
- def test_scale_factor_2(self, device):
+ def test_scale_factor_2(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[0., 0., 0., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 0., 0., 0.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- scale_factor = torch.tensor([[2., 2.]]).to(device)
+ scale_factor = torch.tensor([[2., 2.]], device=device, dtype=dtype)
transform = kornia.Scale(scale_factor)
- assert_allclose(transform(inp).sum().item(), 12.25)
+ assert_allclose(transform(inp).sum().item(), 12.25, atol=1e-4, rtol=1e-4)
- def test_scale_factor_05(self, device):
+ def test_scale_factor_05(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor([[
[0., 0., 0., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 0., 0., 0.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- scale_factor = torch.tensor([[0.5, 0.5]]).to(device)
+ scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
transform = kornia.Scale(scale_factor)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_scale_factor_05_batch2(self, device):
+ def test_scale_factor_05_batch2(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[
[0., 0., 0., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 0., 0., 0.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- scale_factor = torch.tensor([[0.5, 0.5]]).to(device)
+ scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
transform = kornia.Scale(scale_factor)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_scale_factor_05_batch2_broadcast(self, device):
+ def test_scale_factor_05_batch2_broadcast(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[
[0., 0., 0., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 0., 0., 0.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# prepare transformation
- scale_factor = torch.tensor([[0.5, 0.5]]).to(device)
+ scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
transform = kornia.Scale(scale_factor)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# test parameters
- scale_factor = torch.tensor([[0.5, 0.5]]).to(device)
+ scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
scale_factor = utils.tensor_to_gradcheck_var(
scale_factor, requires_grad=False) # to var
# evaluate function gradient
- input = torch.rand(1, 2, 3, 4).to(device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.scale, (input, scale_factor,),
raise_exception=True)
@pytest.mark.skip('Need deep look into it since crashes everywhere.')
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- scale_factor = torch.tensor([[0.5, 0.5]]).to(device)
+ def test_jit(self, device, dtype):
+ scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
trans = kornia.Scale(scale_factor)
trans_traced = torch.jit.trace(kornia.Scale(scale_factor), img)
- assert_allclose(trans(img), trans_traced(img))
+ assert_allclose(trans(img), trans_traced(img), atol=1e-4, rtol=1e-4)
class TestShear:
- def test_shear_x(self, device):
+ def test_shear_x(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor(
[[[0.75, 1., 1., 1.],
[0.25, 1., 1., 1.],
[0., 0.75, 1., 1.],
- [0., 0.25, 1., 1.]]]).to(device)
+ [0., 0.25, 1., 1.]]], device=device, dtype=dtype)
# prepare transformation
- shear = torch.tensor([[0.5, 0.0]]).to(device)
+ shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
transform = kornia.Shear(shear)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_shear_y(self, device):
+ def test_shear_y(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor(
[[[0.75, 0.25, 0., 0.],
[1., 1., 0.75, 0.25],
[1., 1., 1., 1.],
- [1., 1., 1., 1.]]]).to(device)
+ [1., 1., 1., 1.]]], device=device, dtype=dtype)
# prepare transformation
- shear = torch.tensor([[0.0, 0.5]]).to(device)
+ shear = torch.tensor([[0.0, 0.5]], device=device, dtype=dtype)
transform = kornia.Shear(shear)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_shear_batch2(self, device):
+ def test_shear_batch2(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor(
[[[[0.75, 1., 1., 1.],
@@ -409,53 +409,53 @@ class TestShear:
[[[0.75, 0.25, 0., 0.],
[1., 1., 0.75, 0.25],
[1., 1., 1., 1.],
- [1., 1., 1., 1.]]]]).to(device)
+ [1., 1., 1., 1.]]]], device=device, dtype=dtype)
# prepare transformation
- shear = torch.tensor([[0.5, 0.0], [0.0, 0.5]]).to(device)
+ shear = torch.tensor([[0.5, 0.0], [0.0, 0.5]], device=device, dtype=dtype)
transform = kornia.Shear(shear)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_shear_batch2_broadcast(self, device):
+ def test_shear_batch2_broadcast(self, device, dtype):
# prepare input data
inp = torch.tensor([[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
- ]]).repeat(2, 1, 1, 1).to(device)
+ ]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[0.75, 1., 1., 1.],
[0.25, 1., 1., 1.],
[0., 0.75, 1., 1.],
- [0., 0.25, 1., 1.]]]]).to(device)
+ [0., 0.25, 1., 1.]]]], device=device, dtype=dtype)
# prepare transformation
- shear = torch.tensor([[0.5, 0.0]]).to(device)
+ shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
transform = kornia.Shear(shear)
- assert_allclose(transform(inp), expected)
+ assert_allclose(transform(inp), expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# test parameters
- shear = torch.tensor([[0.5, 0.0]]).to(device)
+ shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
shear = utils.tensor_to_gradcheck_var(
shear, requires_grad=False) # to var
# evaluate function gradient
- input = torch.rand(1, 2, 3, 4).to(device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.shear, (input, shear,), raise_exception=True)
@pytest.mark.skip('Need deep look into it since crashes everywhere.')
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- shear = torch.tensor([[0.5, 0.0]]).to(device)
+ def test_jit(self, device, dtype):
+ shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
trans = kornia.Shear(shear)
trans_traced = torch.jit.trace(kornia.Shear(shear), img)
- assert_allclose(trans(img), trans_traced(img))
+ assert_allclose(trans(img), trans_traced(img), atol=1e-4, rtol=1e-4)
class TestAffine2d:
@@ -463,13 +463,13 @@ class TestAffine2d:
with pytest.raises(RuntimeError):
kornia.Affine()
- def test_affine_batch_size_mismatch(self, device):
+ def test_affine_batch_size_mismatch(self, device, dtype):
with pytest.raises(RuntimeError):
- angle = torch.rand(1, device=device)
- translation = torch.rand(2, 2, device=device)
+ angle = torch.rand(1, device=device, dtype=dtype)
+ translation = torch.rand(2, 2, device=device, dtype=dtype)
kornia.Affine(angle, translation)
- def test_affine_rotate(self, device):
+ def test_affine_rotate(self, device, dtype):
# TODO: Remove when #666 is implemented
if device.type == 'cuda':
pytest.skip(
@@ -477,15 +477,15 @@ class TestAffine2d:
"See https://github.com/kornia/kornia/issues/666"
)
torch.manual_seed(0)
- angle = torch.rand(1, device=device) * 90.0
- input = torch.rand(1, 2, 3, 4, device=device)
+ angle = torch.rand(1, device=device, dtype=dtype) * 90.0
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- transform = kornia.Affine(angle=angle).to(device)
+ transform = kornia.Affine(angle=angle).to(device=device, dtype=dtype)
actual = transform(input)
expected = kornia.rotate(input, angle)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_affine_translate(self, device):
+ def test_affine_translate(self, device, dtype):
# TODO: Remove when #666 is implemented
if device.type == 'cuda':
pytest.skip(
@@ -493,15 +493,15 @@ class TestAffine2d:
"See https://github.com/kornia/kornia/issues/666"
)
torch.manual_seed(0)
- translation = torch.rand(1, 2, device=device) * 2.0
- input = torch.rand(1, 2, 3, 4, device=device)
+ translation = torch.rand(1, 2, device=device, dtype=dtype) * 2.0
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- transform = kornia.Affine(translation=translation).to(device)
+ transform = kornia.Affine(translation=translation).to(device=device, dtype=dtype)
actual = transform(input)
expected = kornia.translate(input, translation)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_affine_scale(self, device):
+ def test_affine_scale(self, device, dtype):
# TODO: Remove when #666 is implemented
if device.type == 'cuda':
pytest.skip(
@@ -509,30 +509,30 @@ class TestAffine2d:
"See https://github.com/kornia/kornia/issues/666"
)
torch.manual_seed(0)
- _scale_factor = torch.rand(1, device=device) * 2.0
+ _scale_factor = torch.rand(1, device=device, dtype=dtype) * 2.0
scale_factor = torch.stack([_scale_factor, _scale_factor], dim=1)
- input = torch.rand(1, 2, 3, 4, device=device)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- transform = kornia.Affine(scale_factor=scale_factor).to(device)
+ transform = kornia.Affine(scale_factor=scale_factor).to(device=device, dtype=dtype)
actual = transform(input)
expected = kornia.scale(input, scale_factor)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.skip(
"_compute_shear_matrix and get_affine_matrix2d yield different results. "
"See https://github.com/kornia/kornia/issues/629 for details."
)
- def test_affine_shear(self, device):
+ def test_affine_shear(self, device, dtype):
torch.manual_seed(0)
- shear = torch.rand(1, 2, device=device)
- input = torch.rand(1, 2, 3, 4, device=device)
+ shear = torch.rand(1, 2, device=device, dtype=dtype)
+ input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- transform = kornia.Affine(shear=shear).to(device)
+ transform = kornia.Affine(shear=shear, device=device, dtype=dtype)
actual = transform(input)
expected = kornia.shear(input, shear)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_affine_rotate_translate(self, device):
+ def test_affine_rotate_translate(self, device, dtype):
# TODO: Remove when #666 is implemented
if device.type == 'cuda':
pytest.skip(
@@ -542,21 +542,24 @@ class TestAffine2d:
batch_size = 2
input = torch.tensor(
- [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]], device=device
+ [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
+ device=device, dtype=dtype
).repeat(batch_size, 1, 1, 1)
- angle = torch.tensor(180.0, device=device).repeat(batch_size)
- translation = torch.tensor([1.0, 0.0], device=device).repeat(batch_size, 1)
+ angle = torch.tensor(180.0, device=device, dtype=dtype).repeat(batch_size)
+ translation = torch.tensor([1.0, 0.0], device=device, dtype=dtype).repeat(batch_size, 1)
expected = torch.tensor(
- [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]]], device=device,
+ [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]]],
+ device=device, dtype=dtype
).repeat(batch_size, 1, 1, 1)
- transform = kornia.Affine(angle=angle, translation=translation, align_corners=True).to(device)
+ transform = kornia.Affine(
+ angle=angle, translation=translation, align_corners=True).to(device=device, dtype=dtype)
actual = transform(input)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
- def test_compose_affine_matrix_3x3(self, device):
+ def test_compose_affine_matrix_3x3(self, device, dtype):
""" To get parameters:
import torchvision as tv
from PIL import Image
@@ -602,4 +605,4 @@ class TestAffine2d:
T([math.radians(shear[0])]).view(-1, 1),
T([math.radians(shear[1])]).view(-1, 1))
matrix_kornia = matrix_kornia.inverse()[0, :2].detach().cpu()
- assert_allclose(matrix_kornia, matrix_expected)
+ assert_allclose(matrix_kornia, matrix_expected, atol=1e-4, rtol=1e-4)
diff --git a/test/geometry/transform/test_flip.py b/test/geometry/transform/test_flip.py
index 8fd846022..032417774 100644
--- a/test/geometry/transform/test_flip.py
+++ b/test/geometry/transform/test_flip.py
@@ -9,43 +9,43 @@ from torch.autograd import gradcheck
class TestVflip:
- def smoke_test(self, device):
+ def smoke_test(self, device, dtype):
f = kornia.Vflip()
repr = "Vflip()"
assert str(f) == repr
- def test_vflip(self, device):
+ def test_vflip(self, device, dtype):
f = kornia.Vflip()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor([[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]).to(device) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_vflip(self, device):
+ def test_batch_vflip(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
- input = input.repeat(2, 1, 1).to(device) # 2 x 3 x 3
+ input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.Vflip()
expected = torch.tensor([[[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]]).to(device) # 1 x 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 1 x 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
@@ -53,7 +53,7 @@ class TestVflip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -61,13 +61,13 @@ class TestVflip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [5., 5., 0.]]).to(device) # 3 x 3
+ [5., 5., 0.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[5., 5., 0.],
[0., 0., 0.],
- [0., 0., 0.]]]).to(device) # 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -75,11 +75,11 @@ class TestVflip:
assert_allclose(actual, expected)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
@@ -88,43 +88,43 @@ class TestVflip:
class TestHflip:
- def smoke_test(self, device):
+ def smoke_test(self, device, dtype):
f = kornia.Hflip()
repr = "Hflip()"
assert str(f) == repr
- def test_hflip(self, device):
+ def test_hflip(self, device, dtype):
f = kornia.Hflip()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]).to(device) # 3 x 3
+ [1., 1., 0.]], device=device, dtype=dtype) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_hflip(self, device):
+ def test_batch_hflip(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 1 x 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 1 x 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.Hflip()
expected = torch.tensor([[[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]]).to(device) # 3 x 3
+ [1., 1., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
@@ -132,7 +132,7 @@ class TestHflip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -140,13 +140,13 @@ class TestHflip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]).to(device) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 0., 0.],
[0., 5., 5.],
- [0., 0., 0.]]]).to(device) # 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -154,11 +154,11 @@ class TestHflip:
assert_allclose(actual, expected)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
@@ -167,43 +167,43 @@ class TestHflip:
class TestRot180:
- def smoke_test(self, device):
+ def smoke_test(self, device, dtype):
f = kornia.Rot180()
repr = "Rot180()"
assert str(f) == repr
- def test_rot180(self, device):
+ def test_rot180(self, device, dtype):
f = kornia.Rot180()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor([[1., 1., 0.],
[0., 0., 0.],
- [0., 0., 0.]]).to(device) # 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_rot180(self, device):
+ def test_batch_rot180(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.Rot180()
expected = torch.tensor([[1., 1., 0.],
[0., 0., 0.],
- [0., 0., 0.]]).to(device) # 1 x 3 x 3
+ [0., 0., 0.]], device=device, dtype=dtype) # 1 x 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
@@ -211,7 +211,7 @@ class TestRot180:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -219,13 +219,13 @@ class TestRot180:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [5., 5., 0.]]).to(device) # 3 x 3
+ [5., 5., 0.]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 5., 5.],
[0., 0., 0.],
- [0., 0., 0.]]]).to(device) # 3 x 3
+ [0., 0., 0.]]], device=device, dtype=dtype) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -233,11 +233,11 @@ class TestRot180:
assert_allclose(actual, expected)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).to(device) # 3 x 3
+ [0., 1., 1.]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index b07772987..f1044dc88 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -9,16 +9,16 @@ from torch.testing import assert_allclose
@pytest.mark.parametrize("batch_shape", [(1, 1, 7, 32), (2, 3, 16, 31)])
-def test_warp_perspective_rotation(batch_shape, device):
+def test_warp_perspective_rotation(batch_shape, device, dtype):
# generate input data
batch_size, channels, height, width = batch_shape
- alpha = 0.5 * kornia.pi * torch.ones(batch_size).to(device) # 90 deg rotation
+ alpha = 0.5 * kornia.pi * torch.ones(batch_size, device=device, dtype=dtype) # 90 deg rotation
# create data patch
- patch = torch.rand(batch_shape).to(device)
+ patch = torch.rand(batch_shape, device=device, dtype=dtype)
# create transformation (rotation)
- M = torch.eye(3, device=device).repeat(batch_size, 1, 1) # Bx3x3
+ M = torch.eye(3, device=device, dtype=dtype).repeat(batch_size, 1, 1) # Bx3x3
M[:, 0, 0] = torch.cos(alpha)
M[:, 0, 1] = -torch.sin(alpha)
M[:, 1, 0] = torch.sin(alpha)
@@ -38,7 +38,7 @@ def test_warp_perspective_rotation(batch_shape, device):
dsize=(height, width), align_corners=True)
assert_allclose(mask_warped_inv * patch,
- mask_warped_inv * patch_warped_inv)
+ mask_warped_inv * patch_warped_inv, rtol=1e-4, atol=1e-4)
def test_warp_perspective_gradcheck(device, dtype):
@@ -49,14 +49,14 @@ def test_warp_perspective_gradcheck(device, dtype):
@pytest.mark.parametrize("batch_size", [1, 2, 5])
-def test_get_perspective_transform(batch_size, device):
+def test_get_perspective_transform(batch_size, device, dtype):
# generate input data
h_max, w_max = 64, 32 # height, width
- h = torch.ceil(h_max * torch.rand(batch_size)).to(device)
- w = torch.ceil(w_max * torch.rand(batch_size)).to(device)
+ h = torch.ceil(h_max * torch.rand(batch_size, device=device, dtype=dtype))
+ w = torch.ceil(w_max * torch.rand(batch_size, device=device, dtype=dtype))
- norm = torch.rand(batch_size, 4, 2).to(device)
- points_src = torch.zeros_like(norm)
+ norm = torch.rand(batch_size, 4, 2, device=device, dtype=dtype)
+ points_src = torch.zeros_like(norm, device=device, dtype=dtype)
points_src[:, 1, 0] = h
points_src[:, 2, 1] = w
points_src[:, 3, 0] = h
@@ -67,7 +67,7 @@ def test_get_perspective_transform(batch_size, device):
dst_homo_src = kornia.get_perspective_transform(points_src, points_dst)
assert_allclose(
- kornia.transform_points(dst_homo_src, points_src), points_dst)
+ kornia.transform_points(dst_homo_src, points_src), points_dst, rtol=1e-4, atol=1e-4)
# compute gradient check
points_src = utils.tensor_to_gradcheck_var(points_src) # to var
@@ -81,11 +81,11 @@ def test_get_perspective_transform(batch_size, device):
@pytest.mark.parametrize("batch_size", [1, 2, 5])
-def test_rotation_matrix2d(batch_size, device):
+def test_rotation_matrix2d(batch_size, device, dtype):
# generate input data
- center_base = torch.zeros(batch_size, 2).to(device)
- angle_base = torch.ones(batch_size).to(device)
- scale_base = torch.ones(batch_size, 2).to(device)
+ center_base = torch.zeros(batch_size, 2, device=device, dtype=dtype)
+ angle_base = torch.ones(batch_size, device=device, dtype=dtype)
+ scale_base = torch.ones(batch_size, 2, device=device, dtype=dtype)
# 90 deg rotation
center = center_base
@@ -94,10 +94,10 @@ def test_rotation_matrix2d(batch_size, device):
M = kornia.get_rotation_matrix2d(center, angle, scale)
for i in range(batch_size):
- assert_allclose(M[i, 0, 0].item(), 0.0)
- assert_allclose(M[i, 0, 1].item(), 1.0)
- assert_allclose(M[i, 1, 0].item(), -1.0)
- assert_allclose(M[i, 1, 1].item(), 0.0)
+ assert_allclose(M[i, 0, 0].item(), 0.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 0, 1].item(), 1.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 1, 0].item(), -1.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 1, 1].item(), 0.0, rtol=1e-4, atol=1e-4)
# 90 deg rotation + 2x scale
center = center_base
@@ -106,10 +106,10 @@ def test_rotation_matrix2d(batch_size, device):
M = kornia.get_rotation_matrix2d(center, angle, scale)
for i in range(batch_size):
- assert_allclose(M[i, 0, 0].item(), 0.0)
- assert_allclose(M[i, 0, 1].item(), 2.0)
- assert_allclose(M[i, 1, 0].item(), -2.0)
- assert_allclose(M[i, 1, 1].item(), 0.0)
+ assert_allclose(M[i, 0, 0].item(), 0.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 0, 1].item(), 2.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 1, 0].item(), -2.0, rtol=1e-4, atol=1e-4)
+ assert_allclose(M[i, 1, 1].item(), 0.0, rtol=1e-4, atol=1e-4)
# 45 deg rotation
center = center_base
@@ -135,28 +135,28 @@ def test_rotation_matrix2d(batch_size, device):
class TestWarpPerspective:
@pytest.mark.parametrize("batch_size", [1, 5])
@pytest.mark.parametrize("channels", [1, 5])
- def test_crop(self, device, batch_size, channels):
+ def test_crop(self, batch_size, channels, device, dtype):
# generate input data
src_h, src_w = 3, 3
dst_h, dst_w = 3, 3
# [x, y] origin
# top-left, top-right, bottom-right, bottom-left
- points_src = torch.FloatTensor([[
+ points_src = torch.tensor([[
[0, 0],
[0, src_w - 1],
[src_h - 1, src_w - 1],
[src_h - 1, 0],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# [x, y] destination
# top-left, top-right, bottom-right, bottom-left
- points_dst = torch.FloatTensor([[
+ points_dst = torch.tensor([[
[0, 0],
[0, dst_w - 1],
[dst_h - 1, dst_w - 1],
[dst_h - 1, 0],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# compute transformation between points
dst_trans_src = kornia.get_perspective_transform(points_src,
@@ -164,101 +164,101 @@ class TestWarpPerspective:
batch_size, -1, -1)
# warp tensor
- patch = torch.FloatTensor([[[
+ patch = torch.tensor([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
- ]]]).expand(batch_size, channels, -1, -1).to(device)
+ ]]], device=device, dtype=dtype).expand(batch_size, channels, -1, -1)
expected = torch.tensor(
[[[[0.2500, 0.9167, 1.5833],
[2.1667, 5.1667, 6.5000],
- [4.8333, 10.5000, 11.8333]]]]).to(device)
+ [4.8333, 10.5000, 11.8333]]]], device=device, dtype=dtype)
# warp and assert
patch_warped = kornia.warp_perspective(patch, dst_trans_src,
(dst_h, dst_w))
- assert_allclose(patch_warped, expected)
+ assert_allclose(patch_warped, expected, rtol=1e-4, atol=1e-4)
# check jit
patch_warped_jit = kornia.jit.warp_perspective(patch, dst_trans_src,
(dst_h, dst_w))
- assert_allclose(patch_warped, patch_warped_jit)
+ assert_allclose(patch_warped, patch_warped_jit, rtol=1e-4, atol=1e-4)
- def test_crop_center_resize(self, device):
+ def test_crop_center_resize(self, device, dtype):
# generate input data
dst_h, dst_w = 4, 4
# [x, y] origin
# top-left, top-right, bottom-right, bottom-left
- points_src = torch.FloatTensor([[
+ points_src = torch.tensor([[
[1, 1],
[1, 2],
[2, 2],
[2, 1],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# [x, y] destination
# top-left, top-right, bottom-right, bottom-left
- points_dst = torch.FloatTensor([[
+ points_dst = torch.tensor([[
[0, 0],
[0, dst_w - 1],
[dst_h - 1, dst_w - 1],
[dst_h - 1, 0],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# compute transformation between points
dst_trans_src = kornia.get_perspective_transform(points_src, points_dst)
# warp tensor
- patch = torch.FloatTensor([[[
+ patch = torch.tensor([[[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
expected = torch.tensor(
[[[[5.1667, 5.6111, 6.0556, 6.5000],
[6.9444, 7.3889, 7.8333, 8.2778],
[8.7222, 9.1667, 9.6111, 10.0556],
- [10.5000, 10.9444, 11.3889, 11.8333]]]]).to(device)
+ [10.5000, 10.9444, 11.3889, 11.8333]]]], device=device, dtype=dtype)
# warp and assert
patch_warped = kornia.warp_perspective(patch, dst_trans_src,
(dst_h, dst_w))
- assert_allclose(patch_warped, expected)
+ assert_allclose(patch_warped, expected, rtol=1e-4, atol=1e-4)
# check jit
patch_warped_jit = kornia.jit.warp_perspective(patch, dst_trans_src,
(dst_h, dst_w))
- assert_allclose(patch_warped, patch_warped_jit)
+ assert_allclose(patch_warped, patch_warped_jit, rtol=1e-4, atol=1e-4)
class TestWarpAffine:
- def test_smoke(self, device):
+ def test_smoke(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
- aff_ab = torch.eye(2, 3)[None].to(device) # 1x2x3
- img_b = torch.rand(batch_size, channels, height, width).to(device)
+ aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None] # 1x2x3
+ img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img_a = kornia.warp_affine(img_b, aff_ab, (height, width))
assert img_b.shape == img_a.shape
@pytest.mark.parametrize("batch_size", [1, 2, 5])
- def test_translation(self, device, batch_size):
+ def test_translation(self, batch_size, device, dtype):
offset = 1.
channels, height, width = 1, 3, 4
- aff_ab = torch.eye(2, 3).repeat(batch_size, 1, 1).to(device) # Bx2x3
+ aff_ab = torch.eye(2, 3, device=device, dtype=dtype).repeat(batch_size, 1, 1) # Bx2x3
aff_ab[..., -1] += offset
- img_b = torch.arange(float(height * width)).view(
- 1, channels, height, width).repeat(batch_size, 1, 1, 1).to(device)
+ img_b = torch.arange(float(height * width), device=device, dtype=dtype).view(
+ 1, channels, height, width).repeat(batch_size, 1, 1, 1)
img_a = kornia.warp_affine(img_b, aff_ab, (height, width), align_corners=True)
- assert_allclose(img_b[..., :2, :3], img_a[..., 1:, 1:])
+ assert_allclose(img_b[..., :2, :3], img_a[..., 1:, 1:], rtol=1e-4, atol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
- aff_ab = torch.eye(2, 3)[None].to(device) # 1x2x3
- img_b = torch.rand(batch_size, channels, height, width).to(device)
+ aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None] # 1x2x3
+ img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
aff_ab = utils.tensor_to_gradcheck_var(
aff_ab, requires_grad=False) # to var
img_b = utils.tensor_to_gradcheck_var(img_b) # to var
@@ -272,41 +272,41 @@ class TestWarpAffine:
class TestRemap:
- def test_smoke(self, device):
+ def test_smoke(self, device, dtype):
height, width = 3, 4
- input = torch.ones(1, 1, height, width).to(device)
+ input = torch.ones(1, 1, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
input_warped = kornia.remap(input, grid[..., 0], grid[..., 1], align_corners=True)
- assert_allclose(input, input_warped)
+ assert_allclose(input, input_warped, rtol=1e-4, atol=1e-4)
- def test_shift(self, device):
+ def test_shift(self, device, dtype):
height, width = 3, 4
inp = torch.tensor([[[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
expected = torch.tensor([[[
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1. # apply shift in both x/y direction
input_warped = kornia.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
- assert_allclose(input_warped, expected)
+ assert_allclose(input_warped, expected, rtol=1e-4, atol=1e-4)
- def test_shift_batch(self, device):
+ def test_shift_batch(self, device, dtype):
height, width = 3, 4
inp = torch.tensor([[[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
- ]]]).repeat(2, 1, 1, 1).to(device)
+ ]]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[1., 1., 1., 0.],
@@ -316,45 +316,45 @@ class TestRemap:
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
# generate a batch of grids
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid = grid.repeat(2, 1, 1, 1)
grid[0, ..., 0] += 1. # apply shift in the x direction
grid[1, ..., 1] += 1. # apply shift in the y direction
input_warped = kornia.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
- assert_allclose(input_warped, expected)
+ assert_allclose(input_warped, expected, rtol=1e-4, atol=1e-4)
- def test_shift_batch_broadcast(self, device):
+ def test_shift_batch_broadcast(self, device, dtype):
height, width = 3, 4
inp = torch.tensor([[[
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
- ]]]).repeat(2, 1, 1, 1).to(device)
+ ]]], device=device, dtype=dtype).repeat(2, 1, 1, 1)
expected = torch.tensor([[[
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[0., 0., 0., 0.],
- ]]]).to(device)
+ ]]], device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1. # apply shift in both x/y direction
input_warped = kornia.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
- assert_allclose(input_warped, expected)
+ assert_allclose(input_warped, expected, rtol=1e-4, atol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
- img = torch.rand(batch_size, channels, height, width).to(device)
+ img = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img = utils.tensor_to_gradcheck_var(img) # to var
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid = utils.tensor_to_gradcheck_var(
grid, requires_grad=False) # to var
@@ -362,106 +362,106 @@ class TestRemap:
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(input, map1, map2):
return kornia.remap(input, map1, map2)
batch_size, channels, height, width = 1, 1, 3, 4
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1. # apply some shift
input = (img, grid[..., 0], grid[..., 1],)
actual = op_script(*input)
expected = kornia.remap(*input)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, rtol=1e-4, atol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit_trace(self, device):
+ def test_jit_trace(self, device, dtype):
@torch.jit.script
def op_script(input, map1, map2):
return kornia.remap(input, map1, map2)
# 1. Trace op
batch_size, channels, height, width = 1, 1, 3, 4
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1. # apply some shift
input_tuple = (img, grid[..., 0], grid[..., 1])
op_traced = torch.jit.trace(op_script, input_tuple)
# 2. Generate different input
batch_size, channels, height, width = 2, 2, 2, 5
- img = torch.ones(batch_size, channels, height, width).to(device)
+ img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(
- height, width, normalized_coordinates=False).to(device)
+ height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 2. # apply some shift
# 3. Apply to different input
input_tuple = (img, grid[..., 0], grid[..., 1])
actual = op_script(*input_tuple)
expected = kornia.remap(*input_tuple)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, rtol=1e-4, atol=1e-4)
class TestInvertAffineTransform:
- def test_smoke(self, device):
- matrix = torch.eye(2, 3).to(device)[None]
+ def test_smoke(self, device, dtype):
+ matrix = torch.eye(2, 3, device=device, dtype=dtype)[None]
matrix_inv = kornia.invert_affine_transform(matrix)
- assert_allclose(matrix, matrix_inv)
+ assert_allclose(matrix, matrix_inv, rtol=1e-4, atol=1e-4)
- def test_rot90(self, device):
- angle = torch.tensor([90.]).to(device)
- scale = torch.tensor([[1., 1.]]).to(device)
- center = torch.tensor([[0., 0.]]).to(device)
+ def test_rot90(self, device, dtype):
+ angle = torch.tensor([90.], device=device, dtype=dtype)
+ scale = torch.tensor([[1., 1.]], device=device, dtype=dtype)
+ center = torch.tensor([[0., 0.]], device=device, dtype=dtype)
expected = torch.tensor([[
[0., -1., 0.],
[1., 0., 0.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
matrix = kornia.get_rotation_matrix2d(center, angle, scale)
matrix_inv = kornia.invert_affine_transform(matrix)
- assert_allclose(matrix_inv, expected)
+ assert_allclose(matrix_inv, expected, rtol=1e-4, atol=1e-4)
- def test_rot90_batch(self, device):
- angle = torch.tensor([90.]).to(device)
- scale = torch.tensor([[1., 1.]]).to(device)
- center = torch.tensor([[0., 0.]]).to(device)
+ def test_rot90_batch(self, device, dtype):
+ angle = torch.tensor([90.], device=device, dtype=dtype)
+ scale = torch.tensor([[1., 1.]], device=device, dtype=dtype)
+ center = torch.tensor([[0., 0.]], device=device, dtype=dtype)
expected = torch.tensor([[
[0., -1., 0.],
[1., 0., 0.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
matrix = kornia.get_rotation_matrix2d(
center, angle, scale).repeat(2, 1, 1)
matrix_inv = kornia.invert_affine_transform(matrix)
- assert_allclose(matrix_inv, expected)
+ assert_allclose(matrix_inv, expected, rtol=1e-4, atol=1e-4)
- def test_gradcheck(self, device):
- matrix = torch.eye(2, 3).to(device)[None]
+ def test_gradcheck(self, device, dtype):
+ matrix = torch.eye(2, 3, device=device, dtype=dtype)[None]
matrix = utils.tensor_to_gradcheck_var(matrix) # to var
assert gradcheck(kornia.invert_affine_transform, (matrix,),
raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(input):
return kornia.invert_affine_transform(input)
- matrix = torch.eye(2, 3).to(device)
+ matrix = torch.eye(2, 3, device=device, dtype=dtype)
op_traced = torch.jit.trace(op_script, matrix)
actual = op_traced(matrix)
expected = kornia.invert_affine_transform(matrix)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, rtol=1e-4, atol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit_trace(self, device):
+ def test_jit_trace(self, device, dtype):
@torch.jit.script
def op_script(input):
return kornia.invert_affine_transform(input)
- matrix = torch.eye(2, 3).to(device)
- matrix_2 = torch.eye(2, 3).repeat(2, 1, 1)
+ matrix = torch.eye(2, 3, device=device, dtype=dtype)
+ matrix_2 = torch.eye(2, 3, device=device, dtype=dtype).repeat(2, 1, 1)
op_traced = torch.jit.trace(op_script, matrix)
actual = op_traced(matrix_2)
expected = kornia.invert_affine_transform(matrix_2)
- assert_allclose(actual, expected)
+ assert_allclose(actual, expected, rtol=1e-4, atol=1e-4)
diff --git a/test/geometry/transform/test_projwarp.py b/test/geometry/transform/test_projwarp.py
index c7f42495b..8f8d3e7fd 100644
--- a/test/geometry/transform/test_projwarp.py
+++ b/test/geometry/transform/test_projwarp.py
@@ -38,7 +38,7 @@ class TestWarpAffine3d:
P = torch.rand(2, 3, 4, device=device, dtype=dtype)
P = kornia.geometry.convert_affinematrix_to_homography3d(P)
P_hat = (P.inverse() @ P)[:, :3]
- output = proj.warp_affine3d(input, P_hat, out_shape)
+ output = proj.warp_affine3d(input, P_hat, out_shape, flags='nearest')
assert_allclose(output, input, rtol=1e-4, atol=1e-4)
def test_rotate_x(self, device, dtype):
@@ -79,7 +79,7 @@ class TestWarpAffine3d:
scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
- assert_allclose(output, expected)
+ assert_allclose(output, expected, rtol=1e-4, atol=1e-4)
def test_rotate_y(self, device, dtype):
input = torch.tensor([[[[
@@ -119,7 +119,7 @@ class TestWarpAffine3d:
scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
- assert_allclose(output, expected)
+ assert_allclose(output, expected, rtol=1e-4, atol=1e-4)
def test_rotate_z(self, device, dtype):
input = torch.tensor([[[[
@@ -159,7 +159,7 @@ class TestWarpAffine3d:
scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
- assert_allclose(output, expected)
+ assert_allclose(output, expected, rtol=1e-4, atol=1e-4)
def test_rotate_y_large(self, device, dtype):
"""Rotates 90deg anti-clockwise."""
@@ -224,7 +224,7 @@ class TestWarpAffine3d:
scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
- assert_allclose(output, expected)
+ assert_allclose(output, expected, rtol=1e-4, atol=1e-4)
class TestGetRotationMatrix3d:
diff --git a/test/geometry/transform/test_pyramid.py b/test/geometry/transform/test_pyramid.py
index df3764d38..aeb22da07 100644
--- a/test/geometry/transform/test_pyramid.py
+++ b/test/geometry/transform/test_pyramid.py
@@ -9,17 +9,17 @@ from torch.autograd import gradcheck
class TestPyrUp:
- def test_shape(self, device):
- inp = torch.zeros(1, 2, 4, 4).to(device)
+ def test_shape(self, device, dtype):
+ inp = torch.zeros(1, 2, 4, 4, device=device, dtype=dtype)
pyr = kornia.geometry.PyrUp()
assert pyr(inp).shape == (1, 2, 8, 8)
- def test_shape_batch(self, device):
- inp = torch.zeros(2, 2, 4, 4).to(device)
+ def test_shape_batch(self, device, dtype):
+ inp = torch.zeros(2, 2, 4, 4, device=device, dtype=dtype)
pyr = kornia.geometry.PyrUp()
assert pyr(inp).shape == (2, 2, 8, 8)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 5, 4
# TODO: cuda test is not working
img = torch.rand(batch_size, channels, height, width)
@@ -28,55 +28,55 @@ class TestPyrUp:
@pytest.mark.skip("")
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(input):
return kornia.geometry.pyrup(input)
- img = torch.rand(2, 3, 4, 5).to(device)
+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
actual = op_script(img)
expected = kornia.geometry.pyrup(img)
assert_allclose(actual, expected)
class TestPyrDown:
- def test_shape(self, device):
- inp = torch.zeros(1, 2, 4, 4).to(device)
+ def test_shape(self, device, dtype):
+ inp = torch.zeros(1, 2, 4, 4, device=device, dtype=dtype)
pyr = kornia.geometry.PyrDown()
assert pyr(inp).shape == (1, 2, 2, 2)
- def test_shape_batch(self, device):
- inp = torch.zeros(2, 2, 4, 4).to(device)
+ def test_shape_batch(self, device, dtype):
+ inp = torch.zeros(2, 2, 4, 4, device=device, dtype=dtype)
pyr = kornia.geometry.PyrDown()
assert pyr(inp).shape == (2, 2, 2, 2)
- def test_symmetry_preserving(self, device):
- inp = torch.zeros(1, 1, 6, 6).to(device)
+ def test_symmetry_preserving(self, device, dtype):
+ inp = torch.zeros(1, 1, 6, 6, device=device, dtype=dtype)
inp[:, :, 2:4, 2:4] = 1.0
pyr_out = kornia.geometry.PyrDown()(inp).squeeze()
assert torch.allclose(pyr_out, pyr_out.flip(0))
assert torch.allclose(pyr_out, pyr_out.flip(1))
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width).to(device)
+ img = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.geometry.pyrdown, (img,), raise_exception=True)
@pytest.mark.skip("")
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ def test_jit(self, device, dtype):
@torch.jit.script
def op_script(input):
return kornia.geometry.pyrdown(input)
- img = torch.rand(2, 3, 4, 5).to(device)
+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
actual = op_script(img)
expected = kornia.geometry.pyrdown(img)
assert_allclose(actual, expected)
class TestScalePyramid:
- def test_shape_tuple(self, device):
- inp = torch.zeros(3, 2, 41, 41).to(device)
+ def test_shape_tuple(self, device, dtype):
+ inp = torch.zeros(3, 2, 41, 41, device=device, dtype=dtype)
SP = kornia.geometry.ScalePyramid(n_levels=1, min_size=30)
out = SP(inp)
assert len(out) == 3
@@ -84,26 +84,26 @@ class TestScalePyramid:
assert len(out[1]) == 1
assert len(out[2]) == 1
- def test_shape_batch(self, device):
- inp = torch.zeros(3, 2, 31, 31).to(device)
+ def test_shape_batch(self, device, dtype):
+ inp = torch.zeros(3, 2, 31, 31, device=device, dtype=dtype)
SP = kornia.geometry.ScalePyramid(n_levels=1)
sp, sigmas, pd = SP(inp)
assert sp[0].shape == (3, 2, 3 + 1, 31, 31)
- def test_shape_batch_double(self, device):
- inp = torch.zeros(3, 2, 31, 31).to(device)
+ def test_shape_batch_double(self, device, dtype):
+ inp = torch.zeros(3, 2, 31, 31, device=device, dtype=dtype)
SP = kornia.geometry.ScalePyramid(n_levels=1, double_image=True)
sp, sigmas, pd = SP(inp)
assert sp[0].shape == (3, 2, 1 + 3, 62, 62)
- def test_n_levels_shape(self, device):
- inp = torch.zeros(1, 1, 32, 32).to(device)
+ def test_n_levels_shape(self, device, dtype):
+ inp = torch.zeros(1, 1, 32, 32, device=device, dtype=dtype)
SP = kornia.geometry.ScalePyramid(n_levels=3)
sp, sigmas, pd = SP(inp)
assert sp[0].shape == (1, 1, 3 + 3, 32, 32)
- def test_blur_order(self, device):
- inp = torch.rand(1, 1, 31, 31).to(device)
+ def test_blur_order(self, device, dtype):
+ inp = torch.rand(1, 1, 31, 31, device=device, dtype=dtype)
SP = kornia.geometry.ScalePyramid(n_levels=3)
sp, sigmas, pd = SP(inp)
for i, pyr_level in enumerate(sp):
@@ -113,10 +113,10 @@ class TestScalePyramid:
assert torch.argmax(max_per_blur_level_val).item() == 0
return
- def test_symmetry_preserving(self, device):
+ def test_symmetry_preserving(self, device, dtype):
PS = 16
R = 2
- inp = torch.zeros(1, 1, PS, PS).to(device)
+ inp = torch.zeros(1, 1, PS, PS, device=device, dtype=dtype)
inp[..., PS // 2 - R:PS // 2 + R, PS // 2 - R:PS // 2 + R] = 1.0
SP = kornia.geometry.ScalePyramid(n_levels=3)
sp, sigmas, pd = SP(inp)
@@ -127,7 +127,7 @@ class TestScalePyramid:
assert torch.allclose(img, img.flip(2), atol=1e-5, rtol=1e-3)
return
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 7, 9
# TODO: cuda test is not working
img = torch.rand(batch_size, channels, height, width)
@@ -141,8 +141,8 @@ class TestScalePyramid:
class TestBuildPyramid:
- def test_smoke(self, device):
- input = torch.ones(1, 2, 4, 5).to(device)
+ def test_smoke(self, device, dtype):
+ input = torch.ones(1, 2, 4, 5, device=device, dtype=dtype)
pyramid = kornia.build_pyramid(input, max_level=1)
assert len(pyramid) == 1
assert pyramid[0].shape == (1, 2, 4, 5)
@@ -150,9 +150,9 @@ class TestBuildPyramid:
@pytest.mark.parametrize("batch_size", (1, 2, 3))
@pytest.mark.parametrize("channels", (1, 3))
@pytest.mark.parametrize("max_level", (2, 3, 4))
- def test_num_levels(self, device, batch_size, channels, max_level):
+ def test_num_levels(self, batch_size, channels, max_level, device, dtype):
height, width = 16, 20
- input = torch.rand(batch_size, channels, height, width).to(device)
+ input = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
pyramid = kornia.build_pyramid(input, max_level)
assert len(pyramid) == max_level
for i in range(1, max_level):
@@ -161,9 +161,9 @@ class TestBuildPyramid:
expected_shape = (batch_size, channels, height // denom, width // denom)
assert img.shape == expected_shape
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
max_level = 1
batch_size, channels, height, width = 1, 2, 7, 9
- img = torch.rand(batch_size, channels, height, width).to(device)
+ img = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.build_pyramid, (img, max_level,), raise_exception=True)
diff --git a/test/geometry/warp/test_depth_warper.py b/test/geometry/warp/test_depth_warper.py
index ec98905e9..e2abcfee4 100644
--- a/test/geometry/warp/test_depth_warper.py
+++ b/test/geometry/warp/test_depth_warper.py
@@ -12,7 +12,7 @@ from torch.testing import assert_allclose
class TestDepthWarper:
eps = 1e-6
- def _create_pinhole_pair(self, batch_size):
+ def _create_pinhole_pair(self, batch_size, device, dtype):
# prepare data
fx, fy = 1., 1.
height, width = 3, 5
@@ -21,15 +21,15 @@ class TestDepthWarper:
# create pinhole cameras
pinhole_src = kornia.PinholeCamera.from_parameters(
- fx, fy, cx, cy, height, width, tx, ty, tz, batch_size)
+ fx, fy, cx, cy, height, width, tx, ty, tz, batch_size, device=device, dtype=dtype)
pinhole_dst = kornia.PinholeCamera.from_parameters(
- fx, fy, cx, cy, height, width, tx, ty, tz, batch_size)
+ fx, fy, cx, cy, height, width, tx, ty, tz, batch_size, device=device, dtype=dtype)
return pinhole_src, pinhole_dst
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_compute_projection_matrix(self, batch_size):
+ def test_compute_projection_matrix(self, batch_size, device, dtype):
height, width = 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
pinhole_dst.tx += 1. # apply offset to tx
# create warper
@@ -42,21 +42,20 @@ class TestDepthWarper:
# retreive computed projection matrix and compare to expected
dst_proj_src = warper._dst_proj_src
- dst_proj_src_expected = torch.eye(
- 4)[None].repeat(batch_size, 1, 1) # Bx4x4
+ dst_proj_src_expected = torch.eye(4, device=device, dtype=dtype)[None].repeat(batch_size, 1, 1) # Bx4x4
dst_proj_src_expected[..., 0, -2] += pinhole_src.cx
dst_proj_src_expected[..., 1, -2] += pinhole_src.cy
dst_proj_src_expected[..., 0, -1] += 1. # offset to x-axis
assert_allclose(dst_proj_src, dst_proj_src_expected)
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_warp_grid_offset_x1_depth1(self, device, batch_size):
+ def test_warp_grid_offset_x1_depth1(self, batch_size, device, dtype):
height, width = 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
pinhole_dst.tx += 1. # apply offset to tx
# initialize depth to one
- depth_src = torch.ones(batch_size, 1, height, width)
+ depth_src = torch.ones(batch_size, 1, height, width, device=device, dtype=dtype)
# create warper, initialize projection matrices and warp grid
warper = kornia.DepthWarper(pinhole_dst, height, width)
@@ -66,25 +65,25 @@ class TestDepthWarper:
assert grid_warped.shape == (batch_size, height, width, 2)
# normalize base meshgrid
- grid = warper.grid[..., :2]
+ grid = warper.grid[..., :2].to(device=device, dtype=dtype)
grid_norm = normalize_pixel_coordinates(grid, height, width)
# check offset in x-axis
assert_allclose(
- grid_warped[..., -2, 0], grid_norm[..., -1, 0])
+ grid_warped[..., -2, 0], grid_norm[..., -1, 0], atol=1e-4, rtol=1e-4)
# check that y-axis remain the same
assert_allclose(
- grid_warped[..., -1, 1], grid_norm[..., -1, 1])
+ grid_warped[..., -1, 1], grid_norm[..., -1, 1], rtol=1e-4, atol=1e-4)
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_warp_grid_offset_x1y1_depth1(self, device, batch_size):
+ def test_warp_grid_offset_x1y1_depth1(self, batch_size, device, dtype):
height, width = 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
pinhole_dst.tx += 1. # apply offset to tx
pinhole_dst.ty += 1. # apply offset to ty
# initialize depth to one
- depth_src = torch.ones(batch_size, 1, height, width)
+ depth_src = torch.ones(batch_size, 1, height, width, device=device, dtype=dtype)
# create warper, initialize projection matrices and warp grid
warper = kornia.DepthWarper(pinhole_dst, height, width)
@@ -94,32 +93,32 @@ class TestDepthWarper:
assert grid_warped.shape == (batch_size, height, width, 2)
# normalize base meshgrid
- grid = warper.grid[..., :2]
+ grid = warper.grid[..., :2].to(device=device, dtype=dtype)
grid_norm = normalize_pixel_coordinates(grid, height, width)
# check offset in x-axis
assert_allclose(
- grid_warped[..., -2, 0], grid_norm[..., -1, 0])
+ grid_warped[..., -2, 0], grid_norm[..., -1, 0], atol=1e-4, rtol=1e-4)
# check that y-axis remain the same
assert_allclose(
- grid_warped[..., -2, :, 1], grid_norm[..., -1, :, 1])
+ grid_warped[..., -2, :, 1], grid_norm[..., -1, :, 1], rtol=1e-4, atol=1e-4)
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_warp_tensor_offset_x1y1(self, device, batch_size):
+ def test_warp_tensor_offset_x1y1(self, batch_size, device, dtype):
channels, height, width = 3, 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
pinhole_dst.tx += 1. # apply offset to tx
pinhole_dst.ty += 1. # apply offset to ty
# initialize depth to one
- depth_src = torch.ones(batch_size, 1, height, width)
+ depth_src = torch.ones(batch_size, 1, height, width, device=device, dtype=dtype)
# create warper, initialize projection matrices and warp grid
warper = kornia.DepthWarper(pinhole_dst, height, width)
warper.compute_projection_matrix(pinhole_src)
# create patch to warp
- patch_dst = torch.arange(float(height * width)).view(
+ patch_dst = torch.arange(float(height * width), device=device, dtype=dtype).view(
1, 1, height, width).expand(batch_size, channels, -1, -1)
# warpd source patch by depth
@@ -127,12 +126,12 @@ class TestDepthWarper:
# compare patches
assert_allclose(
- patch_dst[..., 1:, 1:], patch_src[..., :2, :4])
+ patch_dst[..., 1:, 1:], patch_src[..., :2, :4], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_compute_projection(self, device, batch_size):
+ def test_compute_projection(self, batch_size, device, dtype):
height, width = 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
# create warper, initialize projection matrices and warp grid
warper = kornia.DepthWarper(pinhole_dst, height, width)
@@ -143,9 +142,9 @@ class TestDepthWarper:
assert xy_projected.shape == (batch_size, 2)
@pytest.mark.parametrize("batch_size", (1, 2,))
- def test_compute_subpixel_step(self, device, batch_size):
+ def test_compute_subpixel_step(self, batch_size, device, dtype):
height, width = 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
# create warper, initialize projection matrices and warp grid
warper = kornia.DepthWarper(pinhole_dst, height, width)
@@ -156,17 +155,17 @@ class TestDepthWarper:
assert pytest.approx(subpixel_step.item(), 0.3536)
@pytest.mark.parametrize("batch_size", (1, 2))
- def test_gradcheck(self, device, batch_size):
+ def test_gradcheck(self, batch_size, device, dtype):
# prepare data
channels, height, width = 3, 3, 5 # output shape
- pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size)
+ pinhole_src, pinhole_dst = self._create_pinhole_pair(batch_size, device, dtype)
# initialize depth to one
- depth_src = torch.ones(batch_size, 1, height, width)
+ depth_src = torch.ones(batch_size, 1, height, width, device=device, dtype=dtype)
depth_src = utils.tensor_to_gradcheck_var(depth_src) # to var
# create patch to warp
- img_dst = torch.ones(batch_size, channels, height, width)
+ img_dst = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
img_dst = utils.tensor_to_gradcheck_var(img_dst) # to var
# evaluate function gradient
diff --git a/test/geometry/warp/test_homography_warper.py b/test/geometry/warp/test_homography_warper.py
index 46ca239e3..d9e743efc 100644
--- a/test/geometry/warp/test_homography_warper.py
+++ b/test/geometry/warp/test_homography_warper.py
@@ -12,11 +12,11 @@ class TestHomographyWarper:
num_tests = 10
threshold = 0.1
- def test_identity(self, device):
+ def test_identity(self, device, dtype):
# create input data
height, width = 2, 5
- patch_src = torch.rand(1, 1, height, width).to(device)
- dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3).to(device)
+ patch_src = torch.rand(1, 1, height, width, device=device, dtype=dtype)
+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
# instantiate warper
warper = kornia.HomographyWarper(height, width, align_corners=True)
@@ -26,15 +26,15 @@ class TestHomographyWarper:
assert_allclose(patch_src, patch_dst)
@pytest.mark.parametrize("batch_size", [1, 3])
- def test_normalize_homography_identity(self, batch_size, device):
+ def test_normalize_homography_identity(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
- dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)
res = torch.tensor([[[0.5, 0.0, -1.0],
[0.0, 2.0, -1.0],
- [0.0, 0.0, 1.0]]])
- assert (kornia.normal_transform_pixel(height, width) == res).all()
+ [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+ assert (kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) == res).all()
norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width))
assert (norm_homo == dst_homo_src).all()
@@ -50,14 +50,14 @@ class TestHomographyWarper:
(height * 2, width // 2))
res = torch.tensor([[[4.0, 0.0, 3.0],
[0.0, 1 / 3, -2 / 3],
- [0.0, 0.0, 1.0]]]).to(device)
- assert_allclose(norm_homo, res)
+ [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
+ assert_allclose(norm_homo, res, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 3])
- def test_normalize_homography_general(self, batch_size, device):
+ def test_normalize_homography_general(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
- dst_homo_src = torch.eye(3).to(device)
+ dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 2.0
dst_homo_src[..., 0, 2] = 1.0
@@ -67,16 +67,16 @@ class TestHomographyWarper:
norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width))
res = torch.tensor([[[0.5, 0.0, 0.0],
[0.0, 2.0, 5.0],
- [0.0, 0.0, 1.0]]]).to(device)
+ [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (norm_homo == res).all()
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [
(4, 5), (2, 6), (4, 3), (5, 7), ])
- def test_warp_grid_translation(self, shape, offset, device):
+ def test_warp_grid_translation(self, shape, offset, device, dtype):
# create input data
height, width = shape
- dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
dst_homo_src[..., 0, 2] = offset # apply offset in x
grid = kornia.create_meshgrid(height, width, normalized_coordinates=False)
flow = kornia.warp_grid(grid, dst_homo_src)
@@ -84,17 +84,17 @@ class TestHomographyWarper:
# the grid the src plus the offset should be equal to the flow
# on the x-axis, y-axis remains the same.
assert_allclose(
- grid[..., 0].to(device) + offset, flow[..., 0])
+ grid[..., 0].to(device=device, dtype=dtype) + offset, flow[..., 0])
assert_allclose(
- grid[..., 1].to(device), flow[..., 1])
+ grid[..., 1].to(device=device, dtype=dtype), flow[..., 1])
@pytest.mark.parametrize("batch_shape", [
(1, 1, 4, 5), (2, 2, 4, 6), (3, 1, 5, 7), ])
- def test_identity_resize(self, device, batch_shape):
+ def test_identity_resize(self, batch_shape, device, dtype):
# create input data
batch_size, channels, height, width = batch_shape
- patch_src = torch.rand(batch_size, channels, height, width).to(device)
- dst_homo_src = utils.create_eye_batch(batch_size, eye_size=3).to(device)
+ patch_src = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size=3, device=device, dtype=dtype)
# instantiate warper warp from source to destination
warper = kornia.HomographyWarper(height // 2, width // 2, align_corners=True)
@@ -102,37 +102,37 @@ class TestHomographyWarper:
# check the corners
assert_allclose(
- patch_src[..., 0, 0], patch_dst[..., 0, 0])
+ patch_src[..., 0, 0], patch_dst[..., 0, 0], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., 0, -1], patch_dst[..., 0, -1])
+ patch_src[..., 0, -1], patch_dst[..., 0, -1], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., -1, 0], patch_dst[..., -1, 0])
+ patch_src[..., -1, 0], patch_dst[..., -1, 0], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., -1, -1], patch_dst[..., -1, -1])
+ patch_src[..., -1, -1], patch_dst[..., -1, -1], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("shape", [
(4, 5), (2, 6), (4, 3), (5, 7), ])
- def test_translation(self, device, shape):
+ def test_translation(self, shape, device, dtype):
# create input data
offset = 2. # in pixel
height, width = shape
- patch_src = torch.rand(1, 1, height, width).to(device)
- dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3).to(device)
+ patch_src = torch.rand(1, 1, height, width, device=device, dtype=dtype)
+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
dst_homo_src[..., 0, 2] = offset / (width - 1) # apply offset in x
# instantiate warper and from source to destination
warper = kornia.HomographyWarper(height, width, align_corners=True)
patch_dst = warper(patch_src, dst_homo_src)
- assert_allclose(patch_src[..., 1:], patch_dst[..., :-1])
+ assert_allclose(patch_src[..., 1:], patch_dst[..., :-1], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [
(1, 1, 3, 5), (2, 2, 4, 3), (3, 1, 2, 3), ])
- def test_rotation(self, device, batch_shape):
+ def test_rotation(self, batch_shape, device, dtype):
# create input data
batch_size, channels, height, width = batch_shape
- patch_src = torch.rand(batch_size, channels, height, width).to(device)
+ patch_src = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
# rotation of 90deg
- dst_homo_src = torch.eye(3).to(device)
+ dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.0
dst_homo_src[..., 0, 1] = 1.0
dst_homo_src[..., 1, 0] = -1.0
@@ -145,27 +145,27 @@ class TestHomographyWarper:
# check the corners
assert_allclose(
- patch_src[..., 0, 0], patch_dst[..., 0, -1])
+ patch_src[..., 0, 0], patch_dst[..., 0, -1], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., 0, -1], patch_dst[..., -1, -1])
+ patch_src[..., 0, -1], patch_dst[..., -1, -1], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., -1, 0], patch_dst[..., 0, 0])
+ patch_src[..., -1, 0], patch_dst[..., 0, 0], atol=1e-4, rtol=1e-4)
assert_allclose(
- patch_src[..., -1, -1], patch_dst[..., -1, 0])
+ patch_src[..., -1, -1], patch_dst[..., -1, 0], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 3])
- def test_homography_warper(self, device, batch_size):
+ def test_homography_warper(self, batch_size, device, dtype):
# generate input data
height, width = 128, 64
eye_size = 3 # identity 3x3
# create checkerboard
board = utils.create_checkerboard(height, width, 4)
- patch_src = torch.from_numpy(board).view(
- 1, 1, height, width).expand(batch_size, 1, height, width).to(device)
+ patch_src = torch.from_numpy(board).to(device=device, dtype=dtype).view(
+ 1, 1, height, width).expand(batch_size, 1, height, width)
# create base homography
- dst_homo_src = utils.create_eye_batch(batch_size, eye_size).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
# instantiate warper
warper = kornia.HomographyWarper(height, width, align_corners=True)
@@ -196,21 +196,21 @@ class TestHomographyWarper:
patch_dst, torch.inverse(dst_homo_src_i), (height, width), align_corners=True)
assert_allclose(
- patch_dst_to_src, patch_dst_to_src_functional)
+ patch_dst_to_src, patch_dst_to_src_functional, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [
(1, 1, 7, 5), (2, 3, 8, 5), (1, 1, 7, 16), ])
- def test_gradcheck(self, device, batch_shape):
+ def test_gradcheck(self, batch_shape, device, dtype):
# generate input data
eye_size = 3 # identity 3x3
# create checkerboard
- patch_src = torch.rand(batch_shape).to(device)
+ patch_src = torch.rand(batch_shape, device=device, dtype=dtype)
patch_src = utils.tensor_to_gradcheck_var(patch_src) # to var
# create base homography
batch_size, _, height, width = patch_src.shape
- dst_homo_src = utils.create_eye_batch(batch_size, eye_size).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
dst_homo_src = utils.tensor_to_gradcheck_var(
dst_homo_src, requires_grad=False) # to var
@@ -224,18 +224,18 @@ class TestHomographyWarper:
@pytest.mark.parametrize("batch_size", [1, 2, 3])
@pytest.mark.parametrize("align_corners", [True, False])
@pytest.mark.parametrize("normalized_coordinates", [True, False])
- def test_jit_warp_homography(self, device, batch_size, align_corners, normalized_coordinates):
+ def test_jit_warp_homography(self, batch_size, align_corners, normalized_coordinates, device, dtype):
# generate input data
height, width = 128, 64
eye_size = 3 # identity 3x3
# create checkerboard
board = utils.create_checkerboard(height, width, 4)
- patch_src = torch.from_numpy(board).view(1, 1, height, width).expand(batch_size, 1, height,
- width).to(device)
+ patch_src = torch.from_numpy(board).to(device=device, dtype=dtype).view(
+ 1, 1, height, width).expand(batch_size, 1, height, width)
# create base homography
- dst_homo_src = utils.create_eye_batch(batch_size, eye_size).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
for i in range(self.num_tests):
# generate homography noise
@@ -251,7 +251,7 @@ class TestHomographyWarper:
patch_src, dst_homo_src_i, (height, width), align_corners=align_corners,
normalized_coordinates=normalized_coordinates)
- assert_allclose(patch_dst, patch_dst_jit)
+ assert_allclose(patch_dst, patch_dst_jit, atol=1e-4, rtol=1e-4)
class TestHomographyNormalTransform:
@@ -283,47 +283,47 @@ class TestHomographyNormalTransform:
(2, 5, expected_2d_0),
(1, 5, expected_2d_1)
])
- def test_transform2d(self, height, width, expected):
- output = kornia.normal_transform_pixel(height, width)
+ def test_transform2d(self, height, width, expected, device, dtype):
+ output = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype)
- assert_allclose(output, expected)
+ assert_allclose(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
- def test_divide_by_zero2d(self, height, width):
- output = kornia.normal_transform_pixel(height, width)
+ def test_divide_by_zero2d(self, height, width, device, dtype):
+ output = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
- def test_transform2d_apply(self):
+ def test_transform2d_apply(self, device, dtype):
height, width = 2, 5
- input = torch.tensor([[0., 0.], [width - 1, height - 1]])
- expected = torch.tensor([[-1., -1.], [1., 1.]])
- transform = kornia.normal_transform_pixel(height, width)
+ input = torch.tensor([[0., 0.], [width - 1, height - 1]], device=device, dtype=dtype)
+ expected = torch.tensor([[-1., -1.], [1., 1.]], device=device, dtype=dtype)
+ transform = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype)
output = kornia.transform_points(transform, input)
- assert_allclose(output, expected)
+ assert_allclose(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height,width,depth,expected", [
(2, 6, 4, expected_3d_0),
(1, 6, 4, expected_3d_1)
])
- def test_transform3d(self, height, width, depth, expected):
- output = kornia.normal_transform_pixel3d(depth, height, width)
- assert_allclose(output, expected)
+ def test_transform3d(self, height, width, depth, expected, device, dtype):
+ output = kornia.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
+ assert_allclose(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
@pytest.mark.parametrize("depth", [1, 2, 5])
- def test_divide_by_zero3d(self, height, width, depth):
- output = kornia.normal_transform_pixel3d(depth, height, width)
+ def test_divide_by_zero3d(self, height, width, depth, device, dtype):
+ output = kornia.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
- def test_transform3d_apply(self):
+ def test_transform3d_apply(self, device, dtype):
depth, height, width = 3, 2, 5
- input = torch.tensor([[0., 0., 0.], [width - 1, height - 1, depth - 1]])
- expected = torch.tensor([[-1., -1., -1.], [1., 1., 1.]])
- transform = kornia.normal_transform_pixel3d(depth, height, width)
+ input = torch.tensor([[0., 0., 0.], [width - 1, height - 1, depth - 1]], device=device, dtype=dtype)
+ expected = torch.tensor([[-1., -1., -1.], [1., 1., 1.]], device=device, dtype=dtype)
+ transform = kornia.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
output = kornia.transform_points(transform, input)
- assert_allclose(output, expected)
+ assert_allclose(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
class TestHomographyWarper3D:
@@ -365,7 +365,7 @@ class TestHomographyWarper3D:
@pytest.mark.parametrize("batch_size", [1, 3])
def test_normalize_homography_general(self, batch_size, device, dtype):
# create input data
- dst_homo_src = torch.eye(4).to(device)
+ dst_homo_src = torch.eye(4, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 0.5
dst_homo_src[..., 2, 2] = 2.0
@@ -383,10 +383,10 @@ class TestHomographyWarper3D:
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [(4, 5, 6), (2, 4, 6), (4, 3, 9), (5, 7, 8)])
- def test_warp_grid_translation(self, shape, offset, device):
+ def test_warp_grid_translation(self, shape, offset, device, dtype):
# create input data
depth, height, width = shape
- dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=4).to(device)
+ dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=4, device=device, dtype=dtype)
dst_homo_src[..., 0, 3] = offset # apply offset in x
grid = kornia.create_meshgrid3d(depth, height, width, normalized_coordinates=False)
flow = kornia.warp_grid3d(grid, dst_homo_src)
@@ -394,8 +394,8 @@ class TestHomographyWarper3D:
# the grid the src plus the offset should be equal to the flow
# on the x-axis, y-axis remains the same.
assert_allclose(
- grid[..., 0].to(device) + offset, flow[..., 0])
+ grid[..., 0].to(device=device, dtype=dtype) + offset, flow[..., 0], atol=1e-4, rtol=1e-4)
assert_allclose(
- grid[..., 1].to(device), flow[..., 1])
+ grid[..., 1].to(device=device, dtype=dtype), flow[..., 1], atol=1e-4, rtol=1e-4)
assert_allclose(
- grid[..., 2].to(device), flow[..., 2])
+ grid[..., 2].to(device=device, dtype=dtype), flow[..., 2], atol=1e-4, rtol=1e-4)