π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): validate shift_rgb image rank (#5360)
AhmetHakanBezirbda60b847805
diff --git a/changelog.d/5360.breaking.md b/changelog.d/5360.breaking.mdnew file mode 100644index 000000000..032950337--- /dev/null+++ b/changelog.d/5360.breaking.md@@ -0,0 +1,4 @@+`shift_rgb` now raises `ShapeError` for input that is not `(B, 3, H, W)`. It used to accept any input with three+channels at axis -3 and broadcast the `(B, 3, 1, 1)` shifts against its leading axes without an error: a+`(B, T, 3, H, W)` input received the per-batch shifts along `T` instead of `B`, and a `(3, H, W)` input came back+as `(B, 3, H, W)`, one copy per shift. `RandomRGBShift` always passes `(B, 3, H, W)` and is unaffected.diff --git a/kornia/enhance/shift_rgb.py b/kornia/enhance/shift_rgb.pyindex c772c00b6..0a64a2fbf 100644--- a/kornia/enhance/shift_rgb.py+++ b/kornia/enhance/shift_rgb.py@@ -18,24 +18,22 @@import torch-from kornia.core.check import KORNIA_CHECK_IS_COLOR, KORNIA_CHECK_IS_TENSOR+from kornia.core.check import KORNIA_CHECK_IS_COLOR, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPEdef shift_rgb(image: torch.Tensor, r_shift: torch.Tensor, g_shift: torch.Tensor, b_shift: torch.Tensor) -> torch.Tensor:"""Shift rgb channels.Convention:- Input has shape (B, 3, H, W). One RGB shift per batch element is- assembled as (B, 3, 1, 1), then results are clamped to [0, 1].-- .. warning::- Input of another rank is not rejected; rank-5 input receives the shifts along the wrong- axis (`#5327 <https://github.com/kornia/kornia/issues/5327>`_).+ Input must have shape (B, 3, H, W); another rank raises ``ShapeError`` and another channel+ count raises ``ImageError``. One RGB shift per batch element is assembled as (B, 3, 1, 1),+ then results are clamped to [0, 1].Shift each image's channel by either r_shift for red, g_shift for green and b_shift for blue channels."""KORNIA_CHECK_IS_TENSOR(image)KORNIA_CHECK_IS_COLOR(image, f"with shape {image.shape}")+ KORNIA_CHECK_SHAPE(image, ["B", "3", "H", "W"])shifts = [r_shift, g_shift, b_shift]diff --git a/tests/enhance/test_conventions_enhance.py b/tests/enhance/test_conventions_enhance.pyindex a5344524b..11b3d75d5 100644--- a/tests/enhance/test_conventions_enhance.py+++ b/tests/enhance/test_conventions_enhance.py@@ -224,16 +224,11 @@ class TestEnhanceConventions(BaseTester):with pytest.raises(ShapeError):kornia.enhance.image_histogram2d(torch.ones(1, 1, 1, 2, 3, device=device, dtype=dtype), n_bins=2)- @pytest.mark.xfail(strict=True, reason="#5327: rank-5 input receives the shifts along the wrong axis")- def test_wart_shift_rgb_rank5_is_rejected_or_per_batch_5327(self, device, dtype):+ def test_convention_shift_rgb_rank5_is_rejected_5327(self, device, dtype):image = torch.zeros(2, 2, 3, 1, 1, device=device, dtype=dtype)shifts = torch.tensor([0.1, 0.2], device=device, dtype=dtype), torch.zeros(2, device=device, dtype=dtype)- try:- out = kornia.enhance.shift_rgb(image, shifts[0], shifts[1], shifts[1])- except (ValueError, BaseError):- return- expected = torch.tensor([0.1, 0.1, 0.2, 0.2], device=device, dtype=dtype)- self.assert_close(out[:, :, 0].flatten(), expected)+ with pytest.raises(ShapeError):+ kornia.enhance.shift_rgb(image, shifts[0], shifts[1], shifts[1])def test_convention_normalize_min_max_rescales_each_spatial_plane(self, device, dtype):# (B, C, D, H, W): every depth slice is its own (H, W) plane, so each one spans [0, 1].diff --git a/tests/enhance/test_shift_rgb.py b/tests/enhance/test_shift_rgb.pyindex 7cd10d139..7f64a60a1 100644--- a/tests/enhance/test_shift_rgb.py+++ b/tests/enhance/test_shift_rgb.py@@ -24,6 +24,15 @@ from testing.base import BaseTesterclass TestRGBShift(BaseTester):+ @pytest.mark.parametrize("shape", [(3, 1, 1), (2, 2, 3, 1, 1)])+ def test_invalid_image_rank(self, shape, device, dtype):+ from kornia.core.exceptions import ShapeError++ image = torch.zeros(shape, device=device, dtype=dtype)+ shifts = torch.zeros(1 if len(shape) == 3 else 2, device=device, dtype=dtype)+ with pytest.raises(ShapeError):+ kornia.enhance.shift_rgb(image, shifts, shifts, shifts)+def test_rgb_shift_no_shift(self, device, dtype):r_shift, g_shift, b_shift = torch.Tensor([0]), torch.Tensor([0]), torch.Tensor([0])image = torch.rand(2, 3, 5, 5, device=device, dtype=dtype)