π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
docs(feature): choose a local-feature implementation per device from measured speed and accuracy (#6452)
Dmytro Mishkina78a5eea3bd3
diff --git a/changelog.d/6452.added.md b/changelog.d/6452.added.mdnew file mode 100644index 000000000..db67d9fc6--- /dev/null+++ b/changelog.d/6452.added.md@@ -0,0 +1 @@+The image matching guide compares the speed and accuracy of kornia's local-feature pipelines and matchers with the authors' code, OpenCV and faiss on CUDA, an x86 CPU and Apple M1 (CPU and MPS). The `kornia.feature` package docstring and the `ALIKED`, `SIFTFeature`, `SIFTFeatureScaleSpace`, `match_snn` and `match_smnn` docstrings carry the same guidance, including where kornia is slower and what to use instead. The top-level `kornia` docstring, which `help(kornia)` shows, says when kornia pays off and when plain PyTorch or torchvision is just as good, and points to the conventions and performance pages.diff --git a/docs/source/_extra/llms-full.txt b/docs/source/_extra/llms-full.txtindex c5b3a6686..c02c0bdac 100644--- a/docs/source/_extra/llms-full.txt+++ b/docs/source/_extra/llms-full.txt@@ -32,7 +32,9 @@ Kornia pays off where a hand-written version gets long or fragile, or where theGPU without blocking it, and a sync-guard test keeps every public augmentation free of avoidable hostsynchronizations.- **Local features on the GPU.** SIFT and HardNet, DISK, ALIKED, XFeat, DeDoDe, LoFTR and LightGlue, with- mutual-nearest-neighbour and ratio-test matchers.+ mutual-nearest-neighbour and ratio-test matchers. They run at their authors' speed or faster, except eager ALIKED+ on CUDA (compile it) and SIFT on a CPU (OpenCV's is faster); what to use on each device is in the+ [image matching guide](https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device).- **Morphology** with arbitrary structuring elements on batched tensors.- **Compiles and exports.** Nearly all probed operator configurations run under `torch.compile` and match eager,and over 1,000 export to ONNX; the result for every operator is on thediff --git a/docs/source/_extra/llms.txt b/docs/source/_extra/llms.txtindex 8a2948e17..2ef681baf 100644--- a/docs/source/_extra/llms.txt+++ b/docs/source/_extra/llms.txt@@ -50,7 +50,7 @@ gradients. For a single fixed-parameter operation, plain PyTorch or torchvision- [kornia.augmentation](https://kornia.readthedocs.io/en/latest/augmentation.html): batched differentiable augmentations with transform tracking and inversion- [kornia.filters](https://kornia.readthedocs.io/en/latest/filters.html): blur, edges (Sobel, Canny), gradients- [kornia.color](https://kornia.readthedocs.io/en/latest/color.html): color space conversions-- [kornia.feature](https://kornia.readthedocs.io/en/latest/feature.html): detectors, descriptors, and matchers (SIFT, HardNet, DISK, LoFTR, LightGlue, ...)+- [kornia.feature](https://kornia.readthedocs.io/en/latest/feature.html): detectors, descriptors, and matchers (SIFT, HardNet, DISK, LoFTR, LightGlue, ...); which implementation to use on each device: [speed and accuracy against the reference implementations](https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device)- [kornia.enhance](https://kornia.readthedocs.io/en/latest/enhance.html): normalization, histogram, contrast/gamma adjustments- [kornia.losses](https://kornia.readthedocs.io/en/latest/losses.html): SSIM, PSNR, Dice, focal, Hausdorff losses- [kornia.metrics](https://kornia.readthedocs.io/en/latest/metrics.html): accuracy, IoU, PSNR/SSIM metricsdiff --git a/docs/source/applications/image_matching.rst b/docs/source/applications/image_matching.rstindex 383a1ea30..b16a77bb3 100644--- a/docs/source/applications/image_matching.rst+++ b/docs/source/applications/image_matching.rst@@ -112,5 +112,167 @@ The table below shows their performance on the `IMC2021 benchmarkRules of thumb: :class:`~kornia.feature.LoFTR` works best for indoor scenes, whereas:class:`~kornia.feature.DISK` + :class:`~kornia.feature.LightGlue` and :class:`~kornia.feature.DeDoDe` + LightGluework best outdoors. For domains far from natural photographs, e.g. remote sensing or medical imaging, SIFT or-SIFT + :class:`~kornia.feature.HardNet` + LightGlue are often the more robust choice. DeDoDe and speed benchmarks-are coming soon.+SIFT + :class:`~kornia.feature.HardNet` + LightGlue are often the more robust choice. The next section compares+their speed and accuracy with the authors' code, device by device.++.. _feature-speed-by-device:++Speed and accuracy against the reference implementations+--------------------------------------------------------++Kornia's local-feature pipelines run at their authors' speed or faster on every device measured, and are as+accurate on every cohort measured. Choose differently in these cases:++- **ALIKED on CUDA: compile it** for repeated inference. Eager, it runs at 0.79Γ the authors' speed: the authors call+ torchvision's deformable-convolution kernel, while kornia, which does not depend on torchvision, uses a PyTorch one+ that launches many small kernels. Compiled, it runs at 1.63Γ at a fixed image size. On CPUs keep it eager: it runs+ at 1.7β2.0Γ the authors' speed, and compiling it made it slower on the x86 CPU.+- **SIFT on a CPU: use OpenCV's**, which is 8β14Γ faster than kornia's on the Apple M1 CPU. Use kornia's SIFT when+ the images are on a GPU, or you need gradients or batches: :class:`~kornia.feature.SIFTFeature` takes one image per+ call, :class:`~kornia.feature.SIFTFeatureScaleSpace` a batch.+- **Ratio-test matching on CUDA or the Apple M1 CPU: use faiss.** Its exact k-nearest-neighbour search returns the+ same matches as :func:`~kornia.feature.match_snn` and :func:`~kornia.feature.match_smnn`, up to 2.2Γ faster on+ CUDA and up to 2.0Γ faster on the M1 CPU. On the x86 CPU, kornia is the fastest.+- **XFeat\* is more accurate on planar scenes, on purpose.** :meth:`~kornia.feature.XFeat.match_xfeat_star` departs+ from the authors' refinement (see :class:`~kornia.feature.XFeat`): 36.09 % against 20.46 % on HPatches, and from+ 2.7 points below to 0.3 above the authors' on PhotoTourism.++A speed figure is the reference's latency divided by kornia's, so a value above 1 means kornia is faster. The+reference is the authors' code, or OpenCV for SIFT. ``~`` marks a noisy figure: on the M1, a relative timing spread+above 10 %; on CUDA, minimum- and median-based ratios more than 3 % apart. ``β`` marks a cell that was not measured.+*Same* accuracy means within 2.5 percentage points, the weight of one Oxford pair, on every cohort measured.++.. list-table:: kornia against the reference implementations: speed ratio by device, and accuracy+ :header-rows: 1+ :widths: 26 10 10 10 18 26++ * - Pipeline (reference)+ - Apple M1 CPU+ - Apple M1 MPS+ - x86 CPU+ - CUDA+ - Accuracy+ * - :class:`~kornia.feature.ALIKED` n16 (authors' Python code)+ - **1.99Γ**+ - 0.99Γ+ - **1.66Γ**+ - ~0.79Γ eager; **1.63Γ** compiled, ~1.23Γ with ``dynamic=True``+ - Same+ * - :class:`~kornia.feature.XFeat` (authors)+ - **1.53Γ**+ - ~1.03Γ+ - **1.64Γ**+ - 0.98Γ+ - Same+ * - XFeat\*, :meth:`~kornia.feature.XFeat.match_xfeat_star` (authors)+ - 1.00Γ+ - ~1.06Γ+ - 0.98Γ+ - ~1.01Γ+ - Better on planar scenes, mixed on PhotoTourism+ * - :class:`~kornia.feature.DISK` (authors)+ - ~1.23Γ+ - 1.00Γ+ - 1.03Γ+ - 1.05Γ+ - Same+ * - DISK + :class:`~kornia.feature.LightGlue` (authors)+ - ~1.10Γ+ - 1.11Γ+ - **1.50Γ**+ - ~0.97Γ+ - Same+ * - :class:`~kornia.feature.LoFTR` (authors)+ - 0.99Γ+ - ~1.06Γ+ - 1.06Γ+ - **1.26Γ**+ - Same+ * - :class:`~kornia.feature.DeDoDe`, B descriptor (authors)+ - 1.01Γ+ - β+ - 0.96Γ+ - ~0.96Γ+ - Same+ * - DeDoDe, G descriptor (authors)+ - 1.09Γ, one pair+ - β+ - **1.16Γ**+ - **1.19Γ**+ - Same+ * - :class:`~kornia.feature.MKDDescriptor`, polar or concatenated (authors)+ - **2.33β2.45Γ**+ - **5.97β6.34Γ**+ - **7.04β8.50Γ**+ - ~1.14β1.30Γ+ - Same+ * - MKDDescriptor, Cartesian (authors)+ - ~1.54Γ+ - **2.93Γ**+ - **3.92Γ**+ - 0.96Γ+ - Same+ * - :class:`~kornia.feature.HyNet` on shared SIFT frames (authors)+ - 1.00Γ+ - **1.35Γ**+ - **1.21Γ**+ - 1.00Γ+ - Same+ * - :class:`~kornia.feature.HardNet`, :class:`~kornia.feature.SOSNet`, :class:`~kornia.feature.TFeat` on+ shared SIFT frames (authors)+ - 0.99β1.02Γ+ - 0.97β1.06Γ+ - **1.29β1.31Γ**+ - 0.99β1.01Γ+ - Same+ * - :func:`~kornia.feature.match_adalam` (authors)+ - 1.00Γ+ - 1.00Γ+ - β+ - β+ - Same+ * - :class:`~kornia.feature.SIFTFeature`, :class:`~kornia.feature.SIFTFeatureScaleSpace` (OpenCV RootSIFT)+ - **0.07β0.13Γ**+ - β+ - β+ - β+ - Same, Oxford only++**Matching.** :func:`~kornia.feature.match_nn`, :func:`~kornia.feature.match_mnn`,+:func:`~kornia.feature.match_snn` and :func:`~kornia.feature.match_smnn` were timed against OpenCV's ``BFMatcher``+and the exact k-nearest-neighbour search of faiss 1.15.1, on RootSIFT descriptors of PhotoTourism images, 1,024 to+8,192 per image. All three return the same matches.++- **x86 CPU**, four or fourteen threads: kornia is the fastest, at 1.4β4.8Γ the speed of OpenCV and 1.15β2.9Γ that+ of faiss.+- **CUDA**: faiss is 1.2β2.2Γ faster for ``match_snn``, and 1.3β1.6Γ for ``match_smnn`` except at 4,096+ descriptors, where the two tie. ``match_nn`` and ``match_mnn`` tie or favour kornia, except ``match_nn`` at 8,192+ descriptors (faiss 1.3Γ). At 2,048 descriptors each matcher takes under 1 ms per pair. OpenCV's pip build has no+ CUDA matcher.+- **Apple M1 CPU**, four threads: kornia runs at 2.1β3.5Γ the speed of OpenCV, and 1.9β2.4Γ that of faiss for+ ``match_nn`` and ``match_mnn``. faiss is 1.6β2.0Γ faster for ``match_snn`` and 1.3β2.0Γ for ``match_smnn``. faiss+ and kornia ran in separate processes, because each ships its own OpenMP runtime; OpenCV ran 3β14 % faster in+ faiss's process, so the faiss figures may favour faiss by that much.++**How these were measured.** Kornia's main branch between 30 October and 2 November 2026 ran against the authors'+code at pinned commits, with PyTorch 2.14.0. Images were resized to 640 pixels on the long side, rounded to+multiples of 32. Inference was float32, with a budget of 2,048 features; LoFTR is dense and uncapped. The ALIKED+reference is the authors' Python code; their compiled sampler extension was not timed.++- **Apple M1, 8 GB**, four CPU threads. A figure compares median timings of the full pipeline on Oxford pairs: both+ extractions, matching, a shared OpenCV MAGSAC estimate and scoring. That shared step pulls the ratios towards 1.+ Accuracy covers Oxford (40 pairs), HPatches (145 viewpoint pairs from 29 sequences) and PhotoTourism (130 pairs of+ the IMC 2021 validation set, from three scenes). The planar cohorts count the pairs whose mean homography corner+ error is below 3 px; PhotoTourism uses pose mAA over 1β10 degrees. Timing DeDoDe on MPS exhausted the M1's memory.+- **Intel i7-14700K and NVIDIA RTX 4090**, four CPU threads. A figure covers the model path on PhotoTourism pairs:+ both extractions, matching, and the transfer of the matches to the host, with no estimator. CPU figures compare+ median timings. CUDA rounds on that host were bimodal, so CUDA figures compare each pair's fastest round. Accuracy+ is pose mAA on the same 130 PhotoTourism pairs, with one matching recipe per pipeline on both sides, which need not+ be the M1 recipe. Compiled ALIKED compiles kornia's model only; the authors' runs eagerly. On CUDA, both DeDoDe+ implementations ran in their default float16 autocast.++A ratio compares two implementations on the same machine and pairs, never two machines, and need not carry over to+other hardware, image sizes or feature budgets. Pipelines whose two sides follow different recipes are left out,+because their gaps come from the recipes: the native KeyNet + HyNet, HesAffNet + HardNet and C4 recipes, and Harris ++HardNet against OpenCV's Harris. The full tables, per-pair spread and protocol are in+`pull request #6452 <https://github.com/kornia/kornia/pull/6452>`_.diff --git a/docs/source/get-started/gpu-acceleration.rst b/docs/source/get-started/gpu-acceleration.rstindex 65ba81708..055dd147e 100644--- a/docs/source/get-started/gpu-acceleration.rst+++ b/docs/source/get-started/gpu-acceleration.rst@@ -36,6 +36,10 @@ comparisons against torchvision, albumentations, OpenCV and PIL on the devices tcover -- CPU and Apple silicon today, with more hardware being added -- and the:doc:`precision page <precision>` documents ``float16``/``bfloat16`` support.+A GPU does not make every port faster than the library it replaces. For local features, the+:ref:`image matching guide <feature-speed-by-device>` compares each of kornia's pipelines with its reference+implementation on CUDA, an x86 CPU and Apple silicon, including where kornia is slower.+Apple silicon (``device="mps"``) is supported as well. PyTorch's MPS backend has no ``float64`` tensors, so keepinputs in ``float32`` (or half precision) there, and be aware that autocast on MPS can change the effectivedtype of an operation.diff --git a/kornia/__init__.py b/kornia/__init__.pyindex c02a3a6fc..f070ba414 100644--- a/kornia/__init__.py+++ b/kornia/__init__.py@@ -17,8 +17,40 @@"""Kornia β Differentiable computer vision and image processing for PyTorch.-This package exposes core modules (filters, geometry, etc.) and provides-convenience imports at the top level.+Kornia's operators work on batched ``torch.Tensor`` inputs, run on the input's device (CPU, CUDA or Apple MPS)+without a NumPy round trip, and most are differentiable. Kornia pays off where a hand-written version gets long or+fragile, or where the alternatives leave the GPU:++- Breadth: over 1,200 public operators, most with no torchvision equivalent. Camera models and epipolar geometry+ (``kornia.geometry``), local features and matching (``kornia.feature``), filters and edges (``kornia.filters``),+ morphology, Lab and Bayer color, image losses and metrics.+- Per-sample parameters in one batched call: a different blur ``sigma``, rotation angle, warp matrix or brightness+ factor for every image. torchvision's functional ops apply one parameter per call.+- Batched minimal solvers and robust estimation: 7- and 8-point fundamental, 5-point essential, DLT homography and+ PnP solvers over whole batches of samples, and a ``RANSAC`` that samples and scores hypotheses on the device.+- Differentiable geometry: depth to 3D and back, camera models with lens distortion, and Lie groups, with gradients+ through depth, pose and camera parameters.+- Annotated augmentation (``kornia.augmentation``): one sampled transform applied to images, masks, boxes and+ keypoints together, and its inverse.+- Local features: kornia's ports of ALIKED, XFeat, DISK with LightGlue, DeDoDe, LoFTR, HardNet and the other learned+ descriptors run at their authors' speed or faster on every device measured (CUDA, x86 and Apple M1 CPUs, Apple+ MPS), and are as accurate on the cohorts measured. The exceptions are eager ALIKED on CUDA (compile it), SIFT on a+ CPU (OpenCV's is faster) and XFeat*, whose refinement departs from the authors' on purpose. The ``kornia.feature``+ docstring says what to use where.+- Compiles and exports: nearly all probed operator configurations run under ``torch.compile`` and match eager, and+ over 1,000 export to ONNX (``kornia.onnx``).+- Fast on batched GPU tensors: on an RTX 4090 with batches of 32, many filters, warps, matching steps and image+ metrics run about 10x to 70x faster in eager mode than per-image OpenCV or scikit-image on the CPU. On the small+ overlap with torchvision, eager kornia is slower on the same GPU, and OpenCV wins single-image ``uint8`` CPU work.++For a single fixed-parameter operation, such as one blur or one resize, a few lines of PyTorch or torchvision are+just as good.++Before writing code, read the conventions page, which covers image tensors, coordinates, rotations, matrices,+``align_corners``, boxes, color, augmentation and local features, and ends with a pitfall checklist:+https://kornia.readthedocs.io/en/latest/get-started/conventions.html. Operator benchmarks, wins and losses, are at+https://kornia.readthedocs.io/en/latest/get-started/performance.html, and the local-feature comparison at+https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device."""from importlib import import_module as _import_modulediff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.pyindex 650a2fcbf..fde37750d 100644--- a/kornia/feature/__init__.py+++ b/kornia/feature/__init__.py@@ -18,6 +18,21 @@"""Kornia Feature β Feature detection, description, and matching for Kornia.This subpackage provides modules for keypoint detection, descriptors, and feature matching.++Choosing an implementation by device. Kornia's local-feature pipelines run at their authors' speed or faster on+CUDA, x86 and Apple M1 CPUs and Apple MPS, and are as accurate on the cohorts measured. Choose differently here:++- :class:`ALIKED` on CUDA: compile it for repeated inference. Eager, it runs at 0.8x the authors' speed; compiled, at+ 1.6x. On CPUs keep it eager, at 1.7-2.0x.+- SIFT on a CPU: use OpenCV's, 8-14x faster than kornia's on the Apple M1 CPU. Kornia's is for images on a GPU,+ gradients or batches; :class:`SIFTFeature` takes one image per call, :class:`SIFTFeatureScaleSpace` a batch.+- :func:`match_snn` and :func:`match_smnn` on CUDA or the Apple M1 CPU: faiss's exact k-nearest-neighbour search+ returns the same matches up to 2.2x faster. On the x86 CPU, kornia is the fastest.+- :meth:`XFeat.match_xfeat_star` departs from the authors' refinement on purpose: much more accurate on planar+ scenes, mixed on PhotoTourism (see :class:`XFeat`).++Figures per device and the protocol (640 px images, 2,048 features, PyTorch 2.14):+https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device"""from .affine_shape import LAFAffineShapeEstimator, LAFAffNetShapeEstimator, PatchAffineShapeEstimatordiff --git a/kornia/feature/aliked/aliked.py b/kornia/feature/aliked/aliked.pyindex 883cfcb6f..28f413365 100644--- a/kornia/feature/aliked/aliked.py+++ b/kornia/feature/aliked/aliked.py@@ -809,6 +809,12 @@ class ALIKED(nn.Module):See :cite:`zhao2023aliked` for details.+ Speed against the authors' Python code (640 px, 2,048 keypoints): on CUDA, compile the model for repeated+ inference. Eager, it runs at 0.8x the authors' speed on an RTX 4090, because its PyTorch deformable convolution+ launches many small kernels; ``torch.compile(model)`` runs at 1.6x (1.2x with ``dynamic=True``). On CPUs keep it+ eager: it runs at 1.7-2.0x the authors' speed, and compiling it made it slower on an x86 CPU. See the `image+ matching guide <https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device>`_.+.. image:: _static/img/ALIKED.pngConvention:diff --git a/kornia/feature/integrated.py b/kornia/feature/integrated.pyindex 5096a2020..b5cf5bccd 100644--- a/kornia/feature/integrated.py+++ b/kornia/feature/integrated.py@@ -358,6 +358,11 @@ class SIFTFeature(_SIFTFeature):For new scale-space SIFT pipelines, start with :class:`SIFTFeatureScaleSpace` and explicitly select``descriptor_backend="pyramid"``; see :ref:`sift-pipelines`.+ For CPU throughput use OpenCV's SIFT: on an Apple M1 CPU, a matching pipeline on it ran 11x faster than one on+ this module (640 px, 2,048 features). This module takes one image per call; for batches use+ :class:`SIFTFeatureScaleSpace`. See the `image matching guide+ <https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device>`_.+Convention:- A :class:`~kornia.feature.LocalFeature`, whose Convention block gives the output contract, of one grayscaleimage per call, ``(1, 1, H, W)``. The detector is a :class:`~kornia.feature.MultiResolutionDetector` on@@ -432,6 +437,11 @@ class SIFTFeatureScaleSpace(LocalFeature):For new scale-space SIFT pipelines, explicitly select ``descriptor_backend="pyramid"``; see :ref:`sift-pipelines`.+ For CPU throughput use OpenCV's SIFT: on an Apple M1 CPU, a matching pipeline on it ran 8x faster than one on this+ module with ``descriptor_backend="pyramid"``, and 14x faster with ``"patch"`` (640 px, 2,048 features). See the+ `image matching guide+ <https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device>`_.+Convention:- A :class:`~kornia.feature.LocalFeature`, whose Convention block gives the output contract, of grayscaleimages ``(B, 1, H, W)``; the pyramid backend converts an RGB image to grayscale first.diff --git a/kornia/feature/matching.py b/kornia/feature/matching.pyindex ef1b41c51..82676d2cb 100644--- a/kornia/feature/matching.py+++ b/kornia/feature/matching.py@@ -467,6 +467,11 @@ def match_snn(The ratio of the first to the second nearest neighbor distance must be <= th.+ Speed, with the same matches, on 1,024 to 8,192 RootSIFT descriptors: on an x86 CPU this is faster than faiss and+ OpenCV's ``BFMatcher``, while faiss's exact k-nearest-neighbour search runs this ratio test up to 2.2x faster on+ CUDA and up to 2.0x faster on an Apple M1 CPU. See the `image matching guide+ <https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device>`_.+Convention:See the Convention block on :func:`match_nn`; ``dists`` is the ratio of the nearest to the second-nearestdistance.@@ -515,6 +520,11 @@ def match_smnn(The ratio of the first to the second nearest neighbor distance must be <= th in both directions.+ Speed, with the same matches, on 1,024 to 8,192 RootSIFT descriptors: on an x86 CPU this is faster than faiss and+ OpenCV's ``BFMatcher``, while faiss's exact k-nearest-neighbour search, run both ways, is up to 1.6x faster on+ CUDA and up to 2.0x faster on an Apple M1 CPU. See the `image matching guide+ <https://kornia.readthedocs.io/en/latest/applications/image_matching.html#feature-speed-by-device>`_.+Convention:- See the Convention block on :func:`match_nn`. A pair is kept when the two descriptors are mutual nearestneighbours and pass the ratio test, ``ratio <= th``, in both directions; ``dists`` is the larger of the