@@ -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
461462def _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
552515def 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