Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

small refactor in test

edgarriba048efccd006e
diff --git a/test/utils.py b/kornia/testing/__init__.py
similarity index 93%
rename from test/utils.py
rename to kornia/testing/__init__.py
index f6abbb64a..21ed5c50f 100644
--- a/test/utils.py
+++ b/kornia/testing/__init__.py
@@ -1,8 +1,14 @@
+"""
+The testing package contains testing-specific utilities.
+"""
+
+
import torch
import numpy as np
-
-# test utilites
+__all__ = [
+ 'tensor_to_gradcheck_var', 'create_eye_batch',
+]
def create_pinhole(fx, fy, cx, cy, height, width, rx, ry, rz, tx, ty, tz):
diff --git a/test/__init__.py b/test/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index efcd52b95..f1ed242af 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.py
@@ -1,35 +1,35 @@
import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from common import device_type
-
-import kornia as K
-import kornia.color as color
-import utils
class TestRgbToGrayscale:
def test_rgb_to_grayscale(self):
channels, height, width = 3, 4, 5
img = torch.ones(channels, height, width)
- assert K.rgb_to_grayscale(img).shape == (1, height, width)
+ assert kornia.rgb_to_grayscale(img).shape == (1, height, width)
def test_rgb_to_grayscale_batch(self):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width)
- assert K.rgb_to_grayscale(img).shape == \
+ assert kornia.rgb_to_grayscale(img).shape == \
(batch_size, 1, height, width)
def test_gradcheck(self):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(K.rgb_to_grayscale, (img,), raise_exception=True)
+ assert gradcheck(kornia.rgb_to_grayscale, (img,), raise_exception=True)
def test_jit(self):
batch_size, channels, height, width = 2, 3, 64, 64
img = torch.ones(batch_size, channels, height, width)
- gray = color.RgbToGrayscale()
- gray_traced = torch.jit.trace(color.RgbToGrayscale(), img)
+ gray = kornia.color.RgbToGrayscale()
+ gray_traced = torch.jit.trace(kornia.color.RgbToGrayscale(), img)
assert_allclose(gray(img), gray_traced(img))
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index 2c83bc62b..1cc794e92 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.py
@@ -1,11 +1,12 @@
import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from common import device_type
-
-import kornia.color as color
-import utils
class TestRgbToHsv:
@@ -30,7 +31,7 @@ class TestRgbToHsv:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]])
- f = color.RgbToHsv()
+ f = kornia.color.RgbToHsv()
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
def test_batch_rgb_to_hsv(self):
@@ -52,7 +53,7 @@ class TestRgbToHsv:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]]) # 3x2x2
- f = color.RgbToHsv()
+ f = kornia.color.RgbToHsv()
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
@@ -70,14 +71,14 @@ class TestRgbToHsv:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(color.RgbToHsv(), (data,),
+ assert gradcheck(kornia.color.RgbToHsv(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
- return color.rgb_to_hsv(data)
+ return kornia.rgb_to_hsv(data)
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -88,7 +89,7 @@ class TestRgbToHsv:
[8., 8.]]]]) # 3x2x2
actual = op_script(data)
- expected = color.rgb_to_hsv(data)
+ expected = kornia.rgb_to_hsv(data)
assert_allclose(actual, expected)
@@ -114,7 +115,7 @@ class TestHsvToRgb:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]])
- f = color.HsvToRgb()
+ f = kornia.color.HsvToRgb()
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)
def test_batch_hsv_to_rgb(self):
@@ -137,7 +138,7 @@ class TestHsvToRgb:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]]) # 3x2x2
- f = color.HsvToRgb()
+ f = kornia.color.HsvToRgb()
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)
@@ -145,7 +146,7 @@ class TestHsvToRgb:
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
- return color.hsv_to_rgb(data)
+ return kornia.hsv_to_rgb(data)
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -157,5 +158,5 @@ class TestHsvToRgb:
[8., 8.]]]]) # 3x2x2
actual = op_script(data)
- expected = color.hsv_to_rgb(data)
+ expected = kornia.hsv_to_rgb(data)
assert_allclose(actual, expected)
diff --git a/test/color/test_normalize.py b/test/color/test_normalize.py
index 76c028fe7..5c20b9185 100644
--- a/test/color/test_normalize.py
+++ b/test/color/test_normalize.py
@@ -1,11 +1,12 @@
import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from common import device_type
-
-import kornia.color as color
-import utils
class TestNormalize:
@@ -13,7 +14,7 @@ class TestNormalize:
mean = [0.5]
std = [0.1]
repr = 'Normalize(mean=[0.5], std=[0.1])'
- assert str(color.Normalize(mean, std)) == repr
+ assert str(kornia.color.Normalize(mean, std)) == repr
def test_normalize(self):
@@ -25,7 +26,7 @@ class TestNormalize:
# expected output
expected = torch.tensor([0.25]).repeat(1, 2, 2).view_as(data)
- f = color.Normalize(mean, std)
+ f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_broadcast_normalize(self):
@@ -40,7 +41,7 @@ class TestNormalize:
# expected output
expected = torch.tensor([1.25, 1, 0.5]).repeat(2, 1, 1).view_as(data)
- f = color.Normalize(mean, std)
+ f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_batch_normalize(self):
@@ -55,15 +56,14 @@ class TestNormalize:
# expected output
expected = torch.tensor([1.25, 1, 0.5]).repeat(2, 1, 1).view_as(data)
- f = color.Normalize(mean, std)
+ f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor, mean: torch.Tensor,
std: torch.Tensor) -> torch.Tensor:
-
- return color.normalize(data, mean, std)
+ return kornia.normalize(data, mean, std)
data = torch.ones(2, 3, 1, 1)
data += 2
@@ -85,5 +85,5 @@ class TestNormalize:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(color.Normalize(mean, std), (data,),
+ assert gradcheck(kornia.color.Normalize(mean, std), (data,),
raise_exception=True)
diff --git a/test/color/test_rgb.py b/test/color/test_rgb.py
index b0663a96a..a7a61b094 100644
--- a/test/color/test_rgb.py
+++ b/test/color/test_rgb.py
@@ -1,11 +1,12 @@
import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from common import device_type
-
-import kornia.color as color
-import utils
class TestBgrToRgb:
@@ -31,7 +32,7 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]) # 3x2x2
- f = color.BgrToRgb()
+ f = kornia.color.BgrToRgb()
assert_allclose(f(data), expected)
def test_batch_bgr_to_rgb(self):
@@ -73,7 +74,7 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
- f = color.BgrToRgb()
+ f = kornia.color.BgrToRgb()
out = f(data)
assert_allclose(out, expected)
@@ -91,14 +92,13 @@ class TestBgrToRgb:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(color.BgrToRgb(), (data,),
+ assert gradcheck(kornia.color.BgrToRgb(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
-
- return color.bgr_to_rgb(data)
+ return kornia.bgr_to_rgb(data)
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -110,7 +110,7 @@ class TestBgrToRgb:
[3., 3.]]]) # 3x2x2
actual = op_script(data)
- expected = color.bgr_to_rgb(data)
+ expected = kornia.bgr_to_rgb(data)
assert_allclose(actual, expected)
@@ -137,7 +137,7 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]) # 3x2x2
- f = color.RgbToBgr()
+ f = kornia.color.RgbToBgr()
assert_allclose(f(data), expected)
def test_gradcheck(self):
@@ -154,14 +154,13 @@ class TestRgbToBgr:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(color.RgbToBgr(), (data,),
+ assert gradcheck(kornia.color.RgbToBgr(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
-
- return color.rgb_to_bgr(data)
+ return kornia.rgb_to_bgr(data)
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -173,7 +172,7 @@ class TestRgbToBgr:
[3., 3.]]]) # 3x2x2
actual = op_script(data)
- expected = color.rgb_to_bgr(data)
+ expected = kornia.rgb_to_bgr(data)
assert_allclose(actual, expected)
def test_batch_rgb_to_bgr(self):
@@ -215,6 +214,6 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
- f = color.RgbToBgr()
+ f = kornia.color.RgbToBgr()
out = f(data)
assert_allclose(out, expected)
diff --git a/test/feature/test_harris.py b/test/feature/test_harris.py
index b383a65a6..00e7da633 100644
--- a/test/feature/test_harris.py
+++ b/test/feature/test_harris.py
@@ -1,12 +1,12 @@
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+
+import torch
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestCornerHarris:
def test_shape(self):
diff --git a/test/filters/test_blur.py b/test/filters/test_blur.py
index 55412a13c..1a315b9b1 100644
--- a/test/filters/test_blur.py
+++ b/test/filters/test_blur.py
@@ -1,13 +1,14 @@
-import pytest
from typing import Tuple
+import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+
import torch
-import kornia as kornia
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestBoxBlur:
def test_shape(self):
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index daa6d87d7..ee481b99f 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -1,17 +1,18 @@
import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from common import device_type
-
-import kornia.filters as filters
-import utils
@pytest.mark.parametrize("window_size", [5, 11])
@pytest.mark.parametrize("sigma", [1.5, 5.0])
def test_get_gaussian_kernel(window_size, sigma):
- kernel = filters.get_gaussian_kernel(window_size, sigma)
+ kernel = kornia.get_gaussian_kernel(window_size, sigma)
assert kernel.shape == (window_size,)
assert kernel.sum().item() == pytest.approx(1.0)
@@ -20,7 +21,7 @@ def test_get_gaussian_kernel(window_size, sigma):
@pytest.mark.parametrize("ksize_y", [3, 7])
@pytest.mark.parametrize("sigma", [1.5, 2.1])
def test_get_gaussian_kernel2d(ksize_x, ksize_y, sigma):
- kernel = filters.get_gaussian_kernel2d(
+ kernel = kornia.get_gaussian_kernel2d(
(ksize_x, ksize_y), (sigma, sigma))
assert kernel.shape == (ksize_x, ksize_y)
assert kernel.sum().item() == pytest.approx(1.0)
@@ -34,7 +35,7 @@ class TestGaussianBlur:
sigma = (1.5, 2.1)
input = torch.rand(batch_shape).to(torch.device(device_type))
- gauss = filters.GaussianBlur(kernel_size, sigma)
+ gauss = kornia.filters.GaussianBlur(kernel_size, sigma)
assert gauss(input).shape == batch_shape
def test_gradcheck(self):
@@ -46,32 +47,32 @@ class TestGaussianBlur:
# evaluate function gradient
input = torch.rand(batch_shape)
input = utils.tensor_to_gradcheck_var(input) # to var
- assert gradcheck(filters.gaussian_blur, (input, kernel_size, sigma,),
+ assert gradcheck(kornia.gaussian_blur, (input, kernel_size, sigma,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(img):
- return filters.gaussian_blur(img, (5, 5), (1.2, 1.2))
+ return kornia.gaussian_blur(img, (5, 5), (1.2, 1.2))
batch_size, channels, height, width = 2, 3, 64, 64
img = torch.ones(batch_size, channels, height, width)
- expected = filters.GaussianBlur((5, 5), (1.2, 1.2))(img)
+ expected = kornia.filters.GaussianBlur((5, 5), (1.2, 1.2))(img)
actual = op_script(img)
assert_allclose(actual, expected)
@pytest.mark.parametrize("window_size", [5])
def test_get_laplacian_kernel(window_size):
- kernel = filters.get_laplacian_kernel(window_size)
+ kernel = kornia.get_laplacian_kernel(window_size)
assert kernel.shape == (window_size,)
assert kernel.sum().item() == pytest.approx(0.0)
@pytest.mark.parametrize("window_size", [7])
def test_get_laplacian_kernel2d(window_size):
- kernel = filters.get_laplacian_kernel2d(window_size)
+ kernel = kornia.get_laplacian_kernel2d(window_size)
assert kernel.shape == (window_size, window_size)
assert kernel.sum().item() == pytest.approx(0.0)
expected = torch.tensor([[1., 1., 1., 1., 1., 1., 1.],
@@ -91,7 +92,7 @@ class TestLaplacian:
kernel_size = 5
input = torch.rand(batch_shape).to(torch.device(device_type))
- laplace = filters.Laplacian(kernel_size)
+ laplace = kornia.filters.Laplacian(kernel_size)
assert laplace(input).shape == batch_shape
def test_gradcheck(self):
@@ -102,5 +103,5 @@ class TestLaplacian:
# evaluate function gradient
input = torch.rand(batch_shape)
input = utils.tensor_to_gradcheck_var(input)
- assert gradcheck(filters.laplacian, (input, kernel_size,),
+ assert gradcheck(kornia.laplacian, (input, kernel_size,),
raise_exception=True)
diff --git a/test/filters/test_median.py b/test/filters/test_median.py
index ab3a5e08b..19116ef20 100644
--- a/test/filters/test_median.py
+++ b/test/filters/test_median.py
@@ -1,13 +1,14 @@
-import pytest
from typing import Tuple
+import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+
import torch
-import kornia as kornia
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestMedianBlur:
def test_shape(self):
diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.py
index ef531a1e8..cffaece80 100644
--- a/test/filters/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -1,12 +1,12 @@
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+
+import torch
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestSpatialGradient:
def test_shape(self):
diff --git a/test/geometry/__init__.py b/test/geometry/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index 5358cfb21..5f13e103f 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -2,13 +2,12 @@ import pytest
import numpy as np
import torch
-import kornia as kornia
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import utils # test utils
-from utils import check_equal_torch, check_equal_numpy
-from common import device_type
+import kornia as kornia
+from kornia.testing import tensor_to_gradcheck_var, create_eye_batch
+from ..common import device_type
# based on:
@@ -31,26 +30,26 @@ class TestAngleAxisToQuaternion:
angle_axis = torch.Tensor([0, 0, 0])
expected = torch.Tensor([1, 0, 0, 0])
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert utils.check_equal_torch(quaternion, expected)
+ assert_allclose(quaternion, expected)
def test_small_angle(self):
theta = 1e-2
angle_axis = torch.Tensor([theta, 0, 0])
expected = torch.Tensor([np.cos(theta / 2), np.sin(theta / 2), 0, 0])
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert utils.check_equal_torch(quaternion, expected)
+ assert_allclose(quaternion, expected)
def test_x_rotation(self):
half_sqrt2 = 0.5 * np.sqrt(2)
angle_axis = torch.Tensor([kornia.pi / 2, 0, 0])
expected = torch.Tensor([half_sqrt2, half_sqrt2, 0, 0])
quaternion = kornia.angle_axis_to_quaternion(angle_axis)
- assert utils.check_equal_torch(quaternion, expected)
+ assert_allclose(quaternion, expected)
def test_gradcheck(self):
eps = 1e-12
angle_axis = torch.Tensor([0, 0, 0]) + eps
- angle_axis = utils.tensor_to_gradcheck_var(angle_axis)
+ angle_axis = tensor_to_gradcheck_var(angle_axis)
# evaluate function gradient
assert gradcheck(kornia.angle_axis_to_quaternion, (angle_axis,),
raise_exception=True)
@@ -73,38 +72,38 @@ class TestQuaternionToAngleAxis:
quaternion = torch.Tensor([1, 0, 0, 0])
expected = torch.Tensor([0, 0, 0])
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert utils.check_equal_torch(angle_axis, expected)
+ assert_allclose(angle_axis, expected)
def test_y_rotation(self):
quaternion = torch.Tensor([0, 0, 1, 0])
expected = torch.Tensor([0, kornia.pi, 0])
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert utils.check_equal_torch(angle_axis, expected)
+ assert_allclose(angle_axis, expected)
def test_z_rotation(self):
quaternion = torch.Tensor([np.sqrt(3) / 2, 0, 0, 0.5])
expected = torch.Tensor([0, 0, kornia.pi / 3])
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert utils.check_equal_torch(angle_axis, expected)
+ assert_allclose(angle_axis, expected)
def test_small_angle(self):
theta = 1e-2
quaternion = torch.Tensor([np.cos(theta / 2), np.sin(theta / 2), 0, 0])
expected = torch.Tensor([theta, 0, 0])
angle_axis = kornia.quaternion_to_angle_axis(quaternion)
- assert utils.check_equal_torch(angle_axis, expected)
+ assert_allclose(angle_axis, expected)
def test_gradcheck(self):
eps = 1e-12
quaternion = torch.Tensor([1, 0, 0, 0]) + eps
- quaternion = utils.tensor_to_gradcheck_var(quaternion)
+ quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_to_angle_axis, (quaternion,),
raise_exception=True)
def test_pi():
- assert pytest.approx(kornia.pi.item(), 3.141592)
+ assert_allclose(kornia.pi, 3.141592)
@pytest.mark.parametrize("batch_shape", [
@@ -119,10 +118,10 @@ def test_rad2deg(batch_shape, device_type):
x_deg_to_rad = kornia.deg2rad(x_deg)
# compute error
- error = utils.compute_mse(x_rad, x_deg_to_rad)
+ assert_allclose(x_rad, x_deg_to_rad)
# evaluate function gradient
- assert gradcheck(kornia.rad2deg, (utils.tensor_to_gradcheck_var(x_rad),),
+ assert gradcheck(kornia.rad2deg, (tensor_to_gradcheck_var(x_rad),),
raise_exception=True)
@@ -137,11 +136,9 @@ def test_deg2rad(batch_shape, device_type):
x_rad = kornia.deg2rad(x_deg)
x_rad_to_deg = kornia.rad2deg(x_rad)
- # compute error
- error = utils.compute_mse(x_deg, x_rad_to_deg)
- assert pytest.approx(error.item(), 0.0)
+ assert_allclose(x_deg, x_rad_to_deg)
- assert gradcheck(kornia.deg2rad, (utils.tensor_to_gradcheck_var(x_deg),),
+ assert gradcheck(kornia.deg2rad, (tensor_to_gradcheck_var(x_deg),),
raise_exception=True)
@@ -159,7 +156,7 @@ def test_convert_points_to_homogeneous(batch_shape, device_type):
assert (points_h[..., -1] == torch.ones(points_h[..., -1].shape)).all()
# evaluate function gradient
- points = utils.tensor_to_gradcheck_var(points) # to var
+ points = tensor_to_gradcheck_var(points) # to var
assert gradcheck(kornia.convert_points_to_homogeneous, (points,),
raise_exception=True)
@@ -215,7 +212,7 @@ class TestConvertPointsFromHomogeneous:
points_h = torch.rand(batch_shape)
# evaluate function gradient
- points_h = utils.tensor_to_gradcheck_var(points_h) # to var
+ points_h = tensor_to_gradcheck_var(points_h) # to var
assert gradcheck(kornia.convert_points_from_homogeneous, (points_h,),
raise_exception=True)
@@ -225,17 +222,17 @@ def test_angle_axis_to_rotation_matrix(batch_size, device_type):
# generate input data
device = torch.device(device_type)
angle_axis = torch.rand(batch_size, 3).to(device)
- eye_batch = utils.create_eye_batch(batch_size, 4).to(device)
+ eye_batch = create_eye_batch(batch_size, 4).to(device)
# 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 check_equal_torch(rotation_matrix_eye, eye_batch)
+ assert_allclose(rotation_matrix_eye, eye_batch)
# evaluate function gradient
- angle_axis = utils.tensor_to_gradcheck_var(angle_axis) # to var
+ angle_axis = tensor_to_gradcheck_var(angle_axis) # to var
assert gradcheck(kornia.angle_axis_to_rotation_matrix, (angle_axis,),
raise_exception=True)
@@ -246,7 +243,7 @@ def test_rtvec_to_pose_gradcheck(batch_size, device_type):
rtvec = torch.rand(batch_size, 6).to(torch.device(device_type))
# evaluate function gradient
- rtvec = utils.tensor_to_gradcheck_var(rtvec) # to var
+ rtvec = tensor_to_gradcheck_var(rtvec) # to var
assert gradcheck(kornia.rtvec_to_pose, (rtvec,), raise_exception=True)
@@ -256,7 +253,7 @@ def test_rotation_matrix_to_angle_axis_gradcheck(batch_size, device_type):
rmat = torch.rand(batch_size, 3, 4).to(torch.device(device_type))
# evaluate function gradient
- rmat = utils.tensor_to_gradcheck_var(rmat) # to var
+ rmat = tensor_to_gradcheck_var(rmat) # to var
assert gradcheck(kornia.rotation_matrix_to_angle_axis,
(rmat,), raise_exception=True)
@@ -275,7 +272,7 @@ def test_rotation_matrix_to_angle_axis(device_type):
rmat = torch.stack([rmat_2, rmat_1], dim=0).to(device)
rvec = torch.stack([rvec_2, rvec_1], dim=0).to(device)
- assert check_equal_torch(kornia.rotation_matrix_to_angle_axis(rmat), rvec)
+ assert_allclose(kornia.rotation_matrix_to_angle_axis(rmat), rvec)
class TestNormalizePixelCoordinates:
@@ -346,3 +343,73 @@ class TestNormalizePixelCoordinates:
grid, height, width)
assert_allclose(actual, expected)
+
+
+'''class TestDenormalizePixelCoordinates:
+ def test_tensor_bhw2(self):
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=True)
+
+ expected = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+
+ grid_norm = kornia.denormalize_pixel_coordinates(
+ grid, height, width)
+
+ assert_allclose(grid_norm, expected)
+
+ def test_list(self):
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+ grid = grid.contiguous().view(-1, 2)
+
+ expected = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=True)
+ expected = expected.contiguous().view(-1, 2)
+
+ grid_norm = kornia.normalize_pixel_coordinates(
+ grid, height, width)
+
+ assert_allclose(grid_norm, expected)
+
+ def test_jit(self):
+ @torch.jit.script
+ def op_script(input: torch.Tensor, height: int,
+ width: int) -> torch.Tensor:
+ return kornia.normalize_pixel_coordinates(input, height, width)
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+
+ actual = op_script(grid, height, width)
+ expected = kornia.normalize_pixel_coordinates(
+ grid, height, width)
+
+ assert_allclose(actual, expected)
+
+ def test_jit_trace(self):
+ @torch.jit.script
+ def op_script(input, height, width):
+ return kornia.normalize_pixel_coordinates(input, height, width)
+ # 1. Trace op
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=False)
+ op_traced = torch.jit.trace(
+ op_script,
+ (grid, torch.tensor(height), torch.tensor(width),))
+
+ # 2. Generate new input
+ height, width = 2, 5
+ grid = kornia.utils.create_meshgrid(
+ height, width, normalized_coordinates=False).repeat(2, 1, 1, 1)
+
+ # 3. Evaluate
+ actual = op_traced(
+ grid, torch.tensor(height), torch.tensor(width))
+ expected = kornia.normalize_pixel_coordinates(
+ grid, height, width)
+
+ assert_allclose(actual, expected)'''
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index 1969088f1..7f95b7742 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -1,12 +1,12 @@
import pytest
+import kornia
+import kornia.testing as utils # test utils
+from test.common import TEST_DEVICES
+
import torch
-import kornia as kornia
from torch.autograd import gradcheck
-import utils # test utilities
-from common import TEST_DEVICES
-
def identity_matrix(batch_size):
r"""Creates a batched homogeneous identity matrix"""
diff --git a/test/geometry/test_pinhole.py b/test/geometry/test_pinhole.py
index 394796d31..2d654df04 100644
--- a/test/geometry/test_pinhole.py
+++ b/test/geometry/test_pinhole.py
@@ -1,13 +1,13 @@
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
+import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import utils # test utilities
-from common import device_type
-
class TestPinholeCamera:
def _create_intrinsics(self, batch_size, fx, fy, cx, cy):
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index 8b2534712..512f5397a 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -1,13 +1,13 @@
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
+import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import utils
-from common import device_type
-
class TestRotate:
def test_angle90(self):
diff --git a/test/geometry/transform/test_crop.py b/test/geometry/transform/test_crop.py
index aa87b08cb..bc2ec2d7b 100644
--- a/test/geometry/transform/test_crop.py
+++ b/test/geometry/transform/test_crop.py
@@ -2,13 +2,13 @@ from typing import Tuple
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+
+import torch
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestCropAndResize:
def test_crop(self):
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index 5df99d69c..e47b42cd2 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -1,13 +1,13 @@
import pytest
-import torch
import kornia as kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
+import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import utils # test utils
-from common import device_type
-
@pytest.mark.parametrize("batch_shape", [(1, 1, 7, 32), (2, 3, 16, 31)])
def test_warp_perspective_rotation(batch_shape, device_type):
diff --git a/test/geometry/transform/test_pyramid.py b/test/geometry/transform/test_pyramid.py
index e12bed39a..beaf13bbc 100644
--- a/test/geometry/transform/test_pyramid.py
+++ b/test/geometry/transform/test_pyramid.py
@@ -1,12 +1,12 @@
import pytest
+import kornia
+import kornia.testing as utils # test utils
+
import torch
-import kornia as kornia
from torch.testing import assert_allclose
from torch.autograd import gradcheck
-import utils # test utils
-
class TestPyrUp:
def test_shape(self):
diff --git a/test/geometry/warp/test_depth_warper.py b/test/geometry/warp/test_depth_warper.py
index 1703dd6e1..8764f3297 100644
--- a/test/geometry/warp/test_depth_warper.py
+++ b/test/geometry/warp/test_depth_warper.py
@@ -1,12 +1,12 @@
import pytest
+import kornia
+import kornia.testing as utils # test utils
+from kornia.geometry.conversions import normalize_pixel_coordinates
+
import torch
-import kornia as kornia
from torch.autograd import gradcheck
-import utils # test utils
-from kornia.geometry.conversions import normalize_pixel_coordinates
-
class TestDepthWarper:
eps = 1e-6
diff --git a/test/geometry/warp/test_homography_warper.py b/test/geometry/warp/test_homography_warper.py
index 069042e3c..a64b02693 100644
--- a/test/geometry/warp/test_homography_warper.py
+++ b/test/geometry/warp/test_homography_warper.py
@@ -1,11 +1,11 @@
import pytest
-import torch
import kornia as kornia
-from torch.autograd import gradcheck
+import kornia.testing as utils # test utils
+from test.common import device_type
-import utils # test utils
-from common import device_type
+import torch
+from torch.autograd import gradcheck
class TestHomographyWarper:
diff --git a/test/test_contrib.py b/test/test_contrib.py
index bde0730b4..ce7d2768c 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -1,12 +1,12 @@
import pytest
+import kornia
+import kornia.testing as utils # test utils
+
import torch
-import kornia as kornia
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import utils
-
class TestMaxBlurPool2d:
def test_shape(self):
diff --git a/test/test_losses.py b/test/test_losses.py
index 4b9892e80..45be01517 100644
--- a/test/test_losses.py
+++ b/test/test_losses.py
@@ -1,12 +1,12 @@
import pytest
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
import torch
-import kornia as kornia
from torch.autograd import gradcheck
-import utils
-from common import device_type
-
class TestFocalLoss:
def _test_smoke_none(self):
diff --git a/test/utils/test_grid.py b/test/utils/test_grid.py
index c802baea6..ba6d0b101 100644
--- a/test/utils/test_grid.py
+++ b/test/utils/test_grid.py
@@ -2,8 +2,7 @@ import pytest
import torch
import kornia as kornia
-
-import utils # test utils
+import kornia.testing as utils # test utils
def test_create_meshgrid():
diff --git a/test/utils/test_image.py b/test/utils/test_image.py
index 91f93ea19..4f97bb053 100644
--- a/test/utils/test_image.py
+++ b/test/utils/test_image.py
@@ -3,8 +3,7 @@ import numpy as np
import torch
import kornia as kornia
-
-import utils # test utils
+import kornia.testing as utils # test utils
@pytest.mark.parametrize("batch_shape",
diff --git a/test/utils/test_metrics.py b/test/utils/test_metrics.py
index d93376ac7..5ace5f81c 100644
--- a/test/utils/test_metrics.py
+++ b/test/utils/test_metrics.py
@@ -2,9 +2,7 @@ import pytest
import torch
import kornia as kornia
-
-import utils
-from common import device_type
+import kornia.testing as utils
class TestMeanIoU:
diff --git a/test/utils/test_one_hot.py b/test/utils/test_one_hot.py
index d66abcbca..068938de8 100644
--- a/test/utils/test_one_hot.py
+++ b/test/utils/test_one_hot.py
@@ -2,8 +2,7 @@ import pytest
import torch
import kornia as kornia
-
-import utils # test utils
+import kornia.testing as utils # test utils
class TestOneHot: