Models · Image retrieval

MegaLoc

MegaLoc (Politecnico di Torino, 2025): one descriptor per photo to find photos of the same place (place recognition, loop closure, image pairs for 3-D reconstruction); DINOv2 ViT-B with optimal-transport pooling. MIT code and weights; some training sets are research-only.

experimental Fork of gmberton/MegaLoc@5fe0dd6 · backends: portable, kernels

Checkpoint megaloc

Paper 2025 Added in 0.2.0 Evaluated with 0.2.0

Kernels, RTX 5060 Ti 16 GB 4.26 ms 480x640, fp16
vs the faster original 2.3× original fp16: 9.68 ms
Peak memory 485 MiB original: 1,347 MiB
Accuracy vs original fp32 ±0.00 kernels r1, St Lucia (100.00 original)

Use it

pip install fastforks

Lean install with only what this model needs: pip install fastforks-core

from fastforks import retrieval

model = retrieval.load("megaloc")  # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.descriptor
result.backend  # what ran: mode, device, dtype, kernels
fastforks bench megaloc

Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.

Speed and memory

Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.

Input: synthetic, 480x640 (the task’s standard size)

BackenddtypeTime per call (ms)vs original fp16Peak memory
allocated / reserved
Load (s)First call (s)
Original fp32fp32not measured ––––
Original fp16torch.autocast(float16) around the call; gmberton/MegaLoc@5fe0dd697c4a megaloc_model.py, 322 x 322 bilinear antialiased resize, normalisationfp16 9.68–1,347 MiB/1,368 MiB3.10.67
Portable fp32fp32not measured ––––
Portablefp16 5.291.8×483 MiB/508 MiB0.50.37
Kernelsfp16 4.262.3×485 MiB/508 MiB1.40.22
Portable + torch.compile 4.272.3×518 MiB/530 MiB13 (build)–
TensorRT fp16fp16 3.213.0×458 MiBenginenot comparable51 (build)–

Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-13. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.

Other devices

DeviceBackenddtypems / callPeak memoryOutput vs referenceInput
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp3258.5 2,092 MiB process–480x640
Portablefp1645.2 2,617 MiB processmatches480x640
Portablefp3253.8 1,926 MiB processmatches480x640
Kernelsfp16not implemented–480x640

CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.

Licence and weights

Weights

megaloc.safetensors 915 MB· hosted on Hugging Face

fastforks downloads and checks these files on first use (fastforks download megaloc).

Accuracy

The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.

St Lucia

all 1464 queries against the 1549 database images, recall@1 / @5 % (25 m)

Rundtyper1 ↑r5 ↑Δ r1 vs fp32 originalqueriesms / querie
Original fp32fp32100.00100.00 reference 1464 14.6
Original fp16not measured
Portable fp32not measured
Portablefp16100.00100.00 ±0.00 1464 5.40
Kernelsfp16100.00100.00 ±0.00 1464 4.35

Every query's descriptor against every database descriptor (cosine similarity, exact top-k on the GPU); a query counts as found at k when one of its k nearest database images is within 25 m (UTM positions of the file names), the standard visual place recognition protocol. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-13; portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 84333525, 2026-11-13.

St Lucia

every 5th query (293) against the 1549 database images, recall@1 / @5 % (25 m)

Rundtyper1 ↑r5 ↑Δ r1 vs fp32 originalqueriesms / querie
Original fp32fp32100.00100.00 reference 293 14.6
Original fp16fp16100.00100.00 ±0.00 293 8.88
Portable fp32fp32100.00100.00 ±0.00 293 14.6
Portablefp16100.00100.00 ±0.00 293 5.55
Kernelsfp16100.00100.00 ±0.00 293 4.49

Every query's descriptor against every database descriptor (cosine similarity, exact top-k on the GPU); a query counts as found at k when one of its k nearest database images is within 25 m (UTM positions of the file names), the standard visual place recognition protocol. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-13; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 84333525, 2026-11-13.

Closeness to the original

Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 8.8e-08 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 2.1 % and p99 <= 6.6 % of the reference's median |value| (absolute error of the output descriptor).

CheckToleranceMeasured
CPU fp32, max |diff|≤ 8.8e-8 (relative)not recorded
GPU default dtypes, mean |diff|≤ 0.021 (relative)0.01356 (relative)
GPU default dtypes, p99 |diff|≤ 0.066 (relative)0.04384 (relative)

Recorded for megaloc on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-13, with these kernels: dpt_fusedfastbb

Details

MegaLoc (Berton and Masone, CVPR 2025 workshops): one image-retrieval model for visual place recognition, landmark retrieval and structure-from-motion pair selection. A DINOv2 ViT-B/14 fine-tuned on a mix of place-recognition, landmark and SfM datasets; its patch features are assigned to 64 clusters by optimal transport (SALAD-style aggregation) and projected to an 8448-d unit descriptor.

Input / output
Input: one RGB image. Output: a global descriptor [D] (unit L2 norm) for image retrieval / visual place recognition; fastforks.retrieval.top_k and pairs find the nearest images (cosine similarity) on the GPU.
Input size
any H x W >= 32, padded to a multiple of 32; tested up to 3000x4000 (889 MiB allocated)
Outputs
descriptor
CUDA kernels
  • fastbbfast backbone kernels: flash attention, window attention, instance norm and rotary embeddings for ViT and similar backbones
  • dpt_fusedfused fp16 matrix multiplies and convolutions for DINOv2 backbones and DPT depth heads
Apple (MPS)
fp32, fp16
Full reference, generated from the repository

MegaLoc (Politecnico di Torino, 2025): one descriptor per photo to find photos of the same place (place recognition, loop closure, image pairs for 3-D reconstruction); DINOv2 ViT-B with optimal-transport pooling. MIT code and weights; some training sets are research-only.

Commercial use: yes.

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
megalocsynthetic (480x640)original
portable
kernels
portable + torch.compile
TensorRT fp16
9.68
5.29
4.26
4.27
3.21
1347 / 1368
483 / 508
485 / 508
518 / 530
937 / 964
Details
  • Original code: https://github.com/gmberton/MegaLoc at 5fe0dd697c4a
  • Code licence: MIT (permissive); files: LICENSE
  • Weights licence: MIT
  • Licence evidence: https://github.com/gmberton/MegaLoc/blob/5fe0dd697c4a70ba3e23607f6716ab3c606b16db/LICENSE ; https://huggingface.co/gberton/MegaLoc/blob/a0f34722c4297ff787e022433799250180860af7/README.md
  • Training data: LVD-142M (DINOv2's curated web-image set, not released), GSV-Cities (Google Street View images of 62k places in 40 cities), Mapillary Street-Level Sequences, MegaScenes (Internet photo collections of scenes from Wikimedia Commons), ScanNet (RGB-D scans of indoor scenes), San Francisco eXtra Large (street-view images of San Francisco) (Fine-tuned from DINOv2 ViT-B/14 (LVD-142M) on GSV-Cities, MSLS, MegaScenes, ScanNet and SF-XL (paper, arXiv 2502.17237, Sec. 2); MSLS and ScanNet carry non-commercial terms. Only the weights licence (MIT) counts for the commercial flag.)
  • Kernel libraries: fastbb, dpt_fused
  • Variants: megaloc
  • Outputs: descriptor
  • Input size: any H x W >= 32, padded to a multiple of 32
  • Tested up to (portable, fp16, RTX 5060 Ti 16 GB): megaloc 3000x4000 (0.9 GiB peak allocated, 0.9 reserved)
  • megaloc 480x640, first call: portable + torch.compile 13 s (cold Inductor cache), TensorRT fp16 51 s (ONNX + engine).
  • megaloc original 480x640: torch.autocast(float16) around the call; gmberton/MegaLoc@5fe0dd697c4a megaloc_model.py, 322 x 322 bilinear antialiased resize, normalisation
  • megaloc load / first call: original 2.49 s / 0.562 s, portable 0.5 s / 0.367 s, kernels 1.36 s / 0.222 s
  • Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5

Measured

Every query's descriptor against every database descriptor (cosine similarity, exact top-k on the GPU); a query counts as found at k when one of its k nearest database images is within 25 m (UTM positions of the file names), the standard visual place recognition protocol. Original = the original code named under Details, same checkpoint and protocol.

variantdatasetoriginal fp32original fp16portableportable fp32kernelsnote
megalocSt Lucia, all 1464 queries against the 1549 database images, recall@1 / @5 % (25 m)100.00 / 100.00100.00 / 100.00100.00 / 100.00
megalocSt Lucia, every 5th query (293) against the 1549 database images, recall@1 / @5 % (25 m)100.00 / 100.00100.00 / 100.00100.00 / 100.00100.00 / 100.00100.00 / 100.00

Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 84333525, 2026-11-13.

Parity

CPU, float32: max |diff| <= 8.8e-08 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 2.1 % and p99 <= 6.6 % of the reference's median |value| (absolute error of the output descriptor).

variantkernels error vs original, mean / p99GPU, torchkernels used
megaloc0.01356 / 0.04384RTX 5060 Ti, torch 2.14.1+cu130dpt_fused@48e0065bb7cf:sm_120, fastbb@7d058322634d:sm_120

Example output

No example rendered for this checkpoint yet.