Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

added tests

Dmytro Mishkin58b89b680349
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index 68e5a0aa6..76d5933b4 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -56,6 +56,26 @@ class TestFilter2D:
actual = kornia.filter2D(input, kernel)
assert_allclose(actual, expected)
+ def test_normalized_mean_filter(self):
+ kernel = torch.ones(1, 3, 3)
+ 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)
+ 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.],
+ ]]])
+ 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)
diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.py
index acba86460..e8a0d330e 100644
--- a/test/filters/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -45,6 +45,33 @@ class TestSpatialGradient:
edges = kornia.filters.spatial_gradient(inp, normalized=False)
assert_allclose(edges, expected)
+ def test_edges_norm(self):
+ 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.],
+ ]]])
+
+ expected = torch.tensor([[[[
+ [0., 1., 0., -1., 0.],
+ [1., 3., 0., -3., -1.],
+ [2., 4., 0., -4., -2.],
+ [1., 3., 0., -3., -1.],
+ [0., 1., 0., -1., 0.],
+ ], [
+ [0., 1., 2., 1., 0.],
+ [1., 3., 4., 3., 1.],
+ [0., 0., 0., 0., 0],
+ [-1., -3., -4., -3., -1],
+ [0., -1., -2., -1., 0.],
+ ]]]]) / 8.0
+
+ edges = kornia.filters.spatial_gradient(inp, normalized=True)
+ assert_allclose(edges, expected)
+
+
def test_edges_sep(self):
inp = torch.tensor([[[
[0., 0., 0., 0., 0.],
@@ -67,6 +94,31 @@ class TestSpatialGradient:
[0., -1., -1., -1., 0.],
[0., 0., -1., 0., 0.]
]]]])
+ edges = kornia.filters.spatial_gradient(inp, 'diff', False)
+ assert_allclose(edges, expected)
+
+ def test_edges_sep_norm(self):
+ 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.],
+ ]]])
+
+ expected = torch.tensor([[[[
+ [0., 0., 0., 0., 0.],
+ [0., 1., 0., -1., 0.],
+ [1., 1., 0., -1., -1.],
+ [0., 1., 0., -1., 0.],
+ [0., 0., 0., 0., 0.]
+ ], [
+ [0., 0., 1., 0., 0.],
+ [0., 1., 1., 1., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., -1., -1., -1., 0.],
+ [0., 0., -1., 0., 0.]
+ ]]]]) / 2.0
edges = kornia.filters.spatial_gradient(inp, 'diff')
assert_allclose(edges, expected)
@@ -119,11 +171,18 @@ class TestSobel:
edges = kornia.filters.sobel(inp, normalized=False)
assert_allclose(edges, expected)
+
+ def test_gradcheck_unnorm(self):
+ batch_size, channels, height, width = 1, 2, 5, 4
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.filters.sobel, (img, False), raise_exception=True)
+
def test_gradcheck(self):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(kornia.filters.sobel, (img,), raise_exception=True)
+ 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):