Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(color): accept empty tensors in apply_colormap (#5937)

Saksham Sharma8055d7637dbe
diff --git a/changelog.d/5937.fixed.md b/changelog.d/5937.fixed.md
new file mode 100644
index 000000000..8cce06edc
--- /dev/null
+++ b/changelog.d/5937.fixed.md
@@ -0,0 +1,4 @@
+`kornia.color.apply_colormap` and `kornia.color.ApplyColorMap` accept empty tensors. An empty batch or an empty
+channel dimension raised `RuntimeError: cannot reshape tensor of 0 elements`, and a floating image with zero height
+or width raised `IndexError` from `amax`. They now return an empty tensor of the documented `(B, 3*C, H, W)` shape.
+Non-empty outputs are unchanged.
diff --git a/kornia/color/colormap.py b/kornia/color/colormap.py
index 8b95ce51c..5f2b18ed6 100644
--- a/kornia/color/colormap.py
+++ b/kornia/color/colormap.py
@@ -246,12 +246,14 @@ def _apply_colormap(input_tensor: torch.Tensor, colors: torch.Tensor) -> torch.T
input_tensor = input_tensor.unsqueeze(0)
B, C, H, W = input_tensor.shape
- input_tensor = input_tensor.reshape(B, C, -1)
+ # Name the pixel count: `-1` cannot be inferred when the tensor has no elements.
+ input_tensor = input_tensor.reshape(B, C, H * W)
if input_tensor.is_floating_point():
# The tensor-valued condition keeps range selection compile-safe and independent
- # of other samples and channels.
+ # of other samples and channels. `all` rather than `amax`, which refuses a channel without pixels;
+ # a NaN selects 255 either way.
max_value = torch.where(
- input_tensor.amax(dim=-1, keepdim=True) <= 1.0,
+ (input_tensor <= 1.0).all(dim=-1, keepdim=True),
torch.tensor(1.0, device=input_tensor.device, dtype=torch.float),
torch.tensor(255.0, device=input_tensor.device, dtype=torch.float),
)
diff --git a/tests/color/test_colormap.py b/tests/color/test_colormap.py
index 7449e38d0..164fa3909 100644
--- a/tests/color/test_colormap.py
+++ b/tests/color/test_colormap.py
@@ -145,6 +145,28 @@ class TestApplyColorMap(BaseTester):
assert actual.shape == expected_shape
+ @pytest.mark.parametrize(
+ "shape,expected_shape",
+ [
+ ((0, 1, 4, 5), (0, 3, 4, 5)),
+ ((0, 2, 4, 5), (0, 6, 4, 5)),
+ ((2, 0, 4, 5), (2, 0, 4, 5)),
+ ((0, 4, 5), (1, 0, 4, 5)),
+ ((2, 1, 0, 5), (2, 3, 0, 5)),
+ ((1, 2, 0, 0), (1, 6, 0, 0)),
+ ],
+ )
+ @pytest.mark.parametrize("integer_input", [False, True])
+ def test_empty_input(self, shape, expected_shape, integer_input, device, dtype):
+ # An empty tensor raised in reshape(B, C, -1), and a float one without pixels in amax.
+ cm = ColorMap(base="viridis", device=device, dtype=dtype)
+ input_tensor = torch.zeros(shape, device=device, dtype=torch.uint8 if integer_input else dtype)
+ for op in (lambda x: apply_colormap(x, cm), ApplyColorMap(colormap=cm)):
+ actual = op(input_tensor)
+ assert actual.shape == expected_shape
+ assert actual.device == input_tensor.device
+ assert actual.dtype == dtype
+
@pytest.mark.skip(reason="jacobian mismatch")
def test_gradcheck(self, device):
# TODO: implement differentiability