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

Added SIFT features using PyCOLMAP. #69

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 10 additions & 0 deletions hloc/extract_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
'resize_max': 1600,
},
},
'sift': {
'output': 'feats-sift',
'model': {
'name': 'sift'
},
'preprocessing': {
'grayscale': True,
'resize_max': 1600,
},
},
'dir': {
'output': 'global-feats-dir',
'model': {
Expand Down
67 changes: 67 additions & 0 deletions hloc/extractors/sift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import copy
import numpy as np
import torch

from ..utils.base_model import BaseModel

import pycolmap


EPS = 1e-6


def sift_to_rootsift(descs):
l1_norm_descs = descs / (np.linalg.norm(descs, ord=1, axis=-1)[:, np.newaxis] + EPS)
sqrt_l1_norm_descs = np.sqrt(l1_norm_descs)
root_descs = sqrt_l1_norm_descs / (np.linalg.norm(sqrt_l1_norm_descs, axis=-1)[:, np.newaxis] + EPS)
return root_descs


class SIFT(BaseModel):
default_conf = {
'num_octaves': 4,
'octave_resolution': 3,
'first_octave': 0,
'edge_thresh': 10,
'peak_thresh': 0.01,
'upright': False,
'root': True,
'max_keypoints': -1
}
required_inputs = ['image']

def _init(self, conf):
self.root = conf['root']
self.max_keypoints = conf['max_keypoints']

vlfeat_conf = copy.deepcopy(conf)
vlfeat_conf.pop('name', None)
vlfeat_conf.pop('root', None)
vlfeat_conf.pop('max_keypoints', None)
self.extract = lambda image: pycolmap.extract_sift(
image, **vlfeat_conf
)

def _forward(self, data):
image = data['image']
assert image.shape[1] == 1
assert image.min() >= -EPS and image.max() <= 1 + EPS

keypoints, scores, descriptors = self.extract(image[0, 0].cpu().numpy())
keypoints = keypoints[:, : 2] # Keep only x, y.

if self.root:
descriptors = sift_to_rootsift(descriptors)

if self.max_keypoints != -1:
# It is unclear whether scores from PyCOLMAP are 100% correct - follow https://github.com/mihaidusmanu/pycolmap/issues/8.
indices = np.argsort(scores)[:: -1][: self.max_keypoints]
keypoints = keypoints[indices, :]
scores = scores[indices]
descriptors = descriptors[indices, :]

return {
'keypoints': torch.from_numpy(keypoints)[None],
'scores': torch.from_numpy(scores)[None],
'descriptors': torch.from_numpy(descriptors.T)[None],
}
8 changes: 8 additions & 0 deletions hloc/match_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
'mutual_check': True,
'distance_threshold': 0.7,
},
},
'NN-ratio': {
'output': 'matches-NN-mutual-ratio.8',
'model': {
'name': 'nearest_neighbor',
'mutual_check': True,
'ratio_threshold': 0.8,
}
}
}

Expand Down