Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix cuda test issues

edgarriba25c4437dbca6
diff --git a/test/color/test_adjust.py b/test/color/test_adjust.py
index c8d5c1f74..5bfde6bbd 100644
--- a/test/color/test_adjust.py
+++ b/test/color/test_adjust.py
@@ -252,7 +252,7 @@ class TestAdjustGamma:
f = kornia.color.AdjustGamma(torch.tensor([2., 2.]), gain=torch.ones(2))
assert_allclose(f(data), expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width)
img = img.to(device)
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
index a5088e838..9ddf27fb6 100644
--- a/test/color/test_hls.py
+++ b/test/color/test_hls.py
@@ -21,44 +21,31 @@ class TestRgbToHls:
data = torch.rand(3, 5, 5).to(device)
# OpenCV
- data_cv = data.cpu().detach().numpy().transpose(1, 2, 0)
+ data_cv = kornia.tensor_to_image(data.clone())
+
expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)
- expected = torch.from_numpy(expected).to(device)
+ expected = kornia.image_to_tensor(expected, True).to(device)
+ expected[0] = 2 * math.pi * expected[0] / 360.
- h_expected = 2 * math.pi * expected[:, :, 0] / 360.
- l_expected = expected[:, :, 1]
- s_expected = expected[:, :, 2]
-
f = kornia.color.RgbToHls()
- result = f(data)
-
- h = result[0, :, :]
- l = result[1, :, :]
- s = result[2, :, :]
-
- assert_allclose(h, h_expected)
- assert_allclose(l, l_expected)
- assert_allclose(s, s_expected)
+ assert_allclose(f(data), expected)
- def test_batch_rgb_to_hls(self):
+ def test_batch_rgb_to_hls(self, device):
- data = torch.rand(3, 5, 5)
+ data = torch.rand(3, 5, 5).to(device)
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0)
- expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)
+ data_cv = kornia.tensor_to_image(data.clone())
- expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.
- expected = expected.transpose(2, 0, 1)
-
- data = data.to(device)
- expected = expected.to(device)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)
+ expected = kornia.image_to_tensor(expected, False).to(device)
+ expected[:, 0] = 2 * math.pi * expected[:, 0] / 360.
# Kornia
f = kornia.color.RgbToHls()
data = data.repeat(2, 1, 1, 1) # 2x3x5x5
- expected = expected[None].repeat(2, 1, 1, 1) # 2x3x5x5
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
assert_allclose(f(data), expected)
def test_gradcheck(self, device):
@@ -74,7 +61,7 @@ class TestRgbToHls:
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.rgb_to_hls(data)
-
+
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -97,14 +84,15 @@ class TestHlsToRgb:
data = torch.rand(3, 5, 5).to(device)
# OpenCV
- data_cv = data.cpu().detach().numpy().transpose(1, 2, 0).copy()
+ data_cv = kornia.tensor_to_image(data.clone())
data_cv[:, :, 0] = 360 * data_cv[:, :, 0]
+
expected = cv2.cvtColor(data_cv, cv2.COLOR_HLS2RGB)
- expected = torch.from_numpy(expected).to(device)
+ expected = kornia.image_to_tensor(expected, True).to(device)
- r_expected = expected[:, :, 0]
- g_expected = expected[:, :, 1]
- b_expected = expected[:, :, 2]
+ r_expected = expected[0]
+ g_expected = expected[1]
+ b_expected = expected[2]
# Kornia
f = kornia.color.HlsToRgb()
@@ -119,25 +107,23 @@ class TestHlsToRgb:
assert_allclose(g, g_expected)
assert_allclose(b, b_expected)
- def test_batch_hls_to_rgb(self):
+ def test_batch_hls_to_rgb(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0).copy()
+ data_cv = kornia.tensor_to_image(data.clone())
data_cv[:, :, 0] = 360 * data_cv[:, :, 0]
- expected = cv2.cvtColor(data_cv, cv2.COLOR_HLS2RGB)
- expected = expected.transpose(2, 0, 1)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HLS2RGB)
+ expected = kornia.image_to_tensor(expected, False).to(device)
# Kornia
f = kornia.color.HlsToRgb()
data[0] = 2 * pi * data[0]
data = data.repeat(2, 1, 1, 1) # 2x3x5x5
-
- expected = np.expand_dims(expected, 0)
- expected = expected.repeat(2, 0) # 2x3x2x2
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
assert_allclose(f(data), expected)
@@ -147,9 +133,9 @@ class TestHlsToRgb:
data[:, 0] -= 4 * pi
assert_allclose(f(data), expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
data[0] = 2 * pi * data[0]
data = utils.tensor_to_gradcheck_var(data) # to var
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index b6b507b35..7b06f75f3 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.py
@@ -18,19 +18,17 @@ class TestRgbToHsv:
def test_rgb_to_hsv(self, device):
- data = torch.rand(3, 5, 5)
+ data = torch.rand(3, 5, 5).to(device)
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0).copy()
+ data_cv = kornia.tensor_to_image(data.clone())
expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)
+ expected = kornia.image_to_tensor(expected, True).to(device)
- h_expected = 2 * math.pi * expected[:, :, 0] / 360.
- s_expected = expected[:, :, 1]
- v_expected = expected[:, :, 2]
+ h_expected = 2 * math.pi * expected[0] / 360.
+ s_expected = expected[1]
+ v_expected = expected[2]
- data = data.to(device)
- expected = expected.to(device)
-
f = kornia.color.RgbToHsv()
result = f(data)
@@ -42,32 +40,28 @@ class TestRgbToHsv:
assert_allclose(s, s_expected)
assert_allclose(v, v_expected)
- def test_batch_rgb_to_hls(self):
+ def test_batch_rgb_to_hsv(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0).copy()
- expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)
+ data_cv = kornia.tensor_to_image(data.clone())
- expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.
- expected = expected.transpose(2, 0, 1)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)
+ expected = kornia.image_to_tensor(expected, False).to(device)
+ expected[:, 0] = 2 * math.pi * expected[:, 0] / 360.
# Kornia
f = kornia.color.RgbToHsv()
data = data.repeat(2, 1, 1, 1) # 2x3x5x5
-
- expected = np.expand_dims(expected, 0)
- expected = expected.repeat(2, 0) # 2x3x5x5
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
assert_allclose(f(data), expected)
- def test_gradcheck(self):
-
- data = torch.rand(3, 5, 5) # 3x2x2
+ def test_gradcheck(self, device):
- data = data.to(device)
+ data = torch.rand(3, 5, 5).to(device) # 3x2x2
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.RgbToHsv(), (data,),
@@ -98,16 +92,18 @@ class TestHsvToRgb:
def test_hsv_to_rgb(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0).copy()
+ data_cv = kornia.tensor_to_image(data.clone())
data_cv[:, :, 0] = 360 * data_cv[:, :, 0]
+
expected = cv2.cvtColor(data_cv, cv2.COLOR_HSV2RGB)
+ expected = kornia.image_to_tensor(expected, True).to(device)
- r_expected = expected[:, :, 0]
- g_expected = expected[:, :, 1]
- b_expected = expected[:, :, 2]
+ r_expected = expected[0]
+ g_expected = expected[1]
+ b_expected = expected[2]
# Kornia
f = kornia.color.HsvToRgb()
@@ -122,25 +118,23 @@ class TestHsvToRgb:
assert_allclose(g, g_expected)
assert_allclose(b, b_expected)
- def test_batch_hsv_to_rgb(self):
+ def test_batch_hsv_to_rgb(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
# OpenCV
- data_cv = data.numpy().transpose(1, 2, 0).copy()
+ data_cv = kornia.tensor_to_image(data.clone())
data_cv[:, :, 0] = 360 * data_cv[:, :, 0]
- expected = cv2.cvtColor(data_cv, cv2.COLOR_HSV2RGB)
- expected = expected.transpose(2, 0, 1)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HSV2RGB)
+ expected = kornia.image_to_tensor(expected, False).to(device)
# Kornia
f = kornia.color.HsvToRgb()
data[0] = 2 * pi * data[0]
data = data.repeat(2, 1, 1, 1) # 2x3x5x5
-
- expected = np.expand_dims(expected, 0)
- expected = expected.repeat(2, 0) # 2x3x5x5
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
assert_allclose(f(data), expected)
@@ -150,9 +144,9 @@ class TestHsvToRgb:
data[:, 0] -= 4 * pi
assert_allclose(f(data), expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
- data = torch.rand(3, 5, 5) # 3x5x5
+ data = torch.rand(3, 5, 5).to(device) # 3x5x5
data[0] = 2 * pi * data[0]
data = utils.tensor_to_gradcheck_var(data) # to var
diff --git a/test/color/test_yuv.py b/test/color/test_yuv.py
index 4abf25eb7..46309e32f 100644
--- a/test/color/test_yuv.py
+++ b/test/color/test_yuv.py
@@ -2,7 +2,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
-from test.common import device_type
+from test.common import device
import torch
from torch.autograd import gradcheck
@@ -13,15 +13,15 @@ class TestRgbYuvConversion:
# Parameterize for CHW and NCHW shapes
@pytest.mark.parametrize('shape', ((3, 4, 5), (2, 3, 4, 5)))
# RGB to YUV and YUV to RGB should be inverse operations
- def test_inverse_operations(self, shape):
- input = torch.rand(*shape)
+ def test_inverse_operations(self, device, shape):
+ input = torch.rand(*shape).to(device)
yuv_to_rgb_converter = kornia.color.YuvToRgb()
rgb_to_yuv_converter = kornia.color.RgbToYuv()
assert_allclose(input, yuv_to_rgb_converter(rgb_to_yuv_converter(input)), rtol=0.005, atol=0.005)
assert_allclose(input, rgb_to_yuv_converter(yuv_to_rgb_converter(input)), rtol=0.005, atol=0.005)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# prepare input data
data = torch.tensor([[[0.1, 0.2],
@@ -31,7 +31,7 @@ class TestRgbYuvConversion:
[0.4, 0.2]],
[[0.3, 0.3],
- [0.5, 0.5]]]) # 3x2x2
+ [0.5, 0.5]]]).to(device) # 3x2x2
data = utils.tensor_to_gradcheck_var(data) # to var
@@ -40,25 +40,25 @@ class TestRgbYuvConversion:
assert gradcheck(kornia.color.RgbToYuv(), (data,),
raise_exception=True)
- def test_rgb_to_yuv_shape(self):
+ def test_rgb_to_yuv_shape(self, device):
channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width)
+ img = torch.ones(channels, height, width).to(device)
assert kornia.rgb_to_yuv(img).shape == (channels, height, width)
- def test_rgb_to_yuv_batch_shape(self):
+ def test_rgb_to_yuv_batch_shape(self, device):
batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
assert kornia.rgb_to_yuv(img).shape == \
(batch_size, channels, height, width)
- def test_yuv_to_rgb_shape(self):
+ def test_yuv_to_rgb_shape(self, device):
channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width)
+ img = torch.ones(channels, height, width).to(device)
assert kornia.yuv_to_rgb(img).shape == (channels, height, width)
- def test_yuv_to_rgb_batch_shape(self):
+ def test_yuv_to_rgb_batch_shape(self, device):
batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
assert kornia.yuv_to_rgb(img).shape == \
(batch_size, channels, height, width)
diff --git a/test/filters/test_motion.py b/test/filters/test_motion.py
index d33bf1d22..6b35f0f1b 100644
--- a/test/filters/test_motion.py
+++ b/test/filters/test_motion.py
@@ -3,7 +3,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
-from test.common import device_type
+from test.common import device
import torch
from torch.autograd import gradcheck
@@ -21,22 +21,22 @@ def test_get_motion_kernel2d(ksize, angle, direction):
class TestMotionBlur:
@pytest.mark.parametrize("batch_shape", [(1, 4, 8, 15), (2, 3, 11, 7)])
- def test_motion_blur(self, batch_shape, device_type):
+ def test_motion_blur(self, batch_shape, device):
ksize = 5
angle = 200.
direction = 0.3
- input = torch.rand(batch_shape).to(torch.device(device_type))
+ input = torch.rand(batch_shape).to(device)
motion = kornia.filters.MotionBlur(ksize, angle, direction)
assert motion(input).shape == batch_shape
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_shape = (2, 3, 11, 7)
ksize = 9
angle = 34.
direction = -0.2
- input = torch.rand(batch_shape)
+ input = torch.rand(batch_shape).to(device)
input = utils.tensor_to_gradcheck_var(input)
assert gradcheck(
kornia.motion_blur,
@@ -46,7 +46,7 @@ class TestMotionBlur:
@pytest.mark.skip("")
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
@torch.jit.script
def op_script(
input: torch.Tensor,
@@ -56,7 +56,7 @@ class TestMotionBlur:
) -> torch.Tensor:
return kornia.filters.motion_blur(input, ksize, angle, direction)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5).to(device)
ksize = 5
angle = 65.
direction = .1
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index e40c09ddc..d5e08d47d 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -225,18 +225,16 @@ class TestRelativeTransformation:
class TestTransformLAFs:
- @pytest.mark.parametrize("device_type", TEST_DEVICES)
@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_type):
+ self, batch_size, num_points, device):
# generate input data
eye_size = 3
- lafs_src = torch.rand(batch_size, num_points, 2, 3)
- lafs_src = lafs_src.to(torch.device(device_type))
+ lafs_src = torch.rand(batch_size, num_points, 2, 3).to(device)
dst_homo_src = utils.create_random_homography(batch_size, eye_size)
- dst_homo_src = dst_homo_src.to(torch.device(device_type))
+ dst_homo_src = dst_homo_src.to(device)
# transform the points from dst to ref
lafs_dst = kornia.perspective_transform_lafs(dst_homo_src, lafs_src)
@@ -248,12 +246,12 @@ class TestTransformLAFs:
# projected should be equal as initial
assert_allclose(lafs_src, lafs_dst_to_src)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# 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)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size)
+ 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)
# 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