forked from cvg/Hierarchical-Localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_features.py
169 lines (144 loc) · 5.26 KB
/
extract_features.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import argparse
import torch
from pathlib import Path
import h5py
import logging
from types import SimpleNamespace
import cv2
import numpy as np
from tqdm import tqdm
import pprint
from . import extractors
from .utils.base_model import dynamic_load
from .utils.tools import map_tensor
'''
A set of standard configurations that can be directly selected from the command
line using their name. Each is a dictionary with the following entries:
- output: the name of the feature file that will be generated.
- model: the model configuration, as passed to a feature extractor.
- preprocessing: how to preprocess the images read from disk.
'''
confs = {
'superpoint_aachen': {
'output': 'feats-superpoint-n4096-r1024',
'model': {
'name': 'superpoint',
'nms_radius': 3,
'max_keypoints': 4096,
},
'preprocessing': {
'grayscale': True,
'resize_max': 1024,
},
},
'superpoint_inloc': {
'output': 'feats-superpoint-n4096-r1600',
'model': {
'name': 'superpoint',
'nms_radius': 4,
'max_keypoints': 4096,
},
'preprocessing': {
'grayscale': True,
'resize_max': 1600,
},
},
'd2net-ss': {
'output': 'feats-d2net-ss',
'model': {
'name': 'd2net',
'multiscale': False,
},
'preprocessing': {
'grayscale': False,
'resize_max': 1600,
},
},
}
class ImageDataset(torch.utils.data.Dataset):
default_conf = {
'globs': ['*.jpg', '*.png', '*.jpeg', '*.JPG', '*.PNG'],
'grayscale': False,
'resize_max': None,
}
def __init__(self, root, conf):
self.conf = conf = SimpleNamespace(**{**self.default_conf, **conf})
self.root = root
self.paths = []
for g in conf.globs:
self.paths += list(Path(root).glob('**/'+g))
if len(self.paths) == 0:
raise ValueError(f'Could not find any image in root: {root}.')
self.paths = [i.relative_to(root) for i in self.paths]
logging.info(f'Found {len(self.paths)} images in root {root}.')
def __getitem__(self, idx):
path = self.paths[idx]
if self.conf.grayscale:
mode = cv2.IMREAD_GRAYSCALE
else:
mode = cv2.IMREAD_COLOR
image = cv2.imread(str(self.root / path), mode)
if not self.conf.grayscale:
image = image[:, :, ::-1] # BGR to RGB
if image is None:
raise ValueError(f'Cannot read image {str(path)}.')
image = image.astype(np.float32)
size = image.shape[:2][::-1]
w, h = size
if self.conf.resize_max and max(w, h) > self.conf.resize_max:
scale = self.conf.resize_max / max(h, w)
h_new, w_new = int(round(h*scale)), int(round(w*scale))
image = cv2.resize(
image, (w_new, h_new), interpolation=cv2.INTER_LINEAR)
if self.conf.grayscale:
image = image[None]
else:
image = image.transpose((2, 0, 1)) # HxWxC to CxHxW
image = image / 255.
data = {
'name': str(path),
'image': image,
'original_size': np.array(size),
}
return data
def __len__(self):
return len(self.paths)
@torch.no_grad()
def main(conf, image_dir, export_dir, as_half=False):
logging.info('Extracting local features with configuration:'
f'\n{pprint.pformat(conf)}')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
Model = dynamic_load(extractors, conf['model']['name'])
model = Model(conf['model']).eval().to(device)
loader = ImageDataset(image_dir, conf['preprocessing'])
loader = torch.utils.data.DataLoader(loader, num_workers=1)
feature_path = Path(export_dir, conf['output']+'.h5')
feature_path.parent.mkdir(exist_ok=True, parents=True)
feature_file = h5py.File(str(feature_path), 'a')
for data in tqdm(loader):
pred = model(map_tensor(data, lambda x: x.to(device)))
pred = {k: v[0].cpu().numpy() for k, v in pred.items()}
pred['image_size'] = original_size = data['original_size'][0].numpy()
if 'keypoints' in pred:
size = np.array(data['image'].shape[-2:][::-1])
scales = (original_size / size).astype(np.float32)
pred['keypoints'] = (pred['keypoints'] + .5) * scales[None] - .5
if as_half:
for k in pred:
dt = pred[k].dtype
if (dt == np.float32) and (dt != np.float16):
pred[k] = pred[k].astype(np.float16)
grp = feature_file.create_group(data['name'][0])
for k, v in pred.items():
grp.create_dataset(k, data=v)
del pred
feature_file.close()
logging.info('Finished exporting features.')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', type=Path, required=True)
parser.add_argument('--export_dir', type=Path, required=True)
parser.add_argument('--conf', type=str, default='superpoint_aachen',
choices=list(confs.keys()))
args = parser.parse_args()
main(confs[args.conf], args.image_dir, args.export_dir)