Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix off-by-one pixel denormalization bug in DeDoDe (#2941)

Georg Bökman9ef40649affa
diff --git a/kornia/feature/dedode/dedode.py b/kornia/feature/dedode/dedode.py
index 91c343bf8..c56dda8ca 100644
--- a/kornia/feature/dedode/dedode.py
+++ b/kornia/feature/dedode/dedode.py
@@ -6,11 +6,10 @@ import torch.nn.functional as F
from kornia.core import Module, Tensor
from kornia.core.check import KORNIA_CHECK_SHAPE
from kornia.enhance.normalize import Normalize
-from kornia.geometry.conversions import denormalize_pixel_coordinates
from kornia.utils.helpers import map_location_to_cpu
from .dedode_models import DeDoDeDescriptor, DeDoDeDetector, get_descriptor, get_detector
-from .utils import sample_keypoints
+from .utils import dedode_denormalize_pixel_coordinates, sample_keypoints
urls: Dict[str, Dict[str, str]] = {
"detector": {
@@ -93,7 +92,7 @@ class DeDoDe(Module):
images = torch.nn.functional.pad(images, (0, pd_w, 0, pd_h), value=0.0)
keypoints, scores = self.detect(images, n=n, apply_imagenet_normalization=False, crop_h=h, crop_w=w)
descriptions = self.describe(images, keypoints, apply_imagenet_normalization=False)
- return denormalize_pixel_coordinates(keypoints, H, W), scores, descriptions
+ return dedode_denormalize_pixel_coordinates(keypoints, H, W), scores, descriptions
@torch.inference_mode()
def detect(
diff --git a/kornia/feature/dedode/utils.py b/kornia/feature/dedode/utils.py
index 1c4da398a..00bf4b639 100644
--- a/kornia/feature/dedode/utils.py
+++ b/kornia/feature/dedode/utils.py
@@ -34,3 +34,14 @@ def get_grid(B: int, H: int, W: int, device: torch.device) -> torch.Tensor:
)
x1_n = torch.stack((x1_n_[2], x1_n_[1]), dim=-1).reshape(B, H * W, 2)
return x1_n
+
+
+def dedode_denormalize_pixel_coordinates(flow: torch.Tensor, h: int, w: int) -> torch.Tensor:
+ flow = torch.stack(
+ (
+ w * (flow[..., 0] + 1) / 2,
+ h * (flow[..., 1] + 1) / 2,
+ ),
+ dim=-1,
+ )
+ return flow