#!/usr/bin/env python3 """ Benchmark Review - 脚本1: 视频与任务数量对比 输入: benchmark_single 三个 Part 的 JSON;可选 --hf-list 或 --hf-dir。 输出: 每 Part 仅在 benchmark / 仅在 HF 的 video 列表;Part1 每视频 counting/motion 统计。 Metadata 规范来源(用户指定): - Part1:HF Torwnexial/ready4label → new_long_video/corrected_json_2 - Part2+3:HF Torwnexial/ready4label → top20merge_full/corrected_json_2 """ import argparse import json import os import sys from pathlib import Path from typing import Any, Dict, List, Set REPORT_MD = """# Benchmark 整体 Review 报告 ## 1. 结论汇总(含依据) ### 1.1 Part2 与 recheck 源不一致(高优先级) - **结论**:Part2 当前 source_folder 与用户指定的中短视频 recheck 源不一致。 - **依据**:part2 JSON 中 source_folder 为 top20merge/corrected_json;用户指定 recheck 在 top20merge_full/corrected_json_2。 - **建议**:从 top20merge_full/corrected_json_2 重新生成 Part2 benchmark;eval_cambw.py 已支持该映射。 ### 1.2 长视频 30% counting 删除 - **结论**:Part1 object_counting=110,汇总时应对现有 110 条算平均。 - **依据**:metadata.task_counts.object_counting: 110;用户说明 30% counting 被删、算平均时不算这部分。 ### 1.3 80% appearance order 更新 - **结论**:需抽样核对 benchmark 与 HF corrected 的 appearance 题数及「至少 5 条」规则。 - **依据**:用户说明 80% appearance order 被 update、不足 5 个补到 5 个。 ### 1.4 视频列表与 HF 对应 - **结论**:Part1 40 个 video 与 HF new_long_video/corrected_json_2 一致;Part2/Part3 需用 --hf-list 或 --hf-dir 做差集核对。 - **依据**:Part1 total_videos: 40;Part2 426,Part3 382。 ### 1.5 empty_qa_ids - **结论**:需对 empty_qa_ids 中若干 qa_id 在 HF 对应 JSON 中核对是否仍为空或已更新。 - **依据**:part1_empty_qa_ids.txt 共 10 行(motion_direction)。 ### 1.6 submit_jobs 对齐 - **结论**:汇总/Overall 只使用当前 benchmark 存在的 task 集合。 - **建议**:确认 aggregate 与 results_summary 与 benchmark_single 一致。 ## 2. 逐个检查清单 1. 运行 check_part2_source.py 确认 Part2 源;必要时从 top20merge_full/corrected_json_2 重新生成 Part2。 2. 运行 compare_video_and_tasks.py --bench-dir benchmark_single,可选 --hf-dir 做 Part1 逐视频对比。 3. 对 empty_qa_ids 中 3-5 个 qa_id 在 HF 对应 JSON 中核对。 4. 选 3-5 个长视频+短视频对比 benchmark 与 HF 的 appearance 题数及 checkpoints。 5. 确认 aggregate/Overall 只含当前 benchmark 的 task;counting 不包含已删 30%。 ## 3. 脚本说明 - compare_video_and_tasks.py:提取三 Part video 列表与 Part1 每视频任务统计;可选 --hf-list/--hf-dir 输出仅在 benchmark/仅在 HF 及 Part1 与 HF 差异。 - check_part2_source.py:读取 Part2 source_folder 与 eval_cambw 映射,输出是否与 recheck 源一致。 """ def load_benchmark(path: str) -> Dict[str, Any]: with open(path, encoding="utf-8") as f: return json.load(f) def video_names_from_benchmark(data: Dict[str, Any]) -> Set[str]: out: Set[str] = set() for v in data.get("videos") or data.get("data") or []: name = v.get("video_name") if name: out.add(name) return out def count_tasks_by_type(tasks: List[Dict]) -> Dict[str, int]: counts: Dict[str, int] = {} for t in tasks: tt = t.get("task_type") or "" variant = t.get("variant", "") if tt == "frame_recall" and variant: key = "frame_recall_" + str(variant) else: key = tt counts[key] = counts.get(key, 0) + 1 return counts def per_video_task_counts_part1(data: Dict[str, Any]) -> Dict[str, Dict[str, int]]: out: Dict[str, Dict[str, int]] = {} for v in data.get("videos") or []: name = v.get("video_name") if not name: continue tasks = v.get("tasks") or [] counts = count_tasks_by_type(tasks) af = counts.get("first_appearance_recall_choice", 0) + counts.get("first_appearance_recall_direct", 0) al = counts.get("last_appearance_recall_choice", 0) + counts.get("last_appearance_recall_direct", 0) out[name] = { "object_counting": counts.get("object_counting", 0), "motion_direction": counts.get("motion_direction", 0), "first_appearance": af, "last_appearance": al, } return out def count_tasks_in_corrected_json(json_path: str) -> Dict[str, int]: try: with open(json_path, encoding="utf-8") as f: raw = json.load(f) except Exception as e: return {"_error": str(e)} tasks = raw.get("tasks") or raw.get("qa") or (raw if isinstance(raw, list) else []) counts: Dict[str, int] = {} for t in tasks: if not isinstance(t, dict): continue tt = (t.get("task_type") or t.get("type") or "").strip() if not tt: continue variant = t.get("variant", "") key = "frame_recall_" + str(variant) if tt == "frame_recall" and variant else tt counts[key] = counts.get(key, 0) + 1 return counts def hf_video_set_from_list_file(path: str) -> Set[str]: out: Set[str] = set() with open(path, encoding="utf-8") as f: for line in f: s = line.strip() if s: out.add(s.replace(".json", "") if s.endswith(".json") else s) return out def hf_video_set_from_dir(dir_path: str) -> Set[str]: out: Set[str] = set() for f in Path(dir_path).iterdir(): if f.suffix.lower() == ".json" and f.is_file(): out.add(f.stem) return out def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--bench-dir", type=str, default=None) ap.add_argument("--hf-list", type=str, default=None) ap.add_argument("--hf-dir", type=str, default=None) ap.add_argument("--part", type=str, choices=["1", "2", "3", "all"], default="all") ap.add_argument("--write-report", action="store_true", help="将整体 review 报告写入 benchmark_review/BENCHMARK_REVIEW_REPORT.md") args = ap.parse_args() if args.write_report: report_path = Path(__file__).resolve().parent / "BENCHMARK_REVIEW_REPORT.md" report_path.write_text(REPORT_MD, encoding="utf-8") print("Wrote", report_path) return bench_dir = args.bench_dir or str(Path(__file__).resolve().parent.parent / "benchmark_single") if not os.path.isdir(bench_dir): print("Error: bench-dir not found:", bench_dir, file=sys.stderr) sys.exit(1) hf_set = None if args.hf_list and os.path.isfile(args.hf_list): hf_set = hf_video_set_from_list_file(args.hf_list) print("[HF list] Loaded", len(hf_set), "video names") elif args.hf_dir and os.path.isdir(args.hf_dir): hf_set = hf_video_set_from_dir(args.hf_dir) print("[HF dir] Found", len(hf_set), "JSON files") configs = [ ("part1", "part1_long_videos_-_dual_format_appearance.json", "Part1"), ("part2", "part2_short_videos_-_place_&_motion.json", "Part2"), ("part3", "part3_short_videos_-_objects_with_dual_format_fixed_choices.json", "Part3"), ] idx = {"1": 0, "2": 1, "3": 2, "all": None}.get(args.part) for i, (key, filename, label) in enumerate(configs): if idx is not None and idx != i: continue path = os.path.join(bench_dir, filename) if not os.path.isfile(path): continue data = load_benchmark(path) bench_videos = video_names_from_benchmark(data) meta = data.get("metadata") or {} print("\n==========", label, "==========") print(" Benchmark videos:", len(bench_videos), "(metadata total_videos:", meta.get("total_videos"), ")") if hf_set is not None: only_bench = bench_videos - hf_set only_hf = hf_set - bench_videos print(" 仅在 Benchmark:", len(only_bench)) if only_bench: for x in sorted(only_bench)[:15]: print(" -", x) if len(only_bench) > 15: print(" ...", len(only_bench), "total") print(" 仅在 HF:", len(only_hf)) if only_hf: for x in sorted(only_hf)[:15]: print(" -", x) if len(only_hf) > 15: print(" ...", len(only_hf), "total") if key == "part1": per_video = per_video_task_counts_part1(data) total_c = sum(p["object_counting"] for p in per_video.values()) total_m = sum(p["motion_direction"] for p in per_video.values()) print(" Part1 汇总: object_counting=", total_c, ", motion_direction=", total_m) if args.hf_dir and os.path.isdir(args.hf_dir): diffs = [] for vname in sorted(per_video.keys()): bc, bm = per_video[vname]["object_counting"], per_video[vname]["motion_direction"] jpath = os.path.join(args.hf_dir, vname + ".json") if os.path.isfile(jpath): hf_counts = count_tasks_in_corrected_json(jpath) if "_error" in hf_counts: diffs.append((vname, "HF read error")) continue hc, hm = hf_counts.get("object_counting", 0), hf_counts.get("motion_direction", 0) if bc != hc or bm != hm: diffs.append((vname, "c b=%s hf=%s m b=%s hf=%s" % (bc, hc, bm, hm))) else: diffs.append((vname, "HF file not found")) print(" 与 HF 目录差异数:", len(diffs)) for vname, msg in diffs[:20]: print(" ", vname, msg) if len(diffs) > 20: print(" ...", len(diffs), "total") print("\nDone.") if __name__ == "__main__": main()