SceneBench / code /curate.py
EgoF0102's picture
SceneBench v1: frozen source-derived test benchmark and verified artifacts
7735593 verified
Raw History Blame Contribute Delete
3.97 kB
import json,random,collections,hashlib,shutil
from pathlib import Path
from common import *
from select_render import Selector,render,contacts
root=Path('/home/ach18533cl/workspace/SceneBench')
cfg=json.loads((root/'config.json').read_text());review=json.loads((root/'curation.json').read_text())
src=Sources('/home/ach18533cl/workspace/ori_data',cfg['test_videos'])
original=root/'work/precuration_benchmark.jsonl'
if not original.exists():shutil.copy(root/'release/benchmark.jsonl',original)
allrows=list(read_jsonl(original));rows=[r for r in allrows if r['id'] not in review['drop']]
candidates={r['candidate_id']:r for r in read_jsonl(root/'work/selected_candidates.jsonl')}
sel=Selector(src,root,cfg)
for r in rows:sel.accept(candidates[r['candidate_id']])
# Replace three cross-video V04 negatives with same-video late-Calot negatives.
pool=[c for c in read_jsonl(root/'work/candidates/V04.jsonl') if c['video']==66 and c['answer']=='No' and 520<=c['frames'][0]<661]
pool.sort(key=lambda c:(-sum(c['query']['scores']),abs(c['frames'][0]-640)))
replace_ids=['SCB-V04-0001','SCB-V04-0002','SCB-V04-0004'];replacement_log=[]
for qid in replace_ids:
c=next((c for c in pool if sel.allowed(c)),None)
if c is None:break
sel.accept(c);r=next(r for r in rows if r['id']==qid);old=r['candidate_id']
r.update(question=c['question'],options=c['choices'],answer_text=c['answer'],answer_class=c['answer_class'],video_id='VID66',frame_ids=c['frames'],source_frame_ids=[f*25 for f in c['frames']],episode_id=c['episode'],query=c['query'],tags=c['tags'],candidate_id=c['candidate_id'],source_evidence=src.source_evidence(66,c['frames']))
r['images']=[f'images/v66_{c["frames"][0]:06d}.jpg'];r['image_metadata']=[render(c,src.image_path(66,c['frames'][0]),root/'release'/r['images'][0],cfg)]
replacement_log.append(dict(id=qid,old_candidate_id=old,new_candidate_id=c['candidate_id'],frame=c['frames'][0],reason='Same-video, late-Calot negative control'))
for r in rows:
if r['id'] in review['point_patches']:
pt=review['point_patches'][r['id']];b=r['marker']['bbox'];assert b[0]<=pt[0]<=b[2] and b[1]<=pt[1]<=b[3],r['id']
r['marker']['point']=pt;r['marker']['placement']='visually_adjusted_within_source_bbox'
r['image_metadata']=[render(r,src.image_path(int(r['video_id'][3:]),r['frame_ids'][0]),root/'release'/r['images'][0],cfg)]
if r.get('marker'):r['marker']['review_required']=False
r['quality_status']='source_consistency_checked; all-selected-items visual screening' if r['task'] in review['fully_reviewed_tasks'] else 'source_consistency_checked; task-level stratified visual screening' if r['task'] in review['stratified_review_tasks'] else 'source_consistency_and_automated_image_quality_checked'
rng=random.Random(cfg['seed']+3);bytask=collections.defaultdict(list)
for r in rows:bytask[r['task']].append(r)
for task,rr in bytask.items():
for i,r in enumerate(rr):
others=[x for x in r['options'] if x!=r['answer_text']];rng.shuffle(others);pos=i%len(r['options']);others.insert(pos,r['answer_text']);r.update(options=others,answer='ABCD'[pos],answer_index=pos)
write_jsonl(root/'release/benchmark.jsonl',rows)
write_json(root/'release/curation_report.json',dict(initial=len(allrows),final=len(rows),dropped=len(review['drop']),point_corrections=len(review['point_patches']),review=review,replacements=replacement_log,tasks={t:len(v) for t,v in bytask.items()},families=dict(collections.Counter(r['family'] for r in rows)),scope='Source-derived reference answers; visual screening by Codex is not expert medical validation. No baseline-model outputs were used to select or edit questions.'))
used={p for r in rows for p in r['images']}
for p in (root/'release/images').glob('*.jpg'):
if str(p.relative_to(root/'release')) not in used:p.unlink()
contacts(root,rows)
print(json.dumps(dict(final=len(rows),replacements=replacement_log,tasks={t:len(v) for t,v in bytask.items()})),flush=True)