-
Notifications
You must be signed in to change notification settings - Fork 36
/
feature_matching.py
116 lines (105 loc) · 3.8 KB
/
feature_matching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import logging
from typing import Optional
from copy import deepcopy
from hloc import match_features
from .feature_extraction import FeatureExtraction
from .pair_selection import PairSelection
from ..utils.misc import same_configs, write_config
logger = logging.getLogger(__name__)
class FeatureMatchingPaths:
def __init__(self, root, config, query_id, ref_id):
self.root = root
feature_name = config['features']['name']
matches_name = config['name']
self.workdir = root / 'matching' / query_id / ref_id / feature_name / matches_name
self.matches = self.workdir / 'matches.h5'
self.config = self.workdir / 'configuration.json'
class FeatureMatching:
methods = {
'superglue': {
'name': 'superglue',
'hloc': {
'model': {
'name': 'superglue',
'weights': 'outdoor',
'sinkhorn_iterations': 5,
},
},
},
'mnn': {
'name': 'mnn',
'hloc': {
'model': {
'name': 'nearest_neighbor',
'do_mutual_check': True,
},
}
},
'ratio_mnn_0_9': {
'name': 'ratio_mnn',
'hloc': {
'model': {
'name': 'nearest_neighbor',
'do_mutual_check': True,
'ratio_threshold': 0.9,
},
}
},
'ratio_mnn_0_8': {
'name': 'ratio_mnn',
'hloc': {
'model': {
'name': 'nearest_neighbor',
'do_mutual_check': True,
'ratio_threshold': 0.8,
},
}
},
'adalam': {
'name': 'adalam',
'hloc': {
'model': {
'name': 'adalam'
},
}
}
}
def __init__(self, outputs, capture, query_id, ref_id, config,
pair_selection: PairSelection,
extraction: FeatureExtraction,
extraction_ref: Optional[FeatureExtraction] = None,
overwrite=False):
extraction_ref = extraction_ref or extraction
if extraction.config['name'] != extraction_ref.config['name']:
raise ValueError('Matching two different features: '
f'{extraction.config} vs {extraction_ref.config}')
assert query_id == extraction.session_id
assert query_id == pair_selection.query_id
assert ref_id == extraction_ref.session_id
assert ref_id == pair_selection.ref_id
self.config = config = {
**deepcopy(config),
'features': extraction.config, # detect upstream changes
# do not include the pairs so the same file can be reused
}
self.query_id = query_id
self.ref_id = ref_id
self.extraction = extraction
self.extraction_ref = extraction_ref
self.pair_selection = pair_selection
self.paths = FeatureMatchingPaths(outputs, config, query_id, ref_id)
self.paths.workdir.mkdir(parents=True, exist_ok=True)
logger.info('Matching local features with %s for sessions (%s, %s).',
config['name'], query_id, ref_id)
if not same_configs(config, self.paths.config):
logger.warning('Existing matches will be overwritten.')
overwrite = True
match_features.main(
config['hloc'],
pair_selection.paths.pairs_hloc,
extraction.paths.features,
matches=self.paths.matches,
features_ref=extraction_ref.paths.features,
overwrite=overwrite,
)
write_config(config, self.paths.config)