Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add keepdim param to tensor_to_image function. (#1168)

Bulat Suleymanov136941d526d8
diff --git a/kornia/utils/image.py b/kornia/utils/image.py
index 964c550c7..fc2ac67c0 100644
--- a/kornia/utils/image.py
+++ b/kornia/utils/image.py
@@ -93,7 +93,7 @@ def _to_bcdhw(tensor: torch.Tensor) -> torch.Tensor:
return tensor
-def tensor_to_image(tensor: torch.Tensor) -> "np.ndarray":
+def tensor_to_image(tensor: torch.Tensor, keepdim: bool = False) -> "np.ndarray":
"""Converts a PyTorch tensor image to a numpy image.
In case the tensor is in the GPU, it will be copied back to CPU.
@@ -101,13 +101,16 @@ def tensor_to_image(tensor: torch.Tensor) -> "np.ndarray":
Args:
tensor: image of the form :math:`(H, W)`, :math:`(C, H, W)` or
:math:`(B, C, H, W)`.
+ keepdim: If ``False`` squeeze the input image to match the shape
+ :math:`(H, W, C)` or :math:`(H, W)`.
+
Returns:
image of the form :math:`(H, W)`, :math:`(H, W, C)` or :math:`(B, H, W, C)`.
"""
if not isinstance(tensor, torch.Tensor):
- raise TypeError("Input type is not a torch.Tensor. Got {}".format(type(tensor)))
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(tensor)}")
if len(tensor.shape) > 4 or len(tensor.shape) < 2:
raise ValueError("Input size must be a two, three or four dimensional tensor")
@@ -128,12 +131,12 @@ def tensor_to_image(tensor: torch.Tensor) -> "np.ndarray":
elif len(input_shape) == 4:
# (B, C, H, W) -> (B, H, W, C)
image = image.transpose(0, 2, 3, 1)
- if input_shape[0] == 1:
+ if input_shape[0] == 1 and not keepdim:
image = image.squeeze(0)
if input_shape[1] == 1:
image = image.squeeze(-1)
else:
- raise ValueError("Cannot process tensor with shape {}".format(input_shape))
+ raise ValueError(f"Cannot process tensor with shape {input_shape}")
return image
diff --git a/test/utils/test_image.py b/test/utils/test_image.py
index 9a1d64795..dd265b5bf 100644
--- a/test/utils/test_image.py
+++ b/test/utils/test_image.py
@@ -33,6 +33,24 @@ def test_tensor_to_image(device, input_shape, expected):
assert isinstance(image, np.ndarray)
+ "input_shape, expected",
+ [
+ ((4, 4), (4, 4)),
+ ((1, 4, 4), (4, 4)),
+ ((1, 1, 4, 4), (1, 4, 4)),
+ ((3, 4, 4), (4, 4, 3)),
+ ((2, 3, 4, 4), (2, 4, 4, 3)),
+ ((1, 3, 4, 4), (1, 4, 4, 3)),
+ ],
+)
+def test_tensor_to_image_keepdim(device, input_shape, expected):
+ tensor = torch.ones(input_shape).to(device)
+ image = kornia.utils.tensor_to_image(tensor, keepdim=True)
+ assert image.shape == expected
+ assert isinstance(image, np.ndarray)
+
+
@pytest.mark.parametrize(
"input_shape, expected",
[