π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix: return independent grayscale rgb output (#5383)
Karnajeet Gosavi2a67589f3ec9
diff --git a/changelog.d/5383.fixed.md b/changelog.d/5383.fixed.mdnew file mode 100644index 000000000..d45104150--- /dev/null+++ b/changelog.d/5383.fixed.md@@ -0,0 +1,3 @@+`grayscale_to_rgb` returns independent storage again instead of an expanded view of its input: writing one channel of+the result no longer changes the grayscale input and the other two channels, and in-place operations on the result,+such as `add_`, no longer raise `RuntimeError`. Values and shapes are unchanged; the result is now a copy.diff --git a/kornia/color/gray.py b/kornia/color/gray.pyindex 2610b0a2c..587be6a18 100644--- a/kornia/color/gray.py+++ b/kornia/color/gray.py@@ -36,11 +36,6 @@ def grayscale_to_rgb(image: torch.Tensor) -> torch.Tensor:Convention:The singleton channel at axis -3 is repeated into R, G and B. Leading dimensions are preserved.- .. warning::- The result is an expanded view of the input: writing one channel also writes the input and- the other channels, and in-place operations over several channels raise- (`#5321 <https://github.com/kornia/kornia/issues/5321>`_).-Args:image: grayscale image torch.Tensor to be converted to RGB with shape :math:`(*,1,H,W)`.@@ -59,8 +54,7 @@ def grayscale_to_rgb(image: torch.Tensor) -> torch.Tensor:shape = list(image.shape)shape[-3] = 3- # Use expand to create a view that repeats along channel dimension, no memory overhead.- return image.expand(*shape)+ return image.expand(*shape).clone()def rgb_to_grayscale(image: torch.Tensor, rgb_weights: Optional[torch.Tensor] = None) -> torch.Tensor:diff --git a/tests/color/test_conventions_color.py b/tests/color/test_conventions_color.pyindex 0cafcd9ca..1b31be316 100644--- a/tests/color/test_conventions_color.py+++ b/tests/color/test_conventions_color.py@@ -53,8 +53,7 @@ class TestColorConventions(BaseTester):expected = torch.tensor([[[[0.412453]], [[0.212671]], [[0.019334]]]], device=device, dtype=dtype)self.assert_close(xyz, expected)- @pytest.mark.xfail(strict=True, reason="https://github.com/kornia/kornia/issues/5321")- def test_wart_grayscale_to_rgb_5321_does_not_alias_input(self, device, dtype):+ def test_convention_grayscale_to_rgb_5321_does_not_alias_input(self, device, dtype):image = torch.tensor([[[[0.2, 0.7]]]], device=device, dtype=dtype)before = image.clone()@@ -65,6 +64,11 @@ class TestColorConventions(BaseTester):self.assert_close(image, before)self.assert_close(rgb[:, 1:], before.expand(1, 2, 1, 2))+ rgb = kornia.color.grayscale_to_rgb(image)+ rgb.add_(0.1)+ self.assert_close(image, before)+ self.assert_close(rgb, before.expand(1, 3, 1, 2) + 0.1)+def test_convention_rgba_composites_over_white(self, device, dtype):rgba = torch.tensor([[[[0.2]], [[0.4]], [[0.6]], [[0.25]]]], device=device, dtype=dtype)background = (0.0, 0.5, 1.0)