Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Refactor morphology tests using @parametrized_test decorator

arc-wondersc2cd3b4e033e
diff --git a/tests/morphology/test_bottom_hat.py b/tests/morphology/test_bottom_hat.py
index bbf6fa951..f639ff900 100644
--- a/tests/morphology/test_bottom_hat.py
+++ b/tests/morphology/test_bottom_hat.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import bottom_hat
-from testing.base import assert_close
-
-
-class TestBottomHat:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert bottom_hat(img, krnl).shape == shape
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestBottomHat(BaseTester):
+ def setup_method(self) -> None:
+ self.func = bottom_hat
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -81,12 +110,6 @@ class TestBottomHat:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert bottom_hat(sample, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- sample = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(bottom_hat, (sample, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = bottom_hat
diff --git a/tests/morphology/test_closing.py b/tests/morphology/test_closing.py
index cde29d052..747e50542 100644
--- a/tests/morphology/test_closing.py
+++ b/tests/morphology/test_closing.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import closing
-from testing.base import assert_close
-
-
-class TestClosing:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert closing(img, krnl).shape == shape
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestClosing(BaseTester):
+ def setup_method(self) -> None:
+ self.func = closing
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -81,12 +110,6 @@ class TestClosing:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert closing(tensor, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(closing, (tensor, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = closing
diff --git a/tests/morphology/test_dilation.py b/tests/morphology/test_dilation.py
index d167c89c2..feb07c0c8 100644
--- a/tests/morphology/test_dilation.py
+++ b/tests/morphology/test_dilation.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import dilation
-from testing.base import assert_close
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
-class TestDilate:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5), (3, 5), (5, 3)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert dilation(img, krnl).shape == shape
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestDilate(BaseTester):
+ def setup_method(self) -> None:
+ self.func = dilation
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -105,12 +134,6 @@ class TestDilate:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert dilation(tensor, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(dilation, (tensor, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = dilation
diff --git a/tests/morphology/test_erosion.py b/tests/morphology/test_erosion.py
index fc3a93655..63311d763 100644
--- a/tests/morphology/test_erosion.py
+++ b/tests/morphology/test_erosion.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import erosion
-from testing.base import assert_close
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
-class TestErode:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5), (3, 5), (5, 3)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert erosion(img, krnl).shape == shape
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestErode(BaseTester):
+ def setup_method(self) -> None:
+ self.func = erosion
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -104,12 +133,6 @@ class TestErode:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert erosion(tensor, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(erosion, (tensor, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = erosion
diff --git a/tests/morphology/test_gradient.py b/tests/morphology/test_gradient.py
index 3f0c84bc9..052c2bcba 100644
--- a/tests/morphology/test_gradient.py
+++ b/tests/morphology/test_gradient.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import gradient
-from testing.base import assert_close
-
-
-class TestGradient:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert gradient(img, krnl).shape == shape
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestGradient(BaseTester):
+ def setup_method(self) -> None:
+ self.func = gradient
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -81,12 +110,6 @@ class TestGradient:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert gradient(tensor, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(gradient, (tensor, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = gradient
diff --git a/tests/morphology/test_opening.py b/tests/morphology/test_opening.py
index f5ed1f173..369f28319 100644
--- a/tests/morphology/test_opening.py
+++ b/tests/morphology/test_opening.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import opening
-from testing.base import assert_close
-
-
-class TestOpening:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert opening(img, krnl).shape == shape
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestOpening(BaseTester):
+ def setup_method(self) -> None:
+ self.func = opening
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -81,12 +110,6 @@ class TestOpening:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert opening(tensor, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(opening, (tensor, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = opening
diff --git a/tests/morphology/test_top_hat.py b/tests/morphology/test_top_hat.py
index 7b2f13cba..c75cce4c8 100644
--- a/tests/morphology/test_top_hat.py
+++ b/tests/morphology/test_top_hat.py
@@ -17,24 +17,53 @@
import pytest
import torch
-from torch.autograd import gradcheck
from kornia.morphology import top_hat
-from testing.base import assert_close
-
-
-class TestTopHat:
- def test_smoke(self, device, dtype):
- kernel = torch.rand(3, 3, device=device, dtype=dtype)
- assert kernel is not None
-
- @pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
- @pytest.mark.parametrize("kernel", [(3, 3), (5, 5)])
- def test_cardinality(self, device, dtype, shape, kernel):
- img = torch.ones(shape, device=device, dtype=dtype)
- krnl = torch.ones(kernel, device=device, dtype=dtype)
- assert top_hat(img, krnl).shape == shape
+from testing.base import BaseTester, assert_close
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (torch.rand(3, 3, device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([1, 3, 4, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([2, 3, 2, 4]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 3, 4, 1]),
+ },
+ {
+ "inputs": lambda device, dtype: (
+ torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
+ torch.ones((3, 3), device=device, dtype=dtype),
+ ),
+ "expected_shape": torch.Size([3, 2, 5, 5]),
+ },
+ ],
+ gradcheck_inputs=lambda device: (
+ torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
+ torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
+ ),
+)
+class TestTopHat(BaseTester):
+ def setup_method(self) -> None:
+ self.func = top_hat
def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
@@ -81,12 +110,6 @@ class TestTopHat:
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert top_hat(sample, test)
- @pytest.mark.grad()
- def test_gradcheck(self, device, dtype):
- sample = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)
- kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)
- assert gradcheck(top_hat, (sample, kernel), raise_exception=True, fast_mode=True)
-
@pytest.mark.jit()
def test_jit(self, device, dtype):
op = top_hat
diff --git a/tests/utils/test_parametrized_tester_poc.py b/tests/utils/test_parametrized_tester_poc.py
new file mode 100644
index 000000000..1d6681d3a
--- /dev/null
+++ b/tests/utils/test_parametrized_tester_poc.py
@@ -0,0 +1,78 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Proof of Concept: Parametrized test automation using the new decorator.
+
+This demonstrates the usage of the @parametrized_test decorator on existing
+test classes to automatically generate test_smoke, test_cardinality, and
+test_gradcheck methods parametrized across devices and dtypes.
+"""
+
+import torch
+
+from kornia.core import tensor
+from kornia.utils.misc import differentiable_clipping, differentiable_polynomial_floor
+
+from testing.base import BaseTester
+from testing.parametrized_tester import parametrized_test
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (tensor([1.0, 6.0, 10.0, 12.0], device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (tensor([1.0, 6.0, 10.0, 12.0], device=device, dtype=dtype),),
+ "expected_shape": torch.Size([4]),
+ }
+ ],
+ gradcheck_inputs=lambda device: (tensor([1.0, 6.0, 11.0, 12.0], device=device, dtype=torch.float64),),
+)
+class TestDifferentiableClippingAutomated(BaseTester):
+ """Demonstration of automated tests for differentiable_clipping function.
+
+ The @parametrized_test decorator automatically generates:
+ - test_smoke: Runs the function with valid inputs
+ - test_cardinality: Verifies output shapes
+ - test_gradcheck: Validates gradient computation
+
+ All tests are parametrized across devices and dtypes automatically.
+ """
+
+ def setup_method(self) -> None:
+ """Set up test fixtures."""
+ self.func = lambda x: differentiable_clipping(x, min_val=5.0, max_val=10.0)
+
+
+@parametrized_test(
+ smoke_inputs=lambda device, dtype: (tensor([1.5, 3.1, 5.9, 6.6], device=device, dtype=dtype),),
+ cardinality_tests=[
+ {
+ "inputs": lambda device, dtype: (tensor([1.5, 3.1, 5.9, 6.6], device=device, dtype=dtype),),
+ "expected_shape": torch.Size([4]),
+ }
+ ],
+ gradcheck_inputs=lambda device: (tensor([1.5, 3.1, 5.9, 6.6], device=device, dtype=torch.float64),),
+)
+class TestDifferentiablePolynomialFloorAutomated(BaseTester):
+ """Demonstration of automated tests for differentiable_polynomial_floor function.
+
+ Uses the same @parametrized_test decorator pattern as TestDifferentiableClippingAutomated.
+ """
+
+ def setup_method(self) -> None:
+ """Set up test fixtures."""
+ self.func = differentiable_polynomial_floor