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
128 lines
4.6 KiB
Python
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 bottom_hat
|
|
|
|
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 TestBottomHat(BaseTester):
|
|
def setup_method(self) -> None:
|
|
self.func = bottom_hat
|
|
|
|
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.2, 0.0, 0.5], [0.0, 0.4, 0.0], [0.3, 0.0, 0.6]], device=device, dtype=dtype)[
|
|
None, None, :, :
|
|
]
|
|
assert_close(bottom_hat(tensor, kernel), expected, atol=1e-3, rtol=1e-3)
|
|
|
|
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, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
|
|
)
|
|
expected = torch.tensor([[0.2, 0.0, 0.5], [0.0, 0.4, 0.0], [0.3, 0.0, 0.6]], device=device, dtype=dtype)[
|
|
None, None, :, :
|
|
]
|
|
assert_close(
|
|
bottom_hat(tensor, torch.ones_like(structural_element), structuring_element=structural_element),
|
|
expected,
|
|
atol=1e-3,
|
|
rtol=1e-3,
|
|
)
|
|
|
|
def test_exception(self, device, dtype):
|
|
sample = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
|
|
kernel = torch.ones(3, 3, device=device, dtype=dtype)
|
|
|
|
with pytest.raises(TypeError):
|
|
assert bottom_hat([0.0], kernel)
|
|
|
|
with pytest.raises(TypeError):
|
|
assert bottom_hat(sample, [0.0])
|
|
|
|
with pytest.raises(ValueError):
|
|
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
|
|
assert bottom_hat(test, kernel)
|
|
|
|
with pytest.raises(ValueError):
|
|
test = torch.ones(2, 3, 4, device=device, dtype=dtype)
|
|
assert bottom_hat(sample, test)
|
|
|
|
@pytest.mark.jit()
|
|
def test_jit(self, device, dtype):
|
|
op = bottom_hat
|
|
op_script = torch.jit.script(op)
|
|
|
|
sample = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)
|
|
kernel = torch.ones(3, 3, device=device, dtype=dtype)
|
|
|
|
actual = op_script(sample, kernel)
|
|
expected = op(sample, kernel)
|
|
|
|
assert_close(actual, expected)
|