Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

9.8 KB
Raw Blame
# 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 pytest
import torch

from kornia.morphology import opening

from testing.base import BaseTester, assert_close, supports_replicate_padding
from testing.parametrized_tester import parametrized_test


@parametrized_test(
    smoke_inputs=lambda device, dtype: (
        torch.rand(1, 3, 4, 4, device=device, dtype=dtype),
        torch.ones((3, 3), device=device, dtype=dtype),
    ),
    cardinality_tests=[
        {
            "inputs": lambda device, dtype: (
                torch.ones((1, 3, 4, 4), device=device, dtype=dtype),
                torch.ones((3, 3), device=device, dtype=dtype),
            ),
            "expected_shape": torch.Size([1, 3, 4, 4]),
        },
        {
            "inputs": lambda device, dtype: (
                torch.ones((2, 3, 2, 4), device=device, dtype=dtype),
                torch.ones((3, 3), device=device, dtype=dtype),
            ),
            "expected_shape": torch.Size([2, 3, 2, 4]),
        },
        {
            "inputs": lambda device, dtype: (
                torch.ones((3, 3, 4, 1), device=device, dtype=dtype),
                torch.ones((3, 3), device=device, dtype=dtype),
            ),
            "expected_shape": torch.Size([3, 3, 4, 1]),
        },
        {
            "inputs": lambda device, dtype: (
                torch.ones((3, 2, 5, 5), device=device, dtype=dtype),
                torch.ones((3, 3), device=device, dtype=dtype),
            ),
            "expected_shape": torch.Size([3, 2, 5, 5]),
        },
    ],
    gradcheck_inputs=lambda device: (
        torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64),
        torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64),
    ),
)
class TestOpening(BaseTester):
    def setup_method(self) -> None:
        self.func = opening

    def test_kernel(self, device, dtype):
        tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
            None, None, :, :
        ]
        kernel = 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 = torch.tensor([[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]], device=device, dtype=dtype)[
            None, None, :, :
        ]
        assert_close(opening(tensor, kernel), expected, atol=1e-4, rtol=1e-4)

    def test_structural_element(self, device, dtype):
        tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
            None, None, :, :
        ]
        structural_element = torch.tensor(
            [[-1.0, 0.0, -10.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
        )
        expected = torch.tensor([[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]], device=device, dtype=dtype)[
            None, None, :, :
        ]
        assert_close(
            opening(tensor, torch.ones_like(structural_element), structuring_element=structural_element),
            expected,
        )

    def test_exception(self, device, dtype):
        tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
        kernel = torch.ones(3, 3, device=device, dtype=dtype)

        with pytest.raises(TypeError):
            assert opening([0.0], kernel)

        with pytest.raises(TypeError):
            assert opening(tensor, [0.0])

        test = torch.ones(2, 3, 4, device=device, dtype=dtype)
        with pytest.raises(ValueError):
            assert opening(test, kernel)

        test = torch.ones(2, 3, 4, device=device, dtype=dtype)
        with pytest.raises(ValueError):
            assert opening(tensor, test)

    def test_jit(self, device, dtype):
        op = opening
        op_script = torch.jit.script(op)

        tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)
        kernel = torch.ones(3, 3, device=device, dtype=dtype)

        actual = op_script(tensor, kernel)
        expected = op(tensor, kernel)

        assert_close(actual, expected)

    def test_opening_custom_origin_is_anti_extensive_and_idempotent(self, device, dtype):
        # opening = dilation(erosion(x)) must stay anti-extensive (opening(x) <= x) and
        # idempotent (opening(opening(x)) == opening(x)) under a custom origin too, not just the
        # default centred one. `dilation`'s origin bug broke both for origin=[0, 0]. `block`
        # already equals its own opening under `ones(3, 3)`, so the equality check also exercises
        # idempotency; 0/1 fixtures compare exactly with `torch.equal`, the `<=` on the rand
        # fixture never needs a tolerance (selection only, no interpolation), and repeating
        # `opening` on its own (already-open) output is likewise exact.
        # Generated with:
        #   block = torch.zeros(1, 1, 7, 10); block[..., 2:5, 3:7] = 1
        #   torch.rand(1, 1, 7, 10, generator=torch.Generator().manual_seed(0))
        # A local `torch.Generator` avoids touching the process-global (and any device) RNG state.
        block = torch.zeros(1, 1, 7, 10, device=device, dtype=dtype)
        block[..., 2:5, 3:7] = 1.0
        kernel = torch.ones(3, 3, device=device, dtype=dtype)

        assert torch.equal(opening(block, kernel, origin=[0, 0]), block)

        tensor = torch.rand(1, 1, 7, 10, generator=torch.Generator().manual_seed(0)).to(device=device, dtype=dtype)
        opened = opening(tensor, kernel, origin=[0, 0])
        assert (opened <= tensor).all()
        assert torch.equal(opening(opened, kernel, origin=[0, 0]), opened)

    def test_convention_opening_is_a_morphological_opening(self, device, dtype):
        # `opening` is `dilation(erosion(x))` with the same kernel in both halves; as only `dilation`
        # reflects, it is anti-extensive and idempotent for an asymmetric kernel too, and leaves a block that
        # is a union of translates of the kernel untouched.
        # Generated with scipy 1.17.1 / scikit-image 0.26.0 / opencv-python-headless 5.0.0 / numpy 2.0.0:
        #   blk = np.zeros((9, 11), np.float32); blk[3:6, 3:7] = 1.0; A = np.array([[0, 1, 1]], bool)
        #   ndi.grey_opening(blk, footprint=A, mode="constant", cval=-np.inf) == blk   -> True
        #   sm.opening(blk, A, mode="ignore") == blk                                   -> True
        #   cv2.morphologyEx(blk, cv2.MORPH_OPEN, A.astype(np.uint8)) == blk           -> False
        asymmetric = torch.tensor([[0.0, 1.0, 1.0]], device=device, dtype=dtype)
        block = torch.zeros(1, 1, 9, 11, device=device, dtype=dtype)
        block[..., 3:6, 3:7] = 1.0
        assert torch.equal(opening(block, asymmetric), block)

        l_kernel = torch.tensor([[0.0, 0.0, 0.0], [0.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
        tensor = torch.rand(1, 1, 7, 10, generator=torch.Generator().manual_seed(0)).to(device=device, dtype=dtype)
        for kernel in (l_kernel, asymmetric):
            opened = opening(tensor, kernel)
            assert (opened <= tensor).all()
            assert torch.equal(opening(opened, kernel), opened)

        # `[[1, 0, 0]]` reads `x(p - 1)` in the erosion half and `y(p + 1)` in the dilation half. Under
        # `replicate` the last column becomes `x(W - 2)`, so the opening is not anti-extensive (idempotence
        # survives); under `circular` the two shifts cancel and the opening is exactly `x`.
        side_kernel = torch.tensor([[1.0, 0.0, 0.0]], device=device, dtype=dtype)
        if supports_replicate_padding(device, dtype):
            bump = torch.tensor([[0.0, 1.0, 0.0]], device=device, dtype=dtype)[None, None]
            replicated = opening(bump, side_kernel, border_type="replicate")
            assert replicated.flatten().tolist() == [0.0, 1.0, 1.0]
            assert not bool((replicated <= bump).all())
            assert torch.equal(opening(replicated, side_kernel, border_type="replicate"), replicated)
        assert torch.equal(opening(tensor, side_kernel, border_type="circular"), tensor)

    @pytest.mark.parametrize("engine", ["unfold", "shift", "convolution"])
    def test_convention_opening_empty_geodesic_window_is_infinite_4734(self, device, dtype, engine):
        # Under `geodesic` the dilation window of `[[1, 0, 0]]` is empty in the last column, which becomes
        # `-inf`; every other column round-trips exactly, so opening is anti-extensive and idempotent on data
        # of either sign, and `[0.5, 0.7]` opens to `[0.5, -inf]` as in scikit-image's `mode="ignore"`. Before
        # #4734 the finite `max_val` sentinel made both miss by up to one ULP of `max_val`.
        side_kernel = torch.tensor([[1.0, 0.0, 0.0]], device=device, dtype=dtype)
        tensor = torch.rand(1, 1, 7, 10, generator=torch.Generator().manual_seed(0), dtype=torch.float64).to(
            device=device, dtype=dtype
        )

        for data in (tensor, -tensor):
            opened = opening(data, side_kernel, engine=engine)
            expected = torch.cat((data[..., :-1], torch.full_like(data[..., :1], -float("inf"))), dim=-1)
            assert torch.equal(opened, expected)
            assert (opened <= data).all()
            assert torch.equal(opening(opened, side_kernel, engine=engine), opened)
        pair = torch.tensor([[0.5, 0.7]], device=device, dtype=dtype)[None, None]
        assert opening(pair, side_kernel, engine=engine).flatten().tolist() == [0.5, float("-inf")]