Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement cuda test for color module

edgarriba8fc134bb8109
diff --git a/test/color/test_adjust.py b/test/color/test_adjust.py
index 42118ff6c..3f1f7ec30 100644
--- a/test/color/test_adjust.py
+++ b/test/color/test_adjust.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
@@ -10,7 +10,7 @@ from torch.testing import assert_allclose
class TestAdjustSaturation:
- def test_saturation_one(self):
+ def test_saturation_one(self, device):
data = torch.tensor([[[.5, .5],
[.5, .5]],
@@ -20,13 +20,15 @@ class TestAdjustSaturation:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ data = data.to(device)
+ expected = data.clone()
+
f = kornia.color.AdjustSaturation(1.)
assert_allclose(f(data), expected)
class TestAdjustHue:
- def test_hue_one(self):
+ def test_hue_one(self, device):
data = torch.tensor([[[.5, .5],
[.5, .5]],
@@ -36,13 +38,15 @@ class TestAdjustHue:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ data = data.to(device)
+ expected = data.clone()
+
f = kornia.color.AdjustHue(1.)
assert_allclose(f(data), expected)
class TestAdjustGamma:
- def test_gamma_zero(self):
+ def test_gamma_zero(self, device):
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -52,11 +56,13 @@ class TestAdjustGamma:
[[.25, .25],
[.25, .25]]]) # 3x2x2
+ data = data.to(device)
expected = torch.ones_like(data)
+
f = kornia.color.AdjustGamma(0.)
assert_allclose(f(data), expected)
- def test_gamma_one(self):
+ def test_gamma_one(self, device):
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -66,11 +72,13 @@ class TestAdjustGamma:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ data = data.to(device)
+ expected = data.clone()
+
f = kornia.color.AdjustGamma(1.)
assert_allclose(f(data), expected)
- def test_gamma_one_gain_two(self):
+ def test_gamma_one_gain_two(self, device):
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -89,10 +97,13 @@ class TestAdjustGamma:
[[.5, .5],
[.5, .5]]]) # 3x2x2
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.AdjustGamma(1., 2.)
assert_allclose(f(data), expected)
- def test_gamma_two(self):
+ def test_gamma_two(self, device):
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -111,19 +122,23 @@ class TestAdjustGamma:
[[.0625, .0625],
[.0625, .0625]]]) # 3x2x2
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.AdjustGamma(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)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.adjust_gamma, (img, 1., 2.),
raise_exception=True)
class TestAdjustContrast:
- def test_factor_zero(self):
+ def test_factor_zero(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -134,13 +149,14 @@ class TestAdjustContrast:
[[.25, .25],
[.25, .25]]]) # 3x2x2
+ data = data.to(device)
expected = torch.zeros_like(data)
f = kornia.color.AdjustContrast(0.)
assert_allclose(f(data), expected)
- def test_factor_one(self):
+ def test_factor_one(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -150,14 +166,14 @@ class TestAdjustContrast:
[[.25, .25],
[.25, .25]]]) # 3x2x2
-
- expected = data
+ data = data.to(device)
+ expected = data.clone()
f = kornia.color.AdjustContrast(1.)
assert_allclose(f(data), expected)
- def test_factor_two(self):
+ def test_factor_two(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -177,11 +193,14 @@ class TestAdjustContrast:
[[.5, .5],
[.5, .5]]]) # 3x2x2
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.AdjustContrast(2.)
assert_allclose(f(data), expected)
- def test_factor_tensor(self):
+ def test_factor_tensor(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -208,10 +227,15 @@ class TestAdjustContrast:
[1., 1.]]]) # 4x2x2
factor = torch.tensor([0, 1, 1.5, 2])
+
+ data = data.to(device)
+ expected = expected.to(device)
+ factor = factor.to(device)
+
f = kornia.color.AdjustContrast(factor)
assert_allclose(f(data), expected)
- def test_factor_tensor_color(self):
+ def test_factor_tensor_color(self, device):
# prepare input data
data = torch.tensor([[[[1., 1.],
[1., 1.]],
@@ -250,10 +274,15 @@ class TestAdjustContrast:
[1., 1.]]]]) # 2x3x2x2
factor = torch.tensor([1, 2])
+
+ data = data.to(device)
+ expected = expected.to(device)
+ factor = factor.to(device)
+
f = kornia.color.AdjustContrast(factor)
assert_allclose(f(data), expected)
- def test_factor_tensor_shape(self):
+ def test_factor_tensor_shape(self, device):
# prepare input data
data = torch.tensor([[[[1., 1., .5],
[1., 1., .5]],
@@ -292,19 +321,25 @@ class TestAdjustContrast:
[.6, .4, .2]]]]) # 2x3x2x3
factor = torch.tensor([1.5, 2.])
+
+ data = data.to(device)
+ expected = expected.to(device)
+ factor = factor.to(device)
+
f = kornia.color.AdjustContrast(factor)
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)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.adjust_contrast, (img, 2.),
raise_exception=True)
class TestAdjustBrightness:
- def test_factor_zero(self):
+ def test_factor_zero(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -315,12 +350,13 @@ class TestAdjustBrightness:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ data = data.to(device)
+ expected = data.clone()
f = kornia.color.AdjustBrightness(0.)
assert_allclose(f(data), expected)
- def test_factor_one(self):
+ def test_factor_one(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -331,12 +367,13 @@ class TestAdjustBrightness:
[[.25, .25],
[.25, .25]]]) # 3x2x2
+ data = data.to(device)
expected = torch.ones_like(data)
f = kornia.color.AdjustBrightness(1.)
assert_allclose(f(data), expected)
- def test_factor_minus(self):
+ def test_factor_minus(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -356,10 +393,13 @@ class TestAdjustBrightness:
[[0., 0.],
[0., 0.]]]) # 3x2x2
- f = kornia.color.AdjustBrightness(-.5)
+ data = data.to(device)
+ expected = expected.to(device)
+
+ f = kornia.color.AdjustBrightness(-0.5)
assert_allclose(f(data), expected)
- def test_factor_tensor(self):
+ def test_factor_tensor(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -373,13 +413,16 @@ class TestAdjustBrightness:
[[.5, .5],
[.5, .5]]]) # 4x2x2
+ factor = torch.tensor([0, 0.5, 0.75, 2])
+
+ data = data.to(device)
expected = torch.ones_like(data)
+ factor = factor.to(device)
- factor = torch.tensor([0, 0.5, 0.75, 2])
f = kornia.color.AdjustBrightness(factor)
assert_allclose(f(data), expected)
- def test_factor_tensor_color(self):
+ def test_factor_tensor_color(self, device):
# prepare input data
data = torch.tensor([[[[1., 1.],
[1., 1.]],
@@ -418,12 +461,18 @@ class TestAdjustBrightness:
[.7, .7]]]]) # 2x3x2x2
factor = torch.tensor([0.25, 0.1])
+
+ data = data.to(device)
+ expected = expected.to(device)
+ factor = factor.to(device)
+
f = kornia.color.AdjustBrightness(factor)
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)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.adjust_brightness, (img, 2.),
raise_exception=True)
diff --git a/test/color/test_core.py b/test/color/test_core.py
index 288f0e7fb..33d626b96 100644
--- a/test/color/test_core.py
+++ b/test/color/test_core.py
@@ -3,7 +3,7 @@ import random
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
@@ -25,30 +25,34 @@ class TestAddWeighted:
return src1, src2, alpha, beta, gamma
@pytest.mark.parametrize("size", [2, 3, 4, 5])
- def test_addweighted(self, size):
+ def test_addweighted(self, size, device):
src1, src2, alpha, beta, gamma = self.get_input(3)
+ src1 = src1.to(device)
+ src2 = src2.to(device)
f = kornia.color.AddWeighted(alpha, beta, gamma)
assert_allclose(f(src1, src2), src1 * alpha + src2 * beta + gamma)
@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(src1: torch.Tensor, alpha: float, src2: torch.Tensor,
beta: float, gamma: float) -> torch.Tensor:
return kornia.color.add_weighted(src1, alpha, src2, beta, gamma)
src1, src2, alpha, beta, gamma = self.get_input(3)
+ src1 = src1.to(device)
+ src2 = src2.to(device)
actual = op_script(src1, alpha, src2, beta, gamma)
expected = kornia.color.add_weighted(src1, alpha, src2, beta, gamma)
assert_allclose(actual, expected)
@pytest.mark.parametrize("size", [2, 3])
- def test_gradcheck(self, size):
+ def test_gradcheck(self, size, device):
shape = random_shape(size, max_elem=5) # to shave time on gradcheck
- src1 = torch.randn(shape)
- src2 = torch.randn(shape)
+ src1 = torch.randn(shape).to(device)
+ src2 = torch.randn(shape).to(device)
alpha = random.random()
beta = random.random()
gamma = random.random()
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index 33ea5cfb5..27456e36a 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.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
@@ -10,51 +10,51 @@ from torch.testing import assert_allclose
class TestRgbToGrayscale:
- def test_rgb_to_grayscale(self):
+ def test_rgb_to_grayscale(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_grayscale(img).shape == (1, height, width)
- def test_rgb_to_grayscale_batch(self):
+ def test_rgb_to_grayscale_batch(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_grayscale(img).shape == \
(batch_size, 1, height, width)
- 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 = torch.ones(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.rgb_to_grayscale, (img,), raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
gray = kornia.color.RgbToGrayscale()
gray_traced = torch.jit.trace(kornia.color.RgbToGrayscale(), img)
assert_allclose(gray(img), gray_traced(img))
class TestBgrToGrayscale:
- def test_bgr_to_grayscale(self):
+ def test_bgr_to_grayscale(self, device):
channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width)
+ img = torch.ones(channels, height, width).to(device)
assert kornia.bgr_to_grayscale(img).shape == (1, height, width)
- def test_bgr_to_grayscale_batch(self):
+ def test_bgr_to_grayscale_batch(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.bgr_to_grayscale(img).shape == \
(batch_size, 1, height, width)
- 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 = torch.ones(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.bgr_to_grayscale, (img,), raise_exception=True)
- def test_module(self):
+ def test_module(self, device):
data = torch.tensor([[[[100., 73.],
[200., 22.]],
@@ -64,12 +64,14 @@ class TestBgrToGrayscale:
[[225., 255.],
[48., 8.]]]])
+ data = data.to(device)
+
assert_allclose(kornia.bgr_to_grayscale(data / 255), kornia.color.BgrToGrayscale()(data / 255))
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
gray = kornia.color.BgrToGrayscale()
gray_traced = torch.jit.trace(kornia.color.BgrToGrayscale(), img)
assert_allclose(gray(img), gray_traced(img))
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
index 1e5314034..107d38c1f 100644
--- a/test/color/test_hls.py
+++ b/test/color/test_hls.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
@@ -11,7 +11,7 @@ from torch.testing import assert_allclose
class TestRgbToHls:
- def test_rgb_to_hls(self):
+ def test_rgb_to_hls(self, device):
data = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -31,10 +31,13 @@ class TestRgbToHls:
[[0.4483, 0.4667],
[0.4667, 0.4667]]])
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.RgbToHls()
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)
- def test_batch_rgb_to_hls(self):
+ def test_batch_rgb_to_hls(self, device):
data = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -53,12 +56,17 @@ class TestRgbToHls:
[[0.4483, 0.4667],
[0.4667, 0.4667]]]) # 3x2x2
- f = kornia.color.RgbToHls()
+
+ data = data.to(device)
+ expected = expected.to(device)
+
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+
+ f = kornia.color.RgbToHls()
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -69,13 +77,14 @@ class TestRgbToHls:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.RgbToHls(), (data,),
raise_exception=True)
@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(data: torch.Tensor) -> torch.Tensor:
@@ -89,6 +98,7 @@ class TestRgbToHls:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
actual = op_script(data)
expected = kornia.rgb_to_hls(data)
assert_allclose(actual, expected)
@@ -96,7 +106,7 @@ class TestRgbToHls:
class TestHlsToRgb:
- def test_hls_to_rgb(self):
+ def test_hls_to_rgb(self, device):
expected = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -119,7 +129,7 @@ class TestHlsToRgb:
f = kornia.color.HlsToRgb()
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)
- def test_batch_hls_to_rgb(self):
+ def test_batch_hls_to_rgb(self, device):
expected = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -139,13 +149,17 @@ class TestHlsToRgb:
[[0.4483, 0.4667],
[0.4667, 0.4667]]]) # 3x2x2
- f = kornia.color.HlsToRgb()
+ data = data.to(device)
+ expected = expected.to(device)
+
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+
+ f = kornia.color.HlsToRgb()
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)
@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(data: torch.Tensor) -> torch.Tensor:
return kornia.hls_to_rgb(data)
@@ -159,6 +173,7 @@ class TestHlsToRgb:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
actual = op_script(data)
expected = kornia.hls_to_rgb(data)
assert_allclose(actual, expected)
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index 341cce647..ec38df9b2 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.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
@@ -11,7 +11,7 @@ from torch.testing import assert_allclose
class TestRgbToHsv:
- def test_rgb_to_hsv(self):
+ def test_rgb_to_hsv(self, device):
data = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -31,10 +31,13 @@ class TestRgbToHsv:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]])
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.RgbToHsv()
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
- def test_batch_rgb_to_hsv(self):
+ def test_batch_rgb_to_hsv(self, device):
data = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -53,12 +56,17 @@ class TestRgbToHsv:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]]) # 3x2x2
- f = kornia.color.RgbToHsv()
+
+ data = data.to(device)
+ expected = expected.to(device)
+
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+
+ f = kornia.color.RgbToHsv()
assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -69,13 +77,14 @@ class TestRgbToHsv:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.RgbToHsv(), (data,),
raise_exception=True)
@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(data: torch.Tensor) -> torch.Tensor:
@@ -89,6 +98,7 @@ class TestRgbToHsv:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
actual = op_script(data)
expected = kornia.rgb_to_hsv(data)
assert_allclose(actual, expected)
@@ -96,7 +106,7 @@ class TestRgbToHsv:
class TestHsvToRgb:
- def test_hsv_to_rgb(self):
+ def test_hsv_to_rgb(self, device):
expected = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -116,10 +126,13 @@ class TestHsvToRgb:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]])
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.HsvToRgb()
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)
- def test_batch_hsv_to_rgb(self):
+ def test_batch_hsv_to_rgb(self, device):
expected = torch.tensor([[[21., 22.],
[22., 22.]],
@@ -139,13 +152,17 @@ class TestHsvToRgb:
[[21.0000 / 255, 22.0000 / 255],
[22.0000 / 255, 22.0000 / 255]]]) # 3x2x2
- f = kornia.color.HsvToRgb()
+ data = data.to(device)
+ expected = expected.to(device)
+
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+
+ f = kornia.color.HsvToRgb()
assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)
@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(data: torch.Tensor) -> torch.Tensor:
return kornia.hsv_to_rgb(data)
@@ -159,6 +176,7 @@ class TestHsvToRgb:
[[8., 8.],
[8., 8.]]]]) # 3x2x2
+ data = data.to(device)
actual = op_script(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 ae074e5d9..6eb0324cc 100644
--- a/test/color/test_normalize.py
+++ b/test/color/test_normalize.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
@@ -16,27 +16,27 @@ class TestNormalize:
repr = "Normalize(mean=[0.5], std=[0.1])"
assert str(kornia.color.Normalize(mean, std)) == repr
- def test_normalize(self):
+ def test_normalize(self, device):
# prepare input data
- data = torch.ones(1, 2, 2)
- mean = torch.tensor([0.5])
- std = torch.tensor([2.0])
+ data = torch.ones(1, 2, 2).to(device)
+ mean = torch.tensor([0.5]).to(device)
+ std = torch.tensor([2.0]).to(device)
# expected output
- expected = torch.tensor([0.25]).repeat(1, 2, 2).view_as(data)
+ expected = torch.tensor([0.25]).repeat(1, 2, 2).view_as(data).to(device)
f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
- def test_broadcast_normalize(self):
+ def test_broadcast_normalize(self, device):
# prepare input data
- data = torch.ones(2, 3, 1, 1)
+ data = torch.ones(2, 3, 1, 1).to(device)
data += 2
- mean = torch.tensor([2.0])
- std = torch.tensor([0.5])
+ mean = torch.tensor([2.0]).to(device)
+ std = torch.tensor([0.5]).to(device)
# expected output
expected = torch.ones_like(data) + 1
@@ -44,9 +44,9 @@ class TestNormalize:
f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
- def test_float_input(self):
+ def test_float_input(self, device):
- data = torch.ones(2, 3, 1, 1)
+ data = torch.ones(2, 3, 1, 1).to(device)
data += 2
mean = 2.0
@@ -58,53 +58,56 @@ class TestNormalize:
f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
- def test_batch_normalize(self):
+ def test_batch_normalize(self, device):
# prepare input data
- data = torch.ones(2, 3, 1, 1)
+ data = torch.ones(2, 3, 1, 1).to(device)
data += 2
- mean = torch.tensor([0.5, 1.0, 2.0]).repeat(2, 1)
- std = torch.tensor([2.0, 2.0, 2.0]).repeat(2, 1)
+ mean = torch.tensor([0.5, 1.0, 2.0]).to(device).repeat(2, 1)
+ std = torch.tensor([2.0, 2.0, 2.0]).to(device).repeat(2, 1)
+
# expected output
- expected = torch.tensor([1.25, 1, 0.5]).repeat(2, 1, 1).view_as(data)
+ expected = torch.tensor([1.25, 1, 0.5]).to(device).repeat(2, 1, 1).view_as(data)
f = kornia.color.Normalize(mean, std)
assert_allclose(f(data), expected)
@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(data: torch.Tensor, mean: torch.Tensor, std: torch.Tensor) -> torch.Tensor:
return kornia.normalize(data, mean, std)
- data = torch.ones(2, 3, 1, 1)
+ data = torch.ones(2, 3, 1, 1).to(device)
data += 2
- mean = torch.tensor([0.5, 1.0, 2.0]).repeat(2, 1)
- std = torch.tensor([2.0, 2.0, 2.0]).repeat(2, 1)
+ mean = torch.tensor([0.5, 1.0, 2.0]).repeat(2, 1).to(device)
+ std = torch.tensor([2.0, 2.0, 2.0]).repeat(2, 1).to(device)
actual = op_script(data, mean, std)
expected = image.normalize(data, mean, std)
assert_allclose(actual, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# prepare input data
- data = torch.ones(2, 3, 1, 1)
+ data = torch.ones(2, 3, 1, 1).to(device)
data += 2
- mean = torch.tensor([0.5, 1.0, 2.0]).double()
- std = torch.tensor([2.0, 2.0, 2.0]).double()
+ mean = torch.tensor([0.5, 1.0, 2.0])
+ std = torch.tensor([2.0, 2.0, 2.0])
data = utils.tensor_to_gradcheck_var(data) # to var
+ mean = utils.tensor_to_gradcheck_var(mean) # to var
+ std = utils.tensor_to_gradcheck_var(std) # to var
assert gradcheck(kornia.color.Normalize(mean, std), (data,), raise_exception=True)
- def test_single_value(self):
+ def test_single_value(self, device):
# prepare input data
- mean = torch.tensor(2)
- std = torch.tensor(3)
- data = torch.ones(2, 3, 256, 313).float()
+ mean = torch.tensor(2).to(device)
+ std = torch.tensor(3).to(device)
+ data = torch.ones(2, 3, 256, 313).to(device)
# expected output
expected = (data - mean) / std
diff --git a/test/color/test_rgb.py b/test/color/test_rgb.py
index 1cc957505..231f303e0 100644
--- a/test/color/test_rgb.py
+++ b/test/color/test_rgb.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
@@ -11,7 +11,7 @@ from torch.testing import assert_allclose
class TestBgrToRgb:
- def test_bgr_to_rgb(self):
+ def test_bgr_to_rgb(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
@@ -32,10 +32,14 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]) # 3x2x2
+ # move data to the device
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.BgrToRgb()
assert_allclose(f(data), expected)
- def test_batch_bgr_to_rgb(self):
+ def test_batch_bgr_to_rgb(self, device):
# prepare input data
data = torch.tensor([[[[1., 1.],
@@ -74,11 +78,15 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
+ # move data to the device
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.BgrToRgb()
out = f(data)
assert_allclose(out, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
@@ -90,13 +98,14 @@ class TestBgrToRgb:
[[3., 3.],
[3., 3.]]]) # 3x2x2
+ data = data.to(device)
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.BgrToRgb(), (data,),
raise_exception=True)
@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(data: torch.Tensor) -> torch.Tensor:
return kornia.bgr_to_rgb(data)
@@ -110,6 +119,8 @@ class TestBgrToRgb:
[[3., 3.],
[3., 3.]]]) # 3x2x2
+ data = data.to(device)
+
actual = op_script(data)
expected = kornia.bgr_to_rgb(data)
assert_allclose(actual, expected)
@@ -117,7 +128,7 @@ class TestBgrToRgb:
class TestRgbToBgr:
- def test_rgb_to_bgr(self):
+ def test_rgb_to_bgr(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
@@ -138,10 +149,14 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]) # 3x2x2
+ # move data to the device
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.RgbToBgr()
assert_allclose(f(data), expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# prepare input data
data = torch.tensor([[[1., 1.],
@@ -153,6 +168,7 @@ class TestRgbToBgr:
[[3., 3.],
[3., 3.]]]) # 3x2x2
+ data = data.to(device)
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.RgbToBgr(), (data,),
@@ -173,11 +189,13 @@ class TestRgbToBgr:
[[3., 3.],
[3., 3.]]]) # 3x2x2
+ data = data.to(device)
+
actual = op_script(data)
expected = kornia.rgb_to_bgr(data)
assert_allclose(actual, expected)
- def test_batch_rgb_to_bgr(self):
+ def test_batch_rgb_to_bgr(self, device):
# prepare input data
data = torch.tensor([[[[1., 1.],
@@ -216,6 +234,10 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
+ # move data to the device
+ data = data.to(device)
+ expected = expected.to(device)
+
f = kornia.color.RgbToBgr()
out = f(data)
assert_allclose(out, expected)