π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color)!: isolate colormap range selection (#5413)
Ryan-amazinghorse53778cd223fa
diff --git a/changelog.d/5413.breaking.md b/changelog.d/5413.breaking.mdnew file mode 100644index 000000000..ef80bd948--- /dev/null+++ b/changelog.d/5413.breaking.md@@ -0,0 +1,5 @@+`apply_colormap` and `ApplyColorMap` no longer choose the input range from the maximum over the whole tensor. Integer+inputs always use `[0, 255]`: previously an integer tensor whose maximum was at most 1, such as a `{0, 1}` mask, was+divided by 1 and mapped to the full palette, and now `1` maps to the palette entry for 1/255. Floating-point inputs+choose `[0, 1]` or `[0, 255]` separately for each sample and channel: previously one sample or channel above 1 made+every other sample and channel of the tensor use `[0, 255]`. Refs #5306.diff --git a/kornia/color/colormap.py b/kornia/color/colormap.pyindex 0b92515f1..8b95ce51c 100644--- a/kornia/color/colormap.py+++ b/kornia/color/colormap.py@@ -193,11 +193,8 @@ def apply_colormap(input_tensor: torch.Tensor, colormap: ColorMap) -> torch.TensConvention:Accepts a rank-3 (C, H, W) or rank-4 (B, C, H, W) tensor and returns(B, 3*C, H, W), with B = 1 for rank-3 input; input channel c becomes output channels- 3c to 3c + 2. Unit-range floating inputs and integer inputs in [0, 255] are the intended ranges.-- .. warning::- The [0, 1] or [0, 255] range is chosen from the maximum over the whole tensor, so scaling- depends on every channel and sample: `#5306 <https://github.com/kornia/kornia/issues/5306>`_.+ 3c to 3c + 2. Integer inputs use [0, 255]. Floating inputs use [0, 1] or [0, 255]+ independently for each sample and channel, based on that channel's maximum.Args:input_tensor: the input torch.Tensor of image.@@ -250,13 +247,16 @@ def _apply_colormap(input_tensor: torch.Tensor, colors: torch.Tensor) -> torch.TB, C, H, W = input_tensor.shapeinput_tensor = input_tensor.reshape(B, C, -1)- # torch.where instead of a Python ternary on input_tensor.max() so the op is- # torch.compile fullgraph-safe (branching on a tensor value breaks the graph).- max_value = torch.where(- input_tensor.max() <= 1.0,- torch.tensor(1.0, device=input_tensor.device, dtype=torch.float),- torch.tensor(255.0, device=input_tensor.device, dtype=torch.float),- )+ if input_tensor.is_floating_point():+ # The tensor-valued condition keeps range selection compile-safe and independent+ # of other samples and channels.+ max_value = torch.where(+ input_tensor.amax(dim=-1, keepdim=True) <= 1.0,+ torch.tensor(1.0, device=input_tensor.device, dtype=torch.float),+ torch.tensor(255.0, device=input_tensor.device, dtype=torch.float),+ )+ else:+ max_value = 255.0input_tensor = input_tensor.float() / max_valuecolors = colors.permute(1, 0)diff --git a/tests/color/test_colormap.py b/tests/color/test_colormap.pyindex 4d0d6f038..7449e38d0 100644--- a/tests/color/test_colormap.py+++ b/tests/color/test_colormap.py@@ -94,6 +94,32 @@ class TestApplyColorMap(BaseTester):self.assert_close(actual, cm.colors.reshape(1, 3, 1, 256), rtol=0.0, atol=0.0)+ def test_uint8_dark_values_use_byte_range(self, device, dtype):+ cm = ColorMap(base=[[i / 255.0] * 3 for i in range(256)], num_colors=256, device=device, dtype=dtype)+ image = torch.tensor([[[[0, 1]]]], device=device, dtype=torch.uint8)++ actual = apply_colormap(image, cm)++ self.assert_close(actual[0, :, 0, 1], cm.colors[:, 1], rtol=0.0, atol=0.0)++ def test_float_channels_choose_ranges_independently(self, device, dtype):+ cm = ColorMap(base="autumn", num_colors=256, device=device, dtype=dtype)+ image = torch.tensor([[[[0.5]], [[0.0]]]], device=device, dtype=dtype)+ image_with_bright_channel = image.clone()+ image_with_bright_channel[:, 1] = 200.0++ actual = apply_colormap(image_with_bright_channel, cm)+ expected = apply_colormap(image, cm)++ self.assert_close(actual[:, :3], expected[:, :3])++ def test_float_samples_choose_ranges_independently(self, device, dtype):+ cm = ColorMap(base="autumn", num_colors=256, device=device, dtype=dtype)+ image = torch.tensor([[[[0.5]]]], device=device, dtype=dtype)+ paired = torch.cat([image, torch.full_like(image, 200.0)])++ self.assert_close(apply_colormap(paired, cm)[:1], apply_colormap(image, cm))+def test_exception(self, device, dtype):cm = ColorMap(base="autumn", device=device, dtype=dtype)with pytest.raises(Exception):diff --git a/tests/color/test_conventions_color.py b/tests/color/test_conventions_color.pyindex 1b31be316..434806257 100644--- a/tests/color/test_conventions_color.py+++ b/tests/color/test_conventions_color.py@@ -212,8 +212,7 @@ class TestColorConventions(BaseTester):leaf = torch.full((1, 1, 1, 1), 0.5, device=device, dtype=torch.float32, requires_grad=True)kornia.color.apply_colormap(leaf, colormap)- @pytest.mark.xfail(strict=True, reason="https://github.com/kornia/kornia/issues/5306")- def test_wart_apply_colormap_5306_is_batch_independent(self, device, dtype):+ def test_convention_apply_colormap_5306_is_batch_independent(self, device, dtype):colormap = kornia.color.ColorMap(base=[[0.0, 0.0, 0.0], [0.25, 0.25, 0.25], [0.75, 0.75, 0.75], [1.0, 1.0, 1.0]],num_colors=4,