Files
wehub-resource-sync 3a2c66702c
Tests on CPU (scheduled) / check-skip (push) Has been cancelled
Tests on CPU (scheduled) / pre-tests (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-ubuntu (float32) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-ubuntu (float64) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.11, float32, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.11, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.11, float64, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.11, float64, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.12, float32, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.12, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.12, float64, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.12, float64, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.13, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-windows (3.13, float64, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-mac (3.11, float32, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-mac (3.11, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-mac (3.12, float32, 2.5.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-mac (3.12, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / tests-cpu-mac (3.13, float32, 2.9.1) (push) Has been cancelled
Tests on CPU (scheduled) / coverage (push) Has been cancelled
Tests on CPU (scheduled) / typing (push) Has been cancelled
Tests on CPU (scheduled) / tutorials (push) Has been cancelled
Tests on CPU (scheduled) / docs (push) Has been cancelled
Lint / TOML Format (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:49:27 +08:00

128 lines
4.6 KiB
Python

# 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
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,
atol=1e-3,
rtol=1e-3,
)
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])
with pytest.raises(ValueError):
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert opening(test, kernel)
with pytest.raises(ValueError):
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
assert opening(tensor, test)
@pytest.mark.jit()
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)