Datasets:
Download source/legacy_project/code/export_qualitative.py from Ethosoft/RefSeg-CA: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/datasets/Ethosoft/RefSeg-CA/resolve/main/source/legacy_project/code/export_qualitative.py
- Command line
-
hf download hf://datasets/Ethosoft/RefSeg-CA/source/legacy_project/code/export_qualitative.py
-
curl -L -o export_qualitative.py https://huggingface.co/datasets/Ethosoft/RefSeg-CA/resolve/main/source/legacy_project/code/export_qualitative.py
2.86 kB
| """Reconstruct extremal controlled examples from stored predictions, without GPU. | |
| Selection is deliberately outcome-based and is disclosed in the figure caption. | |
| It never alters evaluation metrics. RGB and masks are actual experiment files. | |
| """ | |
| from pathlib import Path | |
| import json,gzip,zipfile,io,base64 | |
| import numpy as np | |
| from PIL import Image | |
| from repair import repaired_maps,score_mask | |
| ROOT=Path(__file__).resolve().parents[1] | |
| def main(): | |
| manifest={r['id']:r for r in map(json.loads,(ROOT/'data/manifest.jsonl').read_text().splitlines())} | |
| out=ROOT/'results/qualitative';out.mkdir(parents=True,exist_ok=True);examples=[] | |
| for model in ['clipseg','groundedsam']: | |
| rows=[json.loads(l) for l in gzip.open(ROOT/'results/analysis'/(model+'_evaluated.jsonl.gz'),'rt')] | |
| candidates=[r for r in rows if r['domain']=='controlled' and r['split']=='test' and r['corruption']=='clean' and r['mode']=='relation' and r['target_count']>0] | |
| candidates.sort(key=lambda r:(r['scores']['cacp']['iou']-r['scores']['frozen']['iou'],r['id'])) | |
| for kind,r in [('gain',candidates[-1]),('loss',candidates[0])]: | |
| d=manifest[r['id']];z=np.load(ROOT/r['cache_path']);maps={q:z['p%d'%i].astype(np.float32) for i,q in enumerate(z['texts'].tolist())} | |
| pred,flags=repaired_maps(d['query'],maps,.5 if model=='clipseg' else .25) | |
| gt=np.asarray(Image.open(ROOT/d['gt_path']))>0;key=model+'_'+kind;assets={} | |
| for method in ['frozen','cacp']: | |
| assert score_mask(pred[method],gt)==r['scores'][method] | |
| Image.open(ROOT/d['image_path']).save(out/(key+'_rgb.png'));assets['rgb']=key+'_rgb.png' | |
| for method,mask in [('gt',gt),('frozen',pred['frozen']),('cacp',pred['cacp'])]: | |
| Image.fromarray(mask.astype(np.uint8)*255).save(out/(key+'_'+method+'.png'));assets[method]=key+'_'+method+'.png' | |
| examples.append(dict(model=model,kind=kind,record=d,flags=flags,scores={m:r['scores'][m] for m in ['frozen','cacp']},assets=assets)) | |
| meta={'selection':'Largest gain and largest loss in mask IoU per pipeline among clean, held-out, positive controlled relation records; ties broken by record ID. These are extremal illustrations, not representative samples.','examples':examples} | |
| (out/'qualitative_examples.json').write_text(json.dumps(meta,indent=2)) | |
| buffer=io.BytesIO() | |
| with zipfile.ZipFile(buffer,'w',zipfile.ZIP_DEFLATED) as z: | |
| for f in sorted(out.iterdir()): | |
| if f.suffix in ['.json','.png']:z.write(f,f.name) | |
| (ROOT/'results/qualitative_bundle.b64').write_text(base64.b64encode(buffer.getvalue()).decode()) | |
| print(json.dumps({'bytes':len(buffer.getvalue()),'examples':[(r['model'],r['kind'],r['record']['id'],r['scores']['frozen']['iou'],r['scores']['cacp']['iou']) for r in examples]})) | |
| if __name__=='__main__':main() | |