π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): validate input shapes in LAF estimators (#6531)
Muhammad Talha1b327ef6b016
diff --git a/changelog.d/6531.fixed.md b/changelog.d/6531.fixed.mdnew file mode 100644index 000000000..01fe9eafd--- /dev/null+++ b/changelog.d/6531.fixed.md@@ -0,0 +1 @@+`LAFOrienter` rejects an image with more than one channel with a `ShapeError`, as `LAFAffineShapeEstimator` does. Such an image used to fail later with a reshape `RuntimeError`, or pass without error when there was no LAF to orient. `PatchAffineShapeEstimator` rejects a patch that is not `patch_size` pixels square, as its documentation requires. A `(B, 1, 1, patch_size)` or `(B, 1, patch_size, 1)` strip used to return the circle `(1, 0, 1)` whatever its content, `patch_size=1` weighted a patch of any size uniformly, and other sizes failed later with a broadcasting `RuntimeError`.diff --git a/kornia/feature/affine_shape.py b/kornia/feature/affine_shape.pyindex 3116f4c61..1c3806e97 100644--- a/kornia/feature/affine_shape.py+++ b/kornia/feature/affine_shape.py@@ -91,13 +91,13 @@ class PatchAffineShapeEstimator(nn.Module):"""Run forward.Args:- patch: ``(B, 1, H, W)``+ patch: ``(B, 1, patch_size, patch_size)``Returns:torch.Tensor: ellipse_shape ``(B, 1, 3)``"""- KORNIA_CHECK_SHAPE(patch, ["B", "1", "H", "W"])+ KORNIA_CHECK_SHAPE(patch, ["B", "1", str(self.patch_size), str(self.patch_size)])dtype = patch.dtype# Squared weighted gradients and the degeneracy threshold can underflow in half precision.if dtype in (torch.float16, torch.bfloat16):diff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.pyindex ba966e5ff..64b4801b6 100644--- a/kornia/feature/orientation.py+++ b/kornia/feature/orientation.py@@ -341,7 +341,7 @@ class LAFOrienter(nn.Module):"""KORNIA_CHECK_LAF(laf)- KORNIA_CHECK_SHAPE(img, ["B", "C", "H", "W"])+ KORNIA_CHECK_SHAPE(img, ["B", "1", "H", "W"])if laf.size(0) != img.size(0):raise ValueError(f"Batch size of laf and img should be the same. Got {img.size(0)}, {laf.size(0)}")B, N = laf.shape[:2]diff --git a/tests/feature/test_affine_shape_estimator.py b/tests/feature/test_affine_shape_estimator.pyindex 1d49c6d6d..91fa04a13 100644--- a/tests/feature/test_affine_shape_estimator.py+++ b/tests/feature/test_affine_shape_estimator.py@@ -20,6 +20,7 @@ import mathimport pytestimport torch+from kornia.core.exceptions import ShapeErrorfrom kornia.feature.affine_shape import LAFAffineShapeEstimator, LAFAffNetShapeEstimator, PatchAffineShapeEstimatorfrom kornia.feature.laf import (get_laf_orientation,@@ -183,6 +184,26 @@ class TestPatchAffineShapeEstimator(BaseTester):expected = torch.tensor([[[0.4146, 0.0000, 1.0000]]], device=device)self.assert_close(abc, expected, atol=1e-4, rtol=1e-4)+ @pytest.mark.parametrize(+ ("patch_size", "shape"),+ [+ (19, (2, 1, 1, 19)),+ (19, (2, 1, 19, 1)),+ (19, (2, 1, 1, 1)),+ (19, (2, 1, 18, 19)),+ (19, (2, 1, 20, 20)),+ (19, (2, 3, 19, 19)),+ (32, (2, 1, 19, 19)),+ (1, (2, 1, 32, 32)),+ ],+ )+ def test_rejects_patch_that_is_not_patch_size_square_6530(self, device, dtype, patch_size, shape):+ # Without the check a strip such as (1, 19) broadcasts against the 19 x 19 weighting and gives the circle+ # (1, 0, 1) whatever its content, and patch_size=1 weights a patch of any size uniformly.+ patch = torch.rand(*shape, device=device, dtype=dtype)+ with pytest.raises(ShapeError):+ PatchAffineShapeEstimator(patch_size).to(device, dtype)(patch)+def test_gradcheck(self, device):batch_size, channels, height, width = 1, 1, 13, 13ori = PatchAffineShapeEstimator(width).to(device)diff --git a/tests/feature/test_local_features_orientation.py b/tests/feature/test_local_features_orientation.pyindex 444d77e81..0eb3312ce 100644--- a/tests/feature/test_local_features_orientation.py+++ b/tests/feature/test_local_features_orientation.py@@ -21,6 +21,7 @@ import pytestimport torchimport kornia+from kornia.core.exceptions import ShapeErrorfrom kornia.feature.orientation import LAFOrienter, OriNet, PassLAF, PatchDominantGradientOrientationfrom kornia.geometry.conversions import rad2deg@@ -397,6 +398,15 @@ class TestLAFOrienter(BaseTester):expected = torch.tensor([[[[-5.0, 0.0, 8.0], [0.0, -5.0, 8.0]]]], device=device)self.assert_close(new_laf, expected)+ @pytest.mark.parametrize("num_lafs", [0, 1])+ @pytest.mark.parametrize("channels", [2, 3])+ def test_rejects_multichannel_image_6530(self, device, dtype, channels, num_lafs):+ # Rejected up front, as in LAFAffineShapeEstimator, even when there is no LAF to orient.+ laf = torch.tensor([[[[8.0, 0.0, 60.0], [0.0, 8.0, 50.0]]]], device=device, dtype=dtype)[:, :num_lafs]+ img = torch.rand(1, channels, 120, 160, device=device, dtype=dtype)+ with pytest.raises(ShapeError, match=f"expected 1, got {channels}"):+ LAFOrienter(32).to(device, dtype)(laf, img)+def test_gradcheck(self, device):batch_size, channels, height, width = 1, 1, 21, 21patches = torch.rand(batch_size, channels, height, width, device=device, dtype=torch.float64)