Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

5.4 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 SOSNet

from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available


class TestSOSNet(BaseTester):
    def test_shape(self, device):
        inp = torch.ones(1, 1, 32, 32, device=device)
        sosnet = SOSNet(pretrained=False).to(device)
        sosnet.eval()  # batchnorm with size 1 is not allowed in train mode
        out = sosnet(inp)
        assert out.shape == (1, 128)

    def test_empty_batch(self, device, dtype):
        # A batch of zero patches returns an empty descriptor batch, in the dtype a non-empty call returns.
        model = SOSNet(pretrained=False).to(device, dtype).eval()
        one = model(torch.ones(1, 1, 32, 32, device=device, dtype=dtype))
        out = model(torch.zeros(0, 1, 32, 32, device=device, dtype=dtype))
        assert out.shape == (0, one.shape[1])
        assert out.dtype == one.dtype
        assert out.device == one.device

    def test_shape_batch(self, device):
        inp = torch.ones(16, 1, 32, 32, device=device)
        sosnet = SOSNet(pretrained=False).to(device).eval()
        out = sosnet(inp)
        assert out.shape == (16, 128)

    @pytest.mark.skip("jacobian not well computed")
    def test_gradcheck(self, device):
        patches = torch.rand(2, 1, 32, 32, device=device, dtype=torch.float64)
        sosnet = SOSNet(pretrained=False).to(patches.device, patches.dtype).eval()
        self.gradcheck(sosnet, (patches,), eps=1e-4, atol=1e-4)

    @pytest.mark.parametrize("patch_value", [0.0, 0.5])
    def test_degenerate_patch_gives_finite_descriptors(self, device, dtype, patch_value):
        # `forward`'s `eps` is what keeps `desc_norm`'s division defined for a patch the network maps
        # to exactly zero -- every `Conv2d` here has `bias=False` and every `BatchNorm2d` is
        # `affine=False` with `running_mean == 0`, so a constant patch reaches the normalisation as
        # exactly zero. 1e-10 flushes to 0.0 in float16, so the guard used to be inert and every
        # descriptor came back NaN (kornia#4224). bfloat16 keeps float32's exponent range and never had
        # that failure, but on CPU both half dtypes used to raise out of `avg_pool3d` before reaching
        # the division at all, so both are pinned here.
        patches = torch.full((2, 1, 32, 32), patch_value, device=device, dtype=dtype)
        sosnet = SOSNet(pretrained=False).to(device, dtype).eval()
        descriptors = sosnet(patches)
        assert torch.isfinite(descriptors).all(), (
            f"{int((~torch.isfinite(descriptors)).sum())} non-finite descriptor entries"
        )

    def test_dynamo(self, device, dtype, torch_optimizer, cudnn_tf32_follows_option):
        # The normalisation branches on `dtype` to lift half precision into float32. dtype is static
        # metadata, so dynamo resolves that branch at trace time; what this pins is that the compiled
        # result still matches eager on every dtype -- including the lifted half path, which returns
        # NaN if the branch is dropped. `cudnn_tf32_follows_option` is what makes the float32 leg
        # meaningful on CUDA; see its docstring.
        patches = torch.rand(2, 1, 32, 32, device=device, dtype=dtype)
        model = SOSNet(pretrained=False).to(device, dtype).eval()
        op = torch_optimizer(model)
        self.assert_close(op(patches), model(patches))

    @pytest.mark.skipif(not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
    def test_dynamo_fullgraph(self, device, dtype, cudnn_tf32_follows_option):
        # `export-support.rst` publishes SOSNet as capturable with zero graph breaks. The dtype branch
        # reads static metadata and must not become the thing that breaks the graph.
        patches = torch.rand(2, 1, 32, 32, device=device, dtype=dtype)
        model = SOSNet(pretrained=False).to(device, dtype).eval()
        expected = model(patches)
        torch._dynamo.reset()
        compiled = torch.compile(model, fullgraph=True)
        self.assert_close(compiled(patches), expected)

    def test_jit(self, device, dtype):
        B, C, H, W = 2, 1, 32, 32
        # `torch.ones` drives a randomly initialised SOSNet to exactly zero (a ReLU collapse), which made
        # this comparison vacuous: both descriptors came out as the constant 1/sqrt(128) and the test
        # scripted a *second*, independently initialised model without noticing. `torch.rand` keeps the
        # network's output non-degenerate; scripting the same instance is what makes the two sides
        # comparable at all. See TestHyNet::test_jit for the pattern.
        patches = torch.rand(B, C, H, W, device=device, dtype=dtype)
        model = SOSNet(pretrained=False).to(patches.device, patches.dtype).eval()
        model_jit = torch.jit.script(model)
        self.assert_close(model(patches), model_jit(patches))