Skip to content

Commit 5201bc2

Browse files
Tianyu LiTianyu Li
authored andcommitted
update evaluate.py
1 parent a95f990 commit 5201bc2

File tree

2 files changed

+102
-148
lines changed

2 files changed

+102
-148
lines changed

openlanev2/centerline/evaluation/evaluate.py

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _tpfp(gts, preds, confidences, distance_matrix, distance_threshold):
105105

106106
tp = np.zeros((num_preds), dtype=np.float32)
107107
fp = np.zeros((num_preds), dtype=np.float32)
108-
idx_match_gt = np.ones((num_preds)) * np.nan
108+
idx_match_gt = np.ones((num_preds), dtype=int) * np.nan
109109

110110
if num_gts == 0:
111111
fp[...] = 1
@@ -434,29 +434,30 @@ def _mAP_topology_lclc(gts, preds, distance_thresholds):
434434
435435
"""
436436
acc = []
437-
for r in range(10):
438-
for distance_threshold in distance_thresholds:
439-
for token in gts.keys():
440-
preds_topology_lclc_unmatched = preds[token]['topology_lclc']
437+
for distance_threshold in distance_thresholds:
438+
for token in gts.keys():
439+
preds_topology_lclc_unmatched = preds[token]['topology_lclc']
441440

442-
idx_match_gt = preds[token][f'lane_centerline_{distance_threshold}_idx_match_gt']
443-
confidence = preds[token][f'lane_centerline_{distance_threshold}_confidence']
444-
confidence_thresholds = preds[token][f'lane_centerline_{distance_threshold}_confidence_thresholds']
445-
gt_pred = {m: i for i, (m, c) in enumerate(zip(idx_match_gt, confidence)) if c >= confidence_thresholds[r] and not np.isnan(m)}
441+
idx_match_gt = preds[token][f'lane_centerline_{distance_threshold}_idx_match_gt']
442+
gt_pred = {m: i for i, m in enumerate(idx_match_gt) if not np.isnan(m)}
446443

447-
gts_topology_lclc = gts[token]['topology_lclc']
448-
if 0 in gts_topology_lclc.shape:
449-
continue
444+
gts_topology_lclc = gts[token]['topology_lclc']
445+
if 0 in gts_topology_lclc.shape:
446+
continue
450447

451-
preds_topology_lclc = np.ones_like(gts_topology_lclc, dtype=gts_topology_lclc.dtype) * np.nan
452-
for i in range(preds_topology_lclc.shape[0]):
453-
for j in range(preds_topology_lclc.shape[1]):
454-
if i in gt_pred and j in gt_pred:
455-
preds_topology_lclc[i][j] = preds_topology_lclc_unmatched[gt_pred[i]][gt_pred[j]]
456-
preds_topology_lclc[np.isnan(preds_topology_lclc)] = 1 - gts_topology_lclc[np.isnan(preds_topology_lclc)]
448+
gt_indices = np.array(list(gt_pred.keys())).astype(int)
449+
pred_indices = np.array(list(gt_pred.values())).astype(int)
450+
preds_topology_lclc = np.ones_like(gts_topology_lclc, dtype=gts_topology_lclc.dtype) * np.nan
451+
xs = gt_indices[:, None].repeat(len(gt_indices), 1)
452+
ys = gt_indices[None, :].repeat(len(gt_indices), 0)
453+
preds_topology_lclc[xs, ys] = preds_topology_lclc_unmatched[pred_indices][:, pred_indices]
454+
preds_topology_lclc[np.isnan(preds_topology_lclc)] = (
455+
1 - gts_topology_lclc[np.isnan(preds_topology_lclc)]) * (0.5 + np.finfo(np.float32).eps)
457456

458-
acc.append(_AP_directerd(gts=gts_topology_lclc, preds=preds_topology_lclc))
457+
acc.append(_AP_directerd(gts=gts_topology_lclc, preds=preds_topology_lclc))
459458

459+
if len(acc) == 0:
460+
return np.float32(0)
460461
return np.hstack(acc).mean()
461462

462463
def _mAP_topology_lcte(gts, preds, distance_thresholds):
@@ -479,41 +480,37 @@ def _mAP_topology_lcte(gts, preds, distance_thresholds):
479480
480481
"""
481482
acc = []
482-
for r in range(10):
483-
for distance_threshold_lane_centerline in distance_thresholds['lane_centerline']:
484-
for distance_threshold_traffic_element in distance_thresholds['traffic_element']:
485-
for token in gts.keys():
486-
preds_topology_lcte_unmatched = preds[token]['topology_lcte']
487-
488-
idx_match_gt_lane_centerline = preds[token][f'lane_centerline_{distance_threshold_lane_centerline}_idx_match_gt']
489-
confidence_lane_centerline = preds[token][f'lane_centerline_{distance_threshold_lane_centerline}_confidence']
490-
confidence_thresholds_lane_centerline = preds[token][f'lane_centerline_{distance_threshold_lane_centerline}_confidence_thresholds']
491-
gt_pred_lane_centerline = {
492-
m: i for i, (m, c) in enumerate(zip(idx_match_gt_lane_centerline, confidence_lane_centerline)) \
493-
if c >= confidence_thresholds_lane_centerline[r] and not np.isnan(m)
494-
}
495-
496-
idx_match_gt_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_idx_match_gt']
497-
confidence_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_confidence']
498-
confidence_thresholds_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_confidence_thresholds']
499-
gt_pred_traffic_element = {
500-
m: i for i, (m, c) in enumerate(zip(idx_match_gt_traffic_element, confidence_traffic_element)) \
501-
if c >= confidence_thresholds_traffic_element[r] and not np.isnan(m)
502-
}
503-
504-
gts_topology_lcte = gts[token]['topology_lcte']
505-
if 0 in gts_topology_lcte.shape:
506-
continue
507-
508-
preds_topology_lcte = np.ones_like(gts_topology_lcte, dtype=gts_topology_lcte.dtype) * np.nan
509-
for i in range(preds_topology_lcte.shape[0]):
510-
for j in range(preds_topology_lcte.shape[1]):
511-
if i in gt_pred_lane_centerline and j in gt_pred_traffic_element:
512-
preds_topology_lcte[i][j] = preds_topology_lcte_unmatched[gt_pred_lane_centerline[i]][gt_pred_traffic_element[j]]
513-
preds_topology_lcte[np.isnan(preds_topology_lcte)] = 1 - gts_topology_lcte[np.isnan(preds_topology_lcte)]
514-
515-
acc.append(_AP_undirecterd(gts=gts_topology_lcte, preds=preds_topology_lcte))
516-
483+
for distance_threshold_lane_centerline in distance_thresholds['lane_centerline']:
484+
for distance_threshold_traffic_element in distance_thresholds['traffic_element']:
485+
for token in gts.keys():
486+
preds_topology_lcte_unmatched = preds[token]['topology_lcte']
487+
488+
idx_match_gt_lane_centerline = preds[token][f'lane_centerline_{distance_threshold_lane_centerline}_idx_match_gt']
489+
gt_pred_lane_centerline = {m: i for i, m in enumerate(idx_match_gt_lane_centerline) if not np.isnan(m)}
490+
491+
idx_match_gt_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_idx_match_gt']
492+
gt_pred_traffic_element = {m: i for i, m in enumerate(idx_match_gt_traffic_element) if not np.isnan(m)}
493+
494+
gts_topology_lcte = gts[token]['topology_lcte']
495+
if 0 in gts_topology_lcte.shape:
496+
continue
497+
498+
gt_indices_lc = np.array(list(gt_pred_lane_centerline.keys())).astype(int)
499+
pred_indices_lc = np.array(list(gt_pred_lane_centerline.values())).astype(int)
500+
gt_indices_te = np.array(list(gt_pred_traffic_element.keys())).astype(int)
501+
pred_indices_te = np.array(list(gt_pred_traffic_element.values())).astype(int)
502+
503+
preds_topology_lcte = np.ones_like(gts_topology_lcte, dtype=gts_topology_lcte.dtype) * np.nan
504+
xs = gt_indices_lc[:, None].repeat(len(gt_indices_te), 1)
505+
ys = gt_indices_te[None, :].repeat(len(gt_indices_lc), 0)
506+
preds_topology_lcte[xs, ys] = preds_topology_lcte_unmatched[pred_indices_lc][:, pred_indices_te]
507+
preds_topology_lcte[np.isnan(preds_topology_lcte)] = (
508+
1 - gts_topology_lcte[np.isnan(preds_topology_lcte)]) * (0.5 + np.finfo(np.float32).eps)
509+
510+
acc.append(_AP_undirecterd(gts=gts_topology_lcte, preds=preds_topology_lcte))
511+
512+
if len(acc) == 0:
513+
return np.float32(0)
517514
return np.hstack(acc).mean()
518515

519516
def evaluate(ground_truth, predictions, verbose=True):

openlanev2/lanesegment/evaluation/evaluate.py

Lines changed: 51 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _tpfp(gts, preds, confidences, distance_matrix, distance_threshold):
104104

105105
tp = np.zeros((num_preds), dtype=np.float32)
106106
fp = np.zeros((num_preds), dtype=np.float32)
107-
idx_match_gt = np.ones((num_preds)) * np.nan
107+
idx_match_gt = np.ones((num_preds), dtype=int) * np.nan
108108

109109
if num_gts == 0:
110110
fp[...] = 1
@@ -433,29 +433,30 @@ def _mAP_topology_lsls(gts, preds, distance_thresholds):
433433
434434
"""
435435
acc = []
436-
for r in range(10):
437-
for distance_threshold in distance_thresholds:
438-
for token in gts.keys():
439-
preds_topology_lsls_unmatched = preds[token]['topology_lsls']
436+
for distance_threshold in distance_thresholds:
437+
for token in gts.keys():
438+
preds_topology_lsls_unmatched = preds[token]['topology_lsls']
440439

441-
idx_match_gt = preds[token][f'lane_segment_{distance_threshold}_idx_match_gt']
442-
confidence = preds[token][f'lane_segment_{distance_threshold}_confidence']
443-
confidence_thresholds = preds[token][f'lane_segment_{distance_threshold}_confidence_thresholds']
444-
gt_pred = {m: i for i, (m, c) in enumerate(zip(idx_match_gt, confidence)) if c >= confidence_thresholds[r] and not np.isnan(m)}
440+
idx_match_gt = preds[token][f'lane_segment_{distance_threshold}_idx_match_gt']
441+
gt_pred = {m: i for i, m in enumerate(idx_match_gt) if not np.isnan(m)}
445442

446-
gts_topology_lsls = gts[token]['topology_lsls']
447-
if 0 in gts_topology_lsls.shape:
448-
continue
443+
gts_topology_lsls = gts[token]['topology_lsls']
444+
if 0 in gts_topology_lsls.shape:
445+
continue
449446

450-
preds_topology_lsls = np.ones_like(gts_topology_lsls, dtype=gts_topology_lsls.dtype) * np.nan
451-
for i in range(preds_topology_lsls.shape[0]):
452-
for j in range(preds_topology_lsls.shape[1]):
453-
if i in gt_pred and j in gt_pred:
454-
preds_topology_lsls[i][j] = preds_topology_lsls_unmatched[gt_pred[i]][gt_pred[j]]
455-
preds_topology_lsls[np.isnan(preds_topology_lsls)] = 1 - gts_topology_lsls[np.isnan(preds_topology_lsls)]
447+
gt_indices = np.array(list(gt_pred.keys())).astype(int)
448+
pred_indices = np.array(list(gt_pred.values())).astype(int)
449+
preds_topology_lsls = np.ones_like(gts_topology_lsls, dtype=gts_topology_lsls.dtype) * np.nan
450+
xs = gt_indices[:, None].repeat(len(gt_indices), 1)
451+
ys = gt_indices[None, :].repeat(len(gt_indices), 0)
452+
preds_topology_lsls[xs, ys] = preds_topology_lsls_unmatched[pred_indices][:, pred_indices]
453+
preds_topology_lsls[np.isnan(preds_topology_lsls)] = (
454+
1 - gts_topology_lsls[np.isnan(preds_topology_lsls)]) * (0.5 + np.finfo(np.float32).eps)
456455

457-
acc.append(_AP_directerd(gts=gts_topology_lsls, preds=preds_topology_lsls))
456+
acc.append(_AP_directerd(gts=gts_topology_lsls, preds=preds_topology_lsls))
458457

458+
if len(acc) == 0:
459+
return np.float32(0)
459460
return np.hstack(acc).mean()
460461

461462
def _mAP_topology_lste(gts, preds, distance_thresholds):
@@ -478,76 +479,38 @@ def _mAP_topology_lste(gts, preds, distance_thresholds):
478479
479480
"""
480481
acc = []
481-
for r in range(10):
482-
for distance_threshold_lane_segment in distance_thresholds['lane_segment']:
483-
for distance_threshold_traffic_element in distance_thresholds['traffic_element']:
484-
for token in gts.keys():
485-
preds_topology_lste_unmatched = preds[token]['topology_lste']
486-
487-
idx_match_gt_lane_segment = preds[token][f'lane_segment_{distance_threshold_lane_segment}_idx_match_gt']
488-
confidence_lane_segment = preds[token][f'lane_segment_{distance_threshold_lane_segment}_confidence']
489-
confidence_thresholds_lane_segment = preds[token][f'lane_segment_{distance_threshold_lane_segment}_confidence_thresholds']
490-
gt_pred_lane_segment = {
491-
m: i for i, (m, c) in enumerate(zip(idx_match_gt_lane_segment, confidence_lane_segment)) \
492-
if c >= confidence_thresholds_lane_segment[r] and not np.isnan(m)
493-
}
494-
495-
idx_match_gt_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_idx_match_gt']
496-
confidence_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_confidence']
497-
confidence_thresholds_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_confidence_thresholds']
498-
gt_pred_traffic_element = {
499-
m: i for i, (m, c) in enumerate(zip(idx_match_gt_traffic_element, confidence_traffic_element)) \
500-
if c >= confidence_thresholds_traffic_element[r] and not np.isnan(m)
501-
}
502-
503-
gts_topology_lste = gts[token]['topology_lste']
504-
if 0 in gts_topology_lste.shape:
505-
continue
506-
507-
preds_topology_lste = np.ones_like(gts_topology_lste, dtype=gts_topology_lste.dtype) * np.nan
508-
for i in range(preds_topology_lste.shape[0]):
509-
for j in range(preds_topology_lste.shape[1]):
510-
if i in gt_pred_lane_segment and j in gt_pred_traffic_element:
511-
preds_topology_lste[i][j] = preds_topology_lste_unmatched[gt_pred_lane_segment[i]][gt_pred_traffic_element[j]]
512-
preds_topology_lste[np.isnan(preds_topology_lste)] = 1 - gts_topology_lste[np.isnan(preds_topology_lste)]
513-
514-
acc.append(_AP_undirecterd(gts=gts_topology_lste, preds=preds_topology_lste))
515-
516-
return np.hstack(acc).mean()
482+
for distance_threshold_lane_segment in distance_thresholds['lane_segment']:
483+
for distance_threshold_traffic_element in distance_thresholds['traffic_element']:
484+
for token in gts.keys():
485+
preds_topology_lste_unmatched = preds[token]['topology_lste']
486+
487+
idx_match_gt_lane_segment = preds[token][f'lane_segment_{distance_threshold_lane_segment}_idx_match_gt']
488+
gt_pred_lane_segment = {m: i for i, m in enumerate(idx_match_gt_lane_segment) if not np.isnan(m)}
489+
490+
idx_match_gt_traffic_element = preds[token][f'traffic_element_{distance_threshold_traffic_element}_idx_match_gt']
491+
gt_pred_traffic_element = {m: i for i, m in enumerate(idx_match_gt_traffic_element) if not np.isnan(m)}
492+
493+
gts_topology_lste = gts[token]['topology_lste']
494+
if 0 in gts_topology_lste.shape:
495+
continue
517496

518-
# def _matched_list(gts, preds, distance_matrices, distance_threshold, object_type, filter):
519-
# gts_tp = {}
520-
# preds_tp = {}
521-
# for token in gts.keys():
522-
# gt = [gt for gt in gts[token][object_type] if filter(gt)]
523-
# pred = [pred for pred in preds[token][object_type] if filter(pred)]
524-
# confidence = [pred['confidence'] for pred in preds[token][object_type] if filter(pred)]
525-
# filtered_distance_matrix = distance_matrices[token].copy()
526-
# filtered_distance_matrix = filtered_distance_matrix[[filter(gt) for gt in gts[token][object_type]], :]
527-
# filtered_distance_matrix = filtered_distance_matrix[:, [filter(pred) for pred in preds[token][object_type]]]
528-
# tp, fp, idx_match_gt = _tpfp(
529-
# gts=gt,
530-
# preds=pred,
531-
# confidences=confidence,
532-
# distance_matrix=filtered_distance_matrix,
533-
# distance_threshold=distance_threshold,
534-
# )
535-
536-
# gt_tp = []
537-
# pred_tp = []
538-
# order = []
539-
# for idx, flag in enumerate(idx_match_gt):
540-
# if flag != np.nan:
541-
# gt_tp.append(gt[flag.astype(np.int32)])
542-
# pred_tp.append(pred[idx])
543-
# order.append(flag.astype(np.int32))
544-
# gt_tp = [gt_tp[i] for i in order]
545-
# pred_tp = [pred_tp[i] for i in order]
546-
547-
# gts_tp[token] = {object_type: gt_tp}
548-
# preds_tp[token] = {object_type: pred_tp}
549-
550-
# return gts_tp, preds_tp
497+
gt_indices_ls = np.array(list(gt_pred_lane_segment.keys())).astype(int)
498+
pred_indices_ls = np.array(list(gt_pred_lane_segment.values())).astype(int)
499+
gt_indices_te = np.array(list(gt_pred_traffic_element.keys())).astype(int)
500+
pred_indices_te = np.array(list(gt_pred_traffic_element.values())).astype(int)
501+
502+
preds_topology_lste = np.ones_like(gts_topology_lste, dtype=gts_topology_lste.dtype) * np.nan
503+
xs = gt_indices_ls[:, None].repeat(len(gt_indices_te), 1)
504+
ys = gt_indices_te[None, :].repeat(len(gt_indices_ls), 0)
505+
preds_topology_lste[xs, ys] = preds_topology_lste_unmatched[pred_indices_ls][:, pred_indices_te]
506+
preds_topology_lste[np.isnan(preds_topology_lste)] = (
507+
1 - gts_topology_lste[np.isnan(preds_topology_lste)]) * (0.5 + np.finfo(np.float32).eps)
508+
509+
acc.append(_AP_undirecterd(gts=gts_topology_lste, preds=preds_topology_lste))
510+
511+
if len(acc) == 0:
512+
return np.float32(0)
513+
return np.hstack(acc).mean()
551514

552515
def evaluate(ground_truth, predictions, verbose=True):
553516
r"""
@@ -706,10 +669,4 @@ def evaluate(ground_truth, predictions, verbose=True):
706669
np.sqrt(metrics['OpenLane-V2 UniScore']['TOP_lt']),
707670
]).mean()
708671

709-
"""
710-
Error
711-
"""
712-
713-
# gts_matched, preds_matched = _matched_list(gts=gts, preds=preds, distance_matrices=distance_matrices['laneseg'], distance_threshold=THRESHOLDS_LANESEG[1], object_type='lane_segment', filter=lambda _: True)
714-
715672
return metrics

0 commit comments

Comments
 (0)