π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): reject an even kernel_size in the bilateral filters up front (#5255)
Dmytro Mishkin238dc791dee0
diff --git a/changelog.d/5255.fixed.md b/changelog.d/5255.fixed.mdnew file mode 100644index 000000000..e2eb0a31f--- /dev/null+++ b/changelog.d/5255.fixed.md@@ -0,0 +1 @@+`bilateral_blur`, `joint_bilateral_blur`, `BilateralBlur` and `JointBilateralBlur` now reject an even (or non-positive) `kernel_size` up front with kornia's "Kernel size must be an odd integer bigger than 0" error; the modules raise it when they are constructed (#5163). Previously such a size was accepted and every call then failed inside the filter: usually with a raw torch shape error (`The size of tensor a (7) must match the size of tensor b (8)`), and on small images, where the unfolded window axis has length 1 and broadcasts, with the odd-size error of `get_gaussian_kernel2d`. Odd kernel sizes are unchanged.diff --git a/kornia/filters/bilateral.py b/kornia/filters/bilateral.pyindex 5ef5f039c..2248fe4dd 100644--- a/kornia/filters/bilateral.py+++ b/kornia/filters/bilateral.py@@ -25,7 +25,7 @@ from torch import nnfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE-from .kernels import _unpack_2d_ks, get_gaussian_kernel2d+from .kernels import _check_kernel_size, _unpack_2d_ks, get_gaussian_kernel2dfrom .median import _compute_zero_padding@@ -55,6 +55,7 @@ def _bilateral_blur(sigma_color = sigma_color.to(device=input.device, dtype=input.dtype).view(-1, 1, 1, 1, 1, 1)ky, kx = _unpack_2d_ks(kernel_size)+ _check_kernel_size((ky, kx))pad_y, pad_x = _compute_zero_padding(kernel_size)# Keep both patch axes: flattening them would copy every overlapping window.@@ -102,7 +103,7 @@ def bilateral_blur(Arguments:input: the input torch.Tensor with shape :math:`(B,C,H,W)`.- kernel_size: the size of the kernel.+ kernel_size: the size of the kernel. Each entry must be a positive odd integer.sigma_color: the standard deviation for intensity/color Gaussian kernel.Smaller values preserve more edges.sigma_space: the standard deviation for spatial Gaussian kernel.@@ -118,6 +119,9 @@ def bilateral_blur(Returns:the blurred torch.Tensor with shape :math:`(B, C, H, W)`.+ Raises:+ BaseError: if an entry of ``kernel_size`` is even or not positive.+Examples:>>> input = torch.rand(2, 4, 5, 5)>>> output = bilateral_blur(input, (3, 3), 0.1, (1.5, 1.5))@@ -148,7 +152,7 @@ def joint_bilateral_blur(Arguments:input: the input torch.Tensor with shape :math:`(B,C,H,W)`.guidance: the guidance torch.Tensor with shape :math:`(B,C,H,W)`.- kernel_size: the size of the kernel.+ kernel_size: the size of the kernel. Each entry must be a positive odd integer.sigma_color: the standard deviation for intensity/color Gaussian kernel.Smaller values preserve more edges.sigma_space: the standard deviation for spatial Gaussian kernel.@@ -163,6 +167,9 @@ def joint_bilateral_blur(Returns:the blurred torch.Tensor with shape :math:`(B, C, H, W)`.+ Raises:+ BaseError: if an entry of ``kernel_size`` is even or not positive.+Examples:>>> input = torch.rand(2, 4, 5, 5)>>> guidance = torch.rand(2, 4, 5, 5)@@ -185,6 +192,7 @@ class _BilateralBlur(nn.Module):color_distance_type: str = "l1",) -> None:super().__init__()+ _check_kernel_size(_unpack_2d_ks(kernel_size))self.kernel_size = kernel_sizeself.sigma_color = sigma_colorself.sigma_space = sigma_space@@ -210,7 +218,7 @@ class BilateralBlur(_BilateralBlur):to the center pixel, but also the difference in intensity or color.Arguments:- kernel_size: the size of the kernel.+ kernel_size: the size of the kernel. Each entry must be a positive odd integer.sigma_color: the standard deviation for intensity/color Gaussian kernel.Smaller values preserve more edges.sigma_space: the standard deviation for spatial Gaussian kernel.@@ -230,6 +238,9 @@ class BilateralBlur(_BilateralBlur):- Input: :math:`(B, C, H, W)`- Output: :math:`(B, C, H, W)`+ Raises:+ BaseError: if an entry of ``kernel_size`` is even or not positive; raised from the constructor.+Examples:>>> input = torch.rand(2, 4, 5, 5)>>> blur = BilateralBlur((3, 3), 0.1, (1.5, 1.5))@@ -274,7 +285,7 @@ class JointBilateralBlur(_BilateralBlur):a guidance image. See :class:`BilateralBlur` for more information.Arguments:- kernel_size: the size of the kernel.+ kernel_size: the size of the kernel. Each entry must be a positive odd integer.sigma_color: the standard deviation for intensity/color Gaussian kernel.Smaller values preserve more edges.sigma_space: the standard deviation for spatial Gaussian kernel.@@ -293,6 +304,9 @@ class JointBilateralBlur(_BilateralBlur):- Input: :math:`(B, C, H, W)`, :math:`(B, C, H, W)`- Output: :math:`(B, C, H, W)`+ Raises:+ BaseError: if an entry of ``kernel_size`` is even or not positive; raised from the constructor.+Examples:>>> input = torch.rand(2, 4, 5, 5)>>> guidance = torch.rand(2, 4, 5, 5)diff --git a/tests/filters/test_bilateral.py b/tests/filters/test_bilateral.pyindex 66c40f303..0f78fc87c 100644--- a/tests/filters/test_bilateral.py+++ b/tests/filters/test_bilateral.py@@ -64,6 +64,23 @@ class TestBilateralBlur(BaseTester):bilateral_blur(torch.rand(1, 1, 5, 5), 3, 0.1, (1, 1), color_distance_type="l3")assert "color_distance_type only accepts l1 or l2" in str(errinfo)+ @pytest.mark.parametrize("kernel_size", [4, (3, 4), (4, 3), 0, -1])+ def test_exception_kernel_size(self, kernel_size):+ # The window is centred on the pixel, so every entry must be a positive odd integer. The check runs before any+ # padding, and the module constructors run it too.+ from kornia.core.exceptions import BaseError++ image = torch.rand(1, 1, 8, 9)+ calls = (+ lambda: bilateral_blur(image, kernel_size, 0.1, (1.0, 1.0)),+ lambda: joint_bilateral_blur(image, image, kernel_size, 0.1, (1.0, 1.0)),+ lambda: BilateralBlur(kernel_size, 0.1, (1.0, 1.0)),+ lambda: JointBilateralBlur(kernel_size, 0.1, (1.0, 1.0)),+ )+ for call in calls:+ with pytest.raises(BaseError, match="Kernel size must be an odd integer bigger than 0"):+ call()+def test_noncontiguous(self, device, dtype):batch_size = 3inp = torch.rand(3, 5, 5, device=device, dtype=dtype).expand(batch_size, -1, -1, -1)