Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Move test function for assert_allclose

Pau Riba8f0fd8512115
diff --git a/kornia/contrib/extract_patches.py b/kornia/contrib/extract_patches.py
index 10d7619b6..69cc05ba7 100644
--- a/kornia/contrib/extract_patches.py
+++ b/kornia/contrib/extract_patches.py
@@ -77,20 +77,18 @@ class ExtractTensorPatches(nn.Module):
self.window_size: Tuple[int, int] = _pair(window_size)
self.stride: Tuple[int, int] = _pair(stride)
self.padding: Tuple[int, int] = _pair(padding)
- self.eps: float = 1e-6
# create base kernel
self.kernel: torch.Tensor = self.create_kernel(self.window_size)
@staticmethod
def create_kernel(
- window_size: Tuple[int, int],
- eps: float = 1e-6) -> torch.Tensor:
+ window_size: Tuple[int, int]) -> torch.Tensor:
r"""Creates a binary kernel to extract the patches. If the window size
is HxW will create a (H*W)xHxW kernel.
"""
window_range: int = window_size[0] * window_size[1]
- kernel: torch.Tensor = torch.zeros(window_range, window_range) + eps
+ kernel: torch.Tensor = torch.zeros(window_range, window_range)
for i in range(window_range):
kernel[i, i] += 1.0
return kernel.view(window_range, 1, window_size[0], window_size[1])
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index aaa66a15c..f43e1b0b7 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -45,7 +45,3 @@ def compute_patch_error(x, y, h, w):
"""Compute the absolute error between patches.
"""
return torch.abs(x - y)[..., h // 4:-h // 4, w // 4:-w // 4].mean()
-
-
-def check_equal_torch(a, b, eps=1e-4):
- return (torch.norm(a - b) <= (a.numel() * eps)).item()
diff --git a/kornia/version.py b/kornia/version.py
deleted file mode 100644
index eaba9868b..000000000
--- a/kornia/version.py
+++ /dev/null
@@ -1 +0,0 @@
-__version__ = '0.1.3.post2'
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index 2102e2733..bf506a9b0 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -116,7 +116,7 @@ class TestComposeTransforms:
trans_12[..., :3, -1] += offset # add offset to translation vector
trans_02 = kornia.compose_transformations(trans_01, trans_12)
- assert utils.check_equal_torch(trans_02, trans_12)
+ assert_allclose(trans_02, trans_12)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_translation_Bx4x4(self, batch_size):
@@ -126,7 +126,7 @@ class TestComposeTransforms:
trans_12[..., :3, -1] += offset # add offset to translation vector
trans_02 = kornia.compose_transformations(trans_01, trans_12)
- assert utils.check_equal_torch(trans_02, trans_12)
+ assert_allclose(trans_02, trans_12)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_gradcheck(self, batch_size):
@@ -148,7 +148,7 @@ class TestInverseTransformation:
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert utils.check_equal_torch(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_translation_Bx4x4(self, batch_size):
@@ -158,7 +158,7 @@ class TestInverseTransformation:
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert utils.check_equal_torch(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_rotation_translation_Bx4x4(self, batch_size):
@@ -173,7 +173,7 @@ class TestInverseTransformation:
trans_10 = kornia.inverse_transformation(trans_01)
trans_01_hat = kornia.inverse_transformation(trans_10)
- assert utils.check_equal_torch(trans_01, trans_01_hat)
+ assert_allclose(trans_01, trans_01_hat)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_gradcheck(self, batch_size):
@@ -193,7 +193,7 @@ class TestRelativeTransformation:
trans_12 = kornia.relative_transformation(trans_01, trans_02)
trans_02_hat = kornia.compose_transformations(trans_01, trans_12)
- assert utils.check_equal_torch(trans_02_hat, trans_02)
+ assert_allclose(trans_02_hat, trans_02)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_rotation_translation_Bx4x4(self, batch_size):
@@ -209,7 +209,7 @@ class TestRelativeTransformation:
trans_12 = kornia.relative_transformation(trans_01, trans_02)
trans_02_hat = kornia.compose_transformations(trans_01, trans_12)
- assert utils.check_equal_torch(trans_02_hat, trans_02)
+ assert_allclose(trans_02_hat, trans_02)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_gradcheck(self, batch_size):
diff --git a/test/geometry/test_pinhole.py b/test/geometry/test_pinhole.py
index 2d654df04..3b2a3bb13 100644
--- a/test/geometry/test_pinhole.py
+++ b/test/geometry/test_pinhole.py
@@ -140,22 +140,22 @@ class TestPinholeCamera:
pinhole = kornia.PinholeCamera(intrinsics, extrinsics, height, width)
pinhole_scale = pinhole.scale(scale_factor)
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 0, 0],
pinhole.intrinsics[..., 0, 0] * scale_val) # fx
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 1, 1],
pinhole.intrinsics[..., 1, 1] * scale_val) # fy
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 0, 2],
pinhole.intrinsics[..., 0, 2] * scale_val) # cx
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 1, 2],
pinhole.intrinsics[..., 1, 2] * scale_val) # cy
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.height,
pinhole.height * scale_val)
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.width,
pinhole.width * scale_val)
@@ -176,21 +176,21 @@ class TestPinholeCamera:
pinhole_scale = pinhole.clone()
pinhole_scale.scale_(scale_factor)
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 0, 0],
pinhole.intrinsics[..., 0, 0] * scale_val) # fx
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 1, 1],
pinhole.intrinsics[..., 1, 1] * scale_val) # fy
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 0, 2],
pinhole.intrinsics[..., 0, 2] * scale_val) # cx
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.intrinsics[..., 1, 2],
pinhole.intrinsics[..., 1, 2] * scale_val) # cy
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.height, pinhole.height * scale_val)
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_scale.width, pinhole.width * scale_val)
@@ -202,7 +202,7 @@ def test_scale_pinhole(batch_size, device_type):
scales = torch.rand(batch_size).to(device)
pinholes_scale = kornia.scale_pinhole(pinholes, scales)
- assert utils.check_equal_torch(
+ assert_allclose(
pinholes_scale[..., :6] / scales.unsqueeze(-1), pinholes[..., :6])
# evaluate function gradient
@@ -262,11 +262,11 @@ def test_inverse_pinhole_matrix(batch_size, device_type):
pinhole_matrix = kornia.inverse_pinhole_matrix(pinhole)
ones = torch.ones(batch_size)
- assert utils.check_equal_torch(pinhole_matrix[:, 0, 0], (1. / fx) * ones)
- assert utils.check_equal_torch(pinhole_matrix[:, 1, 1], (1. / fy) * ones)
- assert utils.check_equal_torch(
+ assert_allclose(pinhole_matrix[:, 0, 0], (1. / fx) * ones)
+ assert_allclose(pinhole_matrix[:, 1, 1], (1. / fy) * ones)
+ assert_allclose(
pinhole_matrix[:, 0, 2], (-1. * cx / fx) * ones)
- assert utils.check_equal_torch(
+ assert_allclose(
pinhole_matrix[:, 1, 2], (-1. * cy / fx) * ones)
# functional
@@ -315,7 +315,7 @@ def test_homography_i_H_ref(batch_size, device_type):
# compute homography from i to ref
ref_H_i = kornia.homography_i_H_ref(pinhole_ref, pinhole_i) + eps
- assert utils.check_equal_torch(i_H_ref_inv, ref_H_i)
+ assert_allclose(i_H_ref_inv, ref_H_i)
# evaluate function gradient
assert gradcheck(kornia.homography_i_H_ref,
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index e47b42cd2..8edc6936d 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -39,7 +39,7 @@ def test_warp_perspective_rotation(batch_shape, device_type):
torch.inverse(M),
dsize=(height, width))
- assert utils.check_equal_torch(mask_warped_inv * patch,
+ assert_allclose(mask_warped_inv * patch,
mask_warped_inv * patch_warped_inv)
# evaluate function gradient
@@ -73,7 +73,7 @@ def test_get_perspective_transform(batch_size, device_type):
# compute transform from source to target
dst_homo_src = kornia.get_perspective_transform(points_src, points_dst)
- assert utils.check_equal_torch(
+ assert_allclose(
kornia.transform_points(dst_homo_src, points_src), points_dst)
# compute gradient check
@@ -255,7 +255,7 @@ class TestWarpAffine:
img_b = torch.arange(float(height * width)).view(
1, channels, height, width).repeat(batch_size, 1, 1, 1)
img_a = kornia.warp_affine(img_b, aff_ab, (height, width))
- assert utils.check_equal_torch(img_b[..., :2, :3], img_a[..., 1:, 1:])
+ assert_allclose(img_b[..., :2, :3], img_a[..., 1:, 1:])
def test_gradcheck(self):
batch_size, channels, height, width = 1, 2, 3, 4
diff --git a/test/geometry/warp/test_depth_warper.py b/test/geometry/warp/test_depth_warper.py
index 8764f3297..ddb3285c4 100644
--- a/test/geometry/warp/test_depth_warper.py
+++ b/test/geometry/warp/test_depth_warper.py
@@ -6,6 +6,7 @@ from kornia.geometry.conversions import normalize_pixel_coordinates
import torch
from torch.autograd import gradcheck
+from torch.testing import assert_allclose
class TestDepthWarper:
@@ -46,7 +47,7 @@ class TestDepthWarper:
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 utils.check_equal_torch(dst_proj_src, dst_proj_src_expected)
+ assert_allclose(dst_proj_src, dst_proj_src_expected)
@pytest.mark.parametrize("batch_size", (1, 2,))
def test_warp_grid_offset_x1_depth1(self, batch_size):
@@ -69,11 +70,11 @@ class TestDepthWarper:
grid_norm = normalize_pixel_coordinates(grid, height, width)
# check offset in x-axis
- assert utils.check_equal_torch(
- grid_norm[..., -1, 0], grid_warped[..., -2, 0])
+ assert_allclose(
+ grid_warped[..., -2, 0], grid_norm[..., -1, 0])
# check that y-axis remain the same
- assert utils.check_equal_torch(
- grid_norm[..., -1, 1], grid_warped[..., -1, 1])
+ assert_allclose(
+ grid_warped[..., -1, 1], grid_norm[..., -1, 1])
@pytest.mark.parametrize("batch_size", (1, 2,))
def test_warp_grid_offset_x1y1_depth1(self, batch_size):
@@ -97,11 +98,11 @@ class TestDepthWarper:
grid_norm = normalize_pixel_coordinates(grid, height, width)
# check offset in x-axis
- assert utils.check_equal_torch(
- grid_norm[..., -1, 0], grid_warped[..., -2, 0])
+ assert_allclose(
+ grid_warped[..., -2, 0], grid_norm[..., -1, 0])
# check that y-axis remain the same
- assert utils.check_equal_torch(
- grid_norm[..., -1, :, 1], grid_warped[..., -2, :, 1])
+ assert_allclose(
+ grid_warped[..., -2, :, 1], grid_norm[..., -1, :, 1])
@pytest.mark.parametrize("batch_size", (1, 2,))
def test_warp_tensor_offset_x1y1(self, batch_size):
@@ -125,7 +126,7 @@ class TestDepthWarper:
patch_src = warper(depth_src, patch_dst)
# compare patches
- assert utils.check_equal_torch(
+ assert_allclose(
patch_dst[..., 1:, 1:], patch_src[..., :2, :4])
@pytest.mark.parametrize("batch_size", (1, 2,))
diff --git a/test/geometry/warp/test_homography_warper.py b/test/geometry/warp/test_homography_warper.py
index a64b02693..21b21a0c1 100644
--- a/test/geometry/warp/test_homography_warper.py
+++ b/test/geometry/warp/test_homography_warper.py
@@ -6,6 +6,7 @@ from test.common import device_type
import torch
from torch.autograd import gradcheck
+from torch.testing import assert_allclose
class TestHomographyWarper:
@@ -24,7 +25,7 @@ class TestHomographyWarper:
# warp from source to destination
patch_dst = warper(patch_src, dst_homo_src)
- assert utils.check_equal_torch(patch_src, patch_dst)
+ assert_allclose(patch_src, patch_dst)
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [
@@ -43,9 +44,9 @@ 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 utils.check_equal_torch(
+ assert_allclose(
warper.grid[..., 0] + offset, flow[..., 0])
- assert utils.check_equal_torch(
+ assert_allclose(
warper.grid[..., 1], flow[..., 1])
@pytest.mark.parametrize("batch_shape", [
@@ -61,13 +62,13 @@ class TestHomographyWarper:
patch_dst = warper(patch_src, dst_homo_src)
# check the corners
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., 0, 0], patch_dst[..., 0, 0])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., 0, -1], patch_dst[..., 0, -1])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., -1, 0], patch_dst[..., -1, 0])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., -1, -1], patch_dst[..., -1, -1])
@pytest.mark.parametrize("shape", [
@@ -83,7 +84,7 @@ class TestHomographyWarper:
# instantiate warper and from source to destination
warper = kornia.HomographyWarper(height, width)
patch_dst = warper(patch_src, dst_homo_src)
- assert utils.check_equal_torch(patch_src[..., 1:], patch_dst[..., :-1])
+ assert_allclose(patch_src[..., 1:], patch_dst[..., :-1])
@pytest.mark.parametrize("batch_shape", [
(1, 1, 3, 5), (2, 2, 4, 3), (3, 1, 2, 3), ])
@@ -103,13 +104,13 @@ class TestHomographyWarper:
patch_dst = warper(patch_src, dst_homo_src)
# check the corners
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., 0, 0], patch_dst[..., 0, -1])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., 0, -1], patch_dst[..., -1, -1])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., -1, 0], patch_dst[..., 0, 0])
- assert utils.check_equal_torch(
+ assert_allclose(
patch_src[..., -1, -1], patch_dst[..., -1, 0])
@pytest.mark.parametrize("batch_size", [1, 2, 3])
@@ -152,7 +153,7 @@ class TestHomographyWarper:
patch_dst_to_src_functional = kornia.homography_warp(
patch_dst, torch.inverse(dst_homo_src_i), (height, width))
- assert utils.check_equal_torch(
+ assert_allclose(
patch_dst_to_src, patch_dst_to_src_functional)
@pytest.mark.parametrize("batch_shape", [
diff --git a/test/test_contrib.py b/test/test_contrib.py
index bd3c4417c..892b0182b 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -46,10 +46,10 @@ class TestExtractTensorPatches:
m = kornia.contrib.ExtractTensorPatches(3)
patches = m(input)
assert patches.shape == (1, 4, 1, 3, 3)
- assert utils.check_equal_torch(input[0, :, :3, :3], patches[0, 0])
- assert utils.check_equal_torch(input[0, :, :3, 1:], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 1:, :3], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 1:, 1:], patches[0, 3])
+ assert_allclose(input[0, :, :3, :3], patches[0, 0])
+ assert_allclose(input[0, :, :3, 1:], patches[0, 1])
+ assert_allclose(input[0, :, 1:, :3], patches[0, 2])
+ assert_allclose(input[0, :, 1:, 1:], patches[0, 3])
def test_b1_ch2_h4w4_ws3(self):
input = torch.arange(16.).view(1, 1, 4, 4)
@@ -57,50 +57,50 @@ class TestExtractTensorPatches:
m = kornia.contrib.ExtractTensorPatches(3)
patches = m(input)
assert patches.shape == (1, 4, 2, 3, 3)
- assert utils.check_equal_torch(input[0, :, :3, :3], patches[0, 0])
- assert utils.check_equal_torch(input[0, :, :3, 1:], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 1:, :3], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 1:, 1:], patches[0, 3])
+ assert_allclose(input[0, :, :3, :3], patches[0, 0])
+ assert_allclose(input[0, :, :3, 1:], patches[0, 1])
+ assert_allclose(input[0, :, 1:, :3], patches[0, 2])
+ assert_allclose(input[0, :, 1:, 1:], patches[0, 3])
def test_b1_ch1_h4w4_ws2(self):
input = torch.arange(16.).view(1, 1, 4, 4)
m = kornia.contrib.ExtractTensorPatches(2)
patches = m(input)
assert patches.shape == (1, 9, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 1:3], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 0:2, 2:4], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 1:3, 1:3], patches[0, 4])
- assert utils.check_equal_torch(input[0, :, 2:4, 1:3], patches[0, 7])
+ assert_allclose(input[0, :, 0:2, 1:3], patches[0, 1])
+ assert_allclose(input[0, :, 0:2, 2:4], patches[0, 2])
+ assert_allclose(input[0, :, 1:3, 1:3], patches[0, 4])
+ assert_allclose(input[0, :, 2:4, 1:3], patches[0, 7])
def test_b1_ch1_h4w4_ws2_stride2(self):
input = torch.arange(16.).view(1, 1, 4, 4)
m = kornia.contrib.ExtractTensorPatches(2, stride=2)
patches = m(input)
assert patches.shape == (1, 4, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:2], patches[0, 0])
- assert utils.check_equal_torch(input[0, :, 0:2, 2:4], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 2:4, 0:2], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 2:4, 2:4], patches[0, 3])
+ assert_allclose(input[0, :, 0:2, 0:2], patches[0, 0])
+ assert_allclose(input[0, :, 0:2, 2:4], patches[0, 1])
+ assert_allclose(input[0, :, 2:4, 0:2], patches[0, 2])
+ assert_allclose(input[0, :, 2:4, 2:4], patches[0, 3])
def test_b1_ch1_h4w4_ws2_stride21(self):
input = torch.arange(16.).view(1, 1, 4, 4)
m = kornia.contrib.ExtractTensorPatches(2, stride=(2, 1))
patches = m(input)
assert patches.shape == (1, 6, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 1:3], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 0:2, 2:4], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 2:4, 0:2], patches[0, 3])
- assert utils.check_equal_torch(input[0, :, 2:4, 2:4], patches[0, 5])
+ assert_allclose(input[0, :, 0:2, 1:3], patches[0, 1])
+ assert_allclose(input[0, :, 0:2, 2:4], patches[0, 2])
+ assert_allclose(input[0, :, 2:4, 0:2], patches[0, 3])
+ assert_allclose(input[0, :, 2:4, 2:4], patches[0, 5])
def test_b1_ch1_h3w3_ws2_stride1_padding1(self):
input = torch.arange(9.).view(1, 1, 3, 3)
m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
patches = m(input)
assert patches.shape == (1, 16, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:2], patches[0, 5])
- assert utils.check_equal_torch(input[0, :, 0:2, 1:3], patches[0, 6])
- assert utils.check_equal_torch(input[0, :, 1:3, 0:2], patches[0, 9])
- assert utils.check_equal_torch(input[0, :, 1:3, 1:3], patches[0, 10])
+ assert_allclose(input[0, :, 0:2, 0:2], patches[0, 5])
+ assert_allclose(input[0, :, 0:2, 1:3], patches[0, 6])
+ assert_allclose(input[0, :, 1:3, 0:2], patches[0, 9])
+ assert_allclose(input[0, :, 1:3, 1:3], patches[0, 10])
def test_b2_ch1_h3w3_ws2_stride1_padding1(self):
batch_size = 2
@@ -110,13 +110,13 @@ class TestExtractTensorPatches:
patches = m(input)
assert patches.shape == (batch_size, 16, 1, 2, 2)
for i in range(batch_size):
- assert utils.check_equal_torch(
+ assert_allclose(
input[i, :, 0:2, 0:2], patches[i, 5])
- assert utils.check_equal_torch(
+ assert_allclose(
input[i, :, 0:2, 1:3], patches[i, 6])
- assert utils.check_equal_torch(
+ assert_allclose(
input[i, :, 1:3, 0:2], patches[i, 9])
- assert utils.check_equal_torch(
+ assert_allclose(
input[i, :, 1:3, 1:3], patches[i, 10])
def test_b1_ch1_h3w3_ws23(self):
@@ -124,18 +124,18 @@ class TestExtractTensorPatches:
m = kornia.contrib.ExtractTensorPatches((2, 3))
patches = m(input)
assert patches.shape == (1, 2, 1, 2, 3)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:3], patches[0, 0])
- assert utils.check_equal_torch(input[0, :, 1:3, 0:3], patches[0, 1])
+ assert_allclose(input[0, :, 0:2, 0:3], patches[0, 0])
+ assert_allclose(input[0, :, 1:3, 0:3], patches[0, 1])
def test_b1_ch1_h3w4_ws23(self):
input = torch.arange(12.).view(1, 1, 3, 4)
m = kornia.contrib.ExtractTensorPatches((2, 3))
patches = m(input)
assert patches.shape == (1, 4, 1, 2, 3)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:3], patches[0, 0])
- assert utils.check_equal_torch(input[0, :, 0:2, 1:4], patches[0, 1])
- assert utils.check_equal_torch(input[0, :, 1:3, 0:3], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 1:3, 1:4], patches[0, 3])
+ assert_allclose(input[0, :, 0:2, 0:3], patches[0, 0])
+ assert_allclose(input[0, :, 0:2, 1:4], patches[0, 1])
+ assert_allclose(input[0, :, 1:3, 0:3], patches[0, 2])
+ assert_allclose(input[0, :, 1:3, 1:4], patches[0, 3])
def test_jit(self):
@torch.jit.script
diff --git a/test/test_losses.py b/test/test_losses.py
index 03595eb31..d70df551c 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -6,6 +6,7 @@ from test.common import device_type
import torch
from torch.autograd import gradcheck
+from torch.testing import assert_allclose
class TestFocalLoss:
@@ -214,7 +215,7 @@ def test_ssim(batch_shape, device_type, window_size, reduction_type):
assert pytest.approx(ssim(img2, img2).sum().item(), 0.0)
# functional
- assert utils.check_equal_torch(
+ assert_allclose(
ssim_loss_val,
kornia.losses.ssim(img1, img2, window_size, reduction_type),
)
diff --git a/test/utils/test_grid.py b/test/utils/test_grid.py
index ba6d0b101..fe3540fe9 100644
--- a/test/utils/test_grid.py
+++ b/test/utils/test_grid.py
@@ -4,6 +4,7 @@ import torch
import kornia as kornia
import kornia.testing as utils # test utils
+from torch.testing import assert_allclose
def test_create_meshgrid():
height, width = 4, 6
@@ -39,5 +40,5 @@ def test_normalize_pixel_grid():
# transform grids
grid_pix_to_norm = kornia.transform_points(norm_trans_pix, grid_pix)
grid_norm_to_pix = kornia.transform_points(pix_trans_norm, grid_norm)
- assert utils.check_equal_torch(grid_pix, grid_norm_to_pix)
- assert utils.check_equal_torch(grid_norm, grid_pix_to_norm)
+ assert_allclose(grid_pix, grid_norm_to_pix)
+ assert_allclose(grid_norm, grid_pix_to_norm)
diff --git a/test/utils/test_metrics.py b/test/utils/test_metrics.py
index 5ace5f81c..fa41fd7a7 100644
--- a/test/utils/test_metrics.py
+++ b/test/utils/test_metrics.py
@@ -4,6 +4,8 @@ import torch
import kornia as kornia
import kornia.testing as utils
+from torch.testing import assert_allclose
+
class TestMeanIoU:
def test_two_classes_perfect(self):
@@ -18,7 +20,7 @@ class TestMeanIoU:
mean_iou_real = torch.tensor(
[[1.0, 1.0]], dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
- assert utils.check_equal_torch(mean_iou, mean_iou_real)
+ assert_allclose(mean_iou, mean_iou_real)
def test_two_classes_perfect_batch2(self):
batch_size = 2
@@ -32,7 +34,7 @@ class TestMeanIoU:
mean_iou_real = torch.tensor(
[[1.0, 1.0]], dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
- assert utils.check_equal_torch(mean_iou, mean_iou_real)
+ assert_allclose(mean_iou, mean_iou_real)
def test_two_classes(self):
batch_size = 1
@@ -47,7 +49,7 @@ class TestMeanIoU:
mean_iou_real = torch.tensor(
[[0.75, 0.80]], dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
- assert utils.check_equal_torch(mean_iou, mean_iou_real)
+ assert_allclose(mean_iou, mean_iou_real)
def test_four_classes_2d_perfect(self):
batch_size = 1
@@ -67,7 +69,7 @@ class TestMeanIoU:
mean_iou_real = torch.tensor(
[[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
- assert utils.check_equal_torch(mean_iou, mean_iou_real)
+ assert_allclose(mean_iou, mean_iou_real)
def test_four_classes_one_missing(self):
batch_size = 1
@@ -87,7 +89,7 @@ class TestMeanIoU:
mean_iou_real = torch.tensor(
[[0.0, 1.0, 0.5, 0.5]], dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
- assert utils.check_equal_torch(mean_iou, mean_iou_real)
+ assert_allclose(mean_iou, mean_iou_real)
class TestConfusionMatrix:
@@ -103,7 +105,7 @@ class TestConfusionMatrix:
conf_mat_real = torch.tensor(
[[[3, 1],
[0, 4]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_two_classes_batch2(self):
batch_size = 2
@@ -118,7 +120,7 @@ class TestConfusionMatrix:
conf_mat_real = torch.tensor(
[[[3, 1],
[0, 4]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_three_classes(self):
num_classes = 3
@@ -133,7 +135,7 @@ class TestConfusionMatrix:
[[[4, 1, 2],
[3, 0, 2],
[1, 2, 1]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_four_classes_one_missing(self):
num_classes = 4
@@ -149,7 +151,7 @@ class TestConfusionMatrix:
[0, 4, 1, 2],
[0, 3, 0, 2],
[0, 1, 2, 1]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_three_classes_normalized(self):
num_classes = 3
@@ -166,7 +168,7 @@ class TestConfusionMatrix:
[[[0.5000, 0.3333, 0.4000],
[0.3750, 0.0000, 0.4000],
[0.1250, 0.6667, 0.2000]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_four_classes_2d_perfect(self):
num_classes = 4
@@ -188,7 +190,7 @@ class TestConfusionMatrix:
[0, 4, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 4]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_nonperfect(self):
num_classes = 4
@@ -210,7 +212,7 @@ class TestConfusionMatrix:
[1, 3, 0, 0],
[0, 0, 4, 0],
[0, 1, 0, 3]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_missing(self):
num_classes = 4
@@ -232,7 +234,7 @@ class TestConfusionMatrix:
[0, 4, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 4]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_no_predicted(self):
num_classes = 4
@@ -254,4 +256,4 @@ class TestConfusionMatrix:
[0, 0, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 4]]], dtype=torch.float32)
- assert utils.check_equal_torch(conf_mat, conf_mat_real)
+ assert_allclose(conf_mat, conf_mat_real)