-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate_full.py
390 lines (288 loc) · 14.2 KB
/
evaluate_full.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import pickle
import numpy as np
import pprint
from urbanlanegraph_evaluator.evaluator import GraphEvaluator
from urbanlanegraph_evaluator.utils import adjust_node_positions
from aggregation.utils import visualize_graph, filter_graph
import matplotlib.pyplot as plt
from glob import glob
from PIL import Image
import os
import cv2
city_names = [
"austin",
"detroit",
"miami",
"paloalto",
"pittsburgh",
"washington"
]
def evaluate_successor_lgp(graphs_gt, graphs_pred, split):
'''Evaluate the successor graph prediction task.'''
metric_names = ["TOPO Precision",
"TOPO Recall",
"GEO Precision",
"GEO Recall",
"APLS",
"SDA20",
"SDA50",
"Graph IoU"
]
metrics_all = {}
for city in city_names:
metrics_all[city] = {}
metrics_all[city][split] = {}
for sample_id in graphs_gt[city][split]:
metrics_all[city][split][sample_id] = {}
# print("Successor-LGP evaluating sample", sample_id)
if not sample_id in graphs_pred[city][split]:
print("No prediction for sample", sample_id)
metrics_sample = {metric_name: 0.0 for metric_name in metric_names}
else:
evaluator = GraphEvaluator()
metrics = evaluator.evaluate_graph(graphs_gt[city][split][sample_id],
graphs_pred[city][split][sample_id],
area_size=[256, 256])
metrics_sample = {
"TOPO Precision": metrics['topo_precision'],
"TOPO Recall": metrics['topo_recall'],
"GEO Precision": metrics['topo_precision'],
"GEO Recall": metrics['geo_recall'],
"APLS": metrics['apls'],
"SDA20": metrics['sda@20'],
"SDA50": metrics['sda@50'],
"Graph IoU": metrics['iou'],
}
metrics_all[city][split][sample_id].update(metrics_sample)
# Now we average over the samples
for city in city_names:
metrics_all[city][split]["avg"] = {}
for metric_name in metric_names:
metrics_all[city][split]["avg"][metric_name] = np.nanmean(
[metrics_all[city][split][sample_id][metric_name] for sample_id in graphs_gt[city][split]])
# also get the average over all cities
metrics_all[split] = {}
metrics_all[split]["avg"] = {}
for metric_name in metric_names:
metrics_all[split]["avg"][metric_name] = np.nanmean(
[metrics_all[city][split]["avg"][metric_name] for city in city_names])
return metrics_all
def evaluate_full_lgp(graphs_gt, graphs_pred, split):
metric_names = ["TOPO Precision",
"TOPO Recall",
"GEO Precision",
"GEO Recall",
"APLS",
"Graph IoU"
]
metrics_all = {}
metrics_all[split] = {}
for city in city_names:
metrics_all[split][city] = {}
for sample_id in graphs_gt[city][split]:
metrics_all[split][city][sample_id] = {}
print("Full-LGP evaluating sample", sample_id)
if not sample_id in graphs_pred[city][split]:
print(" No prediction for sample", sample_id)
metrics_sample = {metric_name: 0.0 for metric_name in metric_names}
else:
graph_pred = graphs_pred[city][split][sample_id]
graph_gt = graphs_gt[city][split][sample_id]
# adjust node positions
x_offset = float(sample_id.split("_")[2])
y_offset = float(sample_id.split("_")[3])
#graph_pred = adjust_node_positions(graph_pred, x_offset, y_offset)
graph_gt = adjust_node_positions(graph_gt, x_offset, y_offset)
evaluator = GraphEvaluator()
metrics = evaluator.evaluate_graph(graph_gt,
graph_pred,
area_size=[5000, 5000])
print(" Metrics:", metrics)
metrics_sample = {
"TOPO Precision": metrics['topo_precision'],
"TOPO Recall": metrics['topo_recall'],
"GEO Precision": metrics['topo_precision'],
"GEO Recall": metrics['geo_recall'],
"APLS": metrics['apls'],
"Graph IoU": metrics['iou'],
}
metrics_all[split][city][sample_id].update(metrics_sample)
# Now we average over the samples
for city in city_names:
metrics_all[split][city]["avg"] = {}
for metric_name in metric_names:
metrics_all[split][city]["avg"][metric_name] = np.nanmean(
[metrics_all[split][city][sample_id][metric_name] for sample_id in graphs_gt[city][split]])
# also get the average over all cities
metrics_all[split]["avg"] = {}
for metric_name in metric_names:
metrics_all[split]["avg"][metric_name] = np.nanmean([metrics_all[split][city]["avg"][metric_name] for city in city_names])
return metrics_all
def evaluate_planning(graphs_gt, graphs_pred, split):
metric_names = ["MMD", "MED", "SR"]
metrics_all = {}
metrics_all[split] = {}
for city in city_names:
metrics_all[split][city] = {}
for sample_id in graphs_gt[city][split]:
metrics_all[split][city][sample_id] = {}
print("Planning evaluating sample", sample_id)
if not sample_id in graphs_pred[city][split]:
print(" No prediction for sample", sample_id)
metrics_sample = {metric_name: 0.0 for metric_name in metric_names}
else:
graph_gt = graphs_gt[city][split][sample_id]
graph_pred = graphs_pred[city][split][sample_id]
# adjust node positions
x_offset = float(sample_id.split("_")[2])
y_offset = float(sample_id.split("_")[3])
graph_pred = adjust_node_positions(graph_pred, x_offset, y_offset)
graph_gt = adjust_node_positions(graph_gt, x_offset, y_offset)
evaluator = GraphEvaluator()
paths_gt, paths_pred = evaluator.generate_paths(graph_gt, graph_pred, num_planning_paths=100)
metrics = evaluator.evaluate_paths(graph_gt, graph_pred, paths_gt, paths_pred)
metrics_sample = {
"MMD": metrics['mmd'],
"MED": metrics['med'],
"SR": metrics['sr'],
}
metrics_all[split][city][sample_id].update(metrics_sample)
# Now we average over the samples
for city in city_names:
metrics_all[split][city]["avg"] = {}
for metric_name in metric_names:
metrics_all[split][city]["avg"][metric_name] = np.nanmean(
[metrics_all[split][city][sample_id][metric_name] for sample_id in graphs_gt[city][split]])
# also get the average over all cities
metrics_all[split]["avg"] = {}
for metric_name in metric_names:
metrics_all[split]["avg"][metric_name] = np.nanmean(
[metrics_all[split][city]["avg"][metric_name] for city in city_names])
return metrics_all
def evaluate(annotation_file, user_submission_file, phase_codename, split, **kwargs):
with open(annotation_file, 'rb') as f:
graphs_gt = pickle.load(f)
with open(user_submission_file, 'rb') as f:
graphs_pred = pickle.load(f)
output = {}
if phase_codename == "phase_successor_lgp":
print("%%%%%%%%%%%%%%%%%%%%%\n%%%%%%\tEvaluating for Phase: phase_successor_lgp\n%%%%%%%%%%%%%%%%%%%%%")
out_dict = evaluate_successor_lgp(graphs_gt, graphs_pred, split)
# this goes to the leaderboard (average of all cities
metrics_successor = out_dict[split]["avg"]
output["result"] = [{"{}_split_succ".format(split): metrics_successor}]
# To display the results in the result file (all cities)
output["submission_result"] = out_dict
elif phase_codename == "phase_full_lgp":
print("%%%%%%%%%%%%%%%%%%%%%\n%%%%%%\tEvaluating for Phase: phase_full_lgp\n%%%%%%%%%%%%%%%%%%%%%")
out_dict = evaluate_full_lgp(graphs_gt, graphs_pred, split)
# the average over all cities for the eval split is this dict entry:
metrics_full = out_dict[split]["avg"]
output["result"] = [{"{}_split_full".format(split): metrics_full}]
# To display the results in the result file
output["submission_result"] = output["result"][0]
elif phase_codename == "phase_planning":
print("%%%%%%%%%%%%%%%%%%%%%\n%%%%%%\tEvaluating for Phase: phase_planning\n%%%%%%%%%%%%%%%%%%%%%")
out_dict = evaluate_planning(graphs_gt, graphs_pred, split)
# the average over all cities for the eval split is this dict entry:
metrics_planning = out_dict[split]["avg"]
output["result"] = [{"{}_split_planning".format(split): metrics_planning}]
# To display the results in the result file
output["submission_result"] = output["result"][0]
else:
raise ValueError("Unknown phase codename: {}".format(phase_codename))
return output
def evaluate_single_full_lgp(graph_gt, graph_pred):
evaluator = GraphEvaluator()
metrics = evaluator.evaluate_graph(graph_gt,
graph_pred,
area_size=[5000, 5000])
metrics_sample = {
"TOPO Precision": metrics['topo_precision'],
"TOPO Recall": metrics['topo_recall'],
"GEO Precision": metrics['topo_precision'],
"GEO Recall": metrics['geo_recall'],
"APLS": metrics['apls'],
"Graph IoU": metrics['iou'],
}
return metrics_sample
if __name__ == "__main__":
results_dict = {}
split = 'test'
tile_ids = glob("/data/lanegraph/urbanlanegraph-dataset-dev/*/tiles/{}/*.png".format(split))
# data_source = "lanegraph"
data_source = "tracklets"
tile_ids = [os.path.basename(t).split(".")[0] for t in tile_ids]
for tile_id in tile_ids:
try:
graph_gt = glob('/data/lanegraph/urbanlanegraph-dataset-dev/*/tiles/*/{}.gpickle'.format(tile_id))[0]
aerial_image = glob('/data/lanegraph/urbanlanegraph-dataset-dev/*/tiles/*/{}.png'.format(tile_id))[0]
# graph_pred = '/data/autograph/evaluations/G_agg/{}/{}/G_agg_naive_cleanup.pickle'.format(data_source, tile_id)
graph_pred = '/data/autograph/evaluations/G_agg/{}/{}/G_agg_naive_raw.pickle'.format(data_source, tile_id)
print("Analyzing tile: {}".format(graph_pred))
aerial_image = Image.open(aerial_image)
aerial_image = np.array(aerial_image)
with open(graph_gt, 'rb') as f:
graph_gt = pickle.load(f)
with open(graph_pred, 'rb') as f:
graph_pred = pickle.load(f)
except Exception as e:
print("Could not load graph for tile: {}".format(tile_id))
print(e)
continue
# adjust node positions
x_offset = float(tile_id.split("_")[2])
y_offset = float(tile_id.split("_")[3])
graph_gt = adjust_node_positions(graph_gt, x_offset, y_offset)
graph_pred = adjust_node_positions(graph_pred, 500, 500)
graph_pred_filtered = filter_graph(target=graph_gt, source=graph_pred, threshold=50)
#metrics_dict = evaluate_single_full_lgp(graph_gt, graph_pred)
city = tile_id.split("_")[0]
if city not in results_dict:
results_dict[city] = {}
if split not in results_dict[city]:
results_dict[city][split] = {}
results_dict[city][split][tile_id] = graph_pred_filtered
fig, ax = plt.subplots(1, 3, figsize=(60, 20), sharex=True, sharey=True, dpi=200)
for a in ax:
a.set_xticks([])
a.set_yticks([])
a.set_aspect('equal')
a.axis('off')
a.imshow(aerial_image)
visualize_graph(graph_gt, ax[0])
visualize_graph(graph_pred_filtered, ax[1])
visualize_graph(graph_pred, ax[2])
ax[0].set_title("Ground Truth")
ax[1].set_title("Prediction (filtered)")
ax[2].set_title("Prediction (unfiltered)")
plt.savefig("/data/autograph/evaluations/keep-viz/{}/{}_pred.png".format(data_source, tile_id))
plt.close()
#plt.show()
# svg_filename = "/data/autograph/evaluations/keep-viz/{}/{}_pred.svg".format(model, tile_id)
#plt.savefig(svg_filename)
# # open svg file and delete line containing "<g id="figure_1">"
# with open(svg_filename, "r") as f:
# lines = f.readlines()
# with open(svg_filename, "w") as f:
# for line in lines:
# if "<g id=\"figure_1\">" not in line:
# f.write(line)
user_submission_file = "/data/autograph/evaluations/eval_full_{}_{}.pickle".format(split, data_source)
with open(user_submission_file, "wb") as f:
pickle.dump(results_dict, f)
# Evaluate the submission for each task
# Task: Successor LGP, Eval Split
# metrics_dict = evaluate(annotation_file="annotations_successor_lgp_eval.pickle",
# user_submission_file="succ_lgp_eval_autograph.pickle",
# phase_codename="phase_successor_lgp")
# # Task: Full LGP, Eval Split
metrics_dict = evaluate(annotation_file="/home/zuern/lanegnn-dev/urbanlanegraph_evaluator/annotations_full_lgp_test.pickle",
user_submission_file=user_submission_file,
phase_codename="phase_full_lgp",
split="test")
pprint.pprint(metrics_dict)
# # Task: Planning, Eval Split
# metrics_dict = evaluate(annotation_file="annotations_full_lgp_eval.pickle",
# user_submission_file="annotations_full_lgp_eval.pickle",
# phase_codename="phase_planning")