π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): build the spatial_expectation2d grid in the input dtype (#5027)
RitiGrover862837d4bcaf
diff --git a/changelog.d/5027.fixed.md b/changelog.d/5027.fixed.mdnew file mode 100644index 000000000..def38250a--- /dev/null+++ b/changelog.d/5027.fixed.md@@ -0,0 +1,2 @@+`spatial_expectation2d`, and so `spatial_soft_argmax2d`, now builds its normalised grid in the input dtype. float64+coordinates no longer carry float32 rounding error. float16 and bfloat16 still build it in float32. (#5027)diff --git a/kornia/geometry/subpix/dsnt.py b/kornia/geometry/subpix/dsnt.pyindex 9cb029283..d67fdf8b3 100644--- a/kornia/geometry/subpix/dsnt.py+++ b/kornia/geometry/subpix/dsnt.py@@ -118,8 +118,10 @@ def spatial_expectation2d(input: torch.Tensor, normalized_coordinates: bool = Trbatch_size, channels, height, width = input.shape- # Create coordinates grid.- grid = create_meshgrid(height, width, normalized_coordinates, input.device)+ # Create coordinates grid in the input dtype, so float64 coordinates carry no float32 rounding. float16 and+ # bfloat16 still build it in float32 and round once in the cast below, which keeps their results unchanged.+ grid_dtype = torch.float32 if input.dtype in (torch.float16, torch.bfloat16) else input.dtype+ grid = create_meshgrid(height, width, normalized_coordinates, input.device, grid_dtype)grid = grid.to(input.dtype)pos_x = grid[..., 0].reshape(-1)diff --git a/tests/geometry/subpix/test_dsnt.py b/tests/geometry/subpix/test_dsnt.pyindex 03f262eb0..d9afaa10f 100644--- a/tests/geometry/subpix/test_dsnt.py+++ b/tests/geometry/subpix/test_dsnt.py@@ -230,6 +230,34 @@ class TestSpatialExpectation2d(BaseTester):self.assert_close(actual, expected)+ def test_float64_normalized_grid_is_exact_5019(self, device):+ if device.type == "mps":+ pytest.skip("MPS does not support float64")+ # #5019: the normalised grid was built in float32 and cast afterwards, so float64 coordinates carried+ # float32 rounding error (4e-8 here). A width of 7 has spacing 1/3, which float32 cannot represent.+ heatmap = torch.zeros(1, 1, 4, 7, device=device, dtype=torch.float64)+ heatmap[0, 0, 1, 5] = 1.0+ out = kornia.geometry.subpix.spatial_expectation2d(heatmap, True)+ assert out.dtype == torch.float64+ expected = torch.tensor([[[2 / 3, -1 / 3]]], device=device, dtype=torch.float64)+ self.assert_close(out, expected, rtol=0.0, atol=1e-15)++ probs = torch.softmax(torch.randn(1, 1, 48, 64, device=device, dtype=torch.float64).flatten(-2), -1)+ probs = probs.view(1, 1, 48, 64)+ xs = torch.linspace(-1, 1, 64, device=device, dtype=torch.float64)+ ys = torch.linspace(-1, 1, 48, device=device, dtype=torch.float64)+ reference = torch.stack([(probs.sum(-2) * xs).sum(-1), (probs.sum(-1) * ys).sum(-1)], -1)+ self.assert_close(kornia.geometry.subpix.spatial_expectation2d(probs, True), reference, rtol=0.0, atol=1e-14)++ def test_bfloat16_grid_rounds_once_5019(self, device):+ # bfloat16 keeps building the grid in float32 and rounds each coordinate once. Built directly in bfloat16,+ # the pixel coordinate 2057 comes out as 2048 instead of its nearest bfloat16 value 2064.+ heatmap = torch.zeros(1, 1, 1, 3001, device=device, dtype=torch.bfloat16)+ heatmap[0, 0, 0, 2057] = 1.0+ out = kornia.geometry.subpix.spatial_expectation2d(heatmap, False)+ expected = torch.tensor([[[2064.0, 0.0]]], device=device, dtype=torch.bfloat16)+ self.assert_close(out, expected, rtol=0.0, atol=0.0)+@pytest.mark.skip("After the op be optimized the results are not the same")def test_dynamo(self, dtype, device, torch_optimizer):data = torch.tensor([[[[0.0, 0.0, 1.0], [0.0, 0.0, 0.0]]]], device=device, dtype=dtype)