-
Notifications
You must be signed in to change notification settings - Fork 37
/
feature_extraction.py
150 lines (138 loc) · 4.25 KB
/
feature_extraction.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging
from copy import deepcopy
import numpy as np
from hloc import extract_features
from ..utils.capture import list_images_for_session
from ..utils.misc import same_configs, write_config
logger = logging.getLogger(__name__)
class FeatureExtractionPaths:
def __init__(self, root, config, session_id):
self.root = root
self.workdir = root / 'extraction' / session_id / config['name']
self.features = self.workdir / 'features.h5'
self.config = self.workdir / 'configuration.json'
class FeatureExtraction:
methods = {
'superpoint': {
'name': 'superpoint',
'hloc': {
'model': {
'name': 'superpoint',
'nms_radius': 3,
'max_keypoints': 2048,
},
'preprocessing': {
'grayscale': True,
'resize_max': 1024,
},
},
},
'r2d2': {
'name': 'r2d2',
'hloc': {
'model': {
'name': 'r2d2',
'max_keypoints': 5000,
},
'preprocessing': {
'grayscale': False,
'resize_max': 1024,
}
}
},
'd2net': {
'name': 'd2net',
'hloc': {
'model': {
'name': 'd2net',
'multiscale': False,
},
'preprocessing': {
'grayscale': False,
'resize_max': 1600,
}
}
},
'd2net-ms': {
'name': 'd2net-ms',
'hloc': {
'model': {
'name': 'd2net',
'multiscale': True,
},
'preprocessing': {
'grayscale': False,
'resize_max': 1600,
}
}
},
'sift': {
'name': 'sift',
'hloc': {
'model': {
'name': 'dog',
'options': {
'first_octave': -1,
'upright': True
}
},
'preprocessing': {
'grayscale': True,
'resize_max': 1600,
},
},
},
'sosnet': {
'name': 'sosnet',
'hloc': {
'model': {
'name': 'dog',
'descriptor': 'sosnet',
'options': {
'first_octave': -1,
'upright': True
}
},
'preprocessing': {
'grayscale': True,
'resize_max': 1600,
},
}
},
}
def __init__(self, outputs, capture, session_id, config, query_keys=None, overwrite=False):
self.config = config = deepcopy(config)
self.session_id = session_id
self.paths = FeatureExtractionPaths(outputs, config, session_id)
self.paths.workdir.mkdir(parents=True, exist_ok=True)
if not same_configs(config, self.paths.config):
overwrite = True
logger.info('Extraction local features %s for session %s.', config['name'], session_id)
_, names, image_root = list_images_for_session(capture, session_id, query_keys)
names = np.unique(names)
extract_features.main(
config['hloc'],
image_root,
feature_path=self.paths.features,
image_list=names,
as_half=True,
overwrite=overwrite,
)
write_config(config, self.paths.config)
class RetrievalFeatureExtraction(FeatureExtraction):
methods = {
'netvlad': {
'name': 'netvlad',
'hloc': {
'model': {'name': 'netvlad'},
'preprocessing': {'resize_max': 640},
},
},
'ap-gem': {
'name': 'ap-gem',
'hloc': {
'model': {'name': 'dir'},
'preprocessing': {'resize_max': 640},
}
},
}