Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix: DeDoDe tests coverage (#2850)

João Gustavo A. Amorim00cfbfdd7c91
diff --git a/kornia/feature/dedode/dedode.py b/kornia/feature/dedode/dedode.py
index be6e76831..d71edba25 100644
--- a/kornia/feature/dedode/dedode.py
+++ b/kornia/feature/dedode/dedode.py
@@ -1,4 +1,4 @@
-from typing import Dict, Optional, Tuple
+from typing import Dict, Literal, Optional, Tuple
import torch
import torch.nn.functional as F
@@ -34,6 +34,12 @@ class DeDoDe(Module):
See :cite:`edstedt2024dedode` for details.
.. note:: DeDode takes ImageNet normalized images as input (not in range [0, 1]).
+
+ Args:
+ detector_model: The detector model kind. Available options are: `L`.
+ descriptor_model: The descriptor model kind. Available options are: `G` or `B`
+ amp_dtype: The automatic mixed precision desired.
+
Example:
>>> dedode = DeDoDe.from_pretrained(detector_weights="L-upright", descriptor_weights="B-upright")
>>> images = torch.randn(1, 3, 256, 256)
@@ -44,7 +50,10 @@ class DeDoDe(Module):
# TODO: implement steerers and mnn matchers
def __init__(
- self, detector_model: str = "L", descriptor_model: str = "G", amp_dtype: torch.dtype = torch.float16
+ self,
+ detector_model: Literal["L"] = "L",
+ descriptor_model: Literal["G", "B"] = "G",
+ amp_dtype: torch.dtype = torch.float16,
) -> None:
super().__init__()
self.detector: DeDoDeDetector = get_detector(detector_model, amp_dtype)
diff --git a/tests/feature/test_dedode.py b/tests/feature/test_dedode.py
index 7befb8d10..f27f9042f 100644
--- a/tests/feature/test_dedode.py
+++ b/tests/feature/test_dedode.py
@@ -1,5 +1,3 @@
-import sys
-
import pytest
import torch
@@ -7,11 +5,34 @@ from kornia.feature.dedode import DeDoDe
from kornia.utils._compat import torch_version_le
[email protected](torch_version_le(2, 0, 0), reason="Autocast not supported")
class TestDeDoDe:
- @pytest.mark.skipif(torch_version_le(2, 0, 0), reason="Autocast not supported")
- def test_smoke(self, dtype, device):
- # only testing "B" as dinov2 is quite heavy
- dedode = DeDoDe(descriptor_model="B").to(device, dtype)
+ @pytest.mark.slow
+ @pytest.mark.parametrize("descriptor_model", ["B", "G"])
+ @pytest.mark.parametrize("detector_model", ["L"])
+ def test_smoke(self, dtype, device, descriptor_model, detector_model):
+ if "G" in descriptor_model and device.type != "cuda" and dtype == torch.float16:
+ pytest.skip('G descriptors do not support no cuda device. "LayerNormKernelImpl" not implemented for `Half`')
+ dedode = DeDoDe(descriptor_model=descriptor_model, detector_model=detector_model, amp_dtype=dtype).to(
+ device, dtype
+ )
+ shape = (2, 3, 128, 128)
+ n = 1000
+ inp = torch.randn(*shape, device=device, dtype=dtype)
+ keypoints, scores, descriptions = dedode(inp, n=n)
+ assert keypoints.shape == (shape[0], n, 2)
+ assert scores.shape == (shape[0], n)
+ assert descriptions.shape == (shape[0], n, 256)
+
+ @pytest.mark.slow
+ @pytest.mark.parametrize("descriptor_model", ["B", "G"])
+ @pytest.mark.parametrize("detector_model", ["L"])
+ def test_smoke_amp_fp16(self, dtype, device, descriptor_model, detector_model):
+ if "G" in descriptor_model and device.type != "cuda":
+ pytest.skip('G descriptors do not support no cuda device. "LayerNormKernelImpl" not implemented for `Half`')
+ dedode = DeDoDe(descriptor_model=descriptor_model, detector_model=detector_model, amp_dtype=torch.float16).to(
+ device, dtype
+ )
shape = (2, 3, 128, 128)
n = 1000
inp = torch.randn(*shape, device=device, dtype=dtype)
@@ -21,9 +42,23 @@ class TestDeDoDe:
assert descriptions.shape == (shape[0], n, 256)
@pytest.mark.slow
- @pytest.mark.skipif(sys.platform == "win32", reason="this test takes so much memory in the CI with Windows")
- def load_weights(self, dtype, device):
- # only testing "B" as dinov2 is quite heavy
- dedode = DeDoDe(descriptor_model="B").to(device, dtype)
- dedode = DeDoDe(descriptor_model="G").to(device, dtype)
- print(dedode)
+ @pytest.mark.parametrize("detector_weights", ["L-upright", "L-C4", "L-SO2"])
+ @pytest.mark.parametrize("descriptor_weights", ["B-upright", "B-C4", "B-SO2", "G-upright", "G-C4"])
+ def test_pretrained(self, dtype, device, descriptor_weights, detector_weights):
+ if "G" in descriptor_weights and device.type != "cuda" and dtype == torch.float16:
+ pytest.skip('G descriptors do not support no cuda device. "LayerNormKernelImpl" not implemented for `Half`')
+ dedode = DeDoDe.from_pretrained(
+ detector_weights=detector_weights,
+ descriptor_weights=descriptor_weights,
+ amp_dtype=dtype,
+ ).to(device, dtype)
+ assert isinstance(dedode, DeDoDe)
+
+ shape = (2, 3, 128, 128)
+ n = 1000
+ inp = torch.randn(*shape, device=device, dtype=dtype)
+ keypoints, scores, descriptions = dedode(inp, n=n)
+
+ assert keypoints.shape == (shape[0], n, 2)
+ assert scores.shape == (shape[0], n)
+ assert descriptions.shape == (shape[0], n, 256)