Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): keep the input dtype in bilateral_blur with a wider sigma_space or guidance (#5524)

Animesh Kumarfc8bddba4eb2
diff --git a/changelog.d/5524.breaking.md b/changelog.d/5524.breaking.md
new file mode 100644
index 000000000..0a6cd6b4e
--- /dev/null
+++ b/changelog.d/5524.breaking.md
@@ -0,0 +1 @@
+`bilateral_blur`, `joint_bilateral_blur`, `BilateralBlur` and `JointBilateralBlur` used to return a wider dtype than a floating input when a tensor `sigma_space` or the `guidance` was wider, for example float64 for a float32 image with a float64 `sigma_space`, or float32 for a float16 image with a default float32 `sigma_space` tensor. Both are now cast to the input's dtype, as a tensor `sigma_color` already was, so the output keeps the input's dtype.
diff --git a/changelog.d/5524.fixed.md b/changelog.d/5524.fixed.md
new file mode 100644
index 000000000..8a0a183b9
--- /dev/null
+++ b/changelog.d/5524.fixed.md
@@ -0,0 +1 @@
+`bilateral_blur`, `joint_bilateral_blur`, `BilateralBlur` and `JointBilateralBlur` move a tensor `sigma_space` to the device of a floating input, as they already moved a tensor `sigma_color`. A CPU `sigma_space` with a CUDA or MPS image used to raise a device-mismatch error.
diff --git a/kornia/filters/bilateral.py b/kornia/filters/bilateral.py
index 6b0e39ecd..8a64a1e8c 100644
--- a/kornia/filters/bilateral.py
+++ b/kornia/filters/bilateral.py
@@ -76,6 +76,9 @@ def _bilateral_blur(
if isinstance(sigma_space, torch.Tensor):
KORNIA_CHECK_SHAPE(sigma_space, ["B", "2"])
_check_sigma_batch("sigma_space", sigma_space, input)
+ # the spatial kernel takes the dtype of a tensor sigma, so match a floating input as for sigma_color
+ if input.is_floating_point():
+ sigma_space = sigma_space.to(device=input.device, dtype=input.dtype)
ky, kx = _unpack_2d_ks(kernel_size)
_check_kernel_size((ky, kx))
@@ -89,6 +92,9 @@ def _bilateral_blur(
guidance = input
unfolded_guidance = unfolded_input
else:
+ # the colour kernel takes the guidance's dtype, so a wider guidance would promote the output
+ if input.is_floating_point() and guidance.is_floating_point():
+ guidance = guidance.to(input.dtype)
padded_guidance = F.pad(guidance, (pad_x, pad_x, pad_y, pad_y), mode=border_type)
unfolded_guidance = padded_guidance.unfold(2, ky, 1).unfold(3, kx, 1) # (B, C, H, W, Ky, Kx)
@@ -133,13 +139,12 @@ def bilateral_blur(
- ``sigma_color`` is in the units of the input values: an image scaled by ``s > 0``, filtered with
``sigma_color * s``, gives the result scaled by ``s``.
- The border modes are :func:`~kornia.filters.filter2d`'s, but only in lower case; see its Convention block.
+ - A floating input keeps its dtype: a tensor ``sigma_color`` or ``sigma_space`` is cast to it and moved to
+ its device.
- Known defects:
- an integer input is differenced in its own dtype, so uint8 differences wrap and the filter blends
across edges it should keep (`#5155 <https://github.com/kornia/kornia/issues/5155>`_).
- - a tensor ``sigma_space`` keeps its own dtype, unlike ``sigma_color``, so a wider one promotes the output:
- a float32 image with a float64 ``sigma_space`` comes back float64
- (`#5521 <https://github.com/kornia/kornia/issues/5521>`_).
Arguments:
input: the input torch.Tensor with shape :math:`(B,C,H,W)`.
@@ -196,9 +201,9 @@ def joint_bilateral_blur(
``guidance``, and ``input`` is what gets averaged.
- ``input`` comes first and ``guidance`` second, the opposite of :func:`~kornia.filters.guided_blur`.
- ``guidance`` may have its own channel count; its batch size and :math:`(H, W)` must equal ``input``'s.
+ - A floating ``guidance`` is cast to the dtype of a floating ``input``, so the output keeps ``input``'s dtype.
- Known defects: those of :func:`~kornia.filters.bilateral_blur`, for an integer ``guidance``
- (`#5155 <https://github.com/kornia/kornia/issues/5155>`_) and for a tensor ``sigma_space`` of a wider dtype,
- which ``guidance`` of a wider dtype shares (`#5521 <https://github.com/kornia/kornia/issues/5521>`_).
+ (`#5155 <https://github.com/kornia/kornia/issues/5155>`_).
Arguments:
input: the input torch.Tensor with shape :math:`(B,C,H,W)`.
diff --git a/tests/filters/test_bilateral.py b/tests/filters/test_bilateral.py
index 8b0f8f3e6..8ae4a3976 100644
--- a/tests/filters/test_bilateral.py
+++ b/tests/filters/test_bilateral.py
@@ -167,6 +167,30 @@ class TestBilateralBlur(BaseTester):
single = {k: (v[row : row + 1] if isinstance(v, torch.Tensor) else v) for k, v in kwargs.items()}
self.assert_close(actual[i : i + 1], bilateral_blur(image[i : i + 1], 3, **single))
+ @pytest.mark.parametrize("sigma_dtype", [torch.float16, torch.float64])
+ def test_tensor_sigma_space_keeps_the_input_dtype_5521(self, sigma_dtype, device, dtype):
+ # A tensor sigma_space of another dtype is cast to the input's, as a tensor sigma_color is, so it neither
+ # promotes the output nor changes the result of the equivalent float sigma_space. It is built on the CPU, which
+ # also has float64, so on another device it is moved to the input's device as well.
+ image = torch.rand(2, 3, 8, 9, device=device, dtype=dtype)
+ sigma_space = torch.tensor([[1.25, 1.5]], dtype=sigma_dtype)
+
+ actual = bilateral_blur(image, 3, 0.5, sigma_space)
+ assert actual.dtype == dtype
+ self.assert_close(actual, bilateral_blur(image, 3, 0.5, (1.25, 1.5)))
+ assert BilateralBlur(3, 0.5, sigma_space)(image).dtype == dtype
+
+ def test_integer_input_keeps_a_tensor_sigma_space_5521(self, device):
+ # Only a floating input casts sigma_space: a uint8 cast would truncate 1.5 to 1. With a huge sigma_color every
+ # colour weight is 1 whatever the uint8 differences do (#5155), so the filter is gaussian_blur2d's.
+ if device.type not in ("cpu", "mps"):
+ pytest.skip("uint8 reflect padding is pinned on the CPU and MPS only")
+ image = (torch.arange(63, device=device).view(1, 1, 7, 9) * 4).to(torch.uint8)
+ sigma_space = torch.tensor([[1.5, 1.5]], device=device)
+
+ actual = bilateral_blur(image, 3, 1e6, sigma_space)
+ self.assert_close(actual, gaussian_blur2d(image.float(), 3, sigma_space))
+
@pytest.mark.parametrize("shape", [(), (2,), (1, 1)], ids=["0d", "1d", "one_column"])
def test_sigma_space_shape_is_checked_before_its_batch_5430(self, shape, device, dtype):
# The (B, 2) shape check runs before the batch check: a 0-d sigma_space has no batch to read, and a 1-D pair
@@ -496,6 +520,31 @@ class TestJointBilateralBlur(BaseTester):
out = joint_bilateral_blur(img, guide, kernel_size, sigma_color, sigma_distance)
self.assert_close(out, expected)
+ def test_wider_guidance_keeps_the_input_dtype_5521(self, device, dtype):
+ # A floating guidance is cast to the input's dtype, so a wider one does not promote the output.
+ if device.type == "mps":
+ pytest.skip("MPS has no float64")
+ image = torch.rand(2, 3, 8, 9, device=device, dtype=dtype)
+ guidance = torch.rand(2, 1, 8, 9, device=device, dtype=torch.float64)
+
+ actual = joint_bilateral_blur(image, guidance, 3, 0.5, (1.5, 1.5))
+ assert actual.dtype == dtype
+ self.assert_close(actual, joint_bilateral_blur(image, guidance.to(dtype), 3, 0.5, (1.5, 1.5)))
+ assert JointBilateralBlur(3, 0.5, (1.5, 1.5))(image, guidance).dtype == dtype
+
+ def test_integer_input_keeps_a_floating_guidance_5521(self, device):
+ # Only a floating input casts guidance: a uint8 cast would truncate a [0, 1] guidance to zeros. The guidance is
+ # what gets differenced, so a uint8 input filters as its float copy does. A tuple sigma_space would build the
+ # spatial kernel in uint8 (#5155); a float tensor one keeps it in floating point.
+ if device.type not in ("cpu", "mps"):
+ pytest.skip("uint8 reflect padding is pinned on the CPU and MPS only")
+ image = (torch.arange(63, device=device).view(1, 1, 7, 9) * 4).to(torch.uint8)
+ guidance = torch.linspace(0, 1, 63, device=device).view(1, 1, 7, 9)
+ sigma_space = torch.tensor([[1.5, 1.5]], device=device)
+
+ actual = joint_bilateral_blur(image, guidance, 3, 0.1, sigma_space)
+ self.assert_close(actual, joint_bilateral_blur(image.float(), guidance, 3, 0.1, sigma_space))
+
class TestConventionsBilateralBlur(BaseTester):
"""Pins for the colour and space parameters of :func:`bilateral_blur` and :func:`joint_bilateral_blur`."""
@@ -597,10 +646,8 @@ class TestConventionsBilateralBlur(BaseTester):
# the same filter on the same values in floating point keeps the stripes
self.assert_close(bilateral_blur(stripes.float(), 3, 50.0, (1.0, 1.0)), stripes.float(), rtol=0.0, atol=0.01)
- def test_wart_bilateral_blur_wider_sigma_space_promotes_the_output_5521(self, device, dtype):
- """A tensor sigma_space or guidance of a wider dtype promotes the output; sigma_color does not (#5521)."""
- # sigma_space builds the spatial kernel in its own dtype, and the product with the colour kernel promotes.
- # A fix that casts sigma_space to the input's dtype, as gaussian_blur2d casts sigma, fails this pin.
+ def test_convention_bilateral_blur_wider_sigma_space_keeps_the_input_dtype_5521(self, device, dtype):
+ """A tensor sigma_space or guidance of a wider dtype is cast to the input's, as sigma_color is (#5521)."""
if device.type == "mps":
pytest.skip("MPS has no float64")
if dtype == torch.float64:
@@ -608,10 +655,9 @@ class TestConventionsBilateralBlur(BaseTester):
self._skip_without_reflect_padding(device, dtype)
image = torch.rand(1, 1, 7, 9, device=device).to(dtype)
wide_space = torch.full((1, 2), 1.5, device=device, dtype=torch.float64)
- assert bilateral_blur(image, 3, 0.1, wide_space).dtype == torch.float64
- assert joint_bilateral_blur(image, image, 3, 0.1, wide_space).dtype == torch.float64
- assert joint_bilateral_blur(image, image.double(), 3, 0.1, (1.5, 1.5)).dtype == torch.float64
- # control: a wider sigma_color is cast to the input's dtype
+ assert bilateral_blur(image, 3, 0.1, wide_space).dtype == dtype
+ assert joint_bilateral_blur(image, image, 3, 0.1, wide_space).dtype == dtype
+ assert joint_bilateral_blur(image, image.double(), 3, 0.1, (1.5, 1.5)).dtype == dtype
wide_color = torch.tensor([0.1], device=device, dtype=torch.float64)
assert bilateral_blur(image, 3, wide_color, (1.5, 1.5)).dtype == dtype