Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): reject even edge dilation kernels (#5268)

Karnajeet Gosavifb6c4f7459a6
diff --git a/changelog.d/5268.fixed.md b/changelog.d/5268.fixed.md
new file mode 100644
index 000000000..6c2bb66a7
--- /dev/null
+++ b/changelog.d/5268.fixed.md
@@ -0,0 +1 @@
+Edge-aware blur pooling now rejects even edge-dilation kernel sizes with a clear validation error.
diff --git a/kornia/filters/blur_pool.py b/kornia/filters/blur_pool.py
index 4768fb78e..337bc5200 100644
--- a/kornia/filters/blur_pool.py
+++ b/kornia/filters/blur_pool.py
@@ -17,13 +17,15 @@
from __future__ import annotations
+import operator
+
import torch
import torch.nn.functional as F
from torch import nn
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE
-from .kernels import get_pascal_kernel_2d
+from .kernels import _check_kernel_size, get_pascal_kernel_2d
__all__ = [
"BlurPool2D",
@@ -175,13 +177,16 @@ class EdgeAwareBlurPool2D(nn.Module):
Args:
kernel_size: The size of the Gaussian blur kernel.
edge_threshold: The threshold for detecting edges. Default: 1.25.
- edge_dilation_kernel_size: The kernel size for dilating the edge map. Default: 3.
+ edge_dilation_kernel_size: The kernel size for dilating the edge map. It must be an odd positive integer.
+ Default: 3.
"""
def __init__(
self, kernel_size: tuple[int, int] | int, edge_threshold: float = 1.25, edge_dilation_kernel_size: int = 3
) -> None:
super().__init__()
+ edge_dilation_kernel_size = operator.index(edge_dilation_kernel_size)
+ _check_kernel_size(edge_dilation_kernel_size)
self.kernel_size = kernel_size
self.edge_threshold = edge_threshold
self.edge_dilation_kernel_size = edge_dilation_kernel_size
@@ -345,7 +350,7 @@ def edge_aware_blur_pool2d(
input: the input image to blur with shape :math:`(B, C, H, W)`.
kernel_size: the kernel size for max pooling.
edge_threshold: positive threshold for the edge decision rule; edge/non-edge.
- edge_dilation_kernel_size: the kernel size for dilating the edges.
+ edge_dilation_kernel_size: the kernel size for dilating the edges. It must be an odd positive integer.
epsilon: for numerical stability.
Returns:
@@ -353,6 +358,8 @@ def edge_aware_blur_pool2d(
"""
KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])
+ edge_dilation_kernel_size = operator.index(edge_dilation_kernel_size)
+ _check_kernel_size(edge_dilation_kernel_size)
KORNIA_CHECK(edge_threshold > 0.0, f"edge threshold should be positive, but got '{edge_threshold}'")
input = F.pad(input, (2, 2, 2, 2), mode="reflect") # F.pad to avoid artifacts near physical edges
diff --git a/tests/filters/test_blur_pool.py b/tests/filters/test_blur_pool.py
index 9d1e5e054..14539c0e9 100644
--- a/tests/filters/test_blur_pool.py
+++ b/tests/filters/test_blur_pool.py
@@ -17,9 +17,11 @@
import math
+import numpy as np
import pytest
import torch
+from kornia.core.exceptions import BaseError
from kornia.filters import (
BlurPool2D,
EdgeAwareBlurPool2D,
@@ -211,6 +213,22 @@ class TestBlurPool(BaseTester):
class TestEdgeAwareBlurPool(BaseTester):
+ @pytest.mark.parametrize("edge_dilation_kernel_size", [0, -1, 2, 4])
+ def test_exception_edge_dilation_kernel_size(self, edge_dilation_kernel_size, device, dtype):
+ data = torch.rand(1, 1, 8, 8, device=device, dtype=dtype)
+ with pytest.raises(BaseError, match="Kernel size must be an odd integer"):
+ edge_aware_blur_pool2d(data, 3, edge_dilation_kernel_size=edge_dilation_kernel_size)
+ with pytest.raises(BaseError, match="Kernel size must be an odd integer"):
+ EdgeAwareBlurPool2D(3, edge_dilation_kernel_size=edge_dilation_kernel_size)
+
+ @pytest.mark.parametrize("make_size", [np.int64, torch.tensor], ids=["numpy_int", "zero_dim_tensor"])
+ def test_edge_dilation_kernel_size_accepts_integer_like(self, make_size, device, dtype):
+ # integer-like sizes (numpy ints, 0-d integer tensors) are accepted like Python ints
+ data = torch.rand(1, 1, 8, 8, device=device, dtype=dtype) + 0.1
+ expected = edge_aware_blur_pool2d(data, 3, edge_dilation_kernel_size=3)
+ self.assert_close(edge_aware_blur_pool2d(data, 3, edge_dilation_kernel_size=make_size(3)), expected)
+ self.assert_close(EdgeAwareBlurPool2D(3, edge_dilation_kernel_size=make_size(3))(data), expected)
+
@pytest.mark.parametrize("kernel_size", [3, (5, 5)])
@pytest.mark.parametrize("batch_size", [1, 2])
@pytest.mark.parametrize("edge_threshold", [1.25, 2.5])