Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix tensor_to_image, image_to_tensor inputs/outputs shapes

edgarriba045740a200c1
diff --git a/test/test_utils.py b/test/test_utils.py
index 0fc3e7201..507143ef0 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -1,4 +1,5 @@
import unittest
+import pytest
import numpy as np
import torch
@@ -6,22 +7,24 @@ import torchgeometry as tgm
import utils # test utils
[email protected]("batch_shape",
+ [(4, 4), (1, 4, 4), (3, 4, 4),])
+def test_tensor_to_image(batch_shape):
+ tensor = torch.ones(batch_shape)
+ image = tgm.tensor_to_image(tensor)
+ assert image.shape[:2] == batch_shape[-2:]
+ assert isinstance(image, np.ndarray)
+
[email protected]("batch_shape",
+ [(4, 4), (4, 4, 1), (4, 4, 3),])
+def test_image_to_tensor(batch_shape):
+ image = np.ones(batch_shape)
+ tensor = tgm.image_to_tensor(image)
+ assert tensor.shape[-2:] == batch_shape[:2]
+ assert isinstance(tensor, torch.Tensor)
class Tester(unittest.TestCase):
- def test_tensor_to_image(self):
- tensor = torch.ones(3, 4, 4)
- image = tgm.tensor_to_image(tensor)
-
- self.assertTrue(image.shape == (4, 4, 3))
- self.assertIsInstance(image, np.ndarray)
-
- def test_image_to_tensor(self):
- image = np.ones((4, 4, 3))
- tensor = tgm.image_to_tensor(image)
-
- self.assertTrue(tensor.shape == (3, 4, 4))
- self.assertIsInstance(tensor, torch.Tensor)
def test_create_meshgrid(self):
height, width = 4, 6
diff --git a/torchgeometry/utils.py b/torchgeometry/utils.py
index 8acc68253..1fd522db2 100644
--- a/torchgeometry/utils.py
+++ b/torchgeometry/utils.py
@@ -50,18 +50,22 @@ def image_to_tensor(image):
image (numpy.ndarray): image of the form (H, W, C).
Returns:
- numpy.ndarray: image of the form (H, W, C).
+ torch.Tensor: tensor of the form (C, H, W).
"""
if not type(image) == np.ndarray:
raise TypeError("Input type is not a numpy.ndarray. Got {}".format(
type(image)))
+
if len(image.shape) > 3 or len(image.shape) < 2:
raise ValueError("Input size must be a two or three dimensional array")
+
tensor = torch.from_numpy(image)
+
if len(tensor.shape) == 2:
- tensor = torch.unsqueeze(tensor, dim=0)
- return tensor.permute(2, 0, 1) # CxHxW
+ tensor = torch.unsqueeze(tensor, dim=-1)
+
+ return tensor.permute(2, 0, 1).squeeze_() # CxHxW
def tensor_to_image(tensor):
@@ -69,7 +73,7 @@ def tensor_to_image(tensor):
the GPU, it will be copied back to CPU.
Args:
- tensor (Tensor): image of the form (1, C, H, W).
+ tensor (Tensor): image of the form (C, H, W).
Returns:
numpy.ndarray: image of the form (H, W, C).
@@ -78,13 +82,21 @@ def tensor_to_image(tensor):
if not torch.is_tensor(tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(tensor)))
- tensor = torch.squeeze(tensor)
+
if len(tensor.shape) > 3 or len(tensor.shape) < 2:
raise ValueError(
"Input size must be a two or three dimensional tensor")
- if len(tensor.shape) == 2:
+
+ input_shape = tensor.shape
+ if len(input_shape) == 2:
tensor = torch.unsqueeze(tensor, dim=0)
- return tensor.permute(1, 2, 0).contiguous().cpu().detach().numpy()
+
+ tensor = tensor.permute(1, 2, 0)
+
+ if len(input_shape) == 2:
+ tensor = torch.squeeze(tensor, dim=-1)
+
+ return tensor.contiguous().cpu().detach().numpy()
def create_pinhole(intrinsic, extrinsic, height, width):