import argparse import json import re from tqdm import tqdm HIGHLEVEL_CATEGORIES = { "speed": ["KEEP", "ACCELERATE", "DECELERATE", "STOP"], "path": ["STRAIGHT", "LEFT_TURN", "RIGHT_TURN", "LEFT_CHANGE", "RIGHT_CHANGE", "UNKNOWN"], } REQUIRED_HIGHLEVEL_SUBTASKS = set(HIGHLEVEL_CATEGORIES) def get_numpy(): import numpy as np return np def clean_text(s: str): if not isinstance(s, str): return "" s = s.strip() s = re.sub(r"[.\n\r]+", " ", s) s = re.sub(r"\s+", " ", s) return s.strip() def parse_highlevel_prediction(text, subtask): if subtask not in HIGHLEVEL_CATEGORIES: raise ValueError(f"Unexpected planning high-level subtask: {subtask!r}") text = clean_text(text).upper() categories = HIGHLEVEL_CATEGORIES[subtask] matches = [ word for word in categories if re.search(rf"(? 0: results["HighLevel"] = { "total": total_high, "original_questions": original_questions, "overall_accuracy (%)": round(sum(highlevel_correct.values()) / total_high * 100, 2), "joint_correct": joint_correct, "joint_accuracy (%)": round(joint_correct / original_questions * 100, 2), "subtasks": { subtask: { "total": highlevel_totals[subtask], "accuracy (%)": round(highlevel_correct[subtask] / highlevel_totals[subtask] * 100, 2) if highlevel_totals[subtask] else None, "classwise": { category: round(stats["correct"] / stats["total"] * 100, 2) if stats["total"] else None for category, stats in highlevel_classwise[subtask].items() }, } for subtask in HIGHLEVEL_CATEGORIES }, } total_traj = len(trajectory_items) if total_traj > 0: results["Trajectory"] = { "total": total_traj, "valid_predictions": total_traj, "invalid_predictions": 0, } if traj_errors: np = get_numpy() traj_errs_np = { k: np.mean([error[k] for error in traj_errors if error[k] is not None]) for k in ["1s", "3s", "5s", "mean"] } results["Trajectory"]["avg_L2_error_m"] = {k: round(v, 4) for k, v in traj_errs_np.items()} print("\n=== Evaluation Summary ===") print(json.dumps(results, indent=2)) result_path = pred_json.replace(".json", "_eval_results.json") with open(result_path, "w", encoding="utf-8") as f: json.dump(results, f, indent=2, ensure_ascii=False) with open(pred_json.replace(".json", "_highlevel_mismatch.json"), "w", encoding="utf-8") as f: json.dump(mismatch_high, f, indent=2, ensure_ascii=False) print(f"Evaluation summary saved to {result_path}") return results def parse_args(): parser = argparse.ArgumentParser(description="Evaluate EventDrive planning task results.") parser.add_argument("--pred-json", required=True, help="Path to prediction JSON with model_output fields.") return parser.parse_args() if __name__ == "__main__": args = parse_args() evaluate(args.pred_json)