Clinical3D-Bench / code /examples /custom_model.py
yangbai123's picture
Clinical3D-Bench v1: questions, videos, masks, evaluation code, leaderboard
1ccc67e verified
Raw History Blame Contribute Delete
1.74 kB
"""Evaluate your own model: build the protocol prompt (timestamped frames + question + lettered options), call the
model, parse the letter, write a prediction file that clinical3d_eval.score reads.
python code/examples/custom_model.py --data /path/to/Clinical3D-Bench --out preds/my_model.jsonl --limit 20
Replace answer() with your model. segments is a list of ('text', str) and ('image', path to a JPEG frame) in order.
"""
import argparse
import json
from pathlib import Path
from clinical3d_eval.prompt import build_messages
from clinical3d_eval.sampling import extract_frames, protocol_indices
from clinical3d_eval.vlm.base import parse_reply
def answer(segments):
"""Your model here: return its reply text (e.g. 'B')."""
return 'A'
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--data', type=Path, required=True)
ap.add_argument('--out', type=Path, required=True)
ap.add_argument('--frames', type=int, default=32)
ap.add_argument('--limit', type=int)
a = ap.parse_args()
qs = [json.loads(l) for l in (a.data / 'questions.jsonl').read_text().splitlines()][:a.limit]
a.out.parent.mkdir(parents=True, exist_ok=True)
with a.out.open('w') as f:
for q in qs:
idx = protocol_indices(q['n_frames'], a.frames, q.get('anchor_frames', []))
frames = extract_frames(a.data / q['video'], idx, a.data / '.frames' / 'videos' / q['clip'])
segments = build_messages(q, frames, [i / q['fps'] for i in idx])
raw = answer(segments)
f.write(json.dumps(dict(id=q['id'], raw=raw, pred=parse_reply(raw, q, 'direct'))) + '\n')
print('wrote', len(qs), 'predictions to', a.out)
if __name__ == '__main__':
main()