π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): preserve sepia tint when rescaling (#5388)
AhmetHakanBezir0f6993c4fa54
diff --git a/changelog.d/5388.breaking.md b/changelog.d/5388.breaking.mdnew file mode 100644index 000000000..0fbf411dc--- /dev/null+++ b/changelog.d/5388.breaking.md@@ -0,0 +1,4 @@+`Sepia()` and `sepia_from_rgb` with the default `rescale=True` now divide all three output channels by one maximum per+image, taken over channels and pixels, so the sepia tint survives the rescale. They used to divide each channel by its+own spatial maximum, which made the output almost grey: a uniform 0.5 grey image used to give `(1.0, 1.0, 1.0)` and now+gives `(1.0, 0.8905, 0.6936)`. Default outputs change for every image; `rescale=False` is unchanged.diff --git a/kornia/color/sepia.py b/kornia/color/sepia.pyindex 5c89a41e6..24f5d6e36 100644--- a/kornia/color/sepia.py+++ b/kornia/color/sepia.py@@ -24,16 +24,13 @@ def sepia_from_rgb(input: torch.Tensor, rescale: bool = True, eps: float = 1e-6)r"""Apply the sepia filter to an RGB tensor.Convention:- Applies the fixed sepia matrix to RGB channels at axis -3. rescale divides each output- channel of each image by its own spatial maximum; disable it to keep the raw matrix response.-- .. warning::- With the default rescale=True the output is almost grey: per-channel rescaling removes the- sepia tint (`#5322 <https://github.com/kornia/kornia/issues/5322>`_).+ Applies the fixed sepia matrix to RGB channels at axis -3. rescale divides all output+ channels of each image by one shared maximum over channels and pixels, preserving the tint.+ Disable it to keep the raw matrix response.Args:input: the input tensor with shape :math:`(*, C, H, W)`.- rescale: if True, divide each output channel by its own maximum.+ rescale: if True, divide all output channels by one maximum per image.eps: small constant added to the denominator for numerical stability.Returns:@@ -62,8 +59,8 @@ def sepia_from_rgb(input: torch.Tensor, rescale: bool = True, eps: float = 1e-6)sepia_out = torch.stack([r_out, g_out, b_out], dim=-3)if rescale:- max_values = sepia_out.amax(dim=-1).amax(dim=-1)- sepia_out = sepia_out / (max_values[..., None, None] + eps)+ max_values = sepia_out.amax(dim=(-3, -2, -1), keepdim=True)+ sepia_out = sepia_out / (max_values + eps)return sepia_out@@ -74,7 +71,7 @@ class Sepia(nn.Module):See the Convention block on :func:`sepia_from_rgb`.Args:- rescale: if True, divide each output channel by its own maximum.+ rescale: if True, divide all output channels by one maximum per image.eps: small constant added to the denominator for numerical stability.Returns:@@ -94,7 +91,7 @@ class Sepia(nn.Module):"""Initialize Sepia.Args:- rescale: if True, divide each output channel by its own maximum.+ rescale: if True, divide all output channels by one maximum per image.eps: small constant added to the denominator for numerical stability."""self.rescale = rescalediff --git a/tests/color/test_conventions_color.py b/tests/color/test_conventions_color.pyindex ccaf27081..0cafcd9ca 100644--- a/tests/color/test_conventions_color.py+++ b/tests/color/test_conventions_color.py@@ -133,15 +133,18 @@ class TestColorConventions(BaseTester):self.assert_close(raw_bg, expected)self.assert_close(flipped_gb, raw_bg)- @pytest.mark.xfail(strict=True, reason="https://github.com/kornia/kornia/issues/5322")- def test_wart_sepia_5322_default_keeps_tint(self, device, dtype):- # The sepia matrix maps gray 0.5 to (0.6755, 0.6015, 0.4685), so blue is about 0.69 of red;- # per-channel rescaling brings every channel to about 1.- image = torch.full((1, 3, 1, 1), 0.5, device=device, dtype=dtype)-- out = kornia.color.Sepia()(image).flatten()-- assert out[2] < 0.9 * out[0]+ def test_convention_sepia_5322_default_keeps_tint(self, device, dtype):+ # Gray maps to intensity * (1.351, 1.203, 0.937). Each image uses its red maximum,+ # independently of the other batch member's intensity.+ intensities = torch.tensor([0.25, 0.5], device=device, dtype=dtype).view(2, 1, 1, 1)+ image = intensities.expand(2, 3, 2, 2)+ coefficients = torch.tensor([1.351, 1.203, 0.937], device=device, dtype=dtype).view(1, 3, 1, 1)+ expected = (intensities * coefficients / (intensities * 1.351 + 1e-6)).expand_as(image)++ out = kornia.color.Sepia()(image)++ self.assert_close(out, expected)+ assert (out[:, 2] < 0.9 * out[:, 0]).all()def test_convention_rgb255_scaling_clipping_and_normalization(self, device, dtype):# Expected values follow the documented affine maps in rgb.py.diff --git a/tests/color/test_sepia.py b/tests/color/test_sepia.pyindex 4d64dc525..1b1ffc8d3 100644--- a/tests/color/test_sepia.py+++ b/tests/color/test_sepia.py@@ -31,7 +31,11 @@ class TestSepia(BaseTester):# With rescaleexpected_tensor = torch.tensor(- [[[0.1269, 1.0], [0.2537, 0.5400]], [[0.1269, 1.0], [0.2537, 0.5403]], [[0.1269, 1.0], [0.2538, 0.5403]]],+ [+ [[0.1269, 1.0], [0.2537, 0.5400]],+ [[0.1130, 0.8904], [0.2259, 0.4811]],+ [[0.0880, 0.6935], [0.1760, 0.3747]],+ ],device=device,dtype=dtype,)@@ -96,3 +100,11 @@ class TestSepia(BaseTester):img = torch.ones(1, 3, 5, 5, device=device, dtype=dtype)self.assert_close(op(img), op_module(img))++ def test_black_image_stays_black(self, device, dtype):+ # eps keeps the shared maximum of a black image from dividing 0 by 0.+ image = torch.zeros(2, 3, 4, 4, device=device, dtype=dtype)++ actual = kornia.color.sepia(image)++ self.assert_close(actual, torch.zeros_like(image), rtol=0.0, atol=0.0)