π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(subpix): preserve empty axes in 3D soft-argmax (#5764)
A Aswanth Rajb0f0bc5f72dd
diff --git a/changelog.d/5764.fixed.md b/changelog.d/5764.fixed.mdnew file mode 100644index 000000000..571b9caea--- /dev/null+++ b/changelog.d/5764.fixed.md@@ -0,0 +1 @@+`conv_soft_argmax3d` and `ConvSoftArgmax3d` return correctly shaped empty outputs when the batch or channel axis is empty.diff --git a/kornia/geometry/subpix/spatial_soft_argmax.py b/kornia/geometry/subpix/spatial_soft_argmax.pyindex ff84ca6bb..69d6d70b1 100644--- a/kornia/geometry/subpix/spatial_soft_argmax.py+++ b/kornia/geometry/subpix/spatial_soft_argmax.py@@ -319,8 +319,9 @@ def _unfold3d(padded: torch.Tensor, kernel_size: tuple[int, int, int], stride: tkz, ky, kx = kernel_sizesz, sy, sx = stridecolumns = F.unfold(padded.reshape(n * d_p, 1, h_p, w_p), (ky, kx), stride=(sy, sx))- columns = columns.view(n, d_p, ky * kx, -1).unfold(1, kz, sz) # (N, D_out, ky * kx, L, kz)- return columns.permute(0, 1, 4, 2, 3).reshape(n, columns.shape[1], kz * ky * kx, -1)+ num_windows = columns.shape[-1]+ columns = columns.view(n, d_p, ky * kx, num_windows).unfold(1, kz, sz) # (N, D_out, ky * kx, L, kz)+ return columns.permute(0, 1, 4, 2, 3).reshape(n, columns.shape[1], kz * ky * kx, num_windows)def conv_soft_argmax2d(diff --git a/tests/geometry/subpix/test_spatial_softargmax.py b/tests/geometry/subpix/test_spatial_softargmax.pyindex 9eaf320ac..173e7dadc 100644--- a/tests/geometry/subpix/test_spatial_softargmax.py+++ b/tests/geometry/subpix/test_spatial_softargmax.py@@ -540,6 +540,42 @@ class TestConvSoftArgmax2d(BaseTester):class TestConvSoftArgmax3d(BaseTester):+ @pytest.mark.parametrize("batch_size,channels", [(0, 2), (2, 0)])+ @pytest.mark.parametrize("output_value", [True, False])+ @pytest.mark.parametrize("as_module", [True, False])+ @pytest.mark.parametrize("normalized_coordinates", [True, False])+ def test_empty_batch_or_channel(+ self, device, dtype, batch_size, channels, output_value, as_module, normalized_coordinates+ ):+ """Empty batch and channel axes preserve output shapes and gradients (gh-5754)."""+ data = torch.empty(batch_size, channels, 3, 8, 8, device=device, dtype=dtype, requires_grad=True)+ if as_module:+ result = kornia.geometry.subpix.ConvSoftArgmax3d(+ (3, 3, 5),+ (1, 2, 3),+ (1, 0, 1),+ output_value=output_value,+ normalized_coordinates=normalized_coordinates,+ )(data)+ else:+ result = kornia.geometry.subpix.conv_soft_argmax3d(+ data,+ (3, 3, 5),+ (1, 2, 3),+ (1, 0, 1),+ output_value=output_value,+ normalized_coordinates=normalized_coordinates,+ )+ outputs = result if isinstance(result, tuple) else (result,)+ assert len(outputs) == (2 if output_value else 1)+ expected_shapes = [(batch_size, channels, 3, 3, 3, 2), (batch_size, channels, 3, 3, 2)]+ for output, expected_shape in zip(outputs, expected_shapes):+ assert output.shape == expected_shape+ assert output.device == device+ assert output.dtype == dtype+ gradient = torch.autograd.grad([output.sum() for output in outputs], data)[0]+ assert gradient.shape == data.shape+def test_smoke(self, device, dtype):sample = torch.zeros(1, 1, 3, 3, 3, device=device, dtype=dtype)m = kornia.geometry.subpix.ConvSoftArgmax3d((3, 3, 3), output_value=False)