File size: 5,776 Bytes
8eca19c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #!/usr/bin/env python3
"""
从 Cambrian-W benchmark_single 的 part1 / part2 / part3 JSON 生成 lmms-eval 用的 doc 列表(JSONL)。
- part1_long: part1_long_videos_-_dual_format_appearance.json
- part2_3_short: part2_short_videos_-_place_&_motion.json + part3_short_videos_-_objects_with_dual_format_fixed_choices.json
每个 doc = 一个 checkpoint 级别的评估项(含 video_path, task_type, question, answer, frame_indices 等)。
"""
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path
from typing import Any, Dict, List, Optional
SOURCE_FOLDER_TO_DATA_SUBDIR = {
"new_long_video/corrected_json_2": "new_long_video_persp",
"top20merge/corrected_json": "top20merge_0207_persp",
"long_video/corrected_json_2": "long_video_persp",
"top20merge_full/corrected_json_2": "top20merge_0207_persp",
}
def resolve_video_path(video: Dict[str, Any], data_root: str) -> Optional[str]:
video_path = video.get("video_path") or video.get("video")
if video_path:
video_path = str(video_path).strip()
if os.path.isabs(video_path):
for old_prefix in [
"/lustre/fs12/portfolios/nvr/projects/nvr_av_end2endav/users/ymingli/projects/xty/cambw/data",
"/lustre/fsw/portfolios/nvr/users/ymingli/projects/xty/cambw/data",
"/data",
"/path/to/data",
]:
if video_path.startswith(old_prefix):
video_path = os.path.join(data_root, video_path[len(old_prefix):].lstrip("/"))
break
else:
video_path = os.path.join(data_root, video_path)
return video_path
video_name = video.get("video_name")
source_folder = video.get("source_folder")
if not video_name or not source_folder:
return None
subdir = SOURCE_FOLDER_TO_DATA_SUBDIR.get(source_folder)
if not subdir:
return None
base = video_name if video_name.endswith(".mp4") else f"{video_name}.mp4"
return os.path.join(data_root, subdir, base)
def task_has_valid_checkpoints(task: Dict[str, Any]) -> bool:
return any(cp.get("answer") is not None for cp in task.get("checkpoints", []))
def flatten_part1(bench_path: Path, data_root: str) -> List[Dict[str, Any]]:
with bench_path.open("r") as f:
data = json.load(f)
docs = []
for v in data.get("videos") or []:
video_path = resolve_video_path(v, data_root)
if not video_path:
continue
video_name = v.get("video_name", "")
tasks = v.get("tasks") or []
for ti, t in enumerate(tasks):
if not task_has_valid_checkpoints(t):
continue
ttype = t.get("task_type", "")
if t.get("variant"):
ttype = f"{ttype}_{t['variant']}"
for cpi, cp in enumerate(t.get("checkpoints", [])):
if cp.get("answer") is None:
continue
doc = {
"doc_id": f"{video_name}|{ti}|{ttype}|{cpi}",
"video_name": video_name,
"video_path": video_path,
"task_type": ttype,
"question": t.get("question", ""),
"answer": cp["answer"],
}
if ttype == "frame_recall" and cp.get("frames"):
doc["frame_indices"] = [int(f["frame_idx"]) for f in cp["frames"]]
else:
doc["frame_indices"] = None
if cp.get("options") is not None:
doc["options"] = cp["options"]
else:
doc["options"] = None
if t.get("subset_concepts") is not None:
doc["subset_concepts"] = t["subset_concepts"]
else:
doc["subset_concepts"] = None
docs.append(doc)
return docs
def flatten_part2_or_part3(bench_path: Path, data_root: str) -> List[Dict[str, Any]]:
return flatten_part1(bench_path, data_root)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--benchmark-dir", type=str, required=True)
parser.add_argument("--data-root", type=str, required=True)
parser.add_argument("--out-dir", type=str, default="data")
args = parser.parse_args()
bench_dir = Path(args.benchmark_dir)
out_dir = Path(args.out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
part1_file = bench_dir / "part1_long_videos_-_dual_format_appearance.json"
if part1_file.exists():
docs1 = flatten_part1(part1_file, args.data_root)
out1 = out_dir / "part1_long.jsonl"
with out1.open("w") as f:
for d in docs1:
f.write(json.dumps(d, ensure_ascii=False) + "\n")
print(f"part1_long: {len(docs1)} docs -> {out1}")
else:
print(f"Skip part1: not found {part1_file}")
part2_file = bench_dir / "part2_short_videos_-_place_&_motion.json"
part3_file = bench_dir / "part3_short_videos_-_objects_with_dual_format_fixed_choices.json"
docs2_3 = []
if part2_file.exists():
docs2_3.extend(flatten_part2_or_part3(part2_file, args.data_root))
print(f"part2: {len(docs2_3)} docs")
if part3_file.exists():
n_before = len(docs2_3)
docs2_3.extend(flatten_part2_or_part3(part3_file, args.data_root))
print(f"part3: +{len(docs2_3) - n_before} docs")
if docs2_3:
out2_3 = out_dir / "part2_3_short.jsonl"
with out2_3.open("w") as f:
for d in docs2_3:
f.write(json.dumps(d, ensure_ascii=False) + "\n")
print(f"part2_3_short: {len(docs2_3)} docs -> {out2_3}")
if __name__ == "__main__":
main()
|