RefSeg-CA / source /code /verify_rela_metrics.py
nmsofficial's picture
Add verified public availability and human-evaluation guide
9126e0d verified
Raw
History Blame Contribute Delete
2.11 kB
"""Independent reproduction of the released ReLA evaluator equations."""
from pathlib import Path
import gzip,json,math
ROOT=Path(__file__).resolve().parents[1]
def official(rows):
si=su=sg=0.;n=ntn=ntok=pos=fe=pr50=0
for r in rows:
z=r['official480_scores']['frozen'];gt_nt=r['target_count']==0
pred_nt=bool(z.get('pred_nt',z['empty']))
n+=1
if gt_nt:
ntn+=1;ntok+=pred_nt;sg+=float(pred_nt)
if not pred_nt:su+=z['union']
else:
pos+=1;fe+=pred_nt
i=0 if pred_nt else z['intersection'];u=z['union']
si+=i;su+=u;sg+=0 if u==0 else i/u;pr50+=(0 if u==0 else i/u)>=.5
return dict(n=n,gIoU=sg/n,cIoU=si/su,Nacc=ntok/ntn,
positive_mIoU=(sg-(ntok))/pos,false_empty=fe/pos,Pr50=pr50/pos)
def fixtures():
# (gt_nt,pred_nt,I,U) includes both no-target branches and positive errors.
cases=[(1,1,0,0),(1,0,0,9),(0,0,5,10),(0,1,7,10),(0,0,0,0),(0,0,9,9)]
sg=si=su=0
for gt,nt,i,u in cases:
if gt:sg+=nt;su+=0 if nt else u
else:
i=0 if nt else i;sg+=0 if u==0 else i/u;si+=i;su+=u
assert math.isclose(sg/6,(1+.5+0+0+1)/6)
assert math.isclose(si/su,14/38)
return {'fixtures':6,'gIoU':sg/6,'cIoU':si/su}
def main():
p=ROOT/'results/analysis/natural_rela_evaluated.jsonl.gz'
with gzip.open(p,'rt') as f:rows=[json.loads(x) for x in f]
got=official(rows);summary=json.loads((ROOT/'results/analysis/natural_rela.json').read_text())
ref=next(x for x in summary['tables'] if x['split']=='all' and x['grid']=='official480_scores' and x['variant']=='frozen')
for k in ['gIoU','cIoU','Nacc','positive_mIoU','false_empty','Pr50']:
assert math.isclose(got[k],ref[k],rel_tol=0,abs_tol=1e-12),(k,got[k],ref[k])
out={'status':'PASS','independent_equation_replay':got,'fixture_check':fixtures(),
'reference':'ReLA gres_model/evaluation/refer_evaluation.py'}
(ROOT/'results/analysis/rela_metric_verification.json').write_text(json.dumps(out,indent=2))
print(json.dumps(out))
if __name__=='__main__':main()