π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): accept an empty batch in guided_blur with multichannel guidance (#5909)
Saksham Sharma97388f39f579
diff --git a/changelog.d/5909.fixed.md b/changelog.d/5909.fixed.mdnew file mode 100644index 000000000..853df805f--- /dev/null+++ b/changelog.d/5909.fixed.md@@ -0,0 +1,3 @@+`kornia.filters.guided_blur` and `GuidedBlur` accept an empty batch with a multichannel guidance. They raised+`RuntimeError: cannot reshape tensor of 0 elements` because the input channel count was inferred with `-1`, which+an empty tensor cannot provide; this happened both when the guidance is the input and when it is a separate tensor.diff --git a/kornia/filters/guided.py b/kornia/filters/guided.pyindex f6ff4f332..0dc0e7b8d 100644--- a/kornia/filters/guided.py+++ b/kornia/filters/guided.py@@ -121,6 +121,9 @@ def _guided_blur_multichannel_guidance() -> torch.Tensor:guidance_sub, input_sub, kernel_size = _preprocess_fast_guided_blur(guidance, input, kernel_size, subsample)B, C, H, W = guidance_sub.shape+ # The input channel count is spelled out instead of inferred with ``-1``: an empty batch has no elements, so+ # ``reshape``/``view`` cannot infer that size and raise (#4429).+ C_in = input.shape[1]mean_I = box_blur(guidance_sub, kernel_size, border_type, separable=separable).permute(0, 2, 3, 1)II = (guidance_sub.unsqueeze(1) * guidance_sub.unsqueeze(2)).flatten(1, 2)@@ -135,7 +138,7 @@ def _guided_blur_multichannel_guidance(mean_p = box_blur(input_sub, kernel_size, border_type, separable=separable).permute(0, 2, 3, 1)Ip = (input_sub.unsqueeze(1) * guidance_sub.unsqueeze(2)).flatten(1, 2)corr_Ip = box_blur(Ip, kernel_size, border_type, separable=separable).permute(0, 2, 3, 1)- cov_Ip = torch.addcmul(corr_Ip.reshape(B, H, W, C, -1), mean_p.unsqueeze(-2), mean_I.unsqueeze(-1), value=-1)+ cov_Ip = torch.addcmul(corr_Ip.reshape(B, H, W, C, C_in), mean_p.unsqueeze(-2), mean_I.unsqueeze(-1), value=-1)if isinstance(eps, torch.Tensor):_eps = torch.eye(C, device=guidance.device, dtype=guidance.dtype).view(1, 1, 1, C, C) * eps.view(-1, 1, 1, 1, 1)@@ -164,7 +167,7 @@ def _guided_blur_multichannel_guidance(if subsample > 1:mean_a = interpolate(mean_a, size=guidance.shape[-2:], mode="bilinear")mean_b = interpolate(mean_b, size=guidance.shape[-2:], mode="bilinear")- mean_a = mean_a.view(B, C, -1, *guidance.shape[-2:])+ mean_a = mean_a.view(B, C, C_in, *guidance.shape[-2:])# torch.einsum might not be contiguous, thus mean_b is the first argumentreturn mean_b + torch.einsum("BCHW,BCcHW->BcHW", guidance, mean_a)diff --git a/tests/filters/test_guided.py b/tests/filters/test_guided.pyindex 84669e84d..ad5dbb06f 100644--- a/tests/filters/test_guided.py+++ b/tests/filters/test_guided.py@@ -292,6 +292,22 @@ class TestGuidedBlur(BaseTester):assert actual.dtype == torch.float32self.assert_close(actual, expected)+ @pytest.mark.parametrize("guide_dim", [1, 3])+ @pytest.mark.parametrize("input_dim", [None, 2])+ @pytest.mark.parametrize("subsample", [1, 2])+ @pytest.mark.parametrize("separable", [False, True])+ def test_empty_batch(self, guide_dim, input_dim, subsample, separable, device, dtype):+ # #4429: multichannel guidance inferred the input channel count with ``-1``, which ``view``/``reshape``+ # cannot do for an empty batch. ``input_dim=None`` passes the guidance itself as the input.+ guide = torch.rand(0, guide_dim, 8, 10, device=device, dtype=dtype, requires_grad=True)+ inp = guide if input_dim is None else torch.rand(0, input_dim, 8, 10, device=device, dtype=dtype)+ actual = guided_blur(guide, inp, 3, 0.1, subsample=subsample, separable=separable)+ assert actual.shape == inp.shape+ assert actual.dtype == dtype+ actual.sum().backward()+ assert guide.grad is not None+ assert guide.grad.shape == guide.shape+def test_exception(self):from kornia.core.exceptions import BaseError, TypeCheckError