forked from ubc-vision/image-matching-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 12
/
pack_res.py
283 lines (243 loc) · 11.5 KB
/
pack_res.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2020 Google LLC, University of Victoria, Czech Technical University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
from collections import OrderedDict
from copy import deepcopy
import numpy as np
from time import time
from config import get_config, print_usage
from utils import pack_helper
from utils.io_helper import load_h5, load_json
from utils.path_helper import get_desc_file
def main(cfg):
'''Main function. Takes config as input.
'''
# Back up config
cfg_orig = deepcopy(cfg)
method = cfg_orig.method_dict
# Add config options to the dict
master_dict = OrderedDict()
master_dict['config'] = method
# Add date
master_dict['properties'] = OrderedDict()
master_dict['properties'][
'processing_date'] = pack_helper.get_current_date()
print('Adding processing date: {}'.format(
master_dict['properties']['processing_date']))
# Add submission flag
master_dict['properties'][
'is_submission'] = cfg.is_submission
print('Flagging as user submission: {}'.format(cfg.is_submission))
# Add descriptor properties
cfg_desc = deepcopy(cfg_orig)
cfg_desc.dataset = 'phototourism'
cfg_desc.scene = 'british_museum'
try:
descriptors_dict = load_h5(get_desc_file(cfg_desc))
desc_type, desc_size, desc_nbytes = pack_helper.get_descriptor_properties(
cfg_desc, descriptors_dict)
except Exception:
desc_type = 'none'
desc_size = 0
desc_nbytes = 0
master_dict['properties']['descriptor_type'] = desc_type
master_dict['properties']['descriptor_size'] = desc_size
master_dict['properties']['descriptor_nbytes'] = desc_nbytes
print('Adding descriptor properties: {} {} ({} bytes)'.format(
master_dict['properties']['descriptor_size'],
master_dict['properties']['descriptor_type'],
master_dict['properties']['descriptor_nbytes']))
# get deprecated image list
deprecated_images_list = load_json(cfg.json_deprecated_images)
# Read data and splits
for dataset in ['phototourism']:
setattr(cfg_orig, 'scenes_{}_{}'.format(dataset, cfg_orig.subset),
'./json/data/{}_{}.json'.format(dataset, cfg_orig.subset))
setattr(cfg_orig, 'splits_{}_{}'.format(dataset, cfg_orig.subset),
'./json/bag_size/{}_{}.json'.format(dataset, cfg_orig.subset))
# Create empty dictionary
master_dict[dataset] = OrderedDict()
res_dict = OrderedDict()
master_dict[dataset]['results'] = res_dict
# Save number of runs
master_dict[dataset]['num_runs_stereo'] = getattr(
cfg_orig, 'num_runs_{}_stereo'.format(cfg_orig.subset))
master_dict[dataset]['num_runs_multiview'] = getattr(
cfg_orig, 'num_runs_{}_multiview'.format(cfg_orig.subset))
# Load data config
scene_list = load_json(
getattr(cfg_orig, 'scenes_{}_{}'.format(dataset, cfg_orig.subset)))
bag_size_json = load_json(
getattr(cfg_orig, 'splits_{}_{}'.format(dataset, cfg_orig.subset)))
bag_size_list = [b['bag_size'] for b in bag_size_json]
bag_size_num = [b['num_in_bag'] for b in bag_size_json]
bag_size_str = ['{}bag'.format(b) for b in bag_size_list]
# Create empty dicts
for scene in ['allseq'] + scene_list:
res_dict[scene] = OrderedDict()
for task in ['stereo', 'multiview', 'relocalization']:
res_dict[scene][task] = OrderedDict()
res_dict[scene][task]['run_avg'] = OrderedDict()
if task == 'multiview':
for bag in bag_size_str + ['bag_avg']:
res_dict[scene]['multiview']['run_avg'][
bag] = OrderedDict()
# Stereo -- multiple runs
t = time()
cur_key = 'config_{}_stereo'.format(dataset)
if cfg_orig.eval_stereo and cur_key in method and method[cur_key]:
num_runs = getattr(cfg_orig,
'num_runs_{}_stereo'.format(cfg_orig.subset))
cfg = deepcopy(cfg_orig)
cfg.dataset = dataset
cfg.task = 'stereo'
for scene in scene_list:
# get deprecated images
if scene in deprecated_images_list.keys():
deprecated_images = deprecated_images_list[scene]
else:
deprecated_images = []
cfg.scene = scene
res_dict[scene]['stereo']['run_avg'] = OrderedDict()
for run in range(num_runs):
res_dict[scene]['stereo']['run_{}'.format(
run)] = OrderedDict()
# Create list of things to gather
metric_list = []
metric_list += ['avg_num_keypoints']
# metric_list += ['matching_scores_epipolar']
metric_list += ['num_inliers']
metric_list += ['matching_scores_depth_projection']
metric_list += ['repeatability']
metric_list += ['qt_auc']
metric_list += ['timings']
for run in range(num_runs):
# Compute and pack results
cfg.run = run
cur_dict = res_dict[scene]['stereo']['run_{}'.format(run)]
for metric in metric_list:
t_cur = time()
getattr(pack_helper, 'compute_' + metric)(cur_dict,
deprecated_images, cfg)
print(
' -- Packing "{}"/"{}"/stereo, run: {}/{}, metric: {} [{:.02f} s]'
.format(dataset, scene, run + 1, num_runs, metric,
time() - t_cur))
# Compute average across runs, for stereo
t_cur = time()
pack_helper.average_stereo_over_runs(cfg, res_dict, num_runs)
print(
' -- Packing "{}"/stereo: averaging over {} run(s) [{:.02f} s]'
.format(dataset, num_runs,
time() - t_cur))
# Compute average across scenes, for stereo
t_cur = time()
pack_helper.average_stereo_over_scenes(cfg, res_dict, num_runs)
print(
' -- Packing "{}"/stereo: averaging over {} scene(s) [{:.02f} s]'
.format(dataset, len(scene_list),
time() - t_cur))
print(' -- Finished packing stereo in {:.01f} sec.'.format(time() -
t))
else:
print('Skipping "{}/stereo"'.format(dataset))
# Multiview -- multiple runs
t = time()
cur_key = 'config_{}_multiview'.format(dataset)
if cfg_orig.eval_multiview and cur_key in method and method[cur_key]:
num_runs = getattr(cfg, 'num_runs_{}_multiview'.format(cfg.subset))
cfg = deepcopy(cfg_orig)
cfg.dataset = dataset
cfg.task = 'multiview'
for scene in scene_list:
cfg.scene = scene
# get deprecated images
if scene in deprecated_images_list.keys():
deprecated_images = deprecated_images_list[scene]
else:
deprecated_images = []
for run in ['run_avg'
] + ['run_{}'.format(f) for f in range(num_runs)]:
res_dict[scene]['multiview'][run] = OrderedDict()
for bags_label in ['bag_avg'] + bag_size_str:
res_dict[scene]['multiview'][run][
bags_label] = OrderedDict()
# Create list of things to gather
metric_list = []
metric_list += ['avg_num_keypoints']
metric_list += ['num_input_matches']
metric_list += ['qt_auc_colmap']
metric_list += ['ATE']
metric_list += ['colmap_stats']
for run in range(num_runs):
for bag_size in bag_size_list:
# Compute and pack results
cfg.run = run
cfg.bag_size = bag_size
cur_dict = res_dict[scene]['multiview']
for metric in metric_list:
t_cur = time()
getattr(pack_helper, 'compute_' + metric)(
cur_dict['run_{}'.format(run)]['{}bag'.format(
bag_size)], deprecated_images, cfg)
print(
' -- Packing "{}"/"{}"/multiview, run {}/{}, "{}", metric: {} [{:.02f} s]'
.format(dataset, scene, run + 1, num_runs,
'{}bag'.format(bag_size), metric,
time() - t_cur))
# Compute average across bags
for metric in cur_dict['run_{}'.format(run)]['25bag']:
pack_helper.average_multiview_over_bags(
cfg, cur_dict['run_{}'.format(run)],
bag_size_list)
# Compute average across runs, for multiview
t_cur = time()
pack_helper.average_multiview_over_runs(cfg, res_dict, num_runs,
bag_size_str + ['bag_avg'])
print(
' -- Packing "{}"/multiview: averaging over {} run(s) [{:.02f} s]'
.format(dataset, num_runs,
time() - t_cur))
# Compute average across scenes, for multiview
t_cur = time()
pack_helper.average_multiview_over_scenes(
cfg, res_dict, num_runs, ['bag_avg'] + bag_size_str)
print(
' -- Packing "{}"/multiview: averaging over {} scene(s) [{:.02f} s]'
.format(dataset, len(scene_list),
time() - t_cur))
print(' -- Finished packing multiview in {:.01f} sec.'.format(
time() - t))
# Relocalization -- multiple runs
# TODO
else:
print('Skipping "{}/multiview"'.format(dataset))
# Dump packed result
print(' -- Saving to: "{}"'.format(
cfg.method_dict['config_common']['json_label']))
if not os.path.exists(cfg.path_pack):
os.makedirs(cfg.path_pack)
json_dump_file = os.path.join(
cfg.path_pack,
'{}.json'.format(cfg.method_dict['config_common']['json_label']))
with open(json_dump_file, 'w') as outfile:
json.dump(master_dict, outfile, indent=2)
if __name__ == '__main__':
cfg, unparsed = get_config()
# If we have unparsed arguments, print usage and exit
if len(unparsed) > 0:
print_usage()
exit(1)
main(cfg)