-
Notifications
You must be signed in to change notification settings - Fork 138
/
eval_bop19_pose.py
292 lines (259 loc) · 11.5 KB
/
eval_bop19_pose.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
284
285
286
287
288
289
290
291
292
# Author: Tomas Hodan (hodantom@cmp.felk.cvut.cz)
# Center for Machine Perception, Czech Technical University in Prague
"""Evaluation script for the BOP Challenge 2019/2020."""
import os
import time
import argparse
import multiprocessing
import subprocess
import numpy as np
from bop_toolkit_lib import config
from bop_toolkit_lib import inout
from bop_toolkit_lib import misc
# Get the base name of the file without the .py extension
file_name = os.path.splitext(os.path.basename(__file__))[0]
logger = misc.get_logger(file_name)
# PARAMETERS (some can be overwritten by the command line arguments below).
################################################################################
p = {
# Errors to calculate.
"errors": [
{
"n_top": -1,
"type": "vsd",
"vsd_deltas": {
"hb": 15,
"icbin": 15,
"icmi": 15,
"itodd": 5,
"lm": 15,
"lmo": 15,
"ruapc": 15,
"tless": 15,
"tudl": 15,
"tyol": 15,
"ycbv": 15,
"hope": 15,
},
"vsd_taus": list(np.arange(0.05, 0.51, 0.05)),
"vsd_normalized_by_diameter": True,
"correct_th": [[th] for th in np.arange(0.05, 0.51, 0.05)],
},
{
"n_top": -1,
"type": "mssd",
"correct_th": [[th] for th in np.arange(0.05, 0.51, 0.05)],
},
{
"n_top": -1,
"type": "mspd",
"correct_th": [[th] for th in np.arange(5, 51, 5)],
},
],
# Minimum visible surface fraction of a valid GT pose.
# by default, we consider only objects that are at least 10% visible
"visib_gt_min": -1,
# See misc.get_symmetry_transformations().
"max_sym_disc_step": 0.01,
# Type of the renderer (used for the VSD pose error function).
"renderer_type": "vispy", # Options: 'vispy', 'cpp', 'python'.
# Names of files with results for which to calculate the errors (assumed to be
# stored in folder p['results_path']). See docs/bop_challenge_2019.md for a
# description of the format. Example results can be found at:
# https://bop.felk.cvut.cz/media/data/bop_sample_results/bop_challenge_2019_sample_results.zip
"result_filenames": [
"/relative/path/to/csv/with/results",
],
# Folder with results to be evaluated.
"results_path": config.results_path,
# Folder for the calculated pose errors and performance scores.
"eval_path": config.eval_path,
# File with a list of estimation targets to consider. The file is assumed to
# be stored in the dataset folder.
"targets_filename": "test_targets_bop19.json",
"num_workers": config.num_workers, # Number of parallel workers for the calculation of errors.
"use_gpu": config.use_gpu, # Use torch for the calculation of errors.
}
################################################################################
# Command line arguments.
# ------------------------------------------------------------------------------
parser = argparse.ArgumentParser()
parser.add_argument("--renderer_type", default=p["renderer_type"])
parser.add_argument(
"--result_filenames",
default=",".join(p["result_filenames"]),
help="Comma-separated names of files with results.",
)
parser.add_argument("--results_path", default=p["results_path"])
parser.add_argument("--eval_path", default=p["eval_path"])
parser.add_argument("--targets_filename", default=p["targets_filename"])
parser.add_argument("--num_workers", default=p["num_workers"])
parser.add_argument("--use_gpu", action="store_true", default=p["use_gpu"])
args = parser.parse_args()
p["renderer_type"] = str(args.renderer_type)
p["result_filenames"] = args.result_filenames.split(",")
p["results_path"] = str(args.results_path)
p["eval_path"] = str(args.eval_path)
p["targets_filename"] = str(args.targets_filename)
p["num_workers"] = int(args.num_workers)
eval_time_start = time.time()
# Evaluation.
# ------------------------------------------------------------------------------
for result_filename in p["result_filenames"]:
logger.info("===========")
logger.info("EVALUATING: {}".format(result_filename))
logger.info("===========")
time_start = time.time()
# Volume under recall surface (VSD) / area under recall curve (MSSD, MSPD).
average_recalls = {}
# Name of the result and the dataset.
result_name = os.path.splitext(os.path.basename(result_filename))[0]
dataset = str(result_name.split("_")[1].split("-")[0])
# Calculate the average estimation time per image.
ests = inout.load_bop_results(
os.path.join(p["results_path"], result_filename), version="bop19"
)
times = {}
times_available = True
for est in ests:
result_key = "{:06d}_{:06d}".format(est["scene_id"], est["im_id"])
if est["time"] < 0:
# All estimation times must be provided.
times_available = False
break
elif result_key in times:
if abs(times[result_key] - est["time"]) > 0.001:
raise ValueError(
"The running time for scene {} and image {} is not the same for "
"all estimates.".format(est["scene_id"], est["im_id"])
)
else:
times[result_key] = est["time"]
if times_available:
average_time_per_image = np.mean(list(times.values()))
else:
average_time_per_image = -1.0
# Evaluate the pose estimates.
for error in p["errors"]:
# Calculate error of the pose estimates.
calc_error_script = misc.get_eval_calc_errors_script_name(p["use_gpu"], error["type"], dataset)
calc_errors_cmd = [
"python",
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
calc_error_script,
),
"--n_top={}".format(error["n_top"]),
"--error_type={}".format(error["type"]),
"--result_filenames={}".format(result_filename),
"--renderer_type={}".format(p["renderer_type"]),
"--results_path={}".format(p["results_path"]),
"--eval_path={}".format(p["eval_path"]),
"--targets_filename={}".format(p["targets_filename"]),
"--max_sym_disc_step={}".format(p["max_sym_disc_step"]),
"--skip_missing=1",
"--num_workers={}".format(p["num_workers"]),
]
if error["type"] == "vsd":
vsd_deltas_str = ",".join(
["{}:{}".format(k, v) for k, v in error["vsd_deltas"].items()]
)
calc_errors_cmd += [
"--vsd_deltas={}".format(vsd_deltas_str),
"--vsd_taus={}".format(",".join(map(str, error["vsd_taus"]))),
"--vsd_normalized_by_diameter={}".format(
error["vsd_normalized_by_diameter"]
),
]
logger.info("Running: " + " ".join(calc_errors_cmd))
if subprocess.call(calc_errors_cmd) != 0:
raise RuntimeError("Calculation of pose errors failed.")
# Paths (rel. to p['eval_path']) to folders with calculated pose errors.
# For VSD, there is one path for each setting of tau. For the other pose
# error functions, there is only one path.
error_dir_paths = {}
if error["type"] == "vsd":
for vsd_tau in error["vsd_taus"]:
error_sign = misc.get_error_signature(
error["type"],
error["n_top"],
vsd_delta=error["vsd_deltas"][dataset],
vsd_tau=vsd_tau,
)
error_dir_paths[error_sign] = os.path.join(result_name, error_sign)
else:
error_sign = misc.get_error_signature(error["type"], error["n_top"])
error_dir_paths[error_sign] = os.path.join(result_name, error_sign)
# Recall scores for all settings of the threshold of correctness (and also
# of the misalignment tolerance tau in the case of VSD).
calc_scores_cmds = []
# Calculate performance scores.
for error_sign, error_dir_path in error_dir_paths.items():
for correct_th in error["correct_th"]:
calc_scores_cmd = [
"python",
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"eval_calc_scores.py",
),
"--error_dir_paths={}".format(error_dir_path),
"--eval_path={}".format(p["eval_path"]),
"--targets_filename={}".format(p["targets_filename"]),
"--visib_gt_min={}".format(p["visib_gt_min"]),
]
calc_scores_cmd += [
"--correct_th_{}={}".format(
error["type"], ",".join(map(str, correct_th))
)
]
calc_scores_cmds.append(calc_scores_cmd)
if p["num_workers"] == 1:
for calc_scores_cmd in calc_scores_cmds:
logger.info("Running: " + " ".join(calc_scores_cmd))
if subprocess.call(calc_scores_cmd) != 0:
raise RuntimeError("Calculation of performance scores failed.")
else:
with multiprocessing.Pool(p["num_workers"]) as pool:
pool.map_async(misc.run_command, calc_scores_cmds)
pool.close()
pool.join()
recalls = []
for error_sign, error_dir_path in error_dir_paths.items():
for correct_th in error["correct_th"]:
# Path to file with calculated scores.
score_sign = misc.get_score_signature(correct_th, p["visib_gt_min"])
scores_filename = "scores_{}.json".format(score_sign)
scores_path = os.path.join(
p["eval_path"], result_name, error_sign, scores_filename
)
# Load the scores.
logger.info("Loading calculated scores from: {}".format(scores_path))
scores = inout.load_json(scores_path)
recalls.append(scores["recall"])
average_recalls[error["type"]] = np.mean(recalls)
logger.info("Recall scores: {}".format(" ".join(map(str, recalls))))
logger.info("Average recall: {}".format(average_recalls[error["type"]]))
time_total = time.time() - time_start
logger.info("Evaluation of {} took {}s.".format(result_filename, time_total))
# Calculate the final scores.
final_scores = {}
for error in p["errors"]:
final_scores["bop19_average_recall_{}".format(error["type"])] = average_recalls[
error["type"]
]
# Final score for the given dataset.
final_scores["bop19_average_recall"] = np.mean(
[average_recalls["vsd"], average_recalls["mssd"], average_recalls["mspd"]]
)
# Average estimation time per image.
final_scores["bop19_average_time_per_image"] = average_time_per_image
# Save the final scores.
final_scores_path = os.path.join(p["eval_path"], result_name, "scores_bop19.json")
inout.save_json(final_scores_path, final_scores)
# Print the final scores.
logger.info("FINAL SCORES:")
for score_name, score_value in final_scores.items():
logger.info("- {}: {}".format(score_name, score_value))
total_eval_time = time.time() - eval_time_start
logger.info("Evaluation took {}s.".format(total_eval_time))
logger.info("Done.")