-
Notifications
You must be signed in to change notification settings - Fork 289
/
evaluate.py
334 lines (267 loc) · 12.1 KB
/
evaluate.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
things needed
predictions for image
ground thruth for that image
3d model loaded
compare the poses.
"""
import argparse
import os
import numpy as np
import math
from scipy import spatial
import simplejson as json
import visii
import csv
from utils_eval import *
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--data_prediction",
default="../inference/output",
help="Path to prediction data.",
)
parser.add_argument(
"--data",
default="../sample_data/",
help="path to ground truth data.",
)
parser.add_argument(
"--models",
default="../3d_models/YCB_models/",
help="path to the 3D grocery models. ",
)
parser.add_argument("--outf", default="output", help="where to put the results.")
parser.add_argument(
"--adds", action="store_true", help="run ADDS, this might take a while"
)
parser.add_argument(
"--cuboid", action="store_true", help="use cuboid to compute the ADD"
)
parser.add_argument(
"--thresholds",
nargs="+",
default=[0.02, 0.04, 0.06, 0.08, 0.10],
help="Thresholds to compute accuracy for.",
)
opt = parser.parse_args()
visii.initialize(headless=True)
# Create Models
meshes_gt = loadmodels(opt.models, cuboid=opt.cuboid, suffix="_gt")
meshes_gu = loadmodels(opt.models, cuboid=opt.cuboid, suffix="_gu")
# Get Ground Truth and Prediction Data
data_truth = load_groundtruth(opt.data)
prediction_folders = load_prediction(opt.data_prediction, data_truth)
prediction_folders.sort()
print(f"Number of Ground Truth Data : {len(data_truth)}")
print(f"Number of Prediction Folders: {len(prediction_folders)}")
# Make output directory if it does not exist
os.makedirs(os.path.join(opt.outf), exist_ok=True)
csv_file = open(os.path.join(opt.outf, "result.csv"), "w+")
csv_writer = csv.writer(csv_file)
# Write Header Row
csv_writer.writerow(["--cuboid", opt.cuboid, "--adds", opt.adds])
csv_writer.writerow(["Weights", "Object", "Total AUC"] + opt.thresholds)
for pf_i, pf in enumerate(prediction_folders):
adds_objects = {}
adds_all = []
count_all_gt = 0
count_by_object = {}
count_all_preds = 0
count_by_object_preds = {}
for f_i, gt_file in enumerate(data_truth):
print(
f"({pf_i + 1} of {len(prediction_folders)}) Computing file {f_i + 1} of {len(data_truth)}"
)
gt_json = None
with open(os.path.join(opt.data, gt_file)) as json_file:
gt_json = json.load(json_file)
gu_json = None
with open(os.path.join(opt.data_prediction, pf, gt_file)) as json_file:
gu_json = json.load(json_file)
objects_gt = [] # name of obj, pose
for obj in gt_json["objects"]:
name_gt = obj["class"]
# remove "_16k" suffix from name of predicted object
name_gt = name_gt[:-4] if name_gt.endswith("_16k") else name_gt
objects_gt.append(
[
name_gt,
{
"rotation": visii.quat(
obj["quaternion_xyzw"][3],
obj["quaternion_xyzw"][0],
obj["quaternion_xyzw"][1],
obj["quaternion_xyzw"][2],
),
"position": visii.vec3(
obj["location"][0] / 100,
obj["location"][1] / 100,
obj["location"][2] / 100,
),
},
]
)
count_all_gt += 1
if name_gt in count_by_object:
count_by_object[name_gt] += 1
else:
count_by_object[name_gt] = 1
for obj_pred in gu_json["objects"]:
name_pred = obj_pred["class"]
# remove "_16k" suffix from name of predicted object
name_pred = name_pred[:-4] if name_pred.endswith("_16k") else name_pred
# need to add rotation for DOPE prediction, if your frames are aligned
try:
pose_mesh = {
"rotation": visii.quat(
float(obj_pred["quaternion_xyzw"][3]),
float(obj_pred["quaternion_xyzw"][0]),
float(obj_pred["quaternion_xyzw"][1]),
float(obj_pred["quaternion_xyzw"][2]),
)
* visii.angleAxis(1.57, visii.vec3(1, 0, 0))
* visii.angleAxis(1.57, visii.vec3(0, 0, 1)),
"position": visii.vec3(
float(str(obj_pred["location"][0])) / 100.0,
float(str(obj_pred["location"][1])) / 100.0,
float(str(obj_pred["location"][2])) / 100.0,
),
}
except:
# in case there is an inf or null in the location prediction/gt
pose_mesh = {
"rotation": visii.quat(
float(obj_pred["quaternion_xyzw"][3]),
float(obj_pred["quaternion_xyzw"][0]),
float(obj_pred["quaternion_xyzw"][1]),
float(obj_pred["quaternion_xyzw"][2]),
)
* visii.angleAxis(1.57, visii.vec3(1, 0, 0))
* visii.angleAxis(1.57, visii.vec3(0, 0, 1)),
"position": visii.vec3(
1000000,
1000000,
1000000,
),
}
count_all_preds += 1
if name_pred in count_by_object_preds:
count_by_object_preds[name_pred] += 1
else:
count_by_object_preds[name_pred] = 1
candidates = []
for i_obj_gt, obj_gt in enumerate(objects_gt):
name_gt, pose_mesh_gt = obj_gt
if name_gt == name_pred:
candidates.append([i_obj_gt, pose_mesh_gt, name_gt])
best_dist = float("inf")
best_index = None
for candi_gt in candidates:
# compute the add
i_gt, pose_gt, name_gt = candi_gt
visii_gt = meshes_gt[name_gt]
visii_gt.get_transform().set_position(pose_gt["position"])
visii_gt.get_transform().set_rotation(pose_gt["rotation"])
# name_pred should match the names of the folders containing 3d models
visii_gu = meshes_gu[name_pred]
visii_gu.get_transform().set_position(pose_mesh["position"])
visii_gu.get_transform().set_rotation(pose_mesh["rotation"])
if opt.adds and opt.cuboid:
dist = 0
for i_p in range(9):
corner_gt = visii.transform.get(
f"{name_gt + '_gt'}_cuboid_{i_p}"
)
dist_s = []
for i_ps in range(9):
corner_gu = visii.transform.get(
f"{name_pred + '_gu'}_cuboid_{i_ps}"
)
gt_trans = corner_gt.get_local_to_world_matrix()
gu_trans = corner_gu.get_local_to_world_matrix()
dist_now = math.sqrt(
(gt_trans[3][0] - gu_trans[3][0]) ** 2
+ (gt_trans[3][1] - gu_trans[3][1]) ** 2
+ (gt_trans[3][2] - gu_trans[3][2]) ** 2
)
dist_s.append(dist_now)
dist += min(dist_s)
dist /= 9
elif opt.adds and not opt.cuboid:
dist = []
dist2 = []
vertices = visii_gt.get_mesh().get_vertices()
points_gt = []
points_gu = []
for i in range(len(vertices)):
v = visii.vec4(
vertices[i][0], vertices[i][1], vertices[i][2], 1
)
p0 = (
visii_gt.get_transform().get_local_to_world_matrix() * v
)
p1 = (
visii_gu.get_transform().get_local_to_world_matrix() * v
)
points_gt.append([p0[0], p0[1], p0[2]])
points_gu.append([p1[0], p1[1], p1[2]])
dist = np.mean(
spatial.distance_matrix(
np.array(points_gt), np.array(points_gu), p=2
).min(axis=1)
)
elif not opt.adds and opt.cuboid:
dist = 0
for i_p in range(9):
corner_gt = visii.transform.get(
f"{name_gt + '_gt'}_cuboid_{i_p}"
)
corner_gu = visii.transform.get(
f"{name_pred + '_gu'}_cuboid_{i_p}"
)
gt_trans = corner_gt.get_local_to_world_matrix()
gu_trans = corner_gu.get_local_to_world_matrix()
dist += math.sqrt(
(gt_trans[3][0] - gu_trans[3][0]) ** 2
+ (gt_trans[3][1] - gu_trans[3][1]) ** 2
+ (gt_trans[3][2] - gu_trans[3][2]) ** 2
)
dist /= 9
else:
dist = []
vertices = visii_gt.get_mesh().get_vertices()
for i in range(len(vertices)):
v = visii.vec4(
vertices[i][0], vertices[i][1], vertices[i][2], 1
)
p0 = (
visii_gt.get_transform().get_local_to_world_matrix() * v
)
p1 = (
visii_gu.get_transform().get_local_to_world_matrix() * v
)
dist.append(visii.distance(p0, p1))
dist = np.mean(dist)
if dist < best_dist:
best_dist = dist
best_index = i_gt
if best_index is not None:
if not name_pred in adds_objects.keys():
adds_objects[name_pred] = []
adds_all.append(best_dist)
adds_objects[name_pred].append(best_dist)
# Compute Metrics
for name in adds_objects:
auc_thresh = calculate_auc(
thresholds=opt.thresholds,
add_list=np.array(adds_objects[name]),
total_objects=count_by_object[name],
)
auc_total = calculate_auc_total(
add_list=np.array(adds_objects[name]),
total_objects=count_by_object[name],
)
csv_writer.writerow([pf, name, auc_total] + auc_thresh)
csv_file.close()
visii.deinitialize()