π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fixed conv_argmax2d/3d behavoir for even-size kernel and added test
Dmytro Mishkind4e0bf864248
diff --git a/kornia/geometry/spatial_soft_argmax.py b/kornia/geometry/spatial_soft_argmax.pyindex 25d4bc955..7ae26fae8 100644--- a/kornia/geometry/spatial_soft_argmax.py+++ b/kornia/geometry/spatial_soft_argmax.py@@ -33,8 +33,21 @@ def _get_center_kernel2d(h: int, w: int) -> torch.Tensor:conv_kernel (torch.Tensor) [2x2xhxw]'''center_kernel = torch.zeros(2, 2, h, w)- center_kernel[0, 0, h // 2, w // 2] = 1.0- center_kernel[1, 1, h // 2, w // 2] = 1.0++ # If the size is odd, we have one pixel for center, if even - 2+ if h % 2 != 0:+ h_i1 = h // 2+ h_i2 = (h // 2) + 1+ else:+ h_i1 = (h // 2) - 1+ h_i2 = (h // 2) + 1+ if w % 2 != 0:+ w_i1 = w // 2+ w_i2 = (w // 2) + 1+ else:+ w_i1 = (w // 2) - 1+ w_i2 = (w // 2) + 1+ center_kernel[(0, 1), (0, 1), h_i1: h_i2, w_i1: w_i2] = 1.0 / float(((h_i2 - h_i1) * (w_i2 - w_i1)))return center_kernel@@ -49,9 +62,27 @@ def _get_center_kernel3d(d: int, h: int, w: int) -> torch.Tensor:conv_kernel (torch.Tensor) [3x3xdxhxw]'''center_kernel = torch.zeros(3, 3, d, h, w)- center_kernel[0, 0, d // 2, h // 2, w // 2] = 1.0- center_kernel[1, 1, d // 2, h // 2, w // 2] = 1.0- center_kernel[2, 2, d // 2, h // 2, w // 2] = 1.0+ # If the size is odd, we have one pixel for center, if even - 2+ if h % 2 != 0:+ h_i1 = h // 2+ h_i2 = (h // 2) + 1+ else:+ h_i1 = (h // 2) - 1+ h_i2 = (h // 2) + 1+ if w % 2 != 0:+ w_i1 = w // 2+ w_i2 = (w // 2) + 1+ else:+ w_i1 = (w // 2) - 1+ w_i2 = (w // 2) + 1+ if d % 2 != 0:+ d_i1 = d // 2+ d_i2 = (d // 2) + 1+ else:+ d_i1 = (d // 2) - 1+ d_i2 = (d // 2) + 1+ center_num = float((h_i2 - h_i1) * (w_i2 - w_i1) * (d_i2 - d_i1))+ center_kernel[(0, 1, 2), (0, 1, 2), d_i1: d_i2, h_i1: h_i2, w_i1: w_i2] = 1.0 / center_numreturn center_kerneldiff --git a/test/geometry/test_spatial_softargmax.py b/test/geometry/test_spatial_softargmax.pyindex dafbf348d..e5d2ab8e1 100644--- a/test/geometry/test_spatial_softargmax.py+++ b/test/geometry/test_spatial_softargmax.py@@ -8,6 +8,59 @@ import torchfrom torch.nn.functional import mse_lossfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose+from kornia.geometry.spatial_soft_argmax import _get_center_kernel2d, _get_center_kernel3d+++class TestCenterKernel2d:+ def test_smoke(self):+ kernel = _get_center_kernel2d(3, 4)+ assert kernel.shape == (2, 2, 3, 4)++ def test_odd(self):+ kernel = _get_center_kernel2d(3, 3)+ expected = torch.tensor([+ [[[0., 0., 0.],+ [0., 1., 0.],+ [0., 0., 0.]],+ [[0., 0., 0.],+ [0., 0., 0.],+ [0., 0., 0.]]],+ [[[0., 0., 0.],+ [0., 0., 0.],+ [0., 0., 0.]],+ [[0., 0., 0.],+ [0., 1., 0.],+ [0., 0., 0.]]]])+ assert_allclose(kernel, expected)++ def test_even(self):+ kernel = _get_center_kernel2d(2, 2)+ expected = torch.ones(2, 2, 2, 2) * 0.25+ expected[0, 1] = 0+ expected[1, 0] = 0+ assert_allclose(kernel, expected)+++class TestCenterKernel3d:+ def test_smoke(self):+ kernel = _get_center_kernel3d(6, 3, 4)+ assert kernel.shape == (3, 3, 6, 3, 4)++ def test_odd(self):+ kernel = _get_center_kernel3d(3, 5, 7)+ expected = torch.zeros(3, 3, 3, 5, 7)+ expected[0, 0, 1, 2, 3] = 1.+ expected[1, 1, 1, 2, 3] = 1.+ expected[2, 2, 1, 2, 3] = 1.+ assert_allclose(kernel, expected)++ def test_even(self):+ kernel = _get_center_kernel3d(2, 4, 3)+ expected = torch.zeros(3, 3, 2, 4, 3)+ expected[0, 0, :, 1:3, 1] = 0.25+ expected[1, 1, :, 1:3, 1] = 0.25+ expected[2, 2, :, 1:3, 1] = 0.25+ assert_allclose(kernel, expected)class TestSpatialSoftArgmax2d: