summarize_eval.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """把 `scripts/video_eval.py` 的 JSONL 汇总成可读结论(L3 回放报告)。"""
  2. import json
  3. import os
  4. import sys
  5. from collections import Counter
  6. REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  7. LABELS = {0: "not_on_train", 1: "bed", 2: "flatbed", 255: "unknown"}
  8. def runs(rows, key):
  9. out = []
  10. cur = None
  11. for row in rows:
  12. state = int(row[key])
  13. if cur is None or cur["state"] != state:
  14. cur = {"state": state, "start_ts": row["ts"], "end_ts": row["ts"],
  15. "frames": 0, "stable": 0, "conf_sum": 0.0}
  16. out.append(cur)
  17. cur["frames"] += 1
  18. cur["end_ts"] = row["ts"]
  19. cur["stable"] += 1 if row.get("prod_stable" if key == "prod" else "diag_stable") else 0
  20. cur["conf_sum"] += float(row.get("prod_conf" if key == "prod" else "diag_conf", 0.0))
  21. for run in out:
  22. run["dur_s"] = round(run["end_ts"] - run["start_ts"], 2)
  23. run["conf"] = round(run["conf_sum"] / max(1, run["frames"]), 3)
  24. del run["conf_sum"]
  25. return out
  26. def main():
  27. paths = sys.argv[1:]
  28. if not paths:
  29. print("用法:python scripts/summarize_eval.py <video_eval 输出的 jsonl> [更多 jsonl]")
  30. return 2
  31. report = {}
  32. for path in paths:
  33. rows = [json.loads(line) for line in open(path, encoding="utf-8") if line.strip()]
  34. tag = os.path.splitext(os.path.basename(path))[0]
  35. prod_runs = runs(rows, "prod")
  36. cand = Counter(int(r["cand"]) for r in rows)
  37. total = float(len(rows))
  38. report[tag] = {
  39. "frames": len(rows),
  40. "duration_s": round(rows[-1]["ts"] - rows[0]["ts"], 2) if rows else 0.0,
  41. "prod_frames": {LABELS[k]: v for k, v in sorted(cand.items())},
  42. "prod_runs": len(prod_runs),
  43. "prod_state_frames": {
  44. LABELS[s]: sum(r["frames"] for r in prod_runs if r["state"] == s)
  45. for s in (0, 1, 2, 255)
  46. },
  47. "prod_state_seconds": {
  48. LABELS[s]: round(sum(r["dur_s"] for r in prod_runs if r["state"] == s), 2)
  49. for s in (0, 1, 2, 255)
  50. },
  51. "prod_longest": {
  52. LABELS[s]: max([r for r in prod_runs if r["state"] == s],
  53. key=lambda r: r["frames"], default=None)
  54. for s in (0, 1, 2, 255)
  55. },
  56. "prod_stable_frames": sum(1 for r in rows if r["prod_stable"]),
  57. "commit_frames": sum(1 for r in rows if r["committed"]),
  58. "first_commit_ts": next((r["ts"] for r in rows if r["committed"]), None),
  59. "stable_ratio": round(sum(1 for r in rows if r["prod_stable"]) / max(1.0, total), 3),
  60. "abstain": dict(Counter(r["abstain_reason"] or "-" for r in rows).most_common(6)),
  61. "commit_reasons": dict(Counter(
  62. str(r["commit_reason"]).split(" ")[0] for r in rows).most_common(6)),
  63. "candidate_non255_seconds": round(sum(
  64. 1 for r in rows if int(r["cand"]) != 255) / 30.0, 1),
  65. "first_runs": prod_runs[:8],
  66. }
  67. out = os.path.join(REPO, "runs", "video_eval", "report.json")
  68. os.makedirs(os.path.dirname(out), exist_ok=True)
  69. with open(out, "w", encoding="utf-8") as fh:
  70. json.dump(report, fh, ensure_ascii=False, indent=2)
  71. print(json.dumps(report, ensure_ascii=False, indent=2))
  72. return 0
  73. if __name__ == "__main__":
  74. sys.exit(main())