From 85ec3bdc804c2b230cdf88dab5a16763e5dbd421 Mon Sep 17 00:00:00 2001 From: Raushan Kumar Date: Tue, 11 Aug 2026 10:07:58 +0530 Subject: [PATCH] Optimize Metropolis2D IoU evaluation --- .../metropolis2d/detection_2d_dataset.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/evaluation/cosmos3/reasoner/vlmevalkit/vlmeval/dataset/metropolis2d/detection_2d_dataset.py b/evaluation/cosmos3/reasoner/vlmevalkit/vlmeval/dataset/metropolis2d/detection_2d_dataset.py index 2e29ba90..6bb38768 100644 --- a/evaluation/cosmos3/reasoner/vlmevalkit/vlmeval/dataset/metropolis2d/detection_2d_dataset.py +++ b/evaluation/cosmos3/reasoner/vlmevalkit/vlmeval/dataset/metropolis2d/detection_2d_dataset.py @@ -292,22 +292,38 @@ def evaluate(self, eval_file, **judge_kwargs): total_pred += len(pred_boxes_car) # Sort predictions by confidence (descending) - pred_boxes_car = sorted(pred_boxes_car, key=lambda x: x.get('score', 1.0), reverse=True) + pred_boxes_car = sorted( + pred_boxes_car, + key=lambda x: x.get('score', 1.0), + reverse=True + ) + + # Precompute IoU matrix once for all prediction/ground-truth pairs. + num_preds = len(pred_boxes_car) + num_gts = len(gt_boxes_car) + + iou_matrix = np.zeros((num_preds, num_gts), dtype=np.float32) + + for pred_idx, pred in enumerate(pred_boxes_car): + pred_bbox = pred['bbox'] + for gt_idx, gt in enumerate(gt_boxes_car): + iou_matrix[pred_idx, gt_idx] = compute_2d_iou( + pred_bbox, gt['bbox'] + ) # Match predictions to ground truth at each IoU threshold for iou_thresh in iou_thresholds: gt_matched = [False] * len(gt_boxes_car) - for pred in pred_boxes_car: - pred_bbox = pred['bbox'] + for pred_idx, pred in enumerate(pred_boxes_car): confidence = pred.get('score', 1.0) best_iou = 0.0 best_gt_idx = -1 - for gt_idx, gt in enumerate(gt_boxes_car): + for gt_idx in range(num_gts): if gt_matched[gt_idx]: continue - iou = compute_2d_iou(pred_bbox, gt['bbox']) + iou = iou_matrix[pred_idx, gt_idx] if iou > best_iou: best_iou = iou best_gt_idx = gt_idx