Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(enhance): let a one-element thresholds or additions tensor solarize the whole batch (#6548)

Saksham Sharma9f54a0b2e79a
diff --git a/changelog.d/6548.fixed.md b/changelog.d/6548.fixed.md
new file mode 100644
index 000000000..0cb7e886e
--- /dev/null
+++ b/changelog.d/6548.fixed.md
@@ -0,0 +1 @@
+`kornia.enhance.solarize` applies a one-element `thresholds` or `additions` tensor, such as `torch.tensor([0.5])`, to the whole batch, as its docstring says and as a 0-d tensor does. It used to read a `(1,)` tensor as one value per sample and raise `AssertionError` unless the batch, or for an unbatched `(C, H, W)` image the channel count, was 1; a `(1, 1)` tensor always raised.
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.py
index f68d521e5..a879d30bd 100644
--- a/kornia/enhance/adjust.py
+++ b/kornia/enhance/adjust.py
@@ -855,6 +855,12 @@ def _solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5)
if not isinstance(thresholds, (float, torch.Tensor)):
raise TypeError(f"The factor should be either a float or torch.Tensor. Got {type(thresholds)}")
+ if isinstance(thresholds, torch.Tensor) and thresholds.numel() == 1:
+ # One threshold serves the whole batch, as documented, whatever its shape (#6547). It is
+ # moved and cast as the per-sample path below does, which a (1,) tensor with a batch of 1 took before.
+ if len(thresholds.shape) != 0:
+ thresholds = _to_device_nonblocking(thresholds, input.device).to(input.dtype)
+ thresholds = thresholds.reshape(())
if isinstance(thresholds, torch.Tensor) and len(thresholds.shape) != 0:
if not (input.size(0) == len(thresholds) and len(thresholds.shape) == 1):
raise AssertionError(f"thresholds must be a 1-d vector of shape ({input.size(0)},). Got {thresholds}")
@@ -945,6 +951,12 @@ def solarize(
"for floats, additions.clamp(-0.5, 0.5) for tensors.",
)
+ if isinstance(additions, torch.Tensor) and additions.numel() == 1:
+ # One addition serves the whole batch, as documented, whatever its shape (#6547). It is
+ # moved and cast as the per-sample path below does, which a (1,) tensor with a batch of 1 took before.
+ if len(additions.shape) != 0:
+ additions = _to_device_nonblocking(additions, input.device).to(input.dtype)
+ additions = additions.reshape(())
if isinstance(additions, torch.Tensor) and len(additions.shape) != 0:
if not (input.size(0) == len(additions) and len(additions.shape) == 1):
raise AssertionError(f"additions must be a 1-d vector of shape ({input.size(0)},). Got {additions}")
diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.py
index 4c933700f..6a48142bf 100644
--- a/tests/enhance/test_adjust.py
+++ b/tests/enhance/test_adjust.py
@@ -1626,6 +1626,23 @@ class TestSolarize(BaseTester):
with pytest.raises(TypeError):
assert TestSolarize.f(img, 0.8, 1)
+ @pytest.mark.parametrize("shape", [(2, 3, 4, 5), (3, 4, 5), (2, 2, 3, 4, 5)])
+ @pytest.mark.parametrize("element_shape", [(1,), (1, 1)])
+ @pytest.mark.parametrize("param_dtype", [None, torch.float64])
+ def test_one_element_tensor_serves_the_batch_6547(self, device, dtype, shape, element_shape, param_dtype):
+ # A one-element thresholds or additions tensor solarizes the whole batch, as a 0-d one does; it was read as
+ # per-sample and rejected unless the batch (or, unbatched, the channel count) was 1. It is cast to the image
+ # dtype first, as the per-sample path does.
+ if param_dtype == torch.float64 and device.type == "mps":
+ pytest.skip("MPS has no float64")
+ img = torch.rand(shape, device=device, dtype=dtype)
+ thresholds = torch.full(element_shape, 0.3, device=device, dtype=param_dtype or dtype)
+ additions = torch.full(element_shape, 0.1, device=device, dtype=param_dtype or dtype)
+ threshold = thresholds.to(dtype).reshape(())
+ expected = TestSolarize.f(img, threshold, additions.to(dtype).reshape(()))
+ self.assert_close(TestSolarize.f(img, thresholds, additions), expected, atol=0, rtol=0)
+ self.assert_close(TestSolarize.f(img, thresholds), TestSolarize.f(img, threshold), atol=0, rtol=0)
+
@pytest.mark.parametrize("addition", [0.5, -0.5])
def test_additions_closed_range_4605(self, device, dtype, addition):
# RandomSolarize admits the closed [-0.5, 0.5], and its sampler can draw an endpoint exactly.