π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(image): squeeze only the channel axis in tensor_to_image so a 1-row or 1-column grayscale image keeps its shape (#5572)
εΊζ’ηa122464a925f
diff --git a/changelog.d/5572.breaking.md b/changelog.d/5572.breaking.mdnew file mode 100644index 000000000..4c2c08a52--- /dev/null+++ b/changelog.d/5572.breaking.md@@ -0,0 +1,7 @@+`kornia.image.tensor_to_image` now maps a one-channel `(1, H, W)` tensor to `(H, W)` for every H and W:+`(1, 1, W)` gives `(1, W)`, `(1, H, 1)` gives `(H, 1)` and `(1, 1, 1)` gives `(1, 1)`, with either `keepdim`. They+used to come out as the 1-D arrays `(W,)` and `(H,)` and a 0-d array, because the 3-D branch squeezed every unit+axis, so a 1-row and a 1-column image could not be told apart; code that indexed the 1-D result now gets the 2-D+array. `kornia.io.write_image`, and `ImageModuleMixIn.save()` through it, now saves a 1-row or 1-column grayscale+image like any other, where it used to raise a `TypeError` from the encoder (an `IndexError` for `(1, 1, 1)`)+(#5570).diff --git a/kornia/image/utils.py b/kornia/image/utils.pyindex d15d841b0..ddeb14a60 100644--- a/kornia/image/utils.py+++ b/kornia/image/utils.py@@ -204,8 +204,9 @@ def tensor_to_image(tensor: torch.Tensor, keepdim: bool = False, force_contiguouelif len(input_shape) == 3:# (C, H, W) -> (H, W, C)if input_shape[0] == 1:- # Grayscale for proper plt.imshow needs to be (H,W)- image = image.squeeze()+ # Grayscale for proper plt.imshow needs to be (H,W); squeeze only the channel axis so that a unit H or W+ # of a 1-row or 1-column image survives.+ image = image.squeeze(0)else:image = image.permute(1, 2, 0)elif len(input_shape) == 4:diff --git a/tests/image/test_image_utils.py b/tests/image/test_image_utils.pyindex 340c877f6..31450756b 100644--- a/tests/image/test_image_utils.py+++ b/tests/image/test_image_utils.py@@ -57,7 +57,10 @@ def test_list_of_images_to_tensor(num_of_images, image_shape):[((4, 4), (4, 4)),((1, 4, 4), (4, 4)),+ ((1, 1, 5), (1, 5)),+ ((1, 5, 1), (5, 1)),((1, 1, 4, 4), (4, 4)),+ ((1, 1, 1, 5), (1, 5)),((3, 4, 4), (4, 4, 3)),((2, 3, 4, 4), (2, 4, 4, 3)),((1, 3, 4, 4), (4, 4, 3)),@@ -75,7 +78,10 @@ def test_tensor_to_image(device, input_shape, expected):[((4, 4), (4, 4)),((1, 4, 4), (4, 4)),+ ((1, 1, 5), (1, 5)),+ ((1, 5, 1), (5, 1)),((1, 1, 4, 4), (1, 4, 4)),+ ((1, 1, 1, 5), (1, 1, 5)),((3, 4, 4), (4, 4, 3)),((2, 3, 4, 4), (2, 4, 4, 3)),((1, 3, 4, 4), (1, 4, 4, 3)),diff --git a/tests/io/test_io_image.py b/tests/io/test_io_image.pyindex 9f398402c..4bbebcec1 100644--- a/tests/io/test_io_image.py+++ b/tests/io/test_io_image.py@@ -213,6 +213,18 @@ class TestIoImage:assert file_path.is_file()+ @pytest.mark.parametrize("shape", [(1, 1, 5), (1, 5, 1)])+ def test_write_image_one_row_or_one_column_grayscale(self, tmp_path: Path, shape) -> None:+ # (1, 1, W) and (1, H, 1) must keep their unit height or width on the way to the encoder.+ img = torch.arange(5, dtype=torch.uint8).reshape(shape) * 50+ path = tmp_path / "line.png"++ write_image(path, img)++ loaded = load_image(path, ImageLoadType.UNCHANGED)+ assert loaded.shape == shape+ assert torch.equal(loaded, img)+class TestDownloadImage:"""Offline pins for ``kornia.io.sample.download_image``; ``urlopen`` is mocked, no network is touched."""