🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): accept empty batches in spatial_softmax2d and spatial_expectation2d (#5739)
Rızgar Ozan919ec055dfa1
diff --git a/changelog.d/5739.fixed.md b/changelog.d/5739.fixed.mdnew file mode 100644index 000000000..54ab4b91b--- /dev/null+++ b/changelog.d/5739.fixed.md@@ -0,0 +1,2 @@+`spatial_softmax2d`, `spatial_expectation2d`, `spatial_soft_argmax2d` and `SpatialSoftArgmax2d` now return an+empty result for an empty batch or channel axis instead of raising a reshape error. (#5739)diff --git a/kornia/geometry/subpix/dsnt.py b/kornia/geometry/subpix/dsnt.pyindex ada0faa44..4aaf0c022 100644--- a/kornia/geometry/subpix/dsnt.py+++ b/kornia/geometry/subpix/dsnt.py@@ -84,7 +84,7 @@ def spatial_softmax2d(input: torch.Tensor, temperature: Optional[torch.Tensor |_check_positive_temperature(temperature)if isinstance(temperature, torch.Tensor):temperature = temperature.to(device=input.device, dtype=input.dtype)- x = input.reshape(batch_size, channels, -1)+ x = input.reshape(batch_size, channels, height * width)x_soft = F.softmax(x / temperature, dim=-1)@@ -129,7 +129,7 @@ def spatial_expectation2d(input: torch.Tensor, normalized_coordinates: bool = Trpos_x = grid[..., 0].reshape(-1)pos_y = grid[..., 1].reshape(-1)- input_flat = input.reshape(batch_size, channels, -1)+ input_flat = input.reshape(batch_size, channels, height * width)# Compute the expectation of the coordinates.expected_y = torch.sum(pos_y * input_flat, -1, keepdim=True)diff --git a/tests/geometry/subpix/test_dsnt.py b/tests/geometry/subpix/test_dsnt.pyindex 8032f7aeb..c56869009 100644--- a/tests/geometry/subpix/test_dsnt.py+++ b/tests/geometry/subpix/test_dsnt.py@@ -167,6 +167,13 @@ class TestSpatialSoftmax2d(BaseTester):self.assert_close(actual, expected)+ @pytest.mark.parametrize("shape", [(0, 3, 4, 5), (2, 0, 4, 5)])+ def test_empty_batch_or_channels(self, device, dtype, shape):+ input = torch.rand(shape, device=device, dtype=dtype)+ actual = kornia.geometry.subpix.spatial_softmax2d(input)+ assert actual.shape == shape+ assert actual.dtype == dtype+def test_dynamo(self, input, torch_optimizer):op = kornia.geometry.subpix.spatial_softmax2dop_optimized = torch_optimizer(op)@@ -232,6 +239,14 @@ class TestSpatialExpectation2d(BaseTester):self.assert_close(actual, expected)+ @pytest.mark.parametrize("normalized_coordinates", [True, False])+ @pytest.mark.parametrize("shape", [(0, 3, 4, 5), (2, 0, 4, 5)])+ def test_empty_batch_or_channels(self, device, dtype, shape, normalized_coordinates):+ input = torch.rand(shape, device=device, dtype=dtype)+ actual = kornia.geometry.subpix.spatial_expectation2d(input, normalized_coordinates)+ assert actual.shape == (*shape[:2], 2)+ assert actual.dtype == dtype+def test_float64_normalized_grid_is_exact_5019(self, device):if device.type == "mps":pytest.skip("MPS does not support float64")diff --git a/tests/geometry/subpix/test_spatial_softargmax.py b/tests/geometry/subpix/test_spatial_softargmax.pyindex 04ec44349..9eaf320ac 100644--- a/tests/geometry/subpix/test_spatial_softargmax.py+++ b/tests/geometry/subpix/test_spatial_softargmax.py@@ -165,6 +165,13 @@ class TestSpatialSoftArgmax2d(BaseTester):m = kornia.geometry.subpix.SpatialSoftArgmax2d()assert m(sample).shape == (2, 1, 2)+ @pytest.mark.parametrize("shape", [(0, 3, 4, 5), (2, 0, 4, 5)])+ def test_empty_batch_or_channels(self, device, dtype, shape):+ sample = torch.rand(shape, device=device, dtype=dtype)+ m = kornia.geometry.subpix.SpatialSoftArgmax2d()+ assert m(sample).shape == (*shape[:2], 2)+ assert kornia.geometry.subpix.spatial_soft_argmax2d(sample).shape == (*shape[:2], 2)+def test_top_left_normalized(self, device, dtype):sample = torch.zeros(1, 1, 2, 3, device=device, dtype=dtype)sample[..., 0, 0] = 1e16