File size: 1,735 Bytes
1ccc67e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()