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

DISK feature extraction #233

Merged
merged 6 commits into from
Sep 13, 2022
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "third_party/r2d2"]
path = third_party/r2d2
url = https://github.com/naver/r2d2.git
[submodule "third_party/disk"]
path = third_party/disk
url = https://github.com/cvlab-epfl/disk.git
11 changes: 11 additions & 0 deletions hloc/extract_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@
'resize_max': 1600,
},
},
'disk': {
'output': 'feats-disk',
'model': {
'name': 'disk',
'max_keypoints': 5000,
},
'preprocessing': {
'grayscale': False,
'resize_max': 1600,
},
},
# Global descriptors
'dir': {
'output': 'global-feats-dir',
Expand Down
81 changes: 81 additions & 0 deletions hloc/extractors/disk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import sys
from pathlib import Path
from functools import partial
import torch
import torch.nn.functional as F

from ..utils.base_model import BaseModel

disk_path = Path(__file__).parent / "../../third_party/disk"
sys.path.append(str(disk_path))
from disk import DISK as _DISK # noqa E402


class DISK(BaseModel):
default_conf = {
'model_name': 'depth-save.pth',
'max_keypoints': None,
'desc_dim': 128,
'mode': 'nms',
'nms_window_size': 5,
}
required_inputs = ['image']

def _init(self, conf):
self.model = _DISK(window=8, desc_dim=conf['desc_dim'])

state_dict = torch.load(
disk_path / conf['model_name'], map_location='cpu')
if 'extractor' in state_dict:
weights = state_dict['extractor']
elif 'disk' in state_dict:
weights = state_dict['disk']
else:
raise KeyError('Incompatible weight file!')
self.model.load_state_dict(weights)

if conf['mode'] == 'nms':
self.extract = partial(
self.model.features,
kind='nms',
window_size=conf['nms_window_size'],
cutoff=0.,
n=conf['max_keypoints']
)
elif conf['mode'] == 'rng':
self.extract = partial(self.model.features, kind='rng')
else:
raise KeyError(
f'mode must be `nms` or `rng`, got `{conf["mode"]}`')

def _forward(self, data):
image = data['image']
# make sure that the dimensions of the image are multiple of 16
orig_h, orig_w = image.shape[-2:]
new_h = round(orig_h / 16) * 16
new_w = round(orig_w / 16) * 16
image = F.pad(image, (0, new_w - orig_w, 0, new_h - orig_h))

batched_features = self.extract(image)

assert(len(batched_features) == 1)
features = batched_features[0]

# filter points detected in the padded areas
kpts = features.kp
valid = torch.all(kpts <= kpts.new_tensor([orig_w, orig_h]) - 1, 1)
kpts = kpts[valid]
descriptors = features.desc[valid]
scores = features.kp_logp[valid]

# order the keypoints
indices = torch.argsort(scores, descending=True)
kpts = kpts[indices]
descriptors = descriptors[indices]
scores = scores[indices]

return {
'keypoints': kpts[None],
'descriptors': descriptors.t()[None],
'scores': scores[None],
}
1 change: 1 addition & 0 deletions third_party/disk
Submodule disk added at eafa0e