Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

remap accepts arbitrary grid size (#1617)

Edgar Ribabbfb65587d88
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 61dea7b38..52e8fc749 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -2,6 +2,8 @@ from typing import List, Optional, Tuple
import torch
import torch.nn.functional as F
+from torch import Tensor, stack
+from torch.nn.functional import grid_sample
from kornia.geometry.conversions import (
angle_axis_to_rotation_matrix,
@@ -14,6 +16,7 @@ from kornia.geometry.conversions import (
normalize_pixel_coordinates,
)
from kornia.geometry.linalg import transform_points
+from kornia.testing import KORNIA_CHECK_IS_TENSOR
from kornia.utils import create_meshgrid, create_meshgrid3d, eye_like
from kornia.utils.helpers import _torch_inverse_cast, _torch_solve_cast
@@ -205,11 +208,7 @@ def warp_affine(
def _fill_and_warp(
- src: torch.Tensor,
- grid: torch.Tensor,
- mode: str,
- align_corners: bool,
- fill_value: torch.Tensor,
+ src: torch.Tensor, grid: torch.Tensor, mode: str, align_corners: bool, fill_value: torch.Tensor
) -> torch.Tensor:
r"""Warp a mask of ones, then multiple with fill_value and add to default warp.
@@ -479,15 +478,15 @@ def get_rotation_matrix2d(center: torch.Tensor, angle: torch.Tensor, scale: torc
def remap(
- tensor: torch.Tensor,
- map_x: torch.Tensor,
- map_y: torch.Tensor,
+ image: Tensor,
+ map_x: Tensor,
+ map_y: Tensor,
mode: str = 'bilinear',
padding_mode: str = 'zeros',
align_corners: Optional[bool] = None,
normalized_coordinates: bool = False,
-) -> torch.Tensor:
- r"""Apply a generic geometrical transformation to a tensor.
+) -> Tensor:
+ r"""Apply a generic geometrical transformation to an image tensor.
.. image:: _static/img/remap.png
@@ -497,8 +496,8 @@ def remap(
\text{dst}(x, y) = \text{src}(map_x(x, y), map_y(x, y))
Args:
- tensor: the tensor to remap with shape (B, D, H, W).
- Where D is the number of channels.
+ image: the tensor to remap with shape (B, C, H, W).
+ Where C is the number of channels.
map_x: the flow in the x-direction in pixel coordinates.
The tensor must be in the shape of (B, H, W).
map_y: the flow in the y-direction in pixel coordinates.
@@ -509,12 +508,13 @@ def remap(
``'zeros'`` | ``'border'`` | ``'reflection'``.
align_corners: mode for grid_generation.
normalized_coordinates: whether the input coordinates are
- normalised in the range of [-1, 1].
+ normalized in the range of [-1, 1].
Returns:
the warped tensor with same shape as the input grid maps.
Example:
+ >>> import torch
>>> from kornia.utils import create_meshgrid
>>> grid = create_meshgrid(2, 2, False) # 1x2x2x2
>>> grid += 1 # apply offset in both directions
@@ -526,35 +526,25 @@ def remap(
.. note::
This function is often used in conjunction with :func:`kornia.utils.create_meshgrid`.
"""
- if not isinstance(tensor, torch.Tensor):
- raise TypeError(f"Input tensor type is not a torch.Tensor. Got {type(tensor)}")
-
- if not isinstance(map_x, torch.Tensor):
- raise TypeError(f"Input map_x type is not a torch.Tensor. Got {type(map_x)}")
-
- if not isinstance(map_y, torch.Tensor):
- raise TypeError(f"Input map_y type is not a torch.Tensor. Got {type(map_y)}")
+ KORNIA_CHECK_IS_TENSOR(image, "Expected shape BxCxHxW")
+ KORNIA_CHECK_IS_TENSOR(map_x, "Expected shape BxHxW")
+ KORNIA_CHECK_IS_TENSOR(map_y, "Expected shape BxHxW")
- if not tensor.shape[-2:] == map_x.shape[-2:] == map_y.shape[-2:]:
- raise ValueError("Inputs last two dimensions must match.")
-
- batch_size, _, height, width = tensor.shape
+ batch_size, _, height, width = image.shape
# grid_sample need the grid between -1/1
- map_xy: torch.Tensor = torch.stack([map_x, map_y], dim=-1)
+ map_xy: Tensor = stack([map_x, map_y], -1)
# normalize coordinates if not already normalized
if not normalized_coordinates:
map_xy = normalize_pixel_coordinates(map_xy, height, width)
# simulate broadcasting since grid_sample does not support it
- map_xy_norm: torch.Tensor = map_xy.expand(batch_size, -1, -1, -1)
+ map_xy = map_xy.expand(batch_size, -1, -1, -1)
- # warp ans return
- tensor_warped: torch.Tensor = F.grid_sample(
- tensor, map_xy_norm, mode=mode, padding_mode=padding_mode, align_corners=align_corners
- )
- return tensor_warped
+ # warp the image tensor and return
+ warped = grid_sample(image, map_xy, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
+ return warped
def invert_affine_transform(matrix: torch.Tensor) -> torch.Tensor:
@@ -673,15 +663,7 @@ def get_shear_matrix2d(center: torch.Tensor, sx: Optional[torch.Tensor] = None,
sy_tan = torch.tan(sy) # type: ignore
ones = torch.ones_like(sx) # type: ignore
shear_mat = torch.stack(
- [
- ones,
- -sx_tan,
- sx_tan * y, # type: ignore
- -sy_tan,
- ones + sx_tan * sy_tan,
- sy_tan * (sx_tan * y + x),
- ],
- dim=-1,
+ [ones, -sx_tan, sx_tan * y, -sy_tan, ones + sx_tan * sy_tan, sy_tan * (sx_tan * y + x)], dim=-1 # type: ignore
).view(-1, 2, 3)
shear_mat = convert_affinematrix_to_homography(shear_mat)
@@ -1288,8 +1270,9 @@ def homography_warp(
)
if normalized_homography:
height, width = dsize
- grid = create_meshgrid(height, width, normalized_coordinates=normalized_coordinates,
- device=patch_src.device, dtype=patch_src.dtype)
+ grid = create_meshgrid(
+ height, width, normalized_coordinates=normalized_coordinates, device=patch_src.device, dtype=patch_src.dtype
+ )
warped_grid = warp_grid(grid, src_homo_dst)
return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index e79807edd..a194cc15b 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -7,6 +7,7 @@ from itertools import product
from typing import Any, Optional
import torch
+from torch import Tensor
__all__ = ['tensor_to_gradcheck_var', 'create_eye_batch', 'xla_is_available', 'assert_close']
@@ -50,7 +51,7 @@ def dict_to(data: dict, device: torch.device, dtype: torch.dtype) -> dict:
def compute_patch_error(x, y, h, w):
"""Compute the absolute error between patches."""
- return torch.abs(x - y)[..., h // 4: -h // 4, w // 4: -w // 4].mean()
+ return torch.abs(x - y)[..., h // 4 : -h // 4, w // 4 : -w // 4].mean()
def check_is_tensor(obj):
@@ -179,3 +180,11 @@ except ImportError:
return _assert_allclose(actual, expected, rtol=rtol, atol=atol, **kwargs)
except ValueError as error:
raise UsageError(str(error)) from error
+
+
+# Logger api
+
+
+def KORNIA_CHECK_IS_TENSOR(x, msg: Optional[str] = None):
+ if not isinstance(x, Tensor):
+ raise TypeError(f"Not a Tensor type. Got: {type(x)}.\n{msg}")
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index df9fe5474..d24fb0315 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -413,10 +413,20 @@ class TestRemap:
def test_smoke(self, device, dtype):
height, width = 3, 4
input = torch.ones(1, 1, height, width, device=device, dtype=dtype)
- grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
- input_warped = kornia.geometry.remap(input, grid[..., 0], grid[..., 1], align_corners=True)
+ grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device, dtype=dtype)
+ input_warped = kornia.geometry.remap(
+ input, grid[..., 0], grid[..., 1], normalized_coordinates=False, align_corners=True
+ )
assert_close(input, input_warped, rtol=1e-4, atol=1e-4)
+ def test_different_size(self, device, dtype):
+ height, width = 3, 4
+ grid = kornia.utils.create_meshgrid(height, width, device=device, dtype=dtype)
+
+ img = torch.rand(1, 2, 6, 5, device=device, dtype=dtype)
+ img_warped = kornia.geometry.remap(img, grid[..., 0], grid[..., 1])
+ assert img_warped.shape == (1, 2, height, width)
+
def test_shift(self, device, dtype):
height, width = 3, 4
inp = torch.tensor(