Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix dedode forward with description padding removed. (#3153)

hit2sjtua63f4f0f44da
diff --git a/kornia/feature/dedode/dedode.py b/kornia/feature/dedode/dedode.py
index 9579a955c..5261a4edd 100644
--- a/kornia/feature/dedode/dedode.py
+++ b/kornia/feature/dedode/dedode.py
@@ -111,7 +111,7 @@ class DeDoDe(Module):
pd_w = 14 - w % 14 if w % 14 > 0 else 0
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)
+ descriptions = self.describe(images, keypoints, apply_imagenet_normalization=False, crop_h=h, crop_w=w)
return dedode_denormalize_pixel_coordinates(keypoints, H, W), scores, descriptions
@torch.inference_mode()
@@ -162,7 +162,12 @@ class DeDoDe(Module):
@torch.inference_mode()
def describe(
- self, images: Tensor, keypoints: Optional[Tensor] = None, apply_imagenet_normalization: bool = True
+ self,
+ images: Tensor,
+ keypoints: Optional[Tensor] = None,
+ apply_imagenet_normalization: bool = True,
+ crop_h: Optional[int] = None,
+ crop_w: Optional[int] = None,
) -> Tensor:
"""Describe keypoints in the input images. If keypoints are not provided, returns the dense descriptors.
@@ -170,6 +175,8 @@ class DeDoDe(Module):
images: A tensor of shape :math:`(B, 3, H, W)` containing the input images.
keypoints: An optional tensor of shape :math:`(B, N, 2)` containing the detected keypoints.
apply_imagenet_normalization: Whether to apply ImageNet normalization to the input images.
+ crop_h: The height of the crop to be used for description. If None, the full image is used.
+ crop_w: The width of the crop to be used for description. If None, the full image is used.
Returns:
descriptions: A tensor of shape :math:`(B, N, DIM)` containing the descriptions of the detected keypoints.
@@ -184,6 +191,10 @@ class DeDoDe(Module):
images = self.normalizer(images)
self.train(False)
descriptions = self.descriptor.forward(images)
+ if crop_h is not None and crop_w is not None:
+ descriptions = descriptions[..., :crop_h, :crop_w]
+ H, W = crop_h, crop_w
+
if keypoints is not None:
described_keypoints = F.grid_sample(
descriptions.float(), keypoints[:, None], mode="bilinear", align_corners=False