Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Salad retrieval #435

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hloc/extract_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
"model": {"name": "eigenplaces"},
"preprocessing": {"resize_max": 1024},
},
"salad": {
"output": "global-feats-salad",
"model": {"name": "salad"},
"preprocessing": {"resize_max": 640},
},
}


Expand Down
45 changes: 45 additions & 0 deletions hloc/extractors/salad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Global image feature extractor Salad:
Optimal Transport Aggregation for Visual Place Recognition
Sergio Izquierdo, Javier Civera; CVPR 2024.
https://github.com/serizba/salad
"""
import math

import torch
import torchvision.transforms as tvf

from ..utils.base_model import BaseModel


class Salad(BaseModel):
default_conf = {
"backbone": "dinov2_vitb14",
"pretrained": True,
"patch_size": 14,
}
required_inputs = ["image"]

def _init(self, conf):
self.net = torch.hub.load(
"sarlinpe/salad",
"dinov2_salad",
backbone=conf["backbone"],
pretrained=conf["pretrained"],
).eval()

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
self.norm_rgb = tvf.Normalize(mean=mean, std=std)

def _forward(self, data):
image = self.norm_rgb(data["image"])
_, _, h, w = image.shape
patch_size = self.conf["patch_size"]
if h % patch_size or w % patch_size:
h_inp = math.ceil(h / patch_size) * patch_size
w_inp = math.ceil(w / patch_size) * patch_size
image = torch.nn.functional.pad(image, [0, w_inp - w, 0, h_inp - h])
desc = self.net(image)
return {
"global_descriptor": desc,
}
Loading