Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Test suite: separate contrib tests into individual files (#2802)

Lyngkhoi69a697020a5f
diff --git a/tests/contrib/test_classification_head.py b/tests/contrib/test_classification_head.py
new file mode 100644
index 000000000..9c646b18f
--- /dev/null
+++ b/tests/contrib/test_classification_head.py
@@ -0,0 +1,15 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestClassificationHead(BaseTester):
+ @pytest.mark.parametrize("B, D, N", [(1, 8, 10), (2, 2, 5)])
+ def test_smoke(self, device, dtype, B, D, N):
+ feat = torch.rand(B, D, D, device=device, dtype=dtype)
+ head = kornia.contrib.ClassificationHead(embed_size=D, num_classes=N).to(device, dtype)
+ logits = head(feat)
+ assert logits.shape == (B, N)
diff --git a/tests/contrib/test_combine_tensor_patch.py b/tests/contrib/test_combine_tensor_patch.py
new file mode 100644
index 000000000..37d85ad25
--- /dev/null
+++ b/tests/contrib/test_combine_tensor_patch.py
@@ -0,0 +1,116 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestCombineTensorPatches(BaseTester):
+ def test_smoke(self, device, dtype):
+ img = torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4)
+ m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2), (2, 2))
+ patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2))
+ assert m(patches).shape == (1, 1, 4, 4)
+ self.assert_close(img, m(patches))
+
+ def test_error(self, device, dtype):
+ patches = kornia.contrib.extract_tensor_patches(
+ torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4), window_size=(3, 2), stride=(2, 2), padding=1
+ )
+ with pytest.raises(RuntimeError):
+ kornia.contrib.combine_tensor_patches(patches, original_size=(4, 4), window_size=(2, 2), stride=(2, 2))
+
+ def test_rect_odd_dim(self, device, dtype):
+ img = torch.arange(12, device=device, dtype=dtype).view(1, 1, 4, 3)
+ patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=(0, 2))
+ m = kornia.contrib.combine_tensor_patches(
+ patches, original_size=(4, 3), window_size=(2, 2), stride=(2, 2), unpadding=(0, 2)
+ )
+ assert m.shape == (1, 1, 4, 3)
+ self.assert_close(img, m)
+
+ def test_pad_triple_error(self, device, dtype):
+ patches = kornia.contrib.extract_tensor_patches(
+ torch.arange(36, device=device, dtype=dtype).view(1, 1, 6, 6), window_size=(4, 4), stride=(4, 4), padding=1
+ )
+ with pytest.raises(AssertionError):
+ kornia.contrib.combine_tensor_patches(
+ patches, original_size=(6, 6), window_size=(4, 4), stride=(4, 4), unpadding=(1, 1, 1)
+ )
+
+ def test_rectangle_array(self, device, dtype):
+ img = torch.arange(24, device=device, dtype=dtype).view(1, 1, 4, 6)
+ patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
+ m = kornia.contrib.CombineTensorPatches((4, 6), (2, 2), (2, 2), unpadding=1)
+ assert m(patches).shape == (1, 1, 4, 6)
+ self.assert_close(img, m(patches))
+
+ def test_padding1(self, device, dtype):
+ img = torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4)
+ padding = kornia.contrib.compute_padding((4, 4), (2, 2))
+ patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(1, 1), padding=padding)
+ m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2), stride=(1, 1), unpadding=padding)
+ assert m(patches).shape == (1, 1, 4, 4)
+ self.assert_close(img, m(patches))
+
+ def test_padding2(self, device, dtype):
+ img = torch.arange(64, device=device, dtype=dtype).view(1, 1, 8, 8)
+ patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
+ m = kornia.contrib.CombineTensorPatches((8, 8), (2, 2), stride=(2, 2), unpadding=1)
+ assert m(patches).shape == (1, 1, 8, 8)
+ self.assert_close(img, m(patches))
+
+ def test_compute_padding(self, device, dtype):
+ img_shape = (8, 13)
+ rnge = img_shape[0] * img_shape[1]
+ img = torch.arange(rnge, device=device, dtype=dtype).view(1, 1, *img_shape)
+ window_size = (3, 3)
+ padding = kornia.contrib.compute_padding(img_shape, window_size)
+ patches = kornia.contrib.extract_tensor_patches(
+ img, window_size=window_size, stride=window_size, padding=padding
+ )
+ m = kornia.contrib.CombineTensorPatches(img_shape, window_size, stride=window_size, unpadding=padding)
+ assert m(patches).shape == (1, 1, *img_shape)
+ self.assert_close(img, m(patches))
+
+ def test_stride_greater_than_window_size(self, device, dtype):
+ patches = kornia.contrib.extract_tensor_patches(
+ torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2), padding=1
+ )
+ with pytest.raises(AssertionError):
+ kornia.contrib.combine_tensor_patches(patches, original_size=(4, 4), window_size=(2, 2), stride=(3, 2))
+
+ def test_expl_autopadding(self, device, dtype):
+ img_shape = (8, 13)
+ img = torch.arange(img_shape[0] * img_shape[1], device=device, dtype=dtype).view(
+ 1, 1, img_shape[0], img_shape[1]
+ )
+ window_size = (3, 3)
+ padding = kornia.contrib.compute_padding(img_shape, window_size)
+ patches = kornia.contrib.extract_tensor_patches(
+ img, window_size=window_size, stride=window_size, padding=padding
+ )
+ m = kornia.contrib.CombineTensorPatches(img_shape, window_size, stride=window_size, unpadding=padding)
+ assert m(patches).shape == (1, 1, *img_shape)
+
+ self.assert_close(img, m(patches))
+
+ def test_impl_autopadding(self, device, dtype):
+ img_shape = (11, 16)
+ img = torch.arange(img_shape[0] * img_shape[1], device=device, dtype=dtype).view(1, 1, *img_shape)
+ window_size = (3, 3)
+ patches = kornia.contrib.extract_tensor_patches(
+ img, window_size=window_size, stride=window_size, allow_auto_padding=True
+ )
+ recomb = kornia.contrib.combine_tensor_patches(
+ patches, img_shape, window_size=window_size, stride=window_size, allow_auto_unpadding=True
+ )
+ assert recomb.shape == img.shape
+ self.assert_close(img, recomb)
+
+ def test_gradcheck(self, device):
+ patches = kornia.contrib.extract_tensor_patches(
+ torch.arange(16.0, device=device, dtype=torch.float64).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2)
+ )
+ self.gradcheck(kornia.contrib.combine_tensor_patches, (patches, (4, 4), (2, 2), (2, 2)))
diff --git a/tests/contrib/test_connected_component.py b/tests/contrib/test_connected_component.py
new file mode 100644
index 000000000..60b5290f1
--- /dev/null
+++ b/tests/contrib/test_connected_component.py
@@ -0,0 +1,95 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestConnectedComponents(BaseTester):
+ def test_smoke(self, device, dtype):
+ img = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
+ out = kornia.contrib.connected_components(img, num_iterations=10)
+ assert out.shape == (1, 1, 3, 4)
+
+ @pytest.mark.parametrize("shape", [(1, 3, 4), (2, 1, 3, 4)])
+ def test_cardinality(self, device, dtype, shape):
+ img = torch.rand(shape, device=device, dtype=dtype)
+ out = kornia.contrib.connected_components(img, num_iterations=10)
+ assert out.shape == shape
+
+ def test_exception(self, device, dtype):
+ img = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
+
+ with pytest.raises(TypeError) as errinf:
+ assert kornia.contrib.connected_components(img, 1.0)
+ assert "Input num_iterations must be a positive integer." in str(errinf)
+
+ with pytest.raises(TypeError) as errinf:
+ assert kornia.contrib.connected_components("not a tensor", 0)
+ assert "Input imagetype is not a Tensor. Got:" in str(errinf)
+
+ with pytest.raises(TypeError) as errinf:
+ assert kornia.contrib.connected_components(img, 0)
+ assert "Input num_iterations must be a positive integer." in str(errinf)
+
+ with pytest.raises(ValueError) as errinf:
+ img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
+ assert kornia.contrib.connected_components(img, 2)
+ assert "Input image shape must be (*,1,H,W). Got:" in str(errinf)
+
+ def test_value(self, device, dtype):
+ img = torch.tensor(
+ [
+ [
+ [
+ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 1.0, 1.0, 0.0, 0.0, 1.0],
+ [0.0, 1.0, 1.0, 0.0, 0.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 0.0, 0.0, 1.0, 1.0, 0.0],
+ [0.0, 0.0, 0.0, 1.0, 1.0, 0.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 14.0, 14.0, 0.0, 0.0, 11.0],
+ [0.0, 14.0, 14.0, 0.0, 0.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
+ [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ out = kornia.contrib.connected_components(img, num_iterations=10)
+ self.assert_close(out, expected)
+
+ def test_gradcheck(self, device):
+ B, C, H, W = 2, 1, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ self.gradcheck(kornia.contrib.connected_components, (img,))
+
+ def test_jit(self, device, dtype):
+ B, C, H, W = 2, 1, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ op = kornia.contrib.connected_components
+ op_jit = torch.jit.script(op)
+ self.assert_close(op(img), op_jit(img))
+
+
+def test_compute_padding():
+ assert kornia.contrib.compute_padding((6, 6), (2, 2)) == (0, 0, 0, 0)
+ assert kornia.contrib.compute_padding((7, 7), (2, 2)) == (0, 1, 0, 1)
+ assert kornia.contrib.compute_padding((8, 7), (4, 4)) == (0, 0, 0, 1)
diff --git a/tests/contrib/test_contrib.py b/tests/contrib/test_contrib.py
deleted file mode 100644
index 478b142da..000000000
--- a/tests/contrib/test_contrib.py
+++ /dev/null
@@ -1,869 +0,0 @@
-from pathlib import Path
-from unittest.mock import PropertyMock, patch
-
-import pytest
-import torch
-
-import kornia
-from kornia.contrib.face_detection import FaceKeypoint
-from kornia.contrib.models.rt_detr import RTDETR, DETRPostProcessor, RTDETRConfig
-from kornia.utils._compat import torch_version_lt
-
-from testing.base import BaseTester
-
-
-class TestDiamondSquare(BaseTester):
- def test_smoke(self, device, dtype):
- torch.manual_seed(0)
- output_size = (1, 1, 3, 4)
- roughness = 0.5
- random_scale = 1.0
- out = kornia.contrib.diamond_square(output_size, roughness, random_scale, device=device, dtype=dtype)
- assert out.shape == output_size
- assert out.device == device
- assert out.dtype == dtype
-
- def test_normalize(self, device, dtype):
- torch.manual_seed(0)
- output_size = (1, 1, 3, 4)
- roughness = 0.5
- random_scale = 1.0
- normalize_range = (0.0, 1.0)
- expected_min = torch.tensor(normalize_range[0], device=device, dtype=dtype)
- expected_max = torch.tensor(normalize_range[1], device=device, dtype=dtype)
- out = kornia.contrib.diamond_square(
- output_size, roughness, random_scale, normalize_range=normalize_range, device=device, dtype=dtype
- )
- self.assert_close(out.min(), expected_min)
- self.assert_close(out.max(), expected_max)
-
-
-class TestVisionTransformer(BaseTester):
- @pytest.mark.parametrize("B", [1, 2])
- @pytest.mark.parametrize("H", [1, 3, 8])
- @pytest.mark.parametrize("D", [240, 768])
- @pytest.mark.parametrize("image_size", [32, 224])
- def test_smoke(self, device, dtype, B, H, D, image_size):
- patch_size = 16
- T = image_size**2 // patch_size**2 + 1 # tokens size
-
- img = torch.rand(B, 3, image_size, image_size, device=device, dtype=dtype)
- vit = kornia.contrib.VisionTransformer(image_size=image_size, num_heads=H, embed_dim=D).to(device, dtype)
-
- out = vit(img)
- assert isinstance(out, torch.Tensor)
- assert out.shape == (B, T, D)
-
- feats = vit.encoder_results
- assert isinstance(feats, list)
- assert len(feats) == 12
- for f in feats:
- assert f.shape == (B, T, D)
-
- @pytest.mark.parametrize("H", [3, 8])
- @pytest.mark.parametrize("D", [245, 1001])
- @pytest.mark.parametrize("image_size", [32, 224])
- def test_exception(self, device, dtype, H, D, image_size):
- with pytest.raises(ValueError):
- kornia.contrib.VisionTransformer(image_size=image_size, num_heads=H, embed_dim=D).to(device, dtype)
-
- def test_backbone(self, device, dtype):
- def backbone_mock(x):
- return torch.ones(1, 128, 14, 14, device=device, dtype=dtype)
-
- img = torch.rand(1, 3, 32, 32, device=device, dtype=dtype)
- vit = kornia.contrib.VisionTransformer(backbone=backbone_mock, num_heads=8).to(device, dtype)
- out = vit(img)
- assert out.shape == (1, 197, 128)
-
- @pytest.mark.parametrize("variant", ["vit_ti/16", "vit_b/16"])
- def test_from_config(self, variant):
- model = kornia.contrib.VisionTransformer.from_config(variant)
- assert isinstance(model, kornia.contrib.VisionTransformer)
-
- @pytest.mark.parametrize("variant", ["vit_ti/16"])
- def test_from_config_pretrained(self, variant):
- model = kornia.contrib.VisionTransformer.from_config(variant, pretrained=True)
- assert isinstance(model, kornia.contrib.VisionTransformer)
-
-
-class TestMobileViT(BaseTester):
- @pytest.mark.parametrize("B", [1, 2])
- @pytest.mark.parametrize("image_size", [(256, 256)])
- @pytest.mark.parametrize("mode", ["xxs", "xs", "s"])
- @pytest.mark.parametrize("patch_size", [(2, 2)])
- def test_smoke(self, device, dtype, B, image_size, mode, patch_size):
- ih, iw = image_size
- channel = {"xxs": 320, "xs": 384, "s": 640}
-
- img = torch.rand(B, 3, ih, iw, device=device, dtype=dtype)
- mvit = kornia.contrib.MobileViT(mode=mode, patch_size=patch_size).to(device, dtype)
-
- out = mvit(img)
- assert isinstance(out, torch.Tensor)
- assert out.shape == (B, channel[mode], 8, 8)
-
-
-class TestClassificationHead(BaseTester):
- @pytest.mark.parametrize("B, D, N", [(1, 8, 10), (2, 2, 5)])
- def test_smoke(self, device, dtype, B, D, N):
- feat = torch.rand(B, D, D, device=device, dtype=dtype)
- head = kornia.contrib.ClassificationHead(embed_size=D, num_classes=N).to(device, dtype)
- logits = head(feat)
- assert logits.shape == (B, N)
-
-
-class TestConnectedComponents(BaseTester):
- def test_smoke(self, device, dtype):
- img = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
- out = kornia.contrib.connected_components(img, num_iterations=10)
- assert out.shape == (1, 1, 3, 4)
-
- @pytest.mark.parametrize("shape", [(1, 3, 4), (2, 1, 3, 4)])
- def test_cardinality(self, device, dtype, shape):
- img = torch.rand(shape, device=device, dtype=dtype)
- out = kornia.contrib.connected_components(img, num_iterations=10)
- assert out.shape == shape
-
- def test_exception(self, device, dtype):
- img = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)
-
- with pytest.raises(TypeError) as errinf:
- assert kornia.contrib.connected_components(img, 1.0)
- assert "Input num_iterations must be a positive integer." in str(errinf)
-
- with pytest.raises(TypeError) as errinf:
- assert kornia.contrib.connected_components("not a tensor", 0)
- assert "Input imagetype is not a Tensor. Got:" in str(errinf)
-
- with pytest.raises(TypeError) as errinf:
- assert kornia.contrib.connected_components(img, 0)
- assert "Input num_iterations must be a positive integer." in str(errinf)
-
- with pytest.raises(ValueError) as errinf:
- img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
- assert kornia.contrib.connected_components(img, 2)
- assert "Input image shape must be (*,1,H,W). Got:" in str(errinf)
-
- def test_value(self, device, dtype):
- img = torch.tensor(
- [
- [
- [
- [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 1.0, 1.0, 0.0, 0.0, 1.0],
- [0.0, 1.0, 1.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, 1.0, 1.0, 0.0],
- [0.0, 0.0, 0.0, 1.0, 1.0, 0.0],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- expected = torch.tensor(
- [
- [
- [
- [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 14.0, 14.0, 0.0, 0.0, 11.0],
- [0.0, 14.0, 14.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
- [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- out = kornia.contrib.connected_components(img, num_iterations=10)
- self.assert_close(out, expected)
-
- def test_gradcheck(self, device):
- B, C, H, W = 2, 1, 4, 4
- img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
- self.gradcheck(kornia.contrib.connected_components, (img,))
-
- def test_jit(self, device, dtype):
- B, C, H, W = 2, 1, 4, 4
- img = torch.ones(B, C, H, W, device=device, dtype=dtype)
- op = kornia.contrib.connected_components
- op_jit = torch.jit.script(op)
- self.assert_close(op(img), op_jit(img))
-
-
-def test_compute_padding():
- assert kornia.contrib.compute_padding((6, 6), (2, 2)) == (0, 0, 0, 0)
- assert kornia.contrib.compute_padding((7, 7), (2, 2)) == (0, 1, 0, 1)
- assert kornia.contrib.compute_padding((8, 7), (4, 4)) == (0, 0, 0, 1)
-
-
-class TestExtractTensorPatches(BaseTester):
- def test_smoke(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- m = kornia.contrib.ExtractTensorPatches(3)
- assert m(img).shape == (1, 4, 1, 3, 3)
-
- def test_b1_ch1_h4w4_ws3(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- m = kornia.contrib.ExtractTensorPatches(3)
- patches = m(img)
- assert patches.shape == (1, 4, 1, 3, 3)
- self.assert_close(img[0, :, :3, :3], patches[0, 0])
- self.assert_close(img[0, :, :3, 1:], patches[0, 1])
- self.assert_close(img[0, :, 1:, :3], patches[0, 2])
- self.assert_close(img[0, :, 1:, 1:], patches[0, 3])
-
- def test_b1_ch2_h4w4_ws3(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- img = img.expand(-1, 2, -1, -1) # copy all channels
- m = kornia.contrib.ExtractTensorPatches(3)
- patches = m(img)
- assert patches.shape == (1, 4, 2, 3, 3)
- self.assert_close(img[0, :, :3, :3], patches[0, 0])
- self.assert_close(img[0, :, :3, 1:], patches[0, 1])
- self.assert_close(img[0, :, 1:, :3], patches[0, 2])
- self.assert_close(img[0, :, 1:, 1:], patches[0, 3])
-
- def test_b1_ch1_h4w4_ws2(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- m = kornia.contrib.ExtractTensorPatches(2)
- patches = m(img)
- assert patches.shape == (1, 9, 1, 2, 2)
- self.assert_close(img[0, :, 0:2, 1:3], patches[0, 1])
- self.assert_close(img[0, :, 0:2, 2:4], patches[0, 2])
- self.assert_close(img[0, :, 1:3, 1:3], patches[0, 4])
- self.assert_close(img[0, :, 2:4, 1:3], patches[0, 7])
-
- def test_b1_ch1_h4w4_ws2_stride2(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- m = kornia.contrib.ExtractTensorPatches(2, stride=2)
- patches = m(img)
- assert patches.shape == (1, 4, 1, 2, 2)
- self.assert_close(img[0, :, 0:2, 0:2], patches[0, 0])
- self.assert_close(img[0, :, 0:2, 2:4], patches[0, 1])
- self.assert_close(img[0, :, 2:4, 0:2], patches[0, 2])
- self.assert_close(img[0, :, 2:4, 2:4], patches[0, 3])
-
- def test_b1_ch1_h4w4_ws2_stride21(self, device):
- img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
- m = kornia.contrib.ExtractTensorPatches(2, stride=(2, 1))
- patches = m(img)
- assert patches.shape == (1, 6, 1, 2, 2)
- self.assert_close(img[0, :, 0:2, 1:3], patches[0, 1])
- self.assert_close(img[0, :, 0:2, 2:4], patches[0, 2])
- self.assert_close(img[0, :, 2:4, 0:2], patches[0, 3])
- self.assert_close(img[0, :, 2:4, 2:4], patches[0, 5])
-
- def test_b1_ch1_h3w3_ws2_stride1_padding1(self, device):
- img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
- m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
- patches = m(img)
- assert patches.shape == (1, 16, 1, 2, 2)
- self.assert_close(img[0, :, 0:2, 0:2], patches[0, 5])
- self.assert_close(img[0, :, 0:2, 1:3], patches[0, 6])
- self.assert_close(img[0, :, 1:3, 0:2], patches[0, 9])
- self.assert_close(img[0, :, 1:3, 1:3], patches[0, 10])
-
- def test_b2_ch1_h3w3_ws2_stride1_padding1(self, device):
- batch_size = 2
- img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
- img = img.expand(batch_size, -1, -1, -1)
- m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
- patches = m(img)
- assert patches.shape == (batch_size, 16, 1, 2, 2)
- for i in range(batch_size):
- self.assert_close(img[i, :, 0:2, 0:2], patches[i, 5])
- self.assert_close(img[i, :, 0:2, 1:3], patches[i, 6])
- self.assert_close(img[i, :, 1:3, 0:2], patches[i, 9])
- self.assert_close(img[i, :, 1:3, 1:3], patches[i, 10])
-
- def test_b1_ch1_h3w3_ws23(self, device):
- img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
- m = kornia.contrib.ExtractTensorPatches((2, 3))
- patches = m(img)
- assert patches.shape == (1, 2, 1, 2, 3)
- self.assert_close(img[0, :, 0:2, 0:3], patches[0, 0])
- self.assert_close(img[0, :, 1:3, 0:3], patches[0, 1])
-
- def test_b1_ch1_h3w4_ws23(self, device):
- img = torch.arange(12.0).view(1, 1, 3, 4).to(device)
- m = kornia.contrib.ExtractTensorPatches((2, 3))
- patches = m(img)
- assert patches.shape == (1, 4, 1, 2, 3)
- self.assert_close(img[0, :, 0:2, 0:3], patches[0, 0])
- self.assert_close(img[0, :, 0:2, 1:4], patches[0, 1])
- self.assert_close(img[0, :, 1:3, 0:3], patches[0, 2])
- self.assert_close(img[0, :, 1:3, 1:4], patches[0, 3])
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(img: torch.Tensor, height: int, width: int) -> torch.Tensor:
- return kornia.geometry.denormalize_pixel_coordinates(img, height, width)
-
- height, width = 3, 4
- grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=True).to(device)
-
- actual = op_script(grid, height, width)
- expected = kornia.denormalize_pixel_coordinates(grid, height, width)
-
- self.assert_close(actual, expected)
-
- def test_gradcheck(self, device):
- img = torch.rand(2, 3, 4, 4, device=device, dtype=torch.float64)
- self.gradcheck(kornia.contrib.extract_tensor_patches, (img, 3))
-
- def test_auto_padding_stride(self, device, dtype):
- img_shape = (11, 14)
- window_size = (3, 3)
- stride = 2
- rnge = img_shape[0] * img_shape[1]
- img = torch.arange(rnge, device=device, dtype=dtype).view(1, 1, *img_shape)
- patches = kornia.contrib.extract_tensor_patches(
- img, window_size=window_size, stride=stride, allow_auto_padding=True
- )
- # 5 patches vertical, 6 2/3 = 7 horizontal = 35 patches
- assert patches.shape == (1, 35, 1, *window_size)
-
-
-class TestCombineTensorPatches(BaseTester):
- def test_smoke(self, device, dtype):
- img = torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4)
- m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2), (2, 2))
- patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2))
- assert m(patches).shape == (1, 1, 4, 4)
- self.assert_close(img, m(patches))
-
- def test_error(self, device, dtype):
- patches = kornia.contrib.extract_tensor_patches(
- torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4), window_size=(3, 2), stride=(2, 2), padding=1
- )
- with pytest.raises(RuntimeError):
- kornia.contrib.combine_tensor_patches(patches, original_size=(4, 4), window_size=(2, 2), stride=(2, 2))
-
- def test_rect_odd_dim(self, device, dtype):
- img = torch.arange(12, device=device, dtype=dtype).view(1, 1, 4, 3)
- patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=(0, 2))
- m = kornia.contrib.combine_tensor_patches(
- patches, original_size=(4, 3), window_size=(2, 2), stride=(2, 2), unpadding=(0, 2)
- )
- assert m.shape == (1, 1, 4, 3)
- self.assert_close(img, m)
-
- def test_pad_triple_error(self, device, dtype):
- patches = kornia.contrib.extract_tensor_patches(
- torch.arange(36, device=device, dtype=dtype).view(1, 1, 6, 6), window_size=(4, 4), stride=(4, 4), padding=1
- )
- with pytest.raises(AssertionError):
- kornia.contrib.combine_tensor_patches(
- patches, original_size=(6, 6), window_size=(4, 4), stride=(4, 4), unpadding=(1, 1, 1)
- )
-
- def test_rectangle_array(self, device, dtype):
- img = torch.arange(24, device=device, dtype=dtype).view(1, 1, 4, 6)
- patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
- m = kornia.contrib.CombineTensorPatches((4, 6), (2, 2), (2, 2), unpadding=1)
- assert m(patches).shape == (1, 1, 4, 6)
- self.assert_close(img, m(patches))
-
- def test_padding1(self, device, dtype):
- img = torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4)
- padding = kornia.contrib.compute_padding((4, 4), (2, 2))
- patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(1, 1), padding=padding)
- m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2), stride=(1, 1), unpadding=padding)
- assert m(patches).shape == (1, 1, 4, 4)
- self.assert_close(img, m(patches))
-
- def test_padding2(self, device, dtype):
- img = torch.arange(64, device=device, dtype=dtype).view(1, 1, 8, 8)
- patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
- m = kornia.contrib.CombineTensorPatches((8, 8), (2, 2), stride=(2, 2), unpadding=1)
- assert m(patches).shape == (1, 1, 8, 8)
- self.assert_close(img, m(patches))
-
- def test_compute_padding(self, device, dtype):
- img_shape = (8, 13)
- rnge = img_shape[0] * img_shape[1]
- img = torch.arange(rnge, device=device, dtype=dtype).view(1, 1, *img_shape)
- window_size = (3, 3)
- padding = kornia.contrib.compute_padding(img_shape, window_size)
- patches = kornia.contrib.extract_tensor_patches(
- img, window_size=window_size, stride=window_size, padding=padding
- )
- m = kornia.contrib.CombineTensorPatches(img_shape, window_size, stride=window_size, unpadding=padding)
- assert m(patches).shape == (1, 1, *img_shape)
- self.assert_close(img, m(patches))
-
- def test_stride_greater_than_window_size(self, device, dtype):
- patches = kornia.contrib.extract_tensor_patches(
- torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2), padding=1
- )
- with pytest.raises(AssertionError):
- kornia.contrib.combine_tensor_patches(patches, original_size=(4, 4), window_size=(2, 2), stride=(3, 2))
-
- def test_expl_autopadding(self, device, dtype):
- img_shape = (8, 13)
- img = torch.arange(img_shape[0] * img_shape[1], device=device, dtype=dtype).view(
- 1, 1, img_shape[0], img_shape[1]
- )
- window_size = (3, 3)
- padding = kornia.contrib.compute_padding(img_shape, window_size)
- patches = kornia.contrib.extract_tensor_patches(
- img, window_size=window_size, stride=window_size, padding=padding
- )
- m = kornia.contrib.CombineTensorPatches(img_shape, window_size, stride=window_size, unpadding=padding)
- assert m(patches).shape == (1, 1, *img_shape)
-
- self.assert_close(img, m(patches))
-
- def test_impl_autopadding(self, device, dtype):
- img_shape = (11, 16)
- img = torch.arange(img_shape[0] * img_shape[1], device=device, dtype=dtype).view(1, 1, *img_shape)
- window_size = (3, 3)
- patches = kornia.contrib.extract_tensor_patches(
- img, window_size=window_size, stride=window_size, allow_auto_padding=True
- )
- recomb = kornia.contrib.combine_tensor_patches(
- patches, img_shape, window_size=window_size, stride=window_size, allow_auto_unpadding=True
- )
- assert recomb.shape == img.shape
- self.assert_close(img, recomb)
-
- def test_gradcheck(self, device):
- patches = kornia.contrib.extract_tensor_patches(
- torch.arange(16.0, device=device, dtype=torch.float64).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2)
- )
- self.gradcheck(kornia.contrib.combine_tensor_patches, (patches, (4, 4), (2, 2), (2, 2)))
-
-
-class TestLambdaModule(BaseTester):
- def add_2_layer(self, tensor):
- return tensor + 2
-
- def add_x_mul_y(self, tensor, x, y=2):
- return torch.mul(tensor + x, y)
-
- def test_smoke(self, device, dtype):
- B, C, H, W = 1, 3, 4, 5
- img = torch.rand(B, C, H, W, device=device, dtype=dtype)
- func = self.add_2_layer
- if not callable(func):
- raise TypeError(f"Argument lambd should be callable, got {type(func).__name__!r}")
- assert isinstance(kornia.contrib.Lambda(func)(img), torch.Tensor)
-
- @pytest.mark.parametrize("x", [3, 2, 5])
- def test_lambda_with_arguments(self, x, device, dtype):
- B, C, H, W = 2, 3, 5, 7
- img = torch.rand(B, C, H, W, device=device, dtype=dtype)
- func = self.add_x_mul_y
- lambda_module = kornia.contrib.Lambda(func)
- out = lambda_module(img, x)
- assert isinstance(out, torch.Tensor)
-
- @pytest.mark.parametrize("shape", [(1, 3, 2, 3), (2, 3, 5, 7)])
- def test_lambda(self, shape, device, dtype):
- B, C, H, W = shape
- img = torch.rand(B, C, H, W, device=device, dtype=dtype)
- func = kornia.color.bgr_to_grayscale
- lambda_module = kornia.contrib.Lambda(func)
- out = lambda_module(img)
- assert isinstance(out, torch.Tensor)
-
- def test_gradcheck(self, device):
- B, C, H, W = 1, 3, 4, 5
- img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
- func = kornia.color.bgr_to_grayscale
- self.gradcheck(kornia.contrib.Lambda(func), (img,))
-
-
-class TestImageStitcher(BaseTester):
- @pytest.mark.parametrize("estimator", ["ransac", "vanilla"])
- def test_smoke(self, estimator, device, dtype):
- B, C, H, W = 1, 3, 6, 6
- sample1 = torch.tensor(
- [
- [0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251],
- [0.2071, 0.6297, 0.3653, 0.8513, 0.8549, 0.5509],
- [0.2868, 0.2063, 0.4451, 0.3593, 0.7204, 0.0731],
- [0.9699, 0.1078, 0.8829, 0.4132, 0.7572, 0.6948],
- [0.5209, 0.5932, 0.8797, 0.6286, 0.7653, 0.1132],
- [0.8559, 0.6721, 0.6267, 0.5691, 0.7437, 0.9592],
- ],
- dtype=dtype,
- device=device,
- )
- sample2 = torch.tensor(
- [
- [0.3887, 0.2214, 0.3742, 0.1953, 0.7405, 0.2529],
- [0.2332, 0.9314, 0.9575, 0.5575, 0.4134, 0.4355],
- [0.7369, 0.0331, 0.0914, 0.8994, 0.9936, 0.4703],
- [0.1049, 0.5137, 0.2674, 0.4990, 0.7447, 0.7213],
- [0.4414, 0.5550, 0.6361, 0.1081, 0.3305, 0.5196],
- [0.2147, 0.2816, 0.6679, 0.7878, 0.5070, 0.3055],
- ],
- dtype=dtype,
- device=device,
- )
- sample1 = sample1.expand((B, C, H, W))
- sample2 = sample2.expand((B, C, H, W))
- return_value = {
- "keypoints0": torch.tensor(
- [
- [0.1546, 0.9391],
- [0.8077, 0.1051],
- [0.6768, 0.5596],
- [0.5092, 0.7195],
- [0.2856, 0.8889],
- [0.4342, 0.0203],
- [0.6701, 0.0585],
- [0.3828, 0.9038],
- [0.7301, 0.0762],
- [0.7864, 0.4490],
- [0.3509, 0.0756],
- [0.6782, 0.9297],
- [0.4132, 0.3664],
- [0.3134, 0.5039],
- [0.2073, 0.2552],
- ],
- device=device,
- dtype=dtype,
- ),
- "keypoints1": torch.tensor(
- [
- [0.2076, 0.2669],
- [0.9679, 0.8137],
- [0.9536, 0.8317],
- [0.3718, 0.2456],
- [0.3875, 0.8450],
- [0.7592, 0.1687],
- [0.5173, 0.6760],
- [0.9446, 0.4570],
- [0.6164, 0.1867],
- [0.4732, 0.1786],
- [0.4090, 0.8089],
- [0.9742, 0.8943],
- [0.5996, 0.7427],
- [0.7038, 0.9210],
- [0.6272, 0.0796],
- ],
- device=device,
- dtype=dtype,
- ),
- "confidence": torch.tensor(
- [
- 0.9314,
- 0.5951,
- 0.4187,
- 0.0318,
- 0.1434,
- 0.7952,
- 0.8306,
- 0.7511,
- 0.6407,
- 0.7379,
- 0.4363,
- 0.9220,
- 0.8453,
- 0.5075,
- 0.8141,
- ],
- device=device,
- dtype=dtype,
- ),
- "batch_indexes": torch.zeros((15,), device=device, dtype=dtype),
- }
- with patch(
- "kornia.contrib.ImageStitcher.on_matcher", new_callable=PropertyMock, return_value=lambda x: return_value
- ):
- # NOTE: This will need to download the pretrained weights.
- # To avoid that, we mock as below
- matcher = kornia.feature.LoFTR(None)
- stitcher = kornia.contrib.ImageStitcher(matcher, estimator=estimator).to(device=device, dtype=dtype)
- torch.manual_seed(1) # issue kornia#2027
- out = stitcher(sample1, sample2)
- assert out.shape[:-1] == torch.Size([1, 3, 6])
- assert out.shape[-1] <= 12
-
- @pytest.mark.slow
- def test_exception(self, device, dtype):
- B, C, H, W = 1, 3, 224, 224
- sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
- sample2 = torch.rand(B, C, H, W, device=device, dtype=dtype)
- # NOTE: This will need to download the pretrained weights.
- matcher = kornia.feature.LoFTR(None)
-
- with pytest.raises(NotImplementedError):
- stitcher = kornia.contrib.ImageStitcher(matcher, estimator="random").to(device=device, dtype=dtype)
-
- stitcher = kornia.contrib.ImageStitcher(matcher).to(device=device, dtype=dtype)
- with pytest.raises(RuntimeError):
- stitcher(sample1, sample2)
-
-
-class TestConvDistanceTransform(BaseTester):
- @pytest.mark.parametrize("kernel_size", [3, 5, 7])
- def test_smoke(self, kernel_size, device, dtype):
- sample1 = torch.rand(1, 3, 100, 100, device=device, dtype=dtype)
- sample2 = torch.rand(1, 1, 100, 100, device=device, dtype=dtype)
- distance_transformer = kornia.contrib.DistanceTransform(kernel_size)
-
- output1 = distance_transformer(sample1)
- output2 = kornia.contrib.distance_transform(sample2, kernel_size)
-
- assert isinstance(output1, torch.Tensor)
- assert isinstance(output2, torch.Tensor)
- assert output1.shape == sample1.shape
-
- def test_module(self, device, dtype):
- B, C, H, W = 1, 1, 99, 100
- sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
- distance_transformer = kornia.contrib.DistanceTransform().to(device, dtype)
-
- output1 = distance_transformer(sample1)
- output2 = kornia.contrib.distance_transform(sample1)
- self.assert_close(output1, output2)
-
- def test_exception(self, device, dtype):
- B, C, H, W = 1, 1, 224, 224
- sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
- sample2 = torch.rand(C, H, W, device=device, dtype=dtype)
-
- # Non-odd kernel size
- with pytest.raises(ValueError):
- ConvDT = kornia.contrib.DistanceTransform(6)
- ConvDT.forward(sample1)
-
- with pytest.raises(ValueError):
- kornia.contrib.distance_transform(sample1, 4)
-
- # Invalid input dimensions
- with pytest.raises(ValueError):
- kornia.contrib.distance_transform(sample2)
-
- # Invalid input type
- with pytest.raises(TypeError):
- kornia.contrib.distance_transform(None)
-
- def test_value(self, device, dtype):
- B, C, H, W = 1, 1, 4, 4
- kernel_size = 7
- h = 0.35
- sample1 = torch.zeros(B, C, H, W, device=device, dtype=dtype)
- sample1[:, :, 1, 1] = 1.0
- expected_output1 = torch.tensor(
- [
- [
- [
- [1.4142135382, 1.0000000000, 1.4142135382, 2.2360680103],
- [1.0000000000, 0.0000000000, 1.0000000000, 2.0000000000],
- [1.4142135382, 1.0000000000, 1.4142135382, 2.2360680103],
- [2.2360680103, 2.0000000000, 2.2360680103, 2.8284270763],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
- output1 = kornia.contrib.distance_transform(sample1, kernel_size, h)
- self.assert_close(expected_output1, output1)
-
- def test_gradcheck(self, device):
- B, C, H, W = 1, 1, 32, 32
- sample1 = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
- self.gradcheck(kornia.contrib.distance_transform, (sample1))
-
- def test_loss_grad(self, device, dtype):
- B, C, H, W = 1, 1, 32, 32
- sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype, requires_grad=True)
- sample2 = torch.rand(B, C, H, W, device=device, dtype=dtype, requires_grad=True)
- tiny_module = torch.nn.Conv2d(1, 1, (3, 3), (1, 1), (1, 1)).to(device=device, dtype=dtype)
- sample1 = kornia.contrib.distance_transform(tiny_module(sample1))
- sample2 = kornia.contrib.distance_transform(sample2)
- loss = torch.nn.functional.mse_loss(sample1, sample2)
- loss.backward()
-
-
-class TestHistMatch(BaseTester):
- def test_interp(self, device, dtype):
- xp = torch.tensor([1, 2, 3], device=device, dtype=dtype)
- fp = torch.tensor([4, 2, 0], device=device, dtype=dtype)
- x = torch.tensor([4, 5, 6], device=device, dtype=dtype)
- x_hat_expected = torch.tensor([-2.0, -4.0, -6.0], device=device, dtype=dtype)
- x_hat = kornia.contrib.interp(x, xp, fp)
- self.assert_close(x_hat_expected, x_hat)
-
- def test_histmatch(self, device, dtype):
- torch.manual_seed(44)
- # generate random value by CPU.
- src = torch.randn(1, 4, 4).to(device=device, dtype=dtype)
- dst = torch.randn(1, 16, 16).to(device=device, dtype=dtype)
- out = kornia.contrib.histogram_matching(src, dst)
- exp = torch.tensor(
- [
- [
- [1.5902, 0.9295, 2.9409, 0.1211],
- [0.2472, 1.2137, -0.1098, -0.4272],
- [-0.2644, -1.1983, -0.6065, -0.8091],
- [-1.4999, 0.6370, -0.9800, 0.4474],
- ]
- ],
- device=device,
- dtype=dtype,
- )
- assert exp.shape == out.shape
- self.assert_close(out, exp, rtol=1e-4, atol=1e-4)
-
- @pytest.mark.skip(reason="not differentiable now.")
- def test_grad(self, device):
- B, C, H, W = 1, 3, 32, 32
- src = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
- dst = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
- self.gradcheck(kornia.contrib.histogram_matching, (src, dst))
-
-
-class TestFaceDetection(BaseTester):
- @pytest.mark.slow
- def test_smoke(self, device, dtype):
- assert kornia.contrib.FaceDetector().to(device, dtype) is not None
-
- @pytest.mark.slow
- @pytest.mark.parametrize("batch_size", [1, 2, 4])
- def test_valid(self, batch_size, device, dtype):
- torch.manual_seed(44)
- img = torch.rand(batch_size, 3, 320, 320, device=device, dtype=dtype)
- face_detection = kornia.contrib.FaceDetector().to(device, dtype)
- dets = face_detection(img)
- assert isinstance(dets, list)
- assert len(dets) == batch_size # same as the number of images
- assert isinstance(dets[0], torch.Tensor)
- assert dets[0].shape[0] >= 0 # number of detections
- assert dets[0].shape[1] == 15 # dims of each detection
-
- @pytest.mark.slow
- def test_jit(self, device, dtype):
- op = kornia.contrib.FaceDetector().to(device, dtype)
- op_jit = torch.jit.script(op)
- assert op_jit is not None
-
- @pytest.mark.slow
- def test_results(self, device, dtype):
- data = torch.tensor(
- [0.0, 0.0, 100.0, 200.0, 10.0, 10.0, 20.0, 10.0, 10.0, 50.0, 100.0, 50.0, 150.0, 10.0, 0.99],
- device=device,
- dtype=dtype,
- )
- res = kornia.contrib.FaceDetectorResult(data)
- assert res.xmin == 0.0
- assert res.ymin == 0.0
- assert res.xmax == 100.0
- assert res.ymax == 200.0
- assert res.score == 0.99
- assert res.width == 100.0
- assert res.height == 200.0
- assert res.top_left.tolist() == [0.0, 0.0]
- assert res.top_right.tolist() == [100.0, 0.0]
- assert res.bottom_right.tolist() == [100.0, 200.0]
- assert res.bottom_left.tolist() == [0.0, 200.0]
- assert res.get_keypoint(FaceKeypoint.EYE_LEFT).tolist() == [10.0, 10.0]
- assert res.get_keypoint(FaceKeypoint.EYE_RIGHT).tolist() == [20.0, 10.0]
- assert res.get_keypoint(FaceKeypoint.NOSE).tolist() == [10.0, 50.0]
- assert res.get_keypoint(FaceKeypoint.MOUTH_LEFT).tolist() == [100.0, 50.0]
- assert res.get_keypoint(FaceKeypoint.MOUTH_RIGHT).tolist() == [150.0, 10.0]
-
- @pytest.mark.slow
- def test_results_raise(self, device, dtype):
- data = torch.zeros(14, device=device, dtype=dtype)
- with pytest.raises(ValueError):
- _ = kornia.contrib.FaceDetectorResult(data)
-
-
-class TestEdgeDetector(BaseTester):
- @pytest.mark.slow
- def test_smoke(self, device, dtype):
- img = torch.rand(2, 3, 64, 64, device=device, dtype=dtype)
- net = kornia.contrib.EdgeDetector().to(device, dtype)
- out = net(img)
- assert out.shape == (2, 1, 64, 64)
-
- @pytest.mark.slow
- def test_jit(self, device, dtype):
- op = kornia.contrib.EdgeDetector().to(device, dtype)
- op_jit = torch.jit.script(op)
- assert op_jit is not None
-
-
-class TestObjectDetector(BaseTester):
- def test_smoke(self, device, dtype):
- batch_size = 3
- confidence = 0.3
- config = RTDETRConfig("resnet50d", 10, head_num_queries=10)
- model = RTDETR.from_config(config).to(device, dtype).eval()
- pre_processor = kornia.contrib.object_detection.ResizePreProcessor((32, 32))
- post_processor = DETRPostProcessor(confidence).to(device, dtype).eval()
- detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)
-
- sizes = torch.randint(5, 10, (batch_size, 2)) * 32
- imgs = [torch.randn(3, h, w, device=device, dtype=dtype) for h, w in sizes]
- pre_processor_out = pre_processor(imgs)
- detections = detector(imgs)
-
- assert pre_processor_out[0].shape[-1] == 32
- assert pre_processor_out[0].shape[-2] == 32
- assert len(detections) == batch_size
- for dets in detections:
- assert dets.shape[1] == 6
- assert torch.all(dets[:, 0].int() == dets[:, 0])
- assert torch.all(dets[:, 1] >= 0.3)
-
- @pytest.mark.slow
- @pytest.mark.skipif(torch_version_lt(2, 0, 0), reason="Unsupported ONNX opset version: 16")
- @pytest.mark.parametrize("variant", ("resnet50d", "hgnetv2_l"))
- def test_onnx(self, device, dtype, tmp_path: Path, variant: str):
- config = RTDETRConfig(variant, 1)
- model = RTDETR.from_config(config).to(device=device, dtype=dtype).eval()
- pre_processor = kornia.contrib.object_detection.ResizePreProcessor(640)
- post_processor = DETRPostProcessor(0.3)
- detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)
-
- data = torch.rand(3, 400, 640, device=device, dtype=dtype)
-
- model_path = tmp_path / "rtdetr.onnx"
-
- dynamic_axes = {"images": {0: "N"}}
- torch.onnx.export(
- detector,
- [data],
- model_path,
- input_names=["images"],
- output_names=["detections"],
- dynamic_axes=dynamic_axes,
- opset_version=16,
- )
-
- assert model_path.is_file()
-
- def test_results_from_detections(self, device, dtype):
- # label_id, confidence, data
- detections = torch.tensor(
- [
- [0, 0.9, 0.0, 0.0, 1.0, 1.0],
- [1, 0.8, 0.0, 0.0, 1.0, 1.0],
- [2, 0.7, 0.0, 0.0, 1.0, 1.0],
- [3, 0.6, 0.0, 0.0, 1.0, 1.0],
- [4, 0.5, 0.0, 0.0, 1.0, 1.0],
- ],
- device=device,
- dtype=dtype,
- )
-
- detector_results: list = kornia.contrib.object_detection.results_from_detections(detections, format="xywh")
-
- assert len(detector_results) == 5
- for j, det in enumerate(detector_results):
- for i in range(4):
- assert det.bbox.data[i] == float(detections[j, i + 2])
diff --git a/tests/contrib/test_conv_distance_transformer.py b/tests/contrib/test_conv_distance_transformer.py
new file mode 100644
index 000000000..e439477b9
--- /dev/null
+++ b/tests/contrib/test_conv_distance_transformer.py
@@ -0,0 +1,89 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestConvDistanceTransform(BaseTester):
+ @pytest.mark.parametrize("kernel_size", [3, 5, 7])
+ def test_smoke(self, kernel_size, device, dtype):
+ sample1 = torch.rand(1, 3, 100, 100, device=device, dtype=dtype)
+ sample2 = torch.rand(1, 1, 100, 100, device=device, dtype=dtype)
+ distance_transformer = kornia.contrib.DistanceTransform(kernel_size)
+
+ output1 = distance_transformer(sample1)
+ output2 = kornia.contrib.distance_transform(sample2, kernel_size)
+
+ assert isinstance(output1, torch.Tensor)
+ assert isinstance(output2, torch.Tensor)
+ assert output1.shape == sample1.shape
+
+ def test_module(self, device, dtype):
+ B, C, H, W = 1, 1, 99, 100
+ sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ distance_transformer = kornia.contrib.DistanceTransform().to(device, dtype)
+
+ output1 = distance_transformer(sample1)
+ output2 = kornia.contrib.distance_transform(sample1)
+ self.assert_close(output1, output2)
+
+ def test_exception(self, device, dtype):
+ B, C, H, W = 1, 1, 224, 224
+ sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ sample2 = torch.rand(C, H, W, device=device, dtype=dtype)
+
+ # Non-odd kernel size
+ with pytest.raises(ValueError):
+ ConvDT = kornia.contrib.DistanceTransform(6)
+ ConvDT.forward(sample1)
+
+ with pytest.raises(ValueError):
+ kornia.contrib.distance_transform(sample1, 4)
+
+ # Invalid input dimensions
+ with pytest.raises(ValueError):
+ kornia.contrib.distance_transform(sample2)
+
+ # Invalid input type
+ with pytest.raises(TypeError):
+ kornia.contrib.distance_transform(None)
+
+ def test_value(self, device, dtype):
+ B, C, H, W = 1, 1, 4, 4
+ kernel_size = 7
+ h = 0.35
+ sample1 = torch.zeros(B, C, H, W, device=device, dtype=dtype)
+ sample1[:, :, 1, 1] = 1.0
+ expected_output1 = torch.tensor(
+ [
+ [
+ [
+ [1.4142135382, 1.0000000000, 1.4142135382, 2.2360680103],
+ [1.0000000000, 0.0000000000, 1.0000000000, 2.0000000000],
+ [1.4142135382, 1.0000000000, 1.4142135382, 2.2360680103],
+ [2.2360680103, 2.0000000000, 2.2360680103, 2.8284270763],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ output1 = kornia.contrib.distance_transform(sample1, kernel_size, h)
+ self.assert_close(expected_output1, output1)
+
+ def test_gradcheck(self, device):
+ B, C, H, W = 1, 1, 32, 32
+ sample1 = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ self.gradcheck(kornia.contrib.distance_transform, (sample1))
+
+ def test_loss_grad(self, device, dtype):
+ B, C, H, W = 1, 1, 32, 32
+ sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype, requires_grad=True)
+ sample2 = torch.rand(B, C, H, W, device=device, dtype=dtype, requires_grad=True)
+ tiny_module = torch.nn.Conv2d(1, 1, (3, 3), (1, 1), (1, 1)).to(device=device, dtype=dtype)
+ sample1 = kornia.contrib.distance_transform(tiny_module(sample1))
+ sample2 = kornia.contrib.distance_transform(sample2)
+ loss = torch.nn.functional.mse_loss(sample1, sample2)
+ loss.backward()
diff --git a/tests/contrib/test_diamond_square.py b/tests/contrib/test_diamond_square.py
new file mode 100644
index 000000000..ad3f2e39b
--- /dev/null
+++ b/tests/contrib/test_diamond_square.py
@@ -0,0 +1,31 @@
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestDiamondSquare(BaseTester):
+ def test_smoke(self, device, dtype):
+ torch.manual_seed(0)
+ output_size = (1, 1, 3, 4)
+ roughness = 0.5
+ random_scale = 1.0
+ out = kornia.contrib.diamond_square(output_size, roughness, random_scale, device=device, dtype=dtype)
+ assert out.shape == output_size
+ assert out.device == device
+ assert out.dtype == dtype
+
+ def test_normalize(self, device, dtype):
+ torch.manual_seed(0)
+ output_size = (1, 1, 3, 4)
+ roughness = 0.5
+ random_scale = 1.0
+ normalize_range = (0.0, 1.0)
+ expected_min = torch.tensor(normalize_range[0], device=device, dtype=dtype)
+ expected_max = torch.tensor(normalize_range[1], device=device, dtype=dtype)
+ out = kornia.contrib.diamond_square(
+ output_size, roughness, random_scale, normalize_range=normalize_range, device=device, dtype=dtype
+ )
+ self.assert_close(out.min(), expected_min)
+ self.assert_close(out.max(), expected_max)
diff --git a/tests/contrib/test_edge_detector.py b/tests/contrib/test_edge_detector.py
new file mode 100644
index 000000000..5bfa48485
--- /dev/null
+++ b/tests/contrib/test_edge_detector.py
@@ -0,0 +1,21 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestEdgeDetector(BaseTester):
+ @pytest.mark.slow
+ def test_smoke(self, device, dtype):
+ img = torch.rand(2, 3, 64, 64, device=device, dtype=dtype)
+ net = kornia.contrib.EdgeDetector().to(device, dtype)
+ out = net(img)
+ assert out.shape == (2, 1, 64, 64)
+
+ @pytest.mark.slow
+ def test_jit(self, device, dtype):
+ op = kornia.contrib.EdgeDetector().to(device, dtype)
+ op_jit = torch.jit.script(op)
+ assert op_jit is not None
diff --git a/tests/contrib/test_extract_tensor_patch.py b/tests/contrib/test_extract_tensor_patch.py
new file mode 100644
index 000000000..8ba92b403
--- /dev/null
+++ b/tests/contrib/test_extract_tensor_patch.py
@@ -0,0 +1,135 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestExtractTensorPatches(BaseTester):
+ def test_smoke(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ m = kornia.contrib.ExtractTensorPatches(3)
+ assert m(img).shape == (1, 4, 1, 3, 3)
+
+ def test_b1_ch1_h4w4_ws3(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ m = kornia.contrib.ExtractTensorPatches(3)
+ patches = m(img)
+ assert patches.shape == (1, 4, 1, 3, 3)
+ self.assert_close(img[0, :, :3, :3], patches[0, 0])
+ self.assert_close(img[0, :, :3, 1:], patches[0, 1])
+ self.assert_close(img[0, :, 1:, :3], patches[0, 2])
+ self.assert_close(img[0, :, 1:, 1:], patches[0, 3])
+
+ def test_b1_ch2_h4w4_ws3(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ img = img.expand(-1, 2, -1, -1) # copy all channels
+ m = kornia.contrib.ExtractTensorPatches(3)
+ patches = m(img)
+ assert patches.shape == (1, 4, 2, 3, 3)
+ self.assert_close(img[0, :, :3, :3], patches[0, 0])
+ self.assert_close(img[0, :, :3, 1:], patches[0, 1])
+ self.assert_close(img[0, :, 1:, :3], patches[0, 2])
+ self.assert_close(img[0, :, 1:, 1:], patches[0, 3])
+
+ def test_b1_ch1_h4w4_ws2(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ m = kornia.contrib.ExtractTensorPatches(2)
+ patches = m(img)
+ assert patches.shape == (1, 9, 1, 2, 2)
+ self.assert_close(img[0, :, 0:2, 1:3], patches[0, 1])
+ self.assert_close(img[0, :, 0:2, 2:4], patches[0, 2])
+ self.assert_close(img[0, :, 1:3, 1:3], patches[0, 4])
+ self.assert_close(img[0, :, 2:4, 1:3], patches[0, 7])
+
+ def test_b1_ch1_h4w4_ws2_stride2(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ m = kornia.contrib.ExtractTensorPatches(2, stride=2)
+ patches = m(img)
+ assert patches.shape == (1, 4, 1, 2, 2)
+ self.assert_close(img[0, :, 0:2, 0:2], patches[0, 0])
+ self.assert_close(img[0, :, 0:2, 2:4], patches[0, 1])
+ self.assert_close(img[0, :, 2:4, 0:2], patches[0, 2])
+ self.assert_close(img[0, :, 2:4, 2:4], patches[0, 3])
+
+ def test_b1_ch1_h4w4_ws2_stride21(self, device):
+ img = torch.arange(16.0, device=device).view(1, 1, 4, 4)
+ m = kornia.contrib.ExtractTensorPatches(2, stride=(2, 1))
+ patches = m(img)
+ assert patches.shape == (1, 6, 1, 2, 2)
+ self.assert_close(img[0, :, 0:2, 1:3], patches[0, 1])
+ self.assert_close(img[0, :, 0:2, 2:4], patches[0, 2])
+ self.assert_close(img[0, :, 2:4, 0:2], patches[0, 3])
+ self.assert_close(img[0, :, 2:4, 2:4], patches[0, 5])
+
+ def test_b1_ch1_h3w3_ws2_stride1_padding1(self, device):
+ img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
+ m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
+ patches = m(img)
+ assert patches.shape == (1, 16, 1, 2, 2)
+ self.assert_close(img[0, :, 0:2, 0:2], patches[0, 5])
+ self.assert_close(img[0, :, 0:2, 1:3], patches[0, 6])
+ self.assert_close(img[0, :, 1:3, 0:2], patches[0, 9])
+ self.assert_close(img[0, :, 1:3, 1:3], patches[0, 10])
+
+ def test_b2_ch1_h3w3_ws2_stride1_padding1(self, device):
+ batch_size = 2
+ img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
+ img = img.expand(batch_size, -1, -1, -1)
+ m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
+ patches = m(img)
+ assert patches.shape == (batch_size, 16, 1, 2, 2)
+ for i in range(batch_size):
+ self.assert_close(img[i, :, 0:2, 0:2], patches[i, 5])
+ self.assert_close(img[i, :, 0:2, 1:3], patches[i, 6])
+ self.assert_close(img[i, :, 1:3, 0:2], patches[i, 9])
+ self.assert_close(img[i, :, 1:3, 1:3], patches[i, 10])
+
+ def test_b1_ch1_h3w3_ws23(self, device):
+ img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
+ m = kornia.contrib.ExtractTensorPatches((2, 3))
+ patches = m(img)
+ assert patches.shape == (1, 2, 1, 2, 3)
+ self.assert_close(img[0, :, 0:2, 0:3], patches[0, 0])
+ self.assert_close(img[0, :, 1:3, 0:3], patches[0, 1])
+
+ def test_b1_ch1_h3w4_ws23(self, device):
+ img = torch.arange(12.0).view(1, 1, 3, 4).to(device)
+ m = kornia.contrib.ExtractTensorPatches((2, 3))
+ patches = m(img)
+ assert patches.shape == (1, 4, 1, 2, 3)
+ self.assert_close(img[0, :, 0:2, 0:3], patches[0, 0])
+ self.assert_close(img[0, :, 0:2, 1:4], patches[0, 1])
+ self.assert_close(img[0, :, 1:3, 0:3], patches[0, 2])
+ self.assert_close(img[0, :, 1:3, 1:4], patches[0, 3])
+
+ @pytest.mark.skip(reason="turn off all jit for a while")
+ def test_jit(self, device):
+ @torch.jit.script
+ def op_script(img: torch.Tensor, height: int, width: int) -> torch.Tensor:
+ return kornia.geometry.denormalize_pixel_coordinates(img, height, width)
+
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=True).to(device)
+
+ actual = op_script(grid, height, width)
+ expected = kornia.denormalize_pixel_coordinates(grid, height, width)
+
+ self.assert_close(actual, expected)
+
+ def test_gradcheck(self, device):
+ img = torch.rand(2, 3, 4, 4, device=device, dtype=torch.float64)
+ self.gradcheck(kornia.contrib.extract_tensor_patches, (img, 3))
+
+ def test_auto_padding_stride(self, device, dtype):
+ img_shape = (11, 14)
+ window_size = (3, 3)
+ stride = 2
+ rnge = img_shape[0] * img_shape[1]
+ img = torch.arange(rnge, device=device, dtype=dtype).view(1, 1, *img_shape)
+ patches = kornia.contrib.extract_tensor_patches(
+ img, window_size=window_size, stride=stride, allow_auto_padding=True
+ )
+ # 5 patches vertical, 6 2/3 = 7 horizontal = 35 patches
+ assert patches.shape == (1, 35, 1, *window_size)
diff --git a/tests/contrib/test_face_detection.py b/tests/contrib/test_face_detection.py
new file mode 100644
index 000000000..9c014b0b7
--- /dev/null
+++ b/tests/contrib/test_face_detection.py
@@ -0,0 +1,63 @@
+import pytest
+import torch
+
+import kornia
+from kornia.contrib.face_detection import FaceKeypoint
+
+from testing.base import BaseTester
+
+
+class TestFaceDetection(BaseTester):
+ @pytest.mark.slow
+ def test_smoke(self, device, dtype):
+ assert kornia.contrib.FaceDetector().to(device, dtype) is not None
+
+ @pytest.mark.slow
+ @pytest.mark.parametrize("batch_size", [1, 2, 4])
+ def test_valid(self, batch_size, device, dtype):
+ torch.manual_seed(44)
+ img = torch.rand(batch_size, 3, 320, 320, device=device, dtype=dtype)
+ face_detection = kornia.contrib.FaceDetector().to(device, dtype)
+ dets = face_detection(img)
+ assert isinstance(dets, list)
+ assert len(dets) == batch_size # same as the number of images
+ assert isinstance(dets[0], torch.Tensor)
+ assert dets[0].shape[0] >= 0 # number of detections
+ assert dets[0].shape[1] == 15 # dims of each detection
+
+ @pytest.mark.slow
+ def test_jit(self, device, dtype):
+ op = kornia.contrib.FaceDetector().to(device, dtype)
+ op_jit = torch.jit.script(op)
+ assert op_jit is not None
+
+ @pytest.mark.slow
+ def test_results(self, device, dtype):
+ data = torch.tensor(
+ [0.0, 0.0, 100.0, 200.0, 10.0, 10.0, 20.0, 10.0, 10.0, 50.0, 100.0, 50.0, 150.0, 10.0, 0.99],
+ device=device,
+ dtype=dtype,
+ )
+ res = kornia.contrib.FaceDetectorResult(data)
+ assert res.xmin == 0.0
+ assert res.ymin == 0.0
+ assert res.xmax == 100.0
+ assert res.ymax == 200.0
+ assert res.score == 0.99
+ assert res.width == 100.0
+ assert res.height == 200.0
+ assert res.top_left.tolist() == [0.0, 0.0]
+ assert res.top_right.tolist() == [100.0, 0.0]
+ assert res.bottom_right.tolist() == [100.0, 200.0]
+ assert res.bottom_left.tolist() == [0.0, 200.0]
+ assert res.get_keypoint(FaceKeypoint.EYE_LEFT).tolist() == [10.0, 10.0]
+ assert res.get_keypoint(FaceKeypoint.EYE_RIGHT).tolist() == [20.0, 10.0]
+ assert res.get_keypoint(FaceKeypoint.NOSE).tolist() == [10.0, 50.0]
+ assert res.get_keypoint(FaceKeypoint.MOUTH_LEFT).tolist() == [100.0, 50.0]
+ assert res.get_keypoint(FaceKeypoint.MOUTH_RIGHT).tolist() == [150.0, 10.0]
+
+ @pytest.mark.slow
+ def test_results_raise(self, device, dtype):
+ data = torch.zeros(14, device=device, dtype=dtype)
+ with pytest.raises(ValueError):
+ _ = kornia.contrib.FaceDetectorResult(data)
diff --git a/tests/contrib/test_hist_match.py b/tests/contrib/test_hist_match.py
new file mode 100644
index 000000000..11b3424c1
--- /dev/null
+++ b/tests/contrib/test_hist_match.py
@@ -0,0 +1,44 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestHistMatch(BaseTester):
+ def test_interp(self, device, dtype):
+ xp = torch.tensor([1, 2, 3], device=device, dtype=dtype)
+ fp = torch.tensor([4, 2, 0], device=device, dtype=dtype)
+ x = torch.tensor([4, 5, 6], device=device, dtype=dtype)
+ x_hat_expected = torch.tensor([-2.0, -4.0, -6.0], device=device, dtype=dtype)
+ x_hat = kornia.contrib.interp(x, xp, fp)
+ self.assert_close(x_hat_expected, x_hat)
+
+ def test_histmatch(self, device, dtype):
+ torch.manual_seed(44)
+ # generate random value by CPU.
+ src = torch.randn(1, 4, 4).to(device=device, dtype=dtype)
+ dst = torch.randn(1, 16, 16).to(device=device, dtype=dtype)
+ out = kornia.contrib.histogram_matching(src, dst)
+ exp = torch.tensor(
+ [
+ [
+ [1.5902, 0.9295, 2.9409, 0.1211],
+ [0.2472, 1.2137, -0.1098, -0.4272],
+ [-0.2644, -1.1983, -0.6065, -0.8091],
+ [-1.4999, 0.6370, -0.9800, 0.4474],
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ assert exp.shape == out.shape
+ self.assert_close(out, exp, rtol=1e-4, atol=1e-4)
+
+ @pytest.mark.skip(reason="not differentiable now.")
+ def test_grad(self, device):
+ B, C, H, W = 1, 3, 32, 32
+ src = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ dst = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ self.gradcheck(kornia.contrib.histogram_matching, (src, dst))
diff --git a/tests/contrib/test_image_stitcher.py b/tests/contrib/test_image_stitcher.py
new file mode 100644
index 000000000..665bd6781
--- /dev/null
+++ b/tests/contrib/test_image_stitcher.py
@@ -0,0 +1,132 @@
+from unittest.mock import PropertyMock, patch
+
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestImageStitcher(BaseTester):
+ @pytest.mark.parametrize("estimator", ["ransac", "vanilla"])
+ def test_smoke(self, estimator, device, dtype):
+ B, C, H, W = 1, 3, 6, 6
+ sample1 = torch.tensor(
+ [
+ [0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251],
+ [0.2071, 0.6297, 0.3653, 0.8513, 0.8549, 0.5509],
+ [0.2868, 0.2063, 0.4451, 0.3593, 0.7204, 0.0731],
+ [0.9699, 0.1078, 0.8829, 0.4132, 0.7572, 0.6948],
+ [0.5209, 0.5932, 0.8797, 0.6286, 0.7653, 0.1132],
+ [0.8559, 0.6721, 0.6267, 0.5691, 0.7437, 0.9592],
+ ],
+ dtype=dtype,
+ device=device,
+ )
+ sample2 = torch.tensor(
+ [
+ [0.3887, 0.2214, 0.3742, 0.1953, 0.7405, 0.2529],
+ [0.2332, 0.9314, 0.9575, 0.5575, 0.4134, 0.4355],
+ [0.7369, 0.0331, 0.0914, 0.8994, 0.9936, 0.4703],
+ [0.1049, 0.5137, 0.2674, 0.4990, 0.7447, 0.7213],
+ [0.4414, 0.5550, 0.6361, 0.1081, 0.3305, 0.5196],
+ [0.2147, 0.2816, 0.6679, 0.7878, 0.5070, 0.3055],
+ ],
+ dtype=dtype,
+ device=device,
+ )
+ sample1 = sample1.expand((B, C, H, W))
+ sample2 = sample2.expand((B, C, H, W))
+ return_value = {
+ "keypoints0": torch.tensor(
+ [
+ [0.1546, 0.9391],
+ [0.8077, 0.1051],
+ [0.6768, 0.5596],
+ [0.5092, 0.7195],
+ [0.2856, 0.8889],
+ [0.4342, 0.0203],
+ [0.6701, 0.0585],
+ [0.3828, 0.9038],
+ [0.7301, 0.0762],
+ [0.7864, 0.4490],
+ [0.3509, 0.0756],
+ [0.6782, 0.9297],
+ [0.4132, 0.3664],
+ [0.3134, 0.5039],
+ [0.2073, 0.2552],
+ ],
+ device=device,
+ dtype=dtype,
+ ),
+ "keypoints1": torch.tensor(
+ [
+ [0.2076, 0.2669],
+ [0.9679, 0.8137],
+ [0.9536, 0.8317],
+ [0.3718, 0.2456],
+ [0.3875, 0.8450],
+ [0.7592, 0.1687],
+ [0.5173, 0.6760],
+ [0.9446, 0.4570],
+ [0.6164, 0.1867],
+ [0.4732, 0.1786],
+ [0.4090, 0.8089],
+ [0.9742, 0.8943],
+ [0.5996, 0.7427],
+ [0.7038, 0.9210],
+ [0.6272, 0.0796],
+ ],
+ device=device,
+ dtype=dtype,
+ ),
+ "confidence": torch.tensor(
+ [
+ 0.9314,
+ 0.5951,
+ 0.4187,
+ 0.0318,
+ 0.1434,
+ 0.7952,
+ 0.8306,
+ 0.7511,
+ 0.6407,
+ 0.7379,
+ 0.4363,
+ 0.9220,
+ 0.8453,
+ 0.5075,
+ 0.8141,
+ ],
+ device=device,
+ dtype=dtype,
+ ),
+ "batch_indexes": torch.zeros((15,), device=device, dtype=dtype),
+ }
+ with patch(
+ "kornia.contrib.ImageStitcher.on_matcher", new_callable=PropertyMock, return_value=lambda x: return_value
+ ):
+ # NOTE: This will need to download the pretrained weights.
+ # To avoid that, we mock as below
+ matcher = kornia.feature.LoFTR(None)
+ stitcher = kornia.contrib.ImageStitcher(matcher, estimator=estimator).to(device=device, dtype=dtype)
+ torch.manual_seed(1) # issue kornia#2027
+ out = stitcher(sample1, sample2)
+ assert out.shape[:-1] == torch.Size([1, 3, 6])
+ assert out.shape[-1] <= 12
+
+ @pytest.mark.slow
+ def test_exception(self, device, dtype):
+ B, C, H, W = 1, 3, 224, 224
+ sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ sample2 = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ # NOTE: This will need to download the pretrained weights.
+ matcher = kornia.feature.LoFTR(None)
+
+ with pytest.raises(NotImplementedError):
+ stitcher = kornia.contrib.ImageStitcher(matcher, estimator="random").to(device=device, dtype=dtype)
+
+ stitcher = kornia.contrib.ImageStitcher(matcher).to(device=device, dtype=dtype)
+ with pytest.raises(RuntimeError):
+ stitcher(sample1, sample2)
diff --git a/tests/contrib/test_lambda_module.py b/tests/contrib/test_lambda_module.py
new file mode 100644
index 000000000..3f103b0c9
--- /dev/null
+++ b/tests/contrib/test_lambda_module.py
@@ -0,0 +1,46 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestLambdaModule(BaseTester):
+ def add_2_layer(self, tensor):
+ return tensor + 2
+
+ def add_x_mul_y(self, tensor, x, y=2):
+ return torch.mul(tensor + x, y)
+
+ def test_smoke(self, device, dtype):
+ B, C, H, W = 1, 3, 4, 5
+ img = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ func = self.add_2_layer
+ if not callable(func):
+ raise TypeError(f"Argument lambd should be callable, got {type(func).__name__!r}")
+ assert isinstance(kornia.contrib.Lambda(func)(img), torch.Tensor)
+
+ @pytest.mark.parametrize("x", [3, 2, 5])
+ def test_lambda_with_arguments(self, x, device, dtype):
+ B, C, H, W = 2, 3, 5, 7
+ img = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ func = self.add_x_mul_y
+ lambda_module = kornia.contrib.Lambda(func)
+ out = lambda_module(img, x)
+ assert isinstance(out, torch.Tensor)
+
+ @pytest.mark.parametrize("shape", [(1, 3, 2, 3), (2, 3, 5, 7)])
+ def test_lambda(self, shape, device, dtype):
+ B, C, H, W = shape
+ img = torch.rand(B, C, H, W, device=device, dtype=dtype)
+ func = kornia.color.bgr_to_grayscale
+ lambda_module = kornia.contrib.Lambda(func)
+ out = lambda_module(img)
+ assert isinstance(out, torch.Tensor)
+
+ def test_gradcheck(self, device):
+ B, C, H, W = 1, 3, 4, 5
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ func = kornia.color.bgr_to_grayscale
+ self.gradcheck(kornia.contrib.Lambda(func), (img,))
diff --git a/tests/contrib/test_mobile_vit.py b/tests/contrib/test_mobile_vit.py
new file mode 100644
index 000000000..6f4a23235
--- /dev/null
+++ b/tests/contrib/test_mobile_vit.py
@@ -0,0 +1,23 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestMobileViT(BaseTester):
+ @pytest.mark.parametrize("B", [1, 2])
+ @pytest.mark.parametrize("image_size", [(256, 256)])
+ @pytest.mark.parametrize("mode", ["xxs", "xs", "s"])
+ @pytest.mark.parametrize("patch_size", [(2, 2)])
+ def test_smoke(self, device, dtype, B, image_size, mode, patch_size):
+ ih, iw = image_size
+ channel = {"xxs": 320, "xs": 384, "s": 640}
+
+ img = torch.rand(B, 3, ih, iw, device=device, dtype=dtype)
+ mvit = kornia.contrib.MobileViT(mode=mode, patch_size=patch_size).to(device, dtype)
+
+ out = mvit(img)
+ assert isinstance(out, torch.Tensor)
+ assert out.shape == (B, channel[mode], 8, 8)
diff --git a/tests/contrib/test_object_detector.py b/tests/contrib/test_object_detector.py
new file mode 100644
index 000000000..dd5c07aac
--- /dev/null
+++ b/tests/contrib/test_object_detector.py
@@ -0,0 +1,82 @@
+from pathlib import Path
+
+import pytest
+import torch
+
+import kornia
+from kornia.contrib.models.rt_detr import RTDETR, DETRPostProcessor, RTDETRConfig
+from kornia.utils._compat import torch_version_lt
+
+from testing.base import BaseTester
+
+
+class TestObjectDetector(BaseTester):
+ def test_smoke(self, device, dtype):
+ batch_size = 3
+ confidence = 0.3
+ config = RTDETRConfig("resnet50d", 10, head_num_queries=10)
+ model = RTDETR.from_config(config).to(device, dtype).eval()
+ pre_processor = kornia.contrib.object_detection.ResizePreProcessor((32, 32))
+ post_processor = DETRPostProcessor(confidence).to(device, dtype).eval()
+ detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)
+
+ sizes = torch.randint(5, 10, (batch_size, 2)) * 32
+ imgs = [torch.randn(3, h, w, device=device, dtype=dtype) for h, w in sizes]
+ pre_processor_out = pre_processor(imgs)
+ detections = detector(imgs)
+
+ assert pre_processor_out[0].shape[-1] == 32
+ assert pre_processor_out[0].shape[-2] == 32
+ assert len(detections) == batch_size
+ for dets in detections:
+ assert dets.shape[1] == 6
+ assert torch.all(dets[:, 0].int() == dets[:, 0])
+ assert torch.all(dets[:, 1] >= 0.3)
+
+ @pytest.mark.slow
+ @pytest.mark.skipif(torch_version_lt(2, 0, 0), reason="Unsupported ONNX opset version: 16")
+ @pytest.mark.parametrize("variant", ("resnet50d", "hgnetv2_l"))
+ def test_onnx(self, device, dtype, tmp_path: Path, variant: str):
+ config = RTDETRConfig(variant, 1)
+ model = RTDETR.from_config(config).to(device=device, dtype=dtype).eval()
+ pre_processor = kornia.contrib.object_detection.ResizePreProcessor(640)
+ post_processor = DETRPostProcessor(0.3)
+ detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)
+
+ data = torch.rand(3, 400, 640, device=device, dtype=dtype)
+
+ model_path = tmp_path / "rtdetr.onnx"
+
+ dynamic_axes = {"images": {0: "N"}}
+ torch.onnx.export(
+ detector,
+ [data],
+ model_path,
+ input_names=["images"],
+ output_names=["detections"],
+ dynamic_axes=dynamic_axes,
+ opset_version=16,
+ )
+
+ assert model_path.is_file()
+
+ def test_results_from_detections(self, device, dtype):
+ # label_id, confidence, data
+ detections = torch.tensor(
+ [
+ [0, 0.9, 0.0, 0.0, 1.0, 1.0],
+ [1, 0.8, 0.0, 0.0, 1.0, 1.0],
+ [2, 0.7, 0.0, 0.0, 1.0, 1.0],
+ [3, 0.6, 0.0, 0.0, 1.0, 1.0],
+ [4, 0.5, 0.0, 0.0, 1.0, 1.0],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ detector_results: list = kornia.contrib.object_detection.results_from_detections(detections, format="xywh")
+
+ assert len(detector_results) == 5
+ for j, det in enumerate(detector_results):
+ for i in range(4):
+ assert det.bbox.data[i] == float(detections[j, i + 2])
diff --git a/tests/contrib/test_vision_transformer.py b/tests/contrib/test_vision_transformer.py
new file mode 100644
index 000000000..ca2c3c1b5
--- /dev/null
+++ b/tests/contrib/test_vision_transformer.py
@@ -0,0 +1,45 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestVisionTransformer(BaseTester):
+ @pytest.mark.parametrize("B", [1, 2])
+ @pytest.mark.parametrize("H", [1, 3, 8])
+ @pytest.mark.parametrize("D", [240, 768])
+ @pytest.mark.parametrize("image_size", [32, 224])
+ def test_smoke(self, device, dtype, B, H, D, image_size):
+ patch_size = 16
+ T = image_size**2 // patch_size**2 + 1 # tokens size
+
+ img = torch.rand(B, 3, image_size, image_size, device=device, dtype=dtype)
+ vit = kornia.contrib.VisionTransformer(image_size=image_size, num_heads=H, embed_dim=D).to(device, dtype)
+
+ out = vit(img)
+ assert isinstance(out, torch.Tensor)
+ assert out.shape == (B, T, D)
+
+ feats = vit.encoder_results
+ assert isinstance(feats, list)
+ assert len(feats) == 12
+ for f in feats:
+ assert f.shape == (B, T, D)
+
+ @pytest.mark.parametrize("H", [3, 8])
+ @pytest.mark.parametrize("D", [245, 1001])
+ @pytest.mark.parametrize("image_size", [32, 224])
+ def test_exception(self, device, dtype, H, D, image_size):
+ with pytest.raises(ValueError):
+ kornia.contrib.VisionTransformer(image_size=image_size, num_heads=H, embed_dim=D).to(device, dtype)
+
+ def test_backbone(self, device, dtype):
+ def backbone_mock(x):
+ return torch.ones(1, 128, 14, 14, device=device, dtype=dtype)
+
+ img = torch.rand(1, 3, 32, 32, device=device, dtype=dtype)
+ vit = kornia.contrib.VisionTransformer(backbone=backbone_mock, num_heads=8).to(device, dtype)
+ out = vit(img)
+ assert out.shape == (1, 197, 128)