Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

use utils create_meshgrid in spatial_soft_argmax2d

edgarriba5206356f80fd
diff --git a/kornia/contrib/spatial_soft_argmax2d.py b/kornia/contrib/spatial_soft_argmax2d.py
index 279460a9e..1778497d4 100644
--- a/kornia/contrib/spatial_soft_argmax2d.py
+++ b/kornia/contrib/spatial_soft_argmax2d.py
@@ -4,22 +4,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-
-# TODO: use utils.create_meshgrid and test
-def create_meshgrid(
- x: torch.Tensor,
- normalized_coordinates: Optional[bool]) -> Tuple[torch.Tensor,
- torch.Tensor]:
- assert len(x.shape) == 4, x.shape
- _, _, height, width = x.shape
- _device, _dtype = x.device, x.dtype
- if normalized_coordinates:
- xs = torch.linspace(-1.0, 1.0, width, device=_device, dtype=_dtype)
- ys = torch.linspace(-1.0, 1.0, height, device=_device, dtype=_dtype)
- else:
- xs = torch.linspace(0, width - 1, width, device=_device, dtype=_dtype)
- ys = torch.linspace(0, height - 1, height, device=_device, dtype=_dtype)
- return torch.meshgrid(ys, xs) # pos_y, pos_x
+from kornia.utils import create_meshgrid
class SpatialSoftArgmax2d(nn.Module):
@@ -68,15 +53,20 @@ class SpatialSoftArgmax2d(nn.Module):
1.0) / (exp_x.sum(dim=-1, keepdim=True) + self.eps)
# create coordinates grid
- pos_y, pos_x = create_meshgrid(input, self.normalized_coordinates)
- pos_x = pos_x.reshape(-1)
- pos_y = pos_y.reshape(-1)
+ grid: torch.Tensor = create_meshgrid(
+ height, width, self.normalized_coordinates)
+ grid = grid.to(input.device).to(input.dtype)
+
+ pos_x: torch.Tensor = grid[..., 0].reshape(-1)
+ pos_y: torch.Tensor = grid[..., 1].reshape(-1)
+
# compute the expected coordinates
expected_y: torch.Tensor = torch.sum(
(pos_y * exp_x) * exp_x_sum, dim=-1, keepdim=True)
expected_x: torch.Tensor = torch.sum(
(pos_x * exp_x) * exp_x_sum, dim=-1, keepdim=True)
+
output: torch.Tensor = torch.cat([expected_x, expected_y], dim=-1)
return output.view(batch_size, channels, 2) # BxNx2
diff --git a/test/test_contrib.py b/test/test_contrib.py
index ce7d2768c..bfd65c343 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -148,13 +148,13 @@ class TestExtractTensorPatches:
(input, 3,), raise_exception=True)
-class TestSoftArgmax2d:
- def _test_smoke(self):
+class TestSpatialSoftArgmax2d:
+ def test_smoke(self):
input = torch.zeros(1, 1, 2, 3)
m = kornia.contrib.SpatialSoftArgmax2d()
assert m(input).shape == (1, 1, 2)
- def _test_top_left(self):
+ def test_top_left(self):
input = torch.zeros(1, 1, 2, 3)
input[..., 0, 0] = 10.
@@ -162,15 +162,16 @@ class TestSoftArgmax2d:
assert pytest.approx(coord[..., 0].item(), -1.0)
assert pytest.approx(coord[..., 1].item(), -1.0)
- def _test_top_left_normalized(self):
+ def test_top_left_normalized(self):
input = torch.zeros(1, 1, 2, 3)
input[..., 0, 0] = 10.
+ #import pdb;pdb.set_trace()
coord = kornia.contrib.spatial_soft_argmax2d(input, False)
assert pytest.approx(coord[..., 0].item(), 0.0)
assert pytest.approx(coord[..., 1].item(), 0.0)
- def _test_bottom_right(self):
+ def test_bottom_right(self):
input = torch.zeros(1, 1, 2, 3)
input[..., -1, 1] = 10.
@@ -178,7 +179,7 @@ class TestSoftArgmax2d:
assert pytest.approx(coord[..., 0].item(), 1.0)
assert pytest.approx(coord[..., 1].item(), 1.0)
- def _test_bottom_right_normalized(self):
+ def test_bottom_right_normalized(self):
input = torch.zeros(1, 1, 2, 3)
input[..., -1, 1] = 10.
@@ -186,7 +187,7 @@ class TestSoftArgmax2d:
assert pytest.approx(coord[..., 0].item(), 2.0)
assert pytest.approx(coord[..., 1].item(), 1.0)
- def _test_batch2_n2(self):
+ def test_batch2_n2(self):
input = torch.zeros(2, 2, 2, 3)
input[0, 0, 0, 0] = 10. # top-left
input[0, 1, 0, -1] = 10. # top-right
@@ -207,17 +208,8 @@ class TestSoftArgmax2d:
def _test_jit(self):
pass
- def _test_gradcheck(self):
+ def test_gradcheck(self):
input = torch.rand(2, 3, 3, 2)
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.contrib.spatial_soft_argmax2d,
(input), raise_exception=True)
-
- def test_run_all(self):
- self._test_smoke()
- self._test_top_left()
- self._test_top_left_normalized()
- self._test_bottom_right()
- self._test_bottom_right_normalized()
- self._test_batch2_n2()
- self._test_gradcheck()