Download code/clinical3d_eval/run.py from clinicalspace/Clinical3D-Bench: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/datasets/clinicalspace/Clinical3D-Bench/resolve/main/code/clinical3d_eval/run.py
- Command line
-
hf download hf://datasets/clinicalspace/Clinical3D-Bench/code/clinical3d_eval/run.py
-
curl -L -o run.py https://huggingface.co/datasets/clinicalspace/Clinical3D-Bench/resolve/main/code/clinical3d_eval/run.py
4.58 kB
| """Run a registered MLLM on Clinical3D-Bench with the paper's protocol. | |
| python -m clinical3d_eval.run --model qwen3vl_8b --data /path/to/Clinical3D-Bench --out preds/qwen3vl_8b.jsonl | |
| python -m clinical3d_eval.run --list | |
| Each question gets 32 frames (or the model's frame budget, see --list) chosen as round(linspace(0, 59, k)) from its | |
| 60-frame clip, plus the two frames a Re-ID question names (anchor_frames) when they are not already sampled. Every | |
| frame is preceded by its timestamp; the question and lettered options follow; greedy decoding. | |
| --weights is a local checkpoint directory or a Hugging Face repo id (default: the spec's repo id). | |
| --render evaluates on the source renders (videos_render/) instead of the photorealistic clips. | |
| Resumable: ids already in --out are skipped. Score with python -m clinical3d_eval.score. | |
| """ | |
| import argparse | |
| import hashlib | |
| import json | |
| import time | |
| from pathlib import Path | |
| from clinical3d_eval.prompt import FOOTER, build_messages | |
| from clinical3d_eval.sampling import extract_frames, protocol_indices | |
| from clinical3d_eval.vlm import registry | |
| from clinical3d_eval.vlm.base import COT_FOOTER, parse_reply | |
| def segments(it, spec, frames_k, data, cache, video_dir, lang): | |
| idx = protocol_indices(it['n_frames'], frames_k, it.get('anchor_frames', [])) | |
| frames = extract_frames(data / video_dir / f"{it['clip']}.mp4", idx, cache / video_dir / it['clip']) | |
| seg = build_messages(it, frames, [i / it['fps'] for i in idx], lang) | |
| if spec['mode'] == 'cot': # same question, reasoning asked for before the letter | |
| kind, text = seg[-1] | |
| seg[-1] = (kind, text.replace(FOOTER[lang], COT_FOOTER[lang])) | |
| return seg, len(frames) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument('--model') | |
| ap.add_argument('--weights') | |
| ap.add_argument('--data', type=Path, help='the dataset directory (questions.jsonl, videos/)') | |
| ap.add_argument('--out', type=Path) | |
| ap.add_argument('--cache', type=Path, help='where decoded frames go (default <data>/.frames)') | |
| ap.add_argument('--render', action='store_true', help='use videos_render/ (source renders)') | |
| ap.add_argument('--lang', choices=['en', 'zh'], default='en') | |
| ap.add_argument('--batch', type=int, default=1) | |
| ap.add_argument('--limit', type=int, help='first N questions only (smoke test)') | |
| ap.add_argument('--shard', type=int, nargs=2, metavar=('K', 'N'), help='questions k, k+n, k+2n, ...') | |
| ap.add_argument('--list', action='store_true') | |
| a = ap.parse_args() | |
| reg = registry() | |
| if a.list: | |
| for k, s in reg.items(): | |
| print(f"{k:30s} {s['hf']:46s} frames={s['frames']:<3d} mode={s['mode']:7s} {s['note']}") | |
| return | |
| spec = reg[a.model] | |
| items = [json.loads(l) for l in (a.data / 'questions.jsonl').read_text().splitlines()] | |
| if a.shard: | |
| items = items[a.shard[0]::a.shard[1]] | |
| if a.limit: | |
| items = items[:a.limit] | |
| done = {json.loads(l)['id'] for l in a.out.read_text().splitlines()} if a.out.exists() else set() | |
| todo = [it for it in items if it['id'] not in done] | |
| a.out.parent.mkdir(parents=True, exist_ok=True) | |
| print(a.model, 'todo', len(todo), 'of', len(items), flush=True) | |
| if not todo: | |
| return | |
| video_dir = 'videos_render' if a.render else 'videos' | |
| cache = a.cache or a.data / '.frames' | |
| mod = __import__(f'clinical3d_eval.vlm.{spec["module"]}', fromlist=['x']) | |
| adapter = getattr(mod, spec['adapter'])(a.weights or spec['hf'], spec) | |
| adapter.load() | |
| for k in range(0, len(todo), a.batch): | |
| batch = todo[k:k + a.batch] | |
| segs = [segments(it, spec, spec['frames'], a.data, cache, video_dir, a.lang) for it in batch] | |
| t0 = time.monotonic() | |
| raws = adapter.generate([s for s, _ in segs]) | |
| seconds = round((time.monotonic() - t0) / len(batch), 2) | |
| with a.out.open('a') as f: | |
| for it, (seg, n_img), raw in zip(batch, segs, raws): | |
| text = json.dumps([v if kind == 'text' else '<image>' for kind, v in seg], ensure_ascii=False) | |
| rec = dict(id=it['id'], model=a.model, video=video_dir, frames=spec['frames'], n_images=n_img, | |
| lang=a.lang, raw=raw, pred=parse_reply(raw, it, spec['mode']), seconds=seconds, | |
| prompt_sha256=hashlib.sha256(text.encode()).hexdigest()) | |
| f.write(json.dumps(rec, ensure_ascii=False) + '\n') | |
| print(it['id'], repr(raw.strip()[-30:]), rec['pred'], seconds, flush=True) | |
| if __name__ == '__main__': | |
| main() | |