π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): handle empty batch (B=0) in median_blur
hackbert35808509a3c5
diff --git a/kornia/filters/median.py b/kornia/filters/median.pyindex 97d59e6f5..ee26c7bce 100644--- a/kornia/filters/median.py+++ b/kornia/filters/median.py@@ -57,6 +57,7 @@ def median_blur(input: torch.Tensor, kernel_size: tuple[int, int] | int) -> torcKORNIA_CHECK_IS_TENSOR(input)KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])+ ky, kx = _unpack_2d_ks(kernel_size)padding = _compute_zero_padding(kernel_size)# prepare kernel@@ -65,7 +66,7 @@ def median_blur(input: torch.Tensor, kernel_size: tuple[int, int] | int) -> torc# map the local window to single vectorfeatures: torch.Tensor = F.conv2d(input.reshape(b * c, 1, h, w), kernel, padding=padding, stride=1)- features = features.view(b, c, -1, h, w) # BxCx(K_h * K_w)xHxW+ features = features.view(b, c, ky * kx, h, w) # BxCx(K_h * K_w)xHxW# compute the median along the feature axisreturn features.median(dim=2)[0]diff --git a/tests/filters/test_median.py b/tests/filters/test_median.pyindex 5cd03727e..d02c0bc92 100644--- a/tests/filters/test_median.py+++ b/tests/filters/test_median.py@@ -29,7 +29,7 @@ class TestMedianBlur(BaseTester):actual = median_blur(inp, 3)assert isinstance(actual, torch.Tensor)- @pytest.mark.parametrize("batch_size", [1, 2])+ @pytest.mark.parametrize("batch_size", [0, 1, 2])@pytest.mark.parametrize("kernel_size", [3, (5, 7)])def test_cardinality(self, batch_size, kernel_size, device, dtype):inp = torch.zeros(batch_size, 3, 4, 4, device=device, dtype=dtype)