π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
feat(morphology): add morphological reconstruction (#5257)
David Huangal7e1e87f8c390
diff --git a/changelog.d/5257.added.md b/changelog.d/5257.added.mdnew file mode 100644index 000000000..a26131ccc--- /dev/null+++ b/changelog.d/5257.added.md@@ -0,0 +1 @@+`kornia.morphology.reconstruction`: grayscale morphological reconstruction by dilation or erosion, matching `skimage.morphology.reconstruction`.diff --git a/docs/export_support/cases_misc.py b/docs/export_support/cases_misc.pyindex 6e74bdbc8..5889b021a 100644--- a/docs/export_support/cases_misc.py+++ b/docs/export_support/cases_misc.py@@ -1244,7 +1244,7 @@ add(g,None,[],- skip="kornia.morphology exposes only the 7 functions already covered in the first pass",+ skip="kornia.morphology exposes only functions, no module classes",)add("morphology.dilation[3d_kernel_variants]",diff --git a/docs/source/_data/export_support.json b/docs/source/_data/export_support.jsonindex f4f81ec81..dca9a5a48 100644--- a/docs/source/_data/export_support.json+++ b/docs/source/_data/export_support.json@@ -19994,14 +19994,14 @@},{"compile": "n/a",- "compile_detail": "kornia.morphology exposes only the 7 functions already covered in the first pass",+ "compile_detail": "kornia.morphology exposes only functions, no module classes","export": "n/a",- "export_detail": "kornia.morphology exposes only the 7 functions already covered in the first pass",+ "export_detail": "kornia.morphology exposes only functions, no module classes","graph_breaks": 0,"name": "morphology.[no module classes]","note": "","onnx": "n/a",- "onnx_detail": "kornia.morphology exposes only the 7 functions already covered in the first pass",+ "onnx_detail": "kornia.morphology exposes only functions, no module classes","operator": "","package": "kornia.morphology","ref": "",diff --git a/docs/source/morphology.rst b/docs/source/morphology.rstindex 5428757aa..8c12b70bc 100644--- a/docs/source/morphology.rst+++ b/docs/source/morphology.rst@@ -2,7 +2,7 @@ kornia.morphology======================.. meta::- :description: The Kornia.morphology module offers a set of morphological image processing operations such as dilation, erosion, opening, closing, gradient, top hat, and bottom hat. It enables users to apply these transformations to images for advanced computer vision tasks.+ :description: The Kornia.morphology module offers a set of morphological image processing operations such as dilation, erosion, opening, closing, gradient, top hat, bottom hat, and reconstruction. It enables users to apply these transformations to images for advanced computer vision tasks... currentmodule:: kornia.morphology@@ -13,3 +13,4 @@ kornia.morphology.. autofunction:: gradient.. autofunction:: top_hat.. autofunction:: bottom_hat+.. autofunction:: reconstructiondiff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.pyindex a7fe97651..e7c4da772 100644--- a/kornia/morphology/morphology.py+++ b/kornia/morphology/morphology.py@@ -20,7 +20,10 @@ from typing import List, Optionalimport torchimport torch.nn.functional as F-__all__ = ["bottom_hat", "closing", "dilation", "erosion", "gradient", "opening", "top_hat"]+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SAME_SHAPE, KORNIA_CHECK_SHAPE+from kornia.core.exceptions import ValueCheckError++__all__ = ["bottom_hat", "closing", "dilation", "erosion", "gradient", "opening", "reconstruction", "top_hat"]def _validate_morphology_inputs(@@ -1097,3 +1100,127 @@ def bottom_hat()- tensor)+++def _reconstruct_up(+ seed: torch.Tensor,+ mask: torch.Tensor,+ kernel: torch.Tensor,+ num_iters: Optional[int],+ check_every: int,+ engine: str,+) -> torch.Tensor:+ output = torch.minimum(seed, mask)++ if num_iters is not None:+ for _ in range(num_iters):+ output = torch.minimum(dilation(output, kernel, engine=engine), mask)+ return output++ # Each step can only raise a pixel, so the loop has converged once a batch of steps raises none.+ changed = True+ while changed:+ previous = output+ for _ in range(check_every):+ output = torch.minimum(dilation(output, kernel, engine=engine), mask)+ changed = bool((output > previous).any())++ return output+++def reconstruction(+ seed: torch.Tensor,+ mask: torch.Tensor,+ kernel: Optional[torch.Tensor] = None,+ method: str = "dilation",+ num_iters: Optional[int] = None,+ check_every: int = 4,+ engine: str = "auto",+) -> torch.Tensor:+ r"""Return the morphological reconstruction of ``seed`` bounded by ``mask``, applied to each channel.++ Reconstruction by dilation repeats the geodesic dilation :math:`\min(\delta_B(x), \text{mask})`, starting+ from :math:`\min(\text{seed}, \text{mask})`, until the result stops changing. Reconstruction by erosion is+ its dual, :math:`-\text{reconstruction}(-\text{seed}, -\text{mask})`. See L. Vincent, "Morphological+ grayscale reconstruction in image analysis: applications and efficient algorithms", IEEE TIP 1993.+ Under autograd every step keeps its intermediates, so memory grows with the number of steps.++ Convention:+ Matches ``skimage.morphology.reconstruction``: the default ``kernel`` is a :math:`3 \times 3` square,+ its center cell is always part of the neighborhood, and both methods spread a pixel's value to the+ kernel's offsets, as :func:`dilation` does. scikit-image's ``dilation`` spreads the other way, but its+ ``reconstruction`` agrees. The image border is ``geodesic``. Unlike scikit-image, a ``seed`` above+ ``mask`` (below it for ``"erosion"``) is clipped to ``mask`` instead of raising.++ Args:+ seed: Floating-point starting image with shape :math:`(B, C, H, W)`.+ mask: Floating-point image bounding the reconstruction, with the same shape as ``seed``.+ kernel: Offsets from the center that a pixel's value spreads to in one step, with shape+ :math:`(k_h, k_w)` and odd sizes. Non-zero cells mark an offset; their magnitude and dtype are+ ignored. Default: ``None``, which uses a :math:`3 \times 3` square.+ method: ``"dilation"`` (default) or ``"erosion"``.+ num_iters: Number of steps to run. Default: ``None``, which runs until the result stops changing.+ That can take far more steps than :math:`\max(H, W)` when ``mask`` has winding paths. A fixed+ count always runs that many steps, even past convergence. It has no data-dependent exit, so+ ``torch.compile`` captures it as one graph, but the loop is unrolled and compile time grows with the+ count.+ check_every: Number of steps between convergence checks when ``num_iters`` is ``None``. Each check syncs+ the device with the host, and steps past convergence are wasted, so a larger value suits inputs that+ take many steps. For inputs without NaN, the output does not depend on it. Default: ``4``.+ engine: ``"unfold"``, ``"shift"`` or ``"auto"`` (default), passed to :func:`dilation`. ``"convolution"``+ is rejected: it is not exact on every backend, and an inexact step can keep the loop from converging.++ Returns:+ Reconstructed image with shape :math:`(B, C, H, W)` and the promoted dtype of ``seed`` and ``mask``.++ Raises:+ TypeCheckError: if ``seed``, ``mask`` or ``kernel`` is not a tensor.+ ShapeError: if ``seed`` is not 4-dimensional, ``mask`` has another shape, or ``kernel`` is not+ 2-dimensional.+ ValueCheckError: if ``engine`` is ``"convolution"``, also with checks disabled.+ BaseError: if ``seed`` or ``mask`` is not floating point, a ``kernel`` size is even, ``method`` or+ ``engine`` is not one of the values above, ``num_iters`` is negative, or ``check_every`` is not+ positive.++ Example:+ >>> mask = torch.rand(1, 3, 5, 5)+ >>> seed = mask * 0.5+ >>> output = reconstruction(seed, mask)++ """+ KORNIA_CHECK_IS_TENSOR(seed)+ KORNIA_CHECK_IS_TENSOR(mask)+ KORNIA_CHECK_SHAPE(seed, ["B", "C", "H", "W"])+ KORNIA_CHECK_SAME_SHAPE(seed, mask)+ KORNIA_CHECK(seed.is_floating_point(), f"`seed` must have a floating-point dtype. Got {seed.dtype}.")+ KORNIA_CHECK(mask.is_floating_point(), f"`mask` must have a floating-point dtype. Got {mask.dtype}.")+ KORNIA_CHECK(method in ["dilation", "erosion"], f"Unknown `method`: {method}. Expected 'dilation' or 'erosion'.")+ # Not a KORNIA_CHECK, which `disable_checks()`, `KORNIA_CHECKS=0` and `python -O` turn off: an inexact+ # `conv2d` step makes the convergence loop oscillate forever (macOS CPU float32), not return a wrong value.+ if engine == "convolution":+ raise ValueCheckError("Unsupported `engine`: convolution. Expected one of ['auto', 'unfold', 'shift'].")+ KORNIA_CHECK(+ engine in ["auto", "unfold", "shift"],+ f"Unsupported `engine`: {engine}. Expected one of ['auto', 'unfold', 'shift'].",+ )+ KORNIA_CHECK(num_iters is None or num_iters >= 0, f"`num_iters` must be non-negative. Got {num_iters}.")+ KORNIA_CHECK(check_every >= 1, f"`check_every` must be positive. Got {check_every}.")++ if kernel is None:+ kernel = torch.ones(3, 3, device=seed.device, dtype=torch.bool)++ KORNIA_CHECK_IS_TENSOR(kernel)+ KORNIA_CHECK_SHAPE(kernel, ["KH", "KW"])+ se_h, se_w = kernel.shape+ KORNIA_CHECK(se_h % 2 == 1 and se_w % 2 == 1, f"Kernel sizes must be odd. Got {se_h} x {se_w}.")++ # The kernel is only a membership mask, so a bool copy keeps its dtype out of the result. The center cell+ # keeps each pixel in its own neighborhood, as in scikit-image. It also makes every step non-decreasing,+ # which the convergence test relies on.+ kernel = kernel != 0+ kernel[se_h // 2, se_w // 2] = True++ if method == "erosion":+ return -_reconstruct_up(-seed, -mask, kernel, num_iters, check_every, engine)++ return _reconstruct_up(seed, mask, kernel, num_iters, check_every, engine)diff --git a/tests/api_surface.json b/tests/api_surface.jsonindex 4b2d44c6d..3a647c5b2 100644--- a/tests/api_surface.json+++ b/tests/api_surface.json@@ -716,6 +716,7 @@"gradient","morphology","opening",+ "reconstruction","top_hat"],"kornia.utils": [diff --git a/tests/morphology/test_reconstruction.py b/tests/morphology/test_reconstruction.pynew file mode 100644index 000000000..9bccf658c--- /dev/null+++ b/tests/morphology/test_reconstruction.py@@ -0,0 +1,315 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import math++import pytest+import torch++from kornia.core.check import are_checks_enabled, disable_checks, enable_checks+from kornia.core.exceptions import BaseError, ShapeError, TypeCheckError, ValueCheckError+from kornia.morphology import reconstruction++from testing.base import BaseTester+from testing.parametrized_tester import parametrized_test++# Generated with scikit-image 0.26.0 / numpy 2.5.3:+# seed = np.minimum(MASK, 0.3); seed[2, 2] = 0.9+# reconstruction(seed, MASK) -> DILATION_SQUARE+# reconstruction(seed, MASK, footprint=np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])) -> DILATION_CROSS+# eseed = np.maximum(MASK, 0.6); eseed[0, 3] = 0.8+# reconstruction(eseed, MASK, method="erosion") -> EROSION_SQUARE+MASK = [[0.2, 0.9, 0.1, 0.8, 0.3], [0.4, 0.2, 0.2, 0.7, 0.6], [0.1, 0.5, 0.9, 0.1, 0.4], [0.7, 0.3, 0.6, 0.2, 0.9]]+DILATION_SQUARE = [+ [0.2, 0.4, 0.1, 0.7, 0.3],+ [0.4, 0.2, 0.2, 0.7, 0.6],+ [0.1, 0.5, 0.9, 0.1, 0.4],+ [0.5, 0.3, 0.6, 0.2, 0.4],+]+DILATION_CROSS = [+ [0.2, 0.3, 0.1, 0.3, 0.3],+ [0.3, 0.2, 0.2, 0.3, 0.3],+ [0.1, 0.5, 0.9, 0.1, 0.3],+ [0.3, 0.3, 0.6, 0.2, 0.3],+]+EROSION_SQUARE = [+ [0.6, 0.9, 0.6, 0.8, 0.6],+ [0.6, 0.6, 0.6, 0.7, 0.6],+ [0.6, 0.6, 0.9, 0.6, 0.6],+ [0.7, 0.6, 0.6, 0.6, 0.9],+]+++def _serpentine(n, device, dtype):+ # One corridor that zigzags through every other row, so its path length is about n^2 / 2.+ mask = torch.zeros(1, 1, n, n, device=device, dtype=dtype)+ mask[..., ::2, :] = 1.0+ for i, row in enumerate(range(1, n, 2)):+ mask[..., row, n - 1 if i % 2 == 0 else 0] = 1.0+ return mask+++def _distinct(*shape, device):+ # Values at least 1 / numel apart, so gradcheck's finite differences never cross a min/max kink.+ n = math.prod(shape)+ values = torch.randperm(n, generator=torch.Generator().manual_seed(0)).double() / n+ return values.view(shape).to(device)+++@parametrized_test(+ smoke_inputs=lambda device, dtype: (+ torch.rand(1, 3, 4, 4, device=device, dtype=dtype) * 0.5,+ torch.rand(1, 3, 4, 4, device=device, dtype=dtype),+ ),+ cardinality_tests=[+ {+ "inputs": lambda device, dtype: (+ torch.zeros(2, 3, 2, 4, device=device, dtype=dtype),+ torch.ones(2, 3, 2, 4, device=device, dtype=dtype),+ ),+ "expected_shape": torch.Size([2, 3, 2, 4]),+ },+ {+ "inputs": lambda device, dtype: (+ torch.zeros(3, 2, 5, 1, device=device, dtype=dtype),+ torch.ones(3, 2, 5, 1, device=device, dtype=dtype),+ ),+ "expected_shape": torch.Size([3, 2, 5, 1]),+ },+ ],+ gradcheck_inputs=lambda device: tuple(t.clone().requires_grad_() for t in _distinct(2, 2, 2, 5, 5, device=device)),+)+class TestReconstruction(BaseTester):+ def setup_method(self) -> None:+ self.func = reconstruction++ def test_dilation(self, device, dtype):+ mask = torch.tensor(MASK, device=device, dtype=dtype)[None, None]+ seed = mask.clamp(max=0.3)+ seed[..., 2, 2] = mask[..., 2, 2]+ cross = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)++ square_expected = torch.tensor(DILATION_SQUARE, device=device, dtype=dtype)[None, None]+ cross_expected = torch.tensor(DILATION_CROSS, device=device, dtype=dtype)[None, None]++ self.assert_close(reconstruction(seed, mask), square_expected)+ self.assert_close(reconstruction(seed, mask, cross), cross_expected)++ def test_erosion(self, device, dtype):+ mask = torch.tensor(MASK, device=device, dtype=dtype)[None, None]+ seed = mask.clamp(min=0.6)+ seed[..., 0, 3] = mask[..., 0, 3]++ self.assert_close(+ reconstruction(seed, mask, method="erosion"),+ torch.tensor(EROSION_SQUARE, device=device, dtype=dtype)[None, None],+ )++ @pytest.mark.parametrize("engine", ["unfold", "shift"])+ def test_converges_past_image_size(self, device, dtype, engine):+ # A 31 x 31 serpentine needs hundreds of steps, far more than max(H, W).+ mask = _serpentine(31, device, dtype)+ seed = torch.zeros_like(mask)+ seed[..., 0, 0] = 1.0++ assert torch.equal(reconstruction(seed, mask, engine=engine), mask)+ assert not torch.equal(reconstruction(seed, mask, num_iters=31, engine=engine), mask)++ @pytest.mark.timeout(60)+ @pytest.mark.parametrize("engine", ["auto", "unfold", "shift"])+ @pytest.mark.parametrize("method", ["dilation", "erosion"])+ def test_converges_on_random_data(self, device, dtype, engine, method):+ # Non-binary values expose an inexact step, which can oscillate and never converge. A path through a+ # 12 x 12 image has at most 143 steps, so a fixed 144 steps is the converged result.+ mask = torch.rand(2, 3, 12, 12, device=device, dtype=dtype)+ seed = mask * torch.rand_like(mask) if method == "dilation" else mask + torch.rand_like(mask)++ expected = reconstruction(seed, mask, method=method, num_iters=144, engine=engine)+ assert torch.equal(reconstruction(seed, mask, method=method, engine=engine), expected)++ def test_asymmetric_kernel_direction(self, device, dtype):+ # Both methods spread a value to the kernel's offsets, as `dilation` does. Generated with scikit-image 0.26.0:+ # m = np.full((1, 5), 3.0); s = np.zeros((1, 5)); s[0, 2] = 3; e = np.full((1, 5), 9.0); e[0, 2] = 3+ # reconstruction(s, m, footprint=np.array([[0, 1, 1]])) -> [[0, 0, 3, 3, 3]]+ # reconstruction(e, m, method="erosion", footprint=np.array([[0, 1, 1]])) -> [[9, 9, 3, 3, 3]]+ kernel = torch.tensor([[0.0, 1.0, 1.0]], device=device, dtype=dtype)+ mask = torch.full((1, 1, 1, 5), 3.0, device=device, dtype=dtype)+ seed = torch.tensor([0.0, 0.0, 3.0, 0.0, 0.0], device=device, dtype=dtype).view(1, 1, 1, 5)+ eseed = torch.tensor([9.0, 9.0, 3.0, 9.0, 9.0], device=device, dtype=dtype).view(1, 1, 1, 5)++ assert reconstruction(seed, mask, kernel).flatten().tolist() == [0.0, 0.0, 3.0, 3.0, 3.0]+ assert reconstruction(eseed, mask, kernel, method="erosion").flatten().tolist() == [9.0, 9.0, 3.0, 3.0, 3.0]++ def test_center_always_included(self, device, dtype):+ # scikit-image keeps each pixel in its own neighborhood even when the footprint leaves the center out.+ ring = torch.tensor([[1.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0]], device=device, dtype=dtype)+ mask = torch.rand(1, 1, 6, 6, device=device, dtype=dtype)+ seed = mask * 0.5++ assert torch.equal(reconstruction(seed, mask, ring), reconstruction(seed, mask))++ def test_seed_above_mask_is_clipped(self, device, dtype):+ mask = torch.rand(1, 2, 5, 5, device=device, dtype=dtype)+ seed = mask + 1.0++ assert torch.equal(reconstruction(seed, mask), mask)+ assert torch.equal(reconstruction(mask - 1.0, mask, method="erosion"), mask)++ def test_num_iters(self, device, dtype):+ mask = _serpentine(9, device, dtype)+ seed = torch.zeros_like(mask)+ seed[..., 0, 0] = 1.0++ assert torch.equal(reconstruction(seed, mask, num_iters=0), seed)+ assert torch.equal(reconstruction(seed, mask, num_iters=1000), reconstruction(seed, mask))++ def test_num_iters_runs_exactly(self, device, dtype):+ # Each step spreads the value one pixel, so the count shows in how far it got.+ mask = torch.ones(1, 1, 1, 7, device=device, dtype=dtype)+ seed = torch.zeros_like(mask)+ seed[..., 0] = 1.0++ dilated = reconstruction(seed, mask, num_iters=2)+ eroded = reconstruction(1 - seed, 1 - mask, method="erosion", num_iters=2)++ assert dilated.flatten().tolist() == [1, 1, 1, 0, 0, 0, 0]+ assert eroded.flatten().tolist() == [0, 0, 0, 1, 1, 1, 1]++ @pytest.mark.parametrize("check_every", [1, 3, 64])+ def test_check_every(self, device, dtype, check_every):+ mask = _serpentine(9, device, dtype)+ seed = torch.zeros_like(mask)+ seed[..., 0, 0] = 1.0++ assert torch.equal(reconstruction(seed, mask, check_every=check_every), mask)+ assert torch.equal(reconstruction(1 - seed, 1 - mask, method="erosion", check_every=check_every), 1 - mask)++ def test_kernel_zeros_exclude_neighbors(self, device, dtype):+ # A kernel's zero cells are excluded, so the diagonal 5e4 never reaches the unconnected 3e4 pixel.+ # scikit-image leaves that pixel at 0.+ mask = torch.tensor([[5e4, 0.0], [0.0, 3e4]], device=device, dtype=dtype)[None, None]+ seed = torch.zeros_like(mask)+ seed[..., 0, 0] = mask[..., 0, 0]+ cross = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)++ assert torch.equal(reconstruction(seed, mask, cross), seed)++ @pytest.mark.parametrize("kernel_dtype", [torch.bool, torch.uint8, torch.int64, torch.float32])+ def test_kernel_dtype_is_ignored(self, device, dtype, kernel_dtype):+ # The kernel is only a membership mask: its dtype changes neither the result nor the output dtype. A+ # float32 kernel, such as the default dtype of `torch.ones(3, 3)`, must not widen half-precision inputs.+ mask = torch.rand(2, 3, 6, 7, device=device, dtype=dtype)+ seed = mask * 0.5+ cross = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)+ expected = reconstruction(seed, mask, cross)++ actual = reconstruction(seed, mask, cross.to(kernel_dtype))+ assert actual.dtype == dtype+ assert torch.equal(actual, expected)++ def test_gradcheck_erosion(self, device):+ seed, mask = _distinct(2, 2, 2, 5, 5, device=device)++ self.gradcheck(lambda s, m: reconstruction(s, m, method="erosion"), (seed, mask))++ def test_exception(self, device, dtype):+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)++ with pytest.raises(TypeCheckError):+ reconstruction([0.0], tensor)++ with pytest.raises(TypeCheckError):+ reconstruction(tensor, [0.0])++ with pytest.raises(BaseError, match="floating-point"):+ reconstruction(tensor.int(), tensor)++ with pytest.raises(BaseError, match="floating-point"):+ reconstruction(tensor, tensor.int())++ with pytest.raises(ShapeError):+ reconstruction(tensor[0], tensor[0])++ with pytest.raises(ShapeError):+ reconstruction(tensor, tensor[..., :2])++ with pytest.raises(BaseError, match="method"):+ reconstruction(tensor, tensor, method="opening")++ with pytest.raises(BaseError, match="num_iters"):+ reconstruction(tensor, tensor, num_iters=-1)++ with pytest.raises(BaseError, match="check_every"):+ reconstruction(tensor, tensor, check_every=0)++ with pytest.raises(ValueCheckError, match="engine"):+ reconstruction(tensor, tensor, engine="convolution")++ with pytest.raises(BaseError, match="engine"):+ reconstruction(tensor, tensor, num_iters=0, engine="unknown")++ with pytest.raises(TypeCheckError):+ reconstruction(tensor, tensor, [[1.0]])++ with pytest.raises(ShapeError):+ reconstruction(tensor, tensor, torch.ones(3, 3, 3, device=device, dtype=dtype))++ with pytest.raises(BaseError, match="odd"):+ reconstruction(tensor, tensor, torch.ones(2, 3, device=device, dtype=dtype))++ def test_convolution_engine_rejected_with_checks_disabled(self, device, dtype):+ # The rejection guards termination, not input validity: on macOS CPU float32 the inexact `conv2d` step+ # makes the loop oscillate forever. It is not a KORNIA_CHECK, so disable_checks() leaves it on.+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ checks_were_enabled = are_checks_enabled()+ disable_checks()+ try:+ with pytest.raises(ValueCheckError, match="engine"):+ reconstruction(tensor, tensor, engine="convolution")+ finally:+ if checks_were_enabled:+ enable_checks()++ def test_jit(self, device, dtype):+ op_script = torch.jit.script(reconstruction)++ mask = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ seed = mask * 0.5++ self.assert_close(op_script(seed, mask), reconstruction(seed, mask))++ @pytest.mark.parametrize("num_iters", [None, 3])+ def test_dynamo(self, device, dtype, torch_optimizer, num_iters):+ mask = torch.rand(2, 3, 9, 9, device=device, dtype=dtype)+ seed = mask * 0.5+ op_optimized = torch_optimizer(reconstruction)++ self.assert_close(+ reconstruction(seed, mask, num_iters=num_iters), op_optimized(seed, mask, num_iters=num_iters)+ )++ @pytest.mark.parametrize("engine", ["unfold", "shift"])+ @pytest.mark.parametrize("method", ["dilation", "erosion"])+ def test_dynamo_fullgraph_num_iters(self, device, dtype, torch_optimizer, engine, method):+ # A fixed `num_iters` has no data-dependent exit, so the whole call compiles as one graph.+ mask = torch.rand(2, 3, 9, 9, device=device, dtype=dtype)+ seed = mask * 0.5 if method == "dilation" else mask + 0.5++ def op(s, m):+ return reconstruction(s, m, method=method, num_iters=3, engine=engine)++ self.assert_close(torch_optimizer(op, fullgraph=True)(seed, mask), op(seed, mask))