File size: 10,469 Bytes
84c8a9d
 
3081b91
 
 
 
84c8a9d
 
 
 
 
3081b91
 
84c8a9d
 
 
 
 
3081b91
 
 
 
 
 
84c8a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d1d87
 
 
 
 
 
 
 
84c8a9d
 
 
50d1d87
 
 
84c8a9d
 
50d1d87
 
84c8a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3081b91
 
 
 
84c8a9d
 
50d1d87
 
 
 
 
 
84c8a9d
 
 
 
 
 
 
 
3081b91
 
 
84c8a9d
 
 
50d1d87
 
84c8a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d1d87
 
84c8a9d
 
3081b91
84c8a9d
 
 
 
50d1d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84c8a9d
 
 
 
50d1d87
84c8a9d
 
 
 
 
 
 
 
 
 
 
50d1d87
 
84c8a9d
 
 
 
 
 
 
 
 
 
50d1d87
 
84c8a9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""Standalone Video-MME mini eval for DW-KhotTaeVL-2B-QueryFrames.

This script reproduces the MCQ-mode (no task_type) QA-frame numbers
reported in the model card. It is fully self-contained — only
depends on the `dw_queryframes.py` module shipped in this same
directory plus publicly-available datasets / models from Hugging Face.

Usage::

    pip install torch transformers pillow decord huggingface_hub pandas pyarrow

    # MCQ mode (query-aware frame selection, no task_type)
    python eval_videomme.py --mode mcq --n-questions 50

    # Stock baseline (uniform 8 frames; matches the stock numbers
    # in the model card)
    python eval_videomme.py --mode stock-uniform --n-questions 50

For task-aware MCQ mode (uses Video-MME's own task_type label to
route Object/Temporal Reasoning questions to uniform sampling),
run both modes above then combine via ``build_hybrid.py``.

The legacy CLI value ``--mode wild`` is accepted as a deprecated
alias for ``--mode mcq``.

Outputs JSON with ``summary`` + ``results`` keys.
"""
from __future__ import annotations

import argparse
import json
import os
import re
import sys
import time
import zipfile
from pathlib import Path

import pandas as pd
from huggingface_hub import hf_hub_download
from PIL import Image


# ---------------------------------------------------------------------------
# Public Video-MME mini assets (lmms-lab/Video-MME on Hugging Face).
# ---------------------------------------------------------------------------
REPO_ID = "lmms-lab/Video-MME"
REPO_TYPE = "dataset"
DEFAULT_CHUNKS = ["videos_chunked_01.zip"]
PARQUET_NAME = "videomme/test-00000-of-00001.parquet"

# Cache lives next to this script so a fresh ``git clone`` of the HF
# repo can reproduce results without touching the user's home directory.
CACHE_DIR = Path(__file__).resolve().parent / "cache" / "videomme_mini"
CACHE_DIR.mkdir(parents=True, exist_ok=True)

PROMPT_TEMPLATE = (
    "This is a representative frame from a video.\n"
    "Select the best answer based on the video.\n\n"
    "Question: {question}\n"
    "Options:\n{options}\n"
    "Answer with only the letter."
)

ANSWER_RE = re.compile(r"\b([ABCD])\b", re.IGNORECASE)
ALPTD_ANSWER_RE = re.compile(r"Answer:\s*([ABCD])\b", re.IGNORECASE)


# ---------------------------------------------------------------------------
# Asset management — fetch + unzip into CACHE_DIR.
# ---------------------------------------------------------------------------
def download_assets(chunks: list[str]) -> tuple[Path, list[Path]]:
    print(f"[eval] ensuring {PARQUET_NAME} ...")
    pq_path = Path(hf_hub_download(
        repo_id=REPO_ID, repo_type=REPO_TYPE, filename=PARQUET_NAME,
        cache_dir=str(CACHE_DIR / "hf"),
    ))
    zip_paths: list[Path] = []
    for name in chunks:
        zp = Path(hf_hub_download(
            repo_id=REPO_ID, repo_type=REPO_TYPE, filename=name,
            cache_dir=str(CACHE_DIR / "hf"),
        ))
        zip_paths.append(zp)
    return pq_path, zip_paths


def unzip_chunks(zip_paths: list[Path]) -> Path:
    video_dir = CACHE_DIR / "video"
    video_dir.mkdir(parents=True, exist_ok=True)
    for zp in zip_paths:
        existing = {p.stem for p in video_dir.glob("*.mp4")}
        with zipfile.ZipFile(zp, "r") as zf:
            to_extract = [
                m for m in zf.namelist()
                if m.endswith(".mp4") and Path(m).stem not in existing
            ]
            if to_extract:
                print(f"[eval] extracting {len(to_extract)} mp4s from {zp.name}")
                for m in to_extract:
                    with zf.open(m) as src, open(video_dir / Path(m).name, "wb") as dst:
                        dst.write(src.read())
    return video_dir


def load_questions(pq_path: Path, video_dir: Path, limit: int,
                   start_idx: int = 0) -> pd.DataFrame:
    """Load questions filtered to videos on disk.

    ``start_idx`` skips the first N rows after the videoID filter, which
    is useful for chunked / resumable evaluation when the underlying
    accelerator (e.g. Apple MPS) corrupts state on long runs.
    """
    df = pd.read_parquet(pq_path)
    ids = {p.stem for p in video_dir.glob("*.mp4")}
    df = df[df["videoID"].isin(ids)].reset_index(drop=True)
    total_avail = len(df)
    if start_idx > 0:
        df = df.iloc[start_idx:].reset_index(drop=True)
    if limit > 0 and len(df) > limit:
        df = df.iloc[:limit].copy()
    print(f"[eval] using {len(df)} questions "
          f"(start_idx={start_idx}, total_available={total_avail})")
    return df


def format_options(options) -> str:
    return "\n".join(str(o).strip() for o in options)


def extract_letter(text: str) -> str | None:
    s = text or ""
    m = ALPTD_ANSWER_RE.search(s)
    if m:
        return m.group(1).upper()
    m = ANSWER_RE.search(s)
    return m.group(1).upper() if m else None


# ---------------------------------------------------------------------------
# Frame selection lives in the local QueryFrames module.
# ---------------------------------------------------------------------------
sys.path.insert(0, str(Path(__file__).resolve().parent))
from dw_queryframes import QueryFrames  # noqa: E402


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--base", default="Qwen/Qwen3-VL-2B-Instruct")
    ap.add_argument("--clip-model", default="openai/clip-vit-large-patch14")
    ap.add_argument("--mode", choices=["mcq", "wild", "stock-uniform"],
                    default="mcq",
                    help="'mcq' = query-aware MCQ mode (default); "
                         "'wild' = deprecated alias for 'mcq'; "
                         "'stock-uniform' = stock baseline (uniform 8 frames)")
    ap.add_argument("--tag", default="")
    ap.add_argument("--n-questions", type=int, default=50,
                    help="number of questions to score in this run (after start-idx)")
    ap.add_argument("--start-idx", type=int, default=0,
                    help="skip the first N filtered questions; useful for "
                         "chunked / resumable evaluation when the accelerator "
                         "(e.g. Apple MPS) corrupts state on long runs")
    ap.add_argument("--n-frames", type=int, default=8)
    ap.add_argument("--n-candidates", type=int, default=32)
    ap.add_argument("--max-pixels", type=int, default=262144)
    ap.add_argument("--max-new-tokens", type=int, default=8)
    ap.add_argument("--out-json", default=None,
                    help="output JSON path (auto-named if omitted)")
    ap.add_argument("--chunks", nargs="+", default=DEFAULT_CHUNKS)
    args = ap.parse_args()
    # Legacy alias: 'wild' → 'mcq' (deprecated).
    if args.mode == "wild":
        args.mode = "mcq"

    pq_path, zip_paths = download_assets(args.chunks)
    video_dir = unzip_chunks(zip_paths)
    df = load_questions(pq_path, video_dir, args.n_questions,
                        start_idx=args.start_idx)

    os.environ.setdefault("PYTORCH_ENABLE_MPS_FALLBACK", "1")

    fv = QueryFrames(
        base_model=args.base,
        clip_model=args.clip_model,
        device="auto",
        max_pixels=args.max_pixels,
        max_new_tokens=args.max_new_tokens,
        n_frames=args.n_frames,
        n_candidates=args.n_candidates,
    )

    results = []
    correct = 0
    t0 = time.time()
    for i, row in df.iterrows():
        # Absolute index into the full filtered df (so chunks have unique idx).
        abs_idx = int(i) + args.start_idx
        video_path = video_dir / f"{row['videoID']}.mp4"

        # MCQ mode = query-aware (task_type=None lets QA path run).
        # Stock-uniform = pass a known no-frame-gain task name to force
        #                 the uniform-fallback path (matches stock 8f
        #                 baseline behavior).
        forced_uniform = (args.mode == "stock-uniform")
        try:
            out = fv.answer_mcq(
                video_path=video_path,
                question=row["question"],
                options=list(row["options"]),
                task_type=("Object Reasoning" if forced_uniform else None),
            )
        except Exception as e:
            # MPS / accelerator state corruption sometimes triggers
            # mid-run on long inference. Save what we have and exit so
            # an outer chunked-runner can pick up from start-idx + i.
            print(f"[eval] FATAL at q {abs_idx}: {type(e).__name__}: {e}",
                  flush=True)
            print(f"[eval] saving partial results ({len(results)}) "
                  f"and exiting so caller can resume.", flush=True)
            break
        gold = row["answer"].strip().upper()
        ok = out["pred"] == gold
        correct += int(ok)
        results.append({
            "index": abs_idx,
            "videoID": row["videoID"],
            "task_type": row.get("task_type", ""),
            "gold": gold,
            "pred": out["pred"],
            "raw": out["raw"][:200],
            "frames_used": out["frames_used"],
            "latency_clip_s": out["latency_clip_s"],
            "latency_gen_s": out["latency_gen_s"],
            "correct": ok,
        })
        run = correct / (i + 1)
        print(f"[eval] [{abs_idx+1}/{args.start_idx + len(df)}] "
              f"gold={gold} pred={out['pred']} "
              f"acc_so_far={run:.3f} clip={out['latency_clip_s']}s "
              f"gen={out['latency_gen_s']}s", flush=True)

    n = len(results)
    acc = correct / n if n else 0.0
    summary = {
        "model_base": args.base,
        "clip_model": args.clip_model,
        "mode": args.mode,
        "tag": args.tag,
        "start_idx": args.start_idx,
        "n_questions_attempted": len(df),
        "n_questions": n,
        "n_frames": args.n_frames,
        "n_candidates": args.n_candidates,
        "max_pixels": args.max_pixels,
        "max_new_tokens": args.max_new_tokens,
        "accuracy": round(acc, 4),
        "wall_time_s": round(time.time() - t0, 1),
    }

    out_path = args.out_json
    if out_path is None:
        tag = (args.tag or args.mode)
        out_path = str(CACHE_DIR.parent / f"eval_{tag}_{n}q.json")
    Path(out_path).parent.mkdir(parents=True, exist_ok=True)
    Path(out_path).write_text(json.dumps(
        {"summary": summary, "results": results}, indent=2))
    print(f"\n[eval] mode={args.mode}  acc={acc:.4f}  ({correct}/{n})  saved {out_path}")
    return 0


if __name__ == "__main__":
    sys.exit(main())