Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

5.0 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.feature import KeyNet
from kornia.feature.keynet import keynet_default_config

from testing.base import BaseTester, supports_reflect_padding, supports_replicate_padding


def _skip_without_half_padding(device: torch.device, dtype: torch.dtype) -> None:
    # torch 2.5.1 has no float16 CPU reflection_pad2d or replication_pad2d; these pins blur (reflect) and take
    # gradients (replicate).
    if not (supports_reflect_padding(device, dtype) and supports_replicate_padding(device, dtype)):
        pytest.skip(f"no reflection or replication padding kernel for {dtype} on {device.type}")


class TestKeyNet(BaseTester):
    def test_dynamo(self, device, dtype, torch_optimizer):
        inp = torch.rand(1, 1, 32, 32, device=device, dtype=dtype)
        model = KeyNet(pretrained=False).to(device, dtype).eval()
        self.assert_close(torch_optimizer(model)(inp), model(inp))

    def test_shape(self, device, dtype):
        inp = torch.rand(1, 1, 16, 16, device=device, dtype=dtype)
        keynet = KeyNet(pretrained=False).to(device, dtype).eval()
        out = keynet(inp)
        assert out.shape == inp.shape

    def test_shape_batch(self, device, dtype):
        inp = torch.ones(16, 1, 16, 16, device=device, dtype=dtype)
        keynet = KeyNet(pretrained=False).to(device, dtype).eval()
        out = keynet(inp)
        assert out.shape == inp.shape

    @pytest.mark.parametrize("num_filters", [4, 16])
    def test_non_default_num_filters(self, num_filters, device, dtype):
        # The learnable block used to produce 8 channels per level whatever ``num_filters`` said, while
        # ``last_conv`` expects ``num_filters * num_levels``, so any other value crashed in ``forward`` (#6015).
        keynet_conf = {**keynet_default_config, "num_filters": num_filters}
        keynet = KeyNet(pretrained=False, keynet_conf=keynet_conf).to(device, dtype).eval()
        # As in the reference Key.Net, every learnable block is ``num_filters`` wide, not only the first and last.
        widths = [(m.in_channels, m.out_channels) for m in keynet.modules() if isinstance(m, torch.nn.Conv2d)]
        last_in = num_filters * keynet_default_config["num_levels"]
        assert widths == [(10, num_filters), (num_filters, num_filters), (num_filters, num_filters), (last_in, 1)]
        inp = torch.rand(1, 1, 40, 56, device=device, dtype=dtype)
        out = keynet(inp)
        assert out.shape == inp.shape

    @pytest.mark.device_agnostic
    def test_pretrained_rejects_non_default_num_filters(self, monkeypatch):
        # The checkpoint has 8 filters per level, so a non-default ``num_filters`` must still fail the strict load.
        state_dict = KeyNet(pretrained=False).state_dict()
        monkeypatch.setattr(
            "kornia.feature.keynet.load_state_dict_from_url", lambda *args, **kwargs: {"state_dict": state_dict}
        )
        KeyNet(pretrained=True)
        with pytest.raises(RuntimeError, match="size mismatch"):
            KeyNet(pretrained=True, keynet_conf={**keynet_default_config, "num_filters": 4})

    def test_gradcheck(self, device):
        patches = torch.rand(2, 1, 16, 16, device=device, dtype=torch.float64)
        keynet = KeyNet(pretrained=False).to(patches.device, patches.dtype).eval()
        self.gradcheck(keynet, (patches,), eps=1e-4, atol=1e-4, nondet_tol=1e-8)


class TestConventionsKeyNet(BaseTester):
    def test_convention_keynet_response_is_non_negative_at_the_input_size(self, device, dtype):
        # KeyNet ends in a ReLU and resamples every pyramid level's features, so the response is a non-negative
        # (B, 1, H, W) map at the input size, also for an odd, non-square size. With the random
        # weights of seed 0 and the last bias set to -0.01, the last pre-activation spans [-0.014, 0.013] (CPU float64),
        # so the ReLU clips about a tenth of the map and the bound below is not met by an all-zero map.
        _skip_without_half_padding(device, dtype)
        with torch.random.fork_rng(devices=[]):
            torch.manual_seed(0)
            model = KeyNet(pretrained=False).eval()
        with torch.no_grad():
            model.last_conv[0].bias.fill_(-0.01)
        img = torch.rand(2, 1, 37, 53, generator=torch.Generator().manual_seed(0)).to(device, dtype)
        out = model.to(device, dtype)(img)
        assert out.shape == (2, 1, 37, 53)
        assert bool((out >= 0).all())
        assert bool((out == 0).any())
        assert bool((out > 0).any())