File size: 5,246 Bytes
765ab6e | 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 | """Loaders that turn each benchmark's annotation file into one flat list of
multiple-choice samples:
{"qid", "video", "question", "options": [str, ...], "answer": "A".."E", "meta": {...}}
`video` is an absolute path. `meta` holds the fields the scorer breaks results down by.
"""
import ast
import json
import os
import re
from typing import Dict, List
import pandas as pd
LETTERS = "ABCDEFGH"
DEFAULT_ROOT = "/workspace/chicheng/datasets"
def _videomme(root: str) -> List[Dict]:
d = os.path.join(root, "videomme")
df = pd.read_parquet(os.path.join(d, "videomme", "test-00000-of-00001.parquet"))
out = []
for r in df.itertuples():
# options are already "A. xxx"; strip the letter so every benchmark is formatted the same way
opts = [re.sub(r"^[A-H]\.\s*", "", o) for o in r.options]
out.append(dict(
qid=r.question_id,
video=os.path.join(d, "data", f"{r.videoID}.mp4"),
question=r.question,
options=opts,
answer=r.answer.strip(),
meta=dict(duration=r.duration, domain=r.domain, task_type=r.task_type),
))
return out
def _mlvu(root: str) -> List[Dict]:
d = os.path.join(root, "mlvu")
df = pd.read_parquet(os.path.join(d, "mlvu", "test-00000-of-00001.parquet"))
out = []
for r in df.itertuples():
# the question text embeds "(A) ..." option lines; keep only the question itself
q = re.split(r"\n\(A\)", r.question, maxsplit=1)[0].strip()
out.append(dict(
qid=f"{r.task_type}/{r.question_id}",
video=os.path.join(d, r.video_name),
question=q,
options=list(r.candidates),
answer=r.answer.strip(),
meta=dict(task_type=r.task_type, duration=float(r.duration)),
))
return out
def _longvideobench(root: str) -> List[Dict]:
d = os.path.join(root, "longvideobench")
out = []
for r in json.load(open(os.path.join(d, "lvb_val.json"))):
cands = r["candidates"]
if isinstance(cands, str):
cands = ast.literal_eval(cands)
out.append(dict(
qid=r["id"],
video=os.path.join(d, "videos", r["video_path"]),
question=r["question"],
options=list(cands),
answer=LETTERS[int(r["correct_choice"])],
meta=dict(duration_group=int(r["duration_group"]), question_category=r["question_category"],
level=r["level"]),
))
return out
BENCHMARKS = {
"videomme": _videomme,
"mlvu": _mlvu,
"longvideobench": _longvideobench,
}
def load_benchmark(name: str, root: str = DEFAULT_ROOT) -> List[Dict]:
samples = BENCHMARKS[name](root)
for s in samples:
assert 2 <= len(s["options"]) <= len(LETTERS), s["qid"]
assert s["answer"] in LETTERS[: len(s["options"])], (s["qid"], s["answer"])
return samples
# Prompts and answer parsing follow the official VideoLLaMA3 evaluation
# (VideoLLaMA3/evaluation/benchmarks/{videomme,mlvu,longvideobench}.py), without subtitles.
def build_prompt(bench: str, sample: Dict) -> str:
q, opts = sample["question"], sample["options"]
letters = LETTERS[: len(opts)]
if bench == "videomme":
return (
"Select the best answer to the following multiple-choice question based on the video. "
"Respond with only the letter (A, B, C, or D) of the correct option.\n"
f"{q}\n" + "".join(f"{l}. {o}\n" for l, o in zip(letters, opts))
+ "Answer with the option's letter from the given choices directly and only give the best option. "
"The best answer is:"
)
if bench == "mlvu":
return (
f"Question: {q}\nOptions:\n" + "".join(f"({l}) {o}\n" for l, o in zip(letters, opts))
+ "Answer with the option's letter from the given choices directly and only give the best option."
)
if bench == "longvideobench":
return "\n".join([f"Question: {q}"] + [f"{l}. {o}" for l, o in zip(letters, opts)]
+ ["Answer with the option's letter from the given choices directly."])
raise KeyError(bench)
def extract_choice(bench: str, response: str, options: List[str]) -> str:
"""Official parsing: first option letter (bracketed / spaced) after dropping the word
'answer'; otherwise the first option whose text appears in the response; '' if neither."""
letters = LETTERS[: len(options)]
r = response.replace("answer", "").replace("Answer", "")
if bench == "mlvu":
m = re.findall(rf"[\(,\ ]*[{letters[0]}-{letters[-1]}][\),\ ]*", r)
if m:
return re.search(rf"[{letters[0]}-{letters[-1]}]", m[0]).group(0)
else:
m = re.findall(rf"[\(\ \[]*([{letters[0]}-{letters[-1]}])[\)\.\ \]]*", r)
if m:
return m[0]
digit2word = dict(zip("0123456789", ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]))
for i, opt in enumerate(options):
opt = opt.strip().strip(".")
alt = digit2word.get(opt, opt)
if opt and (opt.lower() in response.lower() or alt.lower() in response.lower()):
return letters[i]
return ""
|