π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(morphology): ignore border_value for non-constant border types (#4758)
Ethan Stonerf84498547338
diff --git a/changelog.d/4758.fixed.md b/changelog.d/4758.fixed.mdnew file mode 100644index 000000000..31479658d--- /dev/null+++ b/changelog.d/4758.fixed.md@@ -0,0 +1 @@+`dilation`, `erosion`, `opening`, `closing`, `gradient`, `top_hat` and `bottom_hat` now ignore `border_value` under `border_type` `reflect`, `replicate` and `circular`, as documented, instead of raising `RuntimeError: Padding mode "reflect" doesn't take in value argument` for any value other than `0.0`.diff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.pyindex c1d1e7b05..40e049ca9 100644--- a/kornia/morphology/morphology.py+++ b/kornia/morphology/morphology.py@@ -217,7 +217,10 @@ def dilation(if border_type == "geodesic":border_value = -max_valborder_type = "constant"- output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)+ if border_type == "constant":+ output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)+ else:+ output = F.pad(tensor, pad_e, mode=border_type)# computationif structuring_element is None:@@ -331,7 +334,10 @@ def erosion(if border_type == "geodesic":border_value = max_valborder_type = "constant"- output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)+ if border_type == "constant":+ output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)+ else:+ output = F.pad(tensor, pad_e, mode=border_type)# computationif structuring_element is None:diff --git a/tests/morphology/test_dilation.py b/tests/morphology/test_dilation.pyindex ebb2f998b..7f7928036 100644--- a/tests/morphology/test_dilation.py+++ b/tests/morphology/test_dilation.py@@ -367,6 +367,31 @@ class TestDilate(BaseTester):assert actual.dtype == tensor.dtypeassert torch.equal(actual, expected)+ @pytest.mark.parametrize(+ ("border_type", "row"),+ [+ ("reflect", [3.0, 4.0, 5.0, 4.0, 3.0]),+ ("replicate", [3.0, 4.0, 5.0, 5.0, 5.0]),+ ("circular", [3.0, 4.0, 5.0, 1.0, 2.0]),+ ],+ )+ def test_border_value_ignored_for_non_constant_border(self, device, dtype, border_type, row):+ # border_value only applies to border_type="constant"; the other modes ignore it+ # instead of forwarding it to F.pad, which rejects a value for them (#4748).+ if border_type == "reflect" and not supports_reflect_padding(device, dtype):+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")+ if border_type == "replicate" and not supports_replicate_padding(device, dtype):+ pytest.skip("replication_pad2d is unavailable for this device/dtype")+ # The single-cell kernel at the default origin [0, 2] makes dilation read x(p + 2), so the two+ # rightmost outputs are pure padding. Were border_value used as a fill they would be -7; were the+ # mode dropped they would be 0. Integral values are exact in every dtype.+ ramp = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0]], device=device, dtype=dtype)[None, None]+ kernel = torch.tensor([[1.0, 0.0, 0.0, 0.0, 0.0]], device=device, dtype=dtype)+ expected = torch.tensor([row], device=device, dtype=dtype)[None, None]++ assert torch.equal(dilation(ramp, kernel, border_type=border_type, border_value=-7.0), expected)+ assert torch.equal(dilation(ramp, kernel, border_type=border_type), expected)+def test_shift_engine_mixed_dtype_non_flat(self, device):tensor = torch.zeros(1, 1, 2, 2, device=device, dtype=torch.float16)kernel = torch.ones(2, 2, device=device, dtype=torch.float32)diff --git a/tests/morphology/test_erosion.py b/tests/morphology/test_erosion.pyindex 9acc5e4d7..9c44b2c91 100644--- a/tests/morphology/test_erosion.py+++ b/tests/morphology/test_erosion.py@@ -354,6 +354,31 @@ class TestErode(BaseTester):assert actual.dtype == tensor.dtypeassert torch.equal(actual, expected)+ @pytest.mark.parametrize(+ ("border_type", "row"),+ [+ ("reflect", [3.0, 2.0, 1.0, 2.0, 3.0]),+ ("replicate", [1.0, 1.0, 1.0, 2.0, 3.0]),+ ("circular", [4.0, 5.0, 1.0, 2.0, 3.0]),+ ],+ )+ def test_border_value_ignored_for_non_constant_border(self, device, dtype, border_type, row):+ # border_value only applies to border_type="constant"; the other modes ignore it+ # instead of forwarding it to F.pad, which rejects a value for them (#4748).+ if border_type == "reflect" and not supports_reflect_padding(device, dtype):+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")+ if border_type == "replicate" and not supports_replicate_padding(device, dtype):+ pytest.skip("replication_pad2d is unavailable for this device/dtype")+ # The single-cell kernel at the default origin [0, 2] makes erosion read x(p - 2), so the two+ # leftmost outputs are pure padding. Were border_value used as a fill they would be -7; were the+ # mode dropped they would be 0. Integral values are exact in every dtype.+ ramp = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0]], device=device, dtype=dtype)[None, None]+ kernel = torch.tensor([[1.0, 0.0, 0.0, 0.0, 0.0]], device=device, dtype=dtype)+ expected = torch.tensor([row], device=device, dtype=dtype)[None, None]++ assert torch.equal(erosion(ramp, kernel, border_type=border_type, border_value=-7.0), expected)+ assert torch.equal(erosion(ramp, kernel, border_type=border_type), expected)+def test_shift_engine_mixed_dtype_non_flat(self, device):tensor = torch.zeros(1, 1, 2, 2, device=device, dtype=torch.float16)kernel = torch.ones(2, 2, device=device, dtype=torch.float32)