Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(feature): return pretrained local feature pipelines and LightGlueMatcher in eval mode (#6142) (#6221)

Dmytro Mishkin5304411260f8
diff --git a/changelog.d/6221.fixed.md b/changelog.d/6221.fixed.md
new file mode 100644
index 000000000..164f3f94d
--- /dev/null
+++ b/changelog.d/6221.fixed.md
@@ -0,0 +1 @@
+`GFTTAffNetHardNet`, `HesAffNetHardNet`, `DoGHardNet`, `DoGAffNetHardNet`, `KeyNetHardNet`, `KeyNetAffNetHardNet` and `LightGlueMatcher` now return in evaluation mode (`training=False`), matching `LocalFeatureMatcher` and the pretrained-model convention; outputs are unchanged because their pretrained networks were already in evaluation mode.
diff --git a/docs/source/get-started/conventions.rst b/docs/source/get-started/conventions.rst
index d8390c7bf..efed94e88 100644
--- a/docs/source/get-started/conventions.rst
+++ b/docs/source/get-started/conventions.rst
@@ -1288,11 +1288,10 @@ Pretrained models
- A model built with complete pretrained weights comes back in evaluation mode (``model.training`` is ``False``).
A randomly initialised model stays in PyTorch's default training mode. So does a model whose weights are only partly
pretrained, such as a pretrained backbone with a newly initialised classification head. Call ``.train()`` to
- fine-tune a pretrained model, and ``.eval()`` before running inference on a random one. The ``kornia.feature``
- pipelines are the exception: ``KeyNetDetector``, ``KeyNetHardNet``, ``KeyNetAffNetHardNet``, ``GFTTAffNetHardNet``,
- ``LAFDescriptor`` and ``LightGlueMatcher`` put only their pretrained networks in evaluation mode and report
- ``training`` as ``True`` themselves, so that an ``ori_module`` or ``aff_module`` you pass to ``KeyNetDetector`` keeps
- its own mode (see :ref:`local-features-conventions`).
+ fine-tune a pretrained model, and ``.eval()`` before running inference on a random one. ``KeyNetDetector`` and
+ ``LAFDescriptor`` are the exceptions: they put only their default pretrained ``KeyNet`` and ``HardNet`` networks in
+ evaluation mode and report ``training`` as ``True`` themselves, so that an ``ori_module``, ``aff_module`` or
+ ``patch_descriptor_module`` you pass keeps its own mode (see :ref:`local-features-conventions`).
- Constructors, ``from_config``, ``from_pretrained`` and the builders set the mode once, when they build the model.
``load_checkpoint`` and ``load_state_dict`` on an existing module leave its mode as it is: after loading your own
weights, call ``.eval()``.
diff --git a/kornia/feature/integrated.py b/kornia/feature/integrated.py
index 66f728f3d..772653e5a 100644
--- a/kornia/feature/integrated.py
+++ b/kornia/feature/integrated.py
@@ -560,6 +560,7 @@ class GFTTAffNetHardNet(LocalFeature):
).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor)
+ self.eval()
class HesAffNetHardNet(LocalFeature):
@@ -596,6 +597,7 @@ class HesAffNetHardNet(LocalFeature):
).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor)
+ self.eval()
def _dog_detector(
@@ -655,6 +657,7 @@ class DoGHardNet(LocalFeature):
detector = _dog_detector(num_features, upright, None, compile_modules).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor, scaling_coef=2.0)
+ self.eval()
class DoGAffNetHardNet(LocalFeature):
@@ -688,6 +691,7 @@ class DoGAffNetHardNet(LocalFeature):
detector = _dog_detector(num_features, upright, aff_module, compile_modules).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor, scaling_coef=2.0)
+ self.eval()
class KeyNetHardNet(LocalFeature):
@@ -722,6 +726,7 @@ class KeyNetHardNet(LocalFeature):
).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor, scale_laf)
+ self.eval()
class KeyNetAffNetHardNet(LocalFeature):
@@ -756,6 +761,7 @@ class KeyNetAffNetHardNet(LocalFeature):
).to(device)
descriptor = LAFDescriptor(None, patch_size=32, grayscale_descriptor=True).to(device)
super().__init__(detector, descriptor, scale_laf)
+ self.eval()
class LocalFeatureMatcher(nn.Module):
@@ -1021,6 +1027,8 @@ class LightGlueMatcher(GeometryAwareDescriptorMatcher):
params = {}
self.params = params
self.matcher = LightGlue(self.feature_name, **params)
+ if not self.matcher.training:
+ self.eval()
def forward(
self,
diff --git a/tests/feature/test_pretrained_mode.py b/tests/feature/test_pretrained_mode.py
index 94525e155..d1fbf0b7a 100644
--- a/tests/feature/test_pretrained_mode.py
+++ b/tests/feature/test_pretrained_mode.py
@@ -35,15 +35,21 @@ from kornia.feature import (
SOLD2,
DeDoDe,
DeFMO,
+ DoGAffNetHardNet,
+ DoGHardNet,
+ GFTTAffNetHardNet,
HardNet,
HardNet8,
+ HesAffNetHardNet,
HyNet,
KeyNet,
KeyNetAffNetHardNet,
KeyNetDetector,
+ KeyNetHardNet,
LAFAffNetShapeEstimator,
LAFDescriptor,
LightGlue,
+ LightGlueMatcher,
LoFTR,
OriNet,
SOLD2_detector,
@@ -89,6 +95,13 @@ _WEIGHTED = [
pytest.param(lambda: LoFTR("outdoor"), id="LoFTR"),
pytest.param(XFeat.from_pretrained, id="XFeat"),
pytest.param(lambda: ALIKED.from_pretrained("aliked-n16"), id="ALIKED"),
+ pytest.param(lambda: GFTTAffNetHardNet(num_features=8), id="GFTTAffNetHardNet"),
+ pytest.param(lambda: HesAffNetHardNet(num_features=8), id="HesAffNetHardNet"),
+ pytest.param(lambda: DoGHardNet(num_features=8), id="DoGHardNet"),
+ pytest.param(lambda: DoGAffNetHardNet(num_features=8), id="DoGAffNetHardNet"),
+ pytest.param(lambda: KeyNetHardNet(num_features=8), id="KeyNetHardNet"),
+ pytest.param(lambda: KeyNetAffNetHardNet(num_features=8), id="KeyNetAffNetHardNet"),
+ pytest.param(LightGlueMatcher, id="LightGlueMatcher"),
pytest.param(DISK.from_pretrained, id="DISK", marks=pytest.mark.slow),
# The pair of the DeDoDe.from_pretrained doctest, which CI prefetches; the default G descriptor is 2.2 GB.
pytest.param(
@@ -185,18 +198,44 @@ class TestPretrainedMode(BaseTester):
assert {module.training for module in model.modules()} == {expected_training}
for name, value in weights.items():
self.assert_close(model.state_dict()[name], value)
+ if source == "url":
+ wrapper = LightGlueMatcher("superpoint", params=config)
+ assert wrapper.training is expected_training
+ assert {module.training for module in wrapper.modules()} == {expected_training}
+ for name, value in weights.items():
+ self.assert_close(wrapper.matcher.state_dict()[name], value)
- @pytest.mark.slow
def test_composites_build_pretrained_eval_modules(self):
- # The containers (LocalFeature, KeyNetDetector, LAFOrienter, LAFDescriptor) never set their own mode; the
- # learned networks they hold are built with their weights and come back in evaluation mode.
+ # Self-contained pipelines (KeyNetAffNetHardNet and the other presets in _WEIGHTED) return in evaluation mode
+ # at the root and across all submodules. KeyNetDetector and LAFDescriptor take caller modules, so they put only
+ # their default pretrained KeyNet / HardNet in evaluation mode, report training=True at the root, and preserve
+ # the mode of any module passed in.
learned = (HardNet, OriNet, LAFAffNetShapeEstimator, KeyNet)
composite = KeyNetAffNetHardNet(num_features=8)
networks = [m for m in composite.modules() if isinstance(m, learned)]
assert {type(m) for m in networks} == set(learned)
- assert not any(m.training for net in networks for m in net.modules())
+ assert not composite.training
+ assert not any(m.training for m in composite.modules())
+
+ detector = KeyNetDetector(num_features=8)
+ assert detector.training is True
+ assert not detector.model.training
+ assert not any(m.training for m in detector.model.modules())
+ for training in (True, False):
+ ori = nn.Identity().train(training)
+ aff = nn.Identity().train(not training)
+ det = KeyNetDetector(num_features=8, ori_module=ori, aff_module=aff)
+ assert det.training is True
+ assert det.ori.training is training
+ assert det.aff.training is (not training)
descriptor = LAFDescriptor(None)
+ assert descriptor.training is True
networks = [m for m in descriptor.modules() if isinstance(m, learned)]
assert [type(m) for m in networks] == [HardNet]
assert not any(m.training for m in networks[0].modules())
+ for training in (True, False):
+ patch_desc = nn.Identity().train(training)
+ wrapped = LAFDescriptor(patch_desc)
+ assert wrapped.training is True
+ assert wrapped.descriptor.training is training