"""官方 BT/RT 与外部时序/动作基准(TempCompass / MotionBench / VideoMME / LongVideoBench)统一题单读取与评分适配。""" import collections import json from pathlib import Path import sys ROOT = Path(__file__).resolve().parents[1] BT_TASKS = ('EPM', 'ASI', 'HLD') RT_TASKS = ('OCR', 'ACR', 'ATR', 'STU', 'FPD', 'OJR') # 外部 benchmark:题单已归一化为 2~5 选项单选。 EXT_BENCHMARKS = ('tempcompass', 'videomme', 'longvideobench', 'motionbench') def questions(benchmark): ext_manifest = ROOT / 'manifests' / f'{benchmark}_questions.jsonl' if benchmark in EXT_BENCHMARKS and ext_manifest.exists(): rows = [json.loads(line) for line in ext_manifest.read_text().splitlines() if line.strip()] else: rows = [json.loads(line) for line in (ROOT / 'manifests/eval_questions.jsonl').read_text().splitlines() if line.strip()] result = [] for item in rows: if item['benchmark'] != benchmark: continue source = item['annotation'] if benchmark == 'ovobench': row = dict(id=str(source['id']), video=source['video'], timestamp=float(source['realtime']), question=source['question'].strip(), options=source['options'], answer=chr(65 + source['gt']), task=source['task'], benchmark=benchmark) elif benchmark in EXT_BENCHMARKS: # 外部 benchmark 的 annotation 已含 question/options/answer(字母)/task; # 题目在整段视频观测完毕后提出,因此 timestamp = query_time = 视频时长。 row = dict(source) row.update(id=str(source['id']), benchmark=benchmark, question=str(source['question']).strip(), options=[str(o) for o in source['options']], answer=str(source['answer']).strip().upper(), task=str(source.get('task', 'default')), timestamp=float(item['query_time'])) else: row = dict(source) row.update(source_video=item['source_video'], query_time=float(item['query_time'])) result.append(row) if len({r['id'] for r in result}) != len(result): raise RuntimeError('官方题目ID不唯一') return result def groups(benchmark, shard=0, shards=1, require_media=True): # 按源视频互斥分片;同视频所有提问按真实时间推进,不能按题分片重复建库。 grouped = collections.defaultdict(list) for q in questions(benchmark): grouped[q['source_video']].append(q) from cgvm.sources import source_plan sources, _, _ = source_plan(benchmark) result = {} for index, video in enumerate(sorted(grouped)): if index % shards != shard: continue path = ROOT / sources[video]['path'] if require_media and not path.exists(): raise FileNotFoundError('正式评测缺媒体,不得缩小题单:' + str(path)) result[str(path)] = sorted(grouped[video], key=lambda q: (q['timestamp'], q['id'])) return result def official_score(q, raw): if raw is None: return False if q['benchmark'] in EXT_BENCHMARKS: # 官方评分:首个非空字符必须等于答案字母(与 StreamingBench count.py 同规则;answer=NA 返回 False,统计时按有效标签过滤)。 ans = str(q['answer']).strip().upper() if ans not in ('A', 'B', 'C', 'D', 'E'): return False text = str(raw).strip() return bool(text) and text[0].upper() == ans if q['benchmark'] == 'ovobench': sys.path.insert(0, str(ROOT / 'repos/OVO-Bench')) from utils.OVOBenchScore import OVOBenchOfflineScore results, _ = OVOBenchOfflineScore(None, None).calculate_score_backward_realtime([ dict(response=raw, ground_truth=q['answer'], task=q['task'])]) return bool(results[0]['score']) # 官方 StreamingBench count.py 按原始第一个字符比较;保留空/失败题在分母内。 return bool(raw) and raw[0] == q['answer'] def diagnostic_scores(q, raw): sys.path.insert(0, str(ROOT / 'repos/MEMO')) try: from memo.benchmarks.scoring import score_answer strict_choice, strict = score_answer(q, raw or '', 'strict') legacy_choice, legacy = score_answer(q, raw or '', 'legacy') except Exception: # 外部 benchmark 的 2/3/5 选项题不适用 MEMO 四选项诊断,主口径为 official_correct。 strict_choice = strict = legacy_choice = legacy = None return dict(official_correct=official_score(q, raw), strict_choice=strict_choice, strict_correct=strict, memo_legacy_choice=legacy_choice, memo_legacy_correct=legacy)