π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): preserve LoFTR positional encoding dtype (#6141)
Devulapalli Naga Sri Vaishnavi2aa1c5340096
diff --git a/changelog.d/6141.fixed.md b/changelog.d/6141.fixed.mdnew file mode 100644index 000000000..5251d4750--- /dev/null+++ b/changelog.d/6141.fixed.md@@ -0,0 +1 @@+`LoFTR` in float16 or bfloat16 no longer raises a dtype-mismatch `RuntimeError` on images taller or wider than 2048 pixels: the positional-encoding cache it grows for feature maps above 256 cells now keeps the model's dtype instead of becoming float32.diff --git a/kornia/feature/loftr/utils/position_encoding.py b/kornia/feature/loftr/utils/position_encoding.pyindex a1c623782..e31240f15 100644--- a/kornia/feature/loftr/utils/position_encoding.py+++ b/kornia/feature/loftr/utils/position_encoding.py@@ -76,7 +76,7 @@ class PositionEncodingSine(nn.Module):For 1/8 feature map (which is standard): If the input image size is H, W (both divisible by 8), the max_shapeshould be (H//8, W//8)."""- self.pe = self._create_position_encoding(max_shape).to(self.pe.device)+ self.pe = self._create_position_encoding(max_shape).to(device=self.pe.device, dtype=self.pe.dtype)def forward(self, x: torch.Tensor) -> torch.Tensor:"""Run forward.diff --git a/tests/feature/test_loftr.py b/tests/feature/test_loftr.pyindex bd3f5ba50..57411f257 100644--- a/tests/feature/test_loftr.py+++ b/tests/feature/test_loftr.py@@ -29,8 +29,10 @@ from kornia.feature.loftr.backbone import ResNetFPN_8_2from kornia.feature.loftr.loftr import default_cfgfrom kornia.feature.loftr.loftr_module import FinePreprocessfrom kornia.feature.loftr.loftr_module.linear_attention import LinearAttention+from kornia.feature.loftr.loftr_module.transformer import LoFTREncoderLayerfrom kornia.feature.loftr.utils.coarse_matching import CoarseMatchingfrom kornia.feature.loftr.utils.fine_matching import FineMatching+from kornia.feature.loftr.utils.position_encoding import PositionEncodingSinefrom kornia.geometry import resize, transform_points, warp_perspectivefrom testing.base import BaseTester@@ -192,6 +194,20 @@ class TestLoFTR(BaseTester):far = out["keypoints1"][out["keypoints1"][:, 0] > 256, 0]assert (far % 2 != 0).any() # sub-2-px positions exist past 256, which bfloat16 cannot hold+ def test_position_encoding_growth_preserves_half_dtype(self, device, dtype):+ if dtype not in (torch.float16, torch.bfloat16):+ pytest.skip("half-precision rule")+ position_encoding = PositionEncodingSine(4, max_shape=(2, 2)).to(device, dtype)+ features = torch.zeros(1, 4, 3, 3, device=device, dtype=dtype)++ encoded = position_encoding(features)++ assert position_encoding.pe.dtype == dtype+ assert encoded.dtype == dtype+ layer = LoFTREncoderLayer(4, 1).to(device, dtype).eval()+ with torch.no_grad():+ layer(encoded.flatten(2).transpose(1, 2), encoded.flatten(2).transpose(1, 2))+def test_mask_is_read_at_the_coarse_cell_pixels_5728(self, device, dtype):# A coarse cell takes the mask value of the pixel it sits on, (8x, 8y). The masks are 1 only on those pixels# (image0: on the ones with y < 64 and x < 40, so coarse rows 0..7 and columns 0..4), so every such cell is