Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

More docs

Wes315f1241a9d5
diff --git a/torchgeometry/utils.py b/torchgeometry/utils.py
index d856545e6..3a422d008 100644
--- a/torchgeometry/utils.py
+++ b/torchgeometry/utils.py
@@ -1,4 +1,5 @@
import torch
+import numpy as np
__all__ = [
"tensor_to_image",
@@ -8,8 +9,19 @@ __all__ = [
def image_to_tensor(image):
"""Converts a numpy image to a torch.Tensor image.
+
+ Args:
+ image (numpy.ndarray): image of the form (H, W, C).
+
+ Returns:
+ numpy.ndarray: image of the form (H, W, C).
+
"""
- # TODO: add asserts and type checkings
+ if not type(image) == np.array:
+ raise TypeError("Input type is not a numpy.array. 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)
@@ -19,8 +31,20 @@ def image_to_tensor(image):
def tensor_to_image(tensor):
"""Converts a torch.Tensor image to a numpy image. In case the tensor is
in the GPU, it will be copied back to CPU.
+
+ Args:
+ tensor (Tensor): image of the form (C, H, W).
+
+ Returns:
+ numpy.ndarray: image of the form (H, W, C).
+
"""
- # TODO: add asserts and type checkings
+ if not torch.is_tensor(tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(tensor)))
+ if len(tensor.shape) > 3 or len(tensor.shape) < 2:
+ raise ValueError(
+ "Input size must be a two or three dimensional tensor")
tensor = torch.squeeze(tensor)
if len(tensor.shape) == 2:
tensor = torch.unsqueeze(tensor, dim=0)