Views
No views yet
1import torch
2import torchvision.transforms as tfm
3from PIL import Image
4
5model = torch.hub.load("gmberton/MegaLoc", "get_trained_model")
6
7# Same preprocessing we use for evaluation: ImageNet normalization, resize to 322x322
8# (any resolution works, paper results are computed at 322x322)
9transform = tfm.Compose([
10 tfm.ToTensor(),
11 tfm.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
12 tfm.Resize(size=[322, 322], antialias=True),
13])
14
15images = torch.stack([transform(Image.open(path).convert("RGB")) for path in ["im1.jpg", "im2.jpg"]])
16with torch.inference_mode():
17 descriptors = model(images) # shape [2, 8448], L2-normalized
18
19similarities = descriptors @ descriptors.T # cosine similarities1@InProceedings{Berton_2025_CVPR,
2 author = {Berton, Gabriele and Masone, Carlo},
3 title = {MegaLoc: One Retrieval to Place Them All},
4 booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) Workshops},
5 month = {June},
6 year = {2025},
7 pages = {2861-2867}
8}