Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement filter cua tests

edgarriba8a4c911471fe
diff --git a/test/filters/test_blur.py b/test/filters/test_blur.py
index 0481c3b68..ab309ebc9 100644
--- a/test/filters/test_blur.py
+++ b/test/filters/test_blur.py
@@ -4,6 +4,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.testing import assert_allclose
@@ -11,37 +12,37 @@ from torch.autograd import gradcheck
class TestBoxBlur:
- def test_shape(self):
- inp = torch.zeros(1, 3, 4, 4)
+ def test_shape(self, device):
+ inp = torch.zeros(1, 3, 4, 4).to(device)
blur = kornia.filters.BoxBlur((3, 3))
assert blur(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4).to(device)
blur = kornia.filters.BoxBlur((3, 3))
assert blur(inp).shape == (2, 6, 4, 4)
- def test_kernel_3x3(self):
+ def test_kernel_3x3(self, device):
inp = torch.tensor([[[
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]
- ]]])
+ ]]]).to(device)
kernel_size = (3, 3)
actual = kornia.filters.box_blur(inp, kernel_size)
- assert_allclose(actual[0, 0, 1, 1:4], torch.tensor(1.))
+ assert_allclose(actual[0, 0, 1, 1:4], torch.tensor(1.).to(device))
- def test_kernel_5x5(self):
+ def test_kernel_5x5(self, device):
inp = torch.tensor([[[
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]
- ]]])
+ ]]]).to(device)
kernel_size = (5, 5)
expected = inp.sum((1, 2, 3)) / torch.mul(*kernel_size)
@@ -49,7 +50,7 @@ class TestBoxBlur:
actual = kornia.filters.box_blur(inp, kernel_size)
assert_allclose(actual[:, 0, 2, 2], expected)
- def test_kernel_5x5_batch(self):
+ def test_kernel_5x5_batch(self, device):
batch_size = 3
inp = torch.tensor([[[
[1., 1., 1., 1., 1.],
@@ -57,7 +58,7 @@ class TestBoxBlur:
[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]
- ]]]).repeat(batch_size, 1, 1, 1)
+ ]]]).repeat(batch_size, 1, 1, 1).to(device)
kernel_size = (5, 5)
expected = inp.sum((1, 2, 3)) / torch.mul(*kernel_size)
@@ -65,21 +66,21 @@ class TestBoxBlur:
actual = kornia.filters.box_blur(inp, kernel_size)
assert_allclose(actual[:, 0, 2, 2], expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.box_blur, (img, (3, 3),),
raise_exception=True)
@pytest.mark.skip(reason="undefined value BoxBlur")
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
op = kornia.filters.box_blur
op_script = torch.jit.script(op)
kernel_size = (3, 3)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5).to(device)
actual = op_script(img, kernel_size)
expected = op(img, kernel_size)
assert_allclose(actual, expected)
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index 3c5f04f93..2fdcd3fbf 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -2,6 +2,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.autograd import gradcheck
@@ -9,74 +10,74 @@ from torch.testing import assert_allclose
class TestFilter2D:
- def test_smoke(self):
- kernel = torch.rand(1, 3, 3)
- input = torch.ones(1, 1, 7, 8)
+ def test_smoke(self, device):
+ kernel = torch.rand(1, 3, 3).to(device)
+ input = torch.ones(1, 1, 7, 8).to(device)
assert kornia.filter2D(input, kernel).shape == input.shape
- def test_mean_filter(self):
- kernel = torch.ones(1, 3, 3)
+ def test_mean_filter(self, device):
+ kernel = torch.ones(1, 3, 3).to(device)
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 5., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
actual = kornia.filter2D(input, kernel)
assert_allclose(actual, expected)
- def test_mean_filter_2batch_2ch(self):
- kernel = torch.ones(1, 3, 3)
+ def test_mean_filter_2batch_2ch(self, device):
+ kernel = torch.ones(1, 3, 3).to(device)
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 5., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]]).expand(2, 2, -1, -1)
+ ]]]).expand(2, 2, -1, -1).to(device)
expected = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
actual = kornia.filter2D(input, kernel)
assert_allclose(actual, expected)
- def test_normalized_mean_filter(self):
- kernel = torch.ones(1, 3, 3)
+ def test_normalized_mean_filter(self, device):
+ kernel = torch.ones(1, 3, 3).to(device)
input = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 5., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]]).expand(2, 2, -1, -1)
+ ]]]).expand(2, 2, -1, -1).to(device)
expected = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 5. / 9., 5. / 9., 5. / 9., 0.],
[0., 5. / 9., 5. / 9., 5. / 9., 0.],
[0., 5. / 9., 5. / 9., 5. / 9., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
actual = kornia.filter2D(input, kernel, normalized=True)
assert_allclose(actual, expected)
- def test_gradcheck(self):
- kernel = torch.rand(1, 3, 3)
- input = torch.ones(1, 1, 7, 8)
+ def test_gradcheck(self, device):
+ kernel = torch.rand(1, 3, 3).to(device)
+ input = torch.ones(1, 1, 7, 8).to(device)
# evaluate function gradient
input = utils.tensor_to_gradcheck_var(input) # to var
@@ -86,7 +87,7 @@ class TestFilter2D:
@pytest.mark.skip(reason="not found compute_padding()")
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
op = kornia.filter2D
op = torch.jit.script(op)
diff --git a/test/filters/test_gaussian.py b/test/filters/test_gaussian.py
index c43d03073..f68c6e11d 100644
--- a/test/filters/test_gaussian.py
+++ b/test/filters/test_gaussian.py
@@ -36,14 +36,14 @@ class TestGaussianBlur:
gauss = kornia.filters.GaussianBlur2d(kernel_size, sigma, "replicate")
assert gauss(input).shape == batch_shape
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# test parameters
batch_shape = (2, 3, 11, 7)
kernel_size = (5, 3)
sigma = (1.5, 2.1)
# evaluate function gradient
- input = torch.rand(batch_shape)
+ input = torch.rand(batch_shape).to(device)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(
kornia.gaussian_blur2d,
@@ -59,7 +59,7 @@ class TestGaussianBlur:
return kornia.gaussian_blur2d(img, (5, 5), (1.2, 1.2), "replicate")
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)
expected = kornia.filters.GaussianBlur2d(
(5, 5), (1.2, 1.2), "replicate"
)(img)
@@ -102,13 +102,13 @@ class TestLaplacian:
laplace = kornia.filters.Laplacian(kernel_size)
assert laplace(input).shape == batch_shape
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
# test parameters
batch_shape = (2, 3, 11, 7)
kernel_size = 9
# evaluate function gradient
- input = torch.rand(batch_shape)
+ input = torch.rand(batch_shape).to(device)
input = utils.tensor_to_gradcheck_var(input)
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 096eb3f4c..8987f8a16 100644
--- a/test/filters/test_median.py
+++ b/test/filters/test_median.py
@@ -4,6 +4,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.testing import assert_allclose
@@ -11,17 +12,17 @@ from torch.autograd import gradcheck
class TestMedianBlur:
- def test_shape(self):
- inp = torch.zeros(1, 3, 4, 4)
+ def test_shape(self, device):
+ inp = torch.zeros(1, 3, 4, 4).to(device)
median = kornia.filters.MedianBlur((3, 3))
assert median(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4).to(device)
blur = kornia.filters.BoxBlur((3, 3))
assert blur(inp).shape == (2, 6, 4, 4)
- def test_kernel_3x3(self):
+ def test_kernel_3x3(self, device):
inp = torch.tensor([[
[0., 0., 0., 0., 0.],
[0., 3., 7., 5., 0.],
@@ -34,29 +35,29 @@ class TestMedianBlur:
[65., 59., 2.0, 0., 0.],
[0.0, 0.0, 0.0, 0., 0.],
[0.0, 0.0, 0.0, 0., 0.]
- ]]).repeat(2, 1, 1, 1)
+ ]]).repeat(2, 1, 1, 1).to(device)
kernel_size = (3, 3)
actual = kornia.filters.median_blur(inp, kernel_size)
- assert_allclose(actual[0, 0, 2, 2], torch.tensor(3.))
- assert_allclose(actual[0, 1, 1, 1], torch.tensor(14.))
+ assert_allclose(actual[0, 0, 2, 2], torch.tensor(3.).to(device))
+ assert_allclose(actual[0, 1, 1, 1], torch.tensor(14.).to(device))
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.median_blur, (img, (5, 3),),
raise_exception=True)
@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,
kernel_size: Tuple[int, int]) -> torch.Tensor:
return kornia.filters.median_blur(input, kernel_size)
kernel_size = (3, 5)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5).to(device)
actual = op_script(img, kernel_size)
expected = kornia.filters.median_blur(img, kernel_size)
assert_allclose(actual, expected)
diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.py
index f41d972b0..0257f1aae 100644
--- a/test/filters/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -2,6 +2,7 @@ import pytest
import kornia as kornia
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.testing import assert_allclose
@@ -9,24 +10,24 @@ from torch.autograd import gradcheck
class TestSpatialGradient:
- def test_shape(self):
- inp = torch.zeros(1, 3, 4, 4)
+ def test_shape(self, device):
+ inp = torch.zeros(1, 3, 4, 4).to(device)
sobel = kornia.filters.SpatialGradient()
assert sobel(inp).shape == (1, 3, 2, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4).to(device)
sobel = kornia.filters.SpatialGradient()
assert sobel(inp).shape == (2, 6, 2, 4, 4)
- def test_edges(self):
+ def test_edges(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[[
[0., 1., 0., -1., 0.],
@@ -40,19 +41,19 @@ class TestSpatialGradient:
[0., 0., 0., 0., 0],
[-1., -3., -4., -3., -1],
[0., -1., -2., -1., 0.],
- ]]]])
+ ]]]]).to(device)
edges = kornia.filters.spatial_gradient(inp, normalized=False)
assert_allclose(edges, expected)
- def test_edges_norm(self):
+ def test_edges_norm(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[[
[0., 1., 0., -1., 0.],
@@ -66,19 +67,19 @@ class TestSpatialGradient:
[0., 0., 0., 0., 0],
[-1., -3., -4., -3., -1],
[0., -1., -2., -1., 0.],
- ]]]]) / 8.0
+ ]]]]).to(device) / 8.0
edges = kornia.filters.spatial_gradient(inp, normalized=True)
assert_allclose(edges, expected)
- def test_edges_sep(self):
+ def test_edges_sep(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[[
[0., 0., 0., 0., 0.],
@@ -92,19 +93,19 @@ class TestSpatialGradient:
[0., 0., 0., 0., 0.],
[0., -1., -1., -1., 0.],
[0., 0., -1., 0., 0.]
- ]]]])
+ ]]]]).to(device)
edges = kornia.filters.spatial_gradient(inp, 'diff',
normalized=False)
assert_allclose(edges, expected)
- def test_edges_sep_norm(self):
+ def test_edges_sep_norm(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[[
[0., 0., 0., 0., 0.],
@@ -118,48 +119,48 @@ class TestSpatialGradient:
[0., 0., 0., 0., 0.],
[0., -1., -1., -1., 0.],
[0., 0., -1., 0., 0.]
- ]]]]) / 2.0
+ ]]]]).to(device) / 2.0
edges = kornia.filters.spatial_gradient(inp, 'diff',
normalized=True)
assert_allclose(edges, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.spatial_gradient, (img,),
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(input):
return kornia.filters.spatial_gradient(input)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5).to(device)
actual = op_script(img)
expected = kornia.filters.spatial_gradient(img)
assert_allclose(actual, expected)
class TestSobel:
- def test_shape(self):
- inp = torch.zeros(1, 3, 4, 4)
+ def test_shape(self, device):
+ inp = torch.zeros(1, 3, 4, 4).to(device)
sobel = kornia.filters.Sobel()
assert sobel(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(3, 2, 4, 4)
+ def test_shape_batch(self, device):
+ inp = torch.zeros(3, 2, 4, 4).to(device)
sobel = kornia.filters.Sobel()
assert sobel(inp).shape == (3, 2, 4, 4)
- def test_magnitude(self):
+ def test_magnitude(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[
[0., 1.4142, 2.0, 1.4142, 0.],
@@ -167,31 +168,31 @@ class TestSobel:
[2.0, 4.0000, 0.00, 4.0000, 2.0],
[1.4142, 4.2426, 4.00, 4.2426, 1.4142],
[0., 1.4142, 2.0, 1.4142, 0.],
- ]]])
+ ]]]).to(device)
edges = kornia.filters.sobel(inp, normalized=False)
assert_allclose(edges, expected)
- def test_gradcheck_unnorm(self):
+ def test_gradcheck_unnorm(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.sobel, (img, False),
raise_exception=True)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.sobel, (img, True),
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(input):
return kornia.filters.sobel(input)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5).to(device)
actual = op_script(img)
expected = kornia.filters.sobel(img)
assert_allclose(actual, expected)
diff --git a/test/utils/test_image.py b/test/utils/test_image.py
index 1d0283459..6b26b5503 100644
--- a/test/utils/test_image.py
+++ b/test/utils/test_image.py
@@ -4,6 +4,7 @@ import numpy as np
import torch
import kornia as kornia
import kornia.testing as utils # test utils
+from test.common import device
@pytest.mark.parametrize("input_shape, expected",
@@ -13,8 +14,8 @@ import kornia.testing as utils # test utils
((3, 4, 4), (4, 4, 3)),
((2, 3, 4, 4), (2, 4, 4, 3)),
((1, 3, 4, 4), (4, 4, 3)), ])
-def test_tensor_to_image(input_shape, expected):
- tensor = torch.ones(input_shape)
+def test_tensor_to_image(device, input_shape, expected):
+ tensor = torch.ones(input_shape).to(device)
image = kornia.utils.tensor_to_image(tensor)
assert image.shape == expected
assert isinstance(image, np.ndarray)