Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix Canny hysteresis for half-precision images (#4393)

SHILIN KONG4e05c6835d0b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 979bb1f46..597011c7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -369,6 +369,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* Canny hysteresis preserves the input dtype, avoiding a convolution dtype mismatch for half-precision images. (#4393)
+
* `rad2deg` and `deg2rad` now handle integer tensor inputs correctly and preserve
float64 precision. `angle_to_rotation_matrix` inherits the corrected conversion,
while the implementation preserves ONNX export compatibility. (#4358)
diff --git a/kornia/filters/canny.py b/kornia/filters/canny.py
index b3befbcb6..facecd745 100644
--- a/kornia/filters/canny.py
+++ b/kornia/filters/canny.py
@@ -144,8 +144,8 @@ def canny(
hysteresis_kernels: torch.Tensor = get_hysteresis_kernel(device, dtype)
while ((edges_old - edges).abs() != 0).any():
- weak: torch.Tensor = (edges == 0.5).float()
- strong: torch.Tensor = (edges == 1).float()
+ weak: torch.Tensor = (edges == 0.5).to(dtype)
+ strong: torch.Tensor = (edges == 1).to(dtype)
hysteresis_magnitude: torch.Tensor = F.conv2d(
edges, hysteresis_kernels, padding=hysteresis_kernels.shape[-1] // 2
diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txt
index 88a2adb40..43df48b69 100644
--- a/testing/half_precision_xfails/cpu_bfloat16.txt
+++ b/testing/half_precision_xfails/cpu_bfloat16.txt
@@ -106,12 +106,9 @@ call builtins.AssertionError tests/contrib/test_hist_match.py::TestHistMatch::te
call builtins.AssertionError tests/core/test_helpers.py::TestSvdCast::test_smoke[cpu-bfloat16]
call builtins.AssertionError tests/enhance/test_jpeg.py::TestDiffJPEG::test_cardinality[cpu-bfloat16]
call builtins.AssertionError tests/enhance/test_zca.py::TestZCA::test_dim_args[cpu-bfloat16-1]
-call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude[cpu-bfloat16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst[cpu-bfloat16]
+call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst[cpu-bfloat16]
call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst_false[cpu-bfloat16]
call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_threshold[cpu-bfloat16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_module[cpu-bfloat16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_noncontiguous[cpu-bfloat16-2]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-bfloat16-same-2]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-bfloat16-same-3]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-bfloat16-same-6]
diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txt
index 1f6fea7d3..0aa7c242e 100644
--- a/testing/half_precision_xfails/cpu_float16.txt
+++ b/testing/half_precision_xfails/cpu_float16.txt
@@ -219,11 +219,9 @@ call builtins.AssertionError tests/color/test_hls.py::TestRgbToHls::test_jit[cpu
call builtins.AssertionError tests/color/test_hls.py::TestRgbToHls::test_module[cpu-float16]
call builtins.AssertionError tests/color/test_rgb.py::TestRgb255Normals::test_back_and_forth[cpu-float16]
call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude[cpu-float16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst[cpu-float16]
+call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst[cpu-float16]
call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_hyst_false[cpu-float16]
call builtins.AssertionError tests/filters/test_canny.py::TestCanny::test_magnitude_threshold[cpu-float16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_module[cpu-float16]
-call builtins.RuntimeError tests/filters/test_canny.py::TestCanny::test_noncontiguous[cpu-float16-2]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-float16-same-2]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-float16-same-3]
call builtins.RuntimeError tests/filters/test_filters.py::TestFilter2D_fftconv::test_cardinality[cpu-float16-same-6]
diff --git a/tests/filters/test_canny.py b/tests/filters/test_canny.py
index 66dc2675f..4240a4b55 100644
--- a/tests/filters/test_canny.py
+++ b/tests/filters/test_canny.py
@@ -49,6 +49,39 @@ class TestCanny(BaseTester):
assert magnitude.shape == (batch_size, 1, 4, 4)
assert edges.shape == (batch_size, 1, 4, 4)
+ @pytest.mark.parametrize("as_module", [False, True])
+ @pytest.mark.parametrize("with_weak_edges", [False, True])
+ def test_hysteresis_preserves_dtype(self, device, dtype, as_module, with_weak_edges):
+ inp = torch.tensor(
+ [
+ [0.5, 0.4, 0.5, 0.45, 0.1],
+ [0.3, 0.2, 0.3, 0.0, 0.3],
+ [0.5, 1.0, 1.0, 0.6, 0.75],
+ [0.2, 0.4, 0.6, 0.0, 0.5],
+ [0.1, 0.35, 0.35, 0.26, 0.1],
+ ],
+ device=device,
+ dtype=dtype,
+ ).view(1, 1, 5, 5)
+ if not with_weak_edges:
+ inp.zero_()
+ _, thresholded = canny(inp, hysteresis=False)
+ if with_weak_edges:
+ # This weak edge is adjacent to a strong one and must survive hysteresis.
+ assert thresholded[0, 0, 2, 0] == 0.5
+ assert thresholded[0, 0, 1, 0] == 1.0
+
+ magnitude, edges = Canny()(inp) if as_module else canny(inp)
+
+ assert magnitude.dtype == edges.dtype == dtype
+ assert magnitude.device == edges.device == inp.device
+ assert magnitude.shape == edges.shape == inp.shape
+ assert ((edges == 0) | (edges == 1)).all()
+ if with_weak_edges:
+ assert edges[0, 0, 2, 0] == 1.0
+ else:
+ assert (edges == 0).all()
+
def test_exception(self, device, dtype):
from kornia.core.exceptions import BaseError, ShapeError, TypeCheckError