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
- Paper MegaLoc: One Retrieval to Place Them All CVPRW 2025 · first released 24 Feb 2025
Checkpoint megaloc
Paper 2025 Added in 0.2.0 Evaluated with 0.2.0
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)
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | not measured | – | – | – | – |
| Original fp16torch.autocast(float16) around the call; gmberton/MegaLoc@5fe0dd697c4a megaloc_model.py, 322 x 322 bilinear antialiased resize, normalisation | fp16 | 9.68 | – | 1,347 MiB/1,368 MiB | 3.1 | 0.67 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 5.29 | 1.8× | 483 MiB/508 MiB | 0.5 | 0.37 |
| Kernels | fp16 | 4.26 | 2.3× | 485 MiB/508 MiB | 1.4 | 0.22 |
| Portable + torch.compile | 4.27 | 2.3× | 518 MiB/530 MiB | 13 (build) | – | |
| TensorRT fp16 | fp16 | 3.21 | 3.0× | 458 MiBenginenot comparable | 51 (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
| Device | Backend | dtype | ms / call | Peak memory | Output vs reference | Input |
|---|---|---|---|---|---|---|
| Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14 | Original | fp32 | 58.5 | 2,092 MiB process | – | 480x640 |
| Portable | fp16 | 45.2 | 2,617 MiB process | matches | 480x640 | |
| Portable | fp32 | 53.8 | 1,926 MiB process | matches | 480x640 | |
| Kernels | fp16 | not 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
Commercial use allowed
- Code
- MIT LICENSE
- Weights
- MIT source
- Training data
-
- LVD-142M (DINOv2's curated web-image set, not released) not stated
- GSV-Cities (Google Street View images of 62k places in 40 cities) not stated
- Mapillary Street-Level Sequences CC-BY-NC-SA-4.0, non-commercial
- MegaScenes (Internet photo collections of scenes from Wikimedia Commons) not stated
- ScanNet (RGB-D scans of indoor scenes) noncommercial, non-commercial
- San Francisco eXtra Large (street-view images of San Francisco) not stated
Our reading of the licences, not legal advice.
Weights
megaloc.safetensors 915 MB· hosted on Hugging Facefastforks 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)
| Run | dtype | r1 ↑ | r5 ↑ | Δ r1 vs fp32 original | queries | ms / querie |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 100.00 | 100.00 | reference | 1464 | 14.6 |
| Original fp16 | not measured | |||||
| Portable fp32 | not measured | |||||
| Portable | fp16 | 100.00 | 100.00 | ±0.00 | 1464 | 5.40 |
| Kernels | fp16 | 100.00 | 100.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)
| Run | dtype | r1 ↑ | r5 ↑ | Δ r1 vs fp32 original | queries | ms / querie |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 100.00 | 100.00 | reference | 293 | 14.6 |
| Original fp16 | fp16 | 100.00 | 100.00 | ±0.00 | 293 | 8.88 |
| Portable fp32 | fp32 | 100.00 | 100.00 | ±0.00 | 293 | 14.6 |
| Portable | fp16 | 100.00 | 100.00 | ±0.00 | 293 | 5.55 |
| Kernels | fp16 | 100.00 | 100.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).
| Check | Tolerance | Measured |
|---|---|---|
| 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_kandpairsfind 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 backbonesdpt_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.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
megaloc | synthetic (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):
megaloc3000x4000 (0.9 GiB peak allocated, 0.9 reserved) megaloc480x640, first call: portable + torch.compile 13 s (cold Inductor cache), TensorRT fp16 51 s (ONNX + engine).megalocoriginal 480x640: torch.autocast(float16) around the call; gmberton/MegaLoc@5fe0dd697c4a megaloc_model.py, 322 x 322 bilinear antialiased resize, normalisationmegalocload / 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.
| variant | dataset | original fp32 | original fp16 | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|---|
megaloc | St Lucia, all 1464 queries against the 1549 database images, recall@1 / @5 % (25 m) | 100.00 / 100.00 | 100.00 / 100.00 | 100.00 / 100.00 | |||
megaloc | St Lucia, every 5th query (293) against the 1549 database images, recall@1 / @5 % (25 m) | 100.00 / 100.00 | 100.00 / 100.00 | 100.00 / 100.00 | 100.00 / 100.00 | 100.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).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
megaloc | 0.01356 / 0.04384 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120, fastbb@7d058322634d:sm_120 |
Example output
No example rendered for this checkpoint yet.