Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[BUG] remove float casting in image_to_tensor (#497)

Edgar Riba4c6b9247bf58
diff --git a/kornia/utils/image.py b/kornia/utils/image.py
index 806db93be..382360a74 100644
--- a/kornia/utils/image.py
+++ b/kornia/utils/image.py
@@ -31,7 +31,7 @@ def image_to_tensor(image: Union[np.ndarray, Image.Image], keepdim: bool = True)
"Input size must be a two, three or four dimensional array")
input_shape = image.shape
- tensor: torch.Tensor = torch.from_numpy(image).to(torch.float)
+ tensor: torch.Tensor = torch.from_numpy(image)
if len(input_shape) == 2:
# (H, W) -> (1, H, W)
diff --git a/test/utils/test_image.py b/test/utils/test_image.py
index 2c3f6b476..c5b919012 100644
--- a/test/utils/test_image.py
+++ b/test/utils/test_image.py
@@ -7,6 +7,16 @@ import kornia.testing as utils # test utils
from test.common import device
[email protected]("input_dtype, expected_dtype",
+ [(np.uint8, torch.uint8),
+ (np.float32, torch.float32),
+ (np.float64, torch.float64), ])
+def test_image_to_tensor_keep_dtype(input_dtype, expected_dtype):
+ image = np.ones((1, 3, 4, 5), dtype=input_dtype)
+ tensor = kornia.image_to_tensor(image)
+ assert tensor.dtype == expected_dtype
+
+
@pytest.mark.parametrize("input_shape, expected",
[((4, 4), (4, 4)),
((1, 4, 4), (4, 4)),