Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix mask bug for loftr (#1580)

Dmytro Mishkinca061b9ed7a8
diff --git a/kornia/feature/loftr/loftr.py b/kornia/feature/loftr/loftr.py
index 4617c389d..6c190b8a8 100644
--- a/kornia/feature/loftr/loftr.py
+++ b/kornia/feature/loftr/loftr.py
@@ -3,6 +3,8 @@ from typing import Dict, Optional
import torch
import torch.nn as nn
+from kornia.geometry import resize
+
from .backbone import build_backbone
from .loftr_module import FinePreprocess, LocalFeatureTransformer
from .utils.coarse_matching import CoarseMatching
@@ -148,7 +150,9 @@ class LoFTR(nn.Module):
mask_c0 = mask_c1 = None # mask is useful in training
if 'mask0' in data:
- mask_c0, mask_c1 = data['mask0'].flatten(-2), data['mask1'].flatten(-2)
+ mask_c0 = resize(data['mask0'], data['hw0_c'], interpolation='nearest').flatten(-2)
+ if 'mask1' in data:
+ mask_c1 = resize(data['mask1'], data['hw1_c'], interpolation='nearest').flatten(-2)
feat_c0, feat_c1 = self.loftr_coarse(feat_c0, feat_c1, mask_c0, mask_c1)
# 3. match coarse-level
diff --git a/test/feature/test_loftr.py b/test/feature/test_loftr.py
index 8470a16ac..2f58b8f16 100644
--- a/test/feature/test_loftr.py
+++ b/test/feature/test_loftr.py
@@ -44,6 +44,15 @@ class TestLoFTR:
assert_close(out['keypoints0'], data_dev["loftr_outdoor_tentatives0"])
assert_close(out['keypoints1'], data_dev["loftr_outdoor_tentatives1"])
+ def test_mask(self, device):
+ patches = torch.rand(1, 1, 32, 32, device=device)
+ mask = torch.rand(1, 32, 32, device=device)
+ loftr = LoFTR().to(patches.device, patches.dtype)
+ input = {"image0": patches, "image1": patches, "mask0": mask, "mask1": mask}
+ with torch.no_grad():
+ out = loftr(input)
+ assert out is not None
+
@pytest.mark.skip("Takes too long time (but works)")
def test_gradcheck(self, device):
patches = torch.rand(1, 1, 32, 32, device=device)