forked from hpatches/hpatches-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 3
/
adapters.py
151 lines (108 loc) · 5.47 KB
/
adapters.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
import numpy as np
from os import listdir
from os.path import join, isfile
from const import *
DATASET_ROOT = '../../../hpatches-sequences-release/'
KPZ_FILENAME = 'kp.npz'
DESC_FILENAME = 'des.npz'
def adapt_lfnet():
algo_name = 'lfnet'
for folder in listdir(DATASET_ROOT):
print('Processing directory: {}'.format(folder))
folder_path = join(DATASET_ROOT, folder)
kp_path = join(folder_path, KPZ_FILENAME)
desc_path = join(folder_path, DESC_FILENAME)
results_dir = join(folder_path, 'out')
kps = [0.] * 6
descs = [0.] * 6
for img_result_filename in filter(lambda f: 'ppm.npz' in f, listdir(results_dir)):
# Take index from filename as filter() doesn't keep the order.
img_idx = int(img_result_filename[0])
img_result_path = join(results_dir, img_result_filename)
img_result = dict(np.load(img_result_path))
orig_img = cv2.imread(join(folder_path, '{}.ppm'.format(img_idx)))
processed_img = cv2.imread(join(results_dir, '{}.ppm'.format(img_idx)))
x_scale = float(orig_img.shape[1]) / float(processed_img.shape[1])
y_scale = float(orig_img.shape[0]) / float(processed_img.shape[0])
img_result['kpts'][:,0] *= x_scale
img_result['kpts'][:,1] *= y_scale
kps[img_idx - 1] = img_result['kpts']
descs[img_idx - 1] = img_result['descs']
kp = dict(np.load(kp_path, allow_pickle=True)) if isfile(kp_path) else dict()
desc = dict(np.load(desc_path, allow_pickle=True)) if isfile(desc_path) else dict()
kp[algo_name] = np.array(kps)
desc[ALGO_TEMPLATE.format(algo_name, algo_name)] = np.array(descs)
np.savez(kp_path, **kp)
np.savez(desc_path, **desc)
def adapt_superpoint():
algo_name = 'superpoint'
x_dim = 320.
y_dim = 240.
for folder in listdir(DATASET_ROOT):
print('Processing directory: {}'.format(folder))
folder_path = join(DATASET_ROOT, folder)
kp_path = join(folder_path, KPZ_FILENAME)
desc_path = join(folder_path, DESC_FILENAME)
results_dir = join(folder_path, algo_name)
kps = [0.] * 6
descs = [0.] * 6
for img_result_filename in filter(lambda f: 'ppm.npz' in f, listdir(results_dir)):
# Take index from filename as filter() doesn't keep the order.
img_idx = int(img_result_filename[0])
img_result_path = join(results_dir, img_result_filename)
img_result = dict(np.load(img_result_path))
orig_img = cv2.imread(join(folder_path, '{}.ppm'.format(img_idx)))
processed_img = cv2.imread(join(results_dir, '{}.ppm'.format(img_idx)))
x_scale = float(orig_img.shape[1]) / x_dim
y_scale = float(orig_img.shape[0]) / y_dim
img_result['kpts'][:,0] *= x_scale
img_result['kpts'][:,1] *= y_scale
kps[img_idx - 1] = img_result['kpts'].T
descs[img_idx - 1] = img_result['descs'].T
kp = dict(np.load(kp_path, allow_pickle=True)) if isfile(kp_path) else dict()
desc = dict(np.load(desc_path, allow_pickle=True)) if isfile(desc_path) else dict()
kp[algo_name] = np.array(kps)
desc[ALGO_TEMPLATE.format(algo_name, algo_name)] = np.array(descs)
np.savez(kp_path, **kp)
np.savez(desc_path, **desc)
def adapt_d2net():
algo_name = 'd2net'
x_dim = 320.
y_dim = 240.
dataset_root = '/home/kristijan/hpatches-sequences-release/i_ajuntament/hpatches-sequences-release'
for folder in listdir(dataset_root):
print('Processing directory: {}'.format(folder))
folder_path = join(dataset_root, folder)
kp_path = join(folder_path, KPZ_FILENAME)
desc_path = join(folder_path, DESC_FILENAME)
# Results dir is folder dir for D2Net.
results_dir = folder_path
kps = [0.] * 6
descs = [0.] * 6
for img_result_filename in filter(lambda f: 'd2-net' in f, listdir(results_dir)):
# Take index from filename as filter() doesn't keep the order.
img_idx = int(img_result_filename[0])
img_result_path = join(results_dir, img_result_filename)
img_result = dict(np.load(img_result_path))
#orig_img = cv2.imread(join(folder_path, '{}.ppm'.format(img_idx)))
#processed_img = cv2.imread(join(results_dir, '{}.ppm'.format(img_idx)))
#x_scale = float(orig_img.shape[1]) / x_dim
#y_scale = float(orig_img.shape[0]) / y_dim
#img_result['kpts'][:,0] *= x_scale
#img_result['kpts'][:,1] *= y_scale
print(img_result_path)
kps[img_idx - 1] = img_result['keypoints'][:, :2]
descs[img_idx - 1] = img_result['descriptors'][:, :2]
kp = dict(np.load(kp_path, allow_pickle=True)) if isfile(kp_path) else dict()
desc = dict(np.load(desc_path, allow_pickle=True)) if isfile(desc_path) else dict()
kp[algo_name] = np.array(kps)
desc[ALGO_TEMPLATE.format(algo_name, algo_name)] = np.array(descs)
np.savez(kp_path, **kp)
np.savez(desc_path, **desc)
if __name__ == '__main__':
import argparse
parser_of_args = argparse.ArgumentParser(description='Select algorithm to adapt')
parser_of_args.add_argument('--algorithm', type=str,
help='name of the algorithm')
args = parser_of_args.parse_args()
result = locals()['adapt_{}'.format(args.algorithm)]()