iris / scripts /evaluate_all_pairs.py
github-actions[bot]
Automated Deploy: 2845ef877db9dd2af4e8ee9da6d857c78ef513f2
b5af7d6
Raw History Blame Contribute Delete
3.49 kB
"""Repeatable evaluation of all discovered client groups and cached research matches.
Finished Photoshop references are never counted as additional source photographs.
Use --recompute-matches to run inference again from the saved native crops.
"""
import argparse,hashlib,itertools,json,sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from PIL import Image
import numpy as np
from backend.experimental.evaluation import evaluate_correspondences
ROOT=Path(__file__).resolve().parents[1]
p=argparse.ArgumentParser();p.add_argument('--source',type=Path,default=Path('/Users/tstore/Desktop/iris test files'));p.add_argument('--recompute-matches',action='store_true');args=p.parse_args()
out=ROOT/'private_benchmarks/evaluation';out.mkdir(exist_ok=True)
manifest=[];results=[];matcher=None
for folder in sorted(x for x in args.source.iterdir() if x.is_dir()):
originals=[];finished=[]
for file in sorted(folder.iterdir()):
if file.suffix.lower() not in ['.jpg','.jpeg','.png']:continue
with Image.open(file) as im:
software=str(im.getexif().get(305,''));entry=dict(file=file.name,size=list(im.size),sha256=hashlib.sha256(file.read_bytes()).hexdigest())
(finished if 'Photoshop' in software else originals).append(entry)
pairs=list(itertools.combinations(originals,2))
manifest.append(dict(group=folder.name,originals=originals,finished_references=finished,original_pair_count=len(pairs)))
# Current crop manifest explicitly identifies reference/moving originals.
base=json.loads((ROOT/'private_benchmarks/client_iris/metrics.json').read_text())
known=next((r for r in base if r['sample']==folder.name),None)
for a,b in pairs:
row=dict(sample=folder.name,originals=[a['file'],b['file']])
if known is None or set(row['originals'])!={known['reference'],known['moving']}:
row.update(status='needs native crop preparation');results.append(row);continue
cropdir=ROOT/'private_benchmarks/client_iris'/folder.name
row.update(reference=known['reference'],moving=known['moving'],reference_crop=known['reference_crop'],moving_crop=known['moving_crop'])
if args.recompute_matches:
import cv2
from backend.experimental.matching import LearnedMatcher
from backend.experimental.patch_validation import validate_patches
if matcher is None:matcher=LearnedMatcher(ROOT/'models/loftr_outdoor.ckpt')
ref=cv2.imread(str(cropdir/'reference.png'));mov=cv2.imread(str(cropdir/'moving.png'))
rawref,rawmov=matcher(ref,mov);rp,mp=validate_patches(ref,mov,rawref,rawmov)
raw_count=len(rawref)
np.savez_compressed(out/f'{folder.name}-correspondences.npz',reference=rp,moving=mp)
else:
cached=ROOT/'private_benchmarks/research'
matches=np.load(cached/f'{folder.name}-loftr_patch-matches.npz');rp,mp=matches['reference'],matches['moving']
raw_count=len(np.load(cached/f'{folder.name}-loftr-matches.npz')['reference'])
row.update(raw_matches=raw_count,verified_matches=len(rp),patch_survival_percent=round(100*len(rp)/max(raw_count,1),2))
row['evaluation']=evaluate_correspondences(rp,mp,(2200,2200))
row['status']='evaluated — needs independent validation' if row['evaluation']['p95_px'] is not None else 'insufficient evidence'
results.append(row);print(json.dumps(row),flush=True)
(out/'inventory.json').write_text(json.dumps(manifest,indent=2))
(out/'cross_validation.json').write_text(json.dumps(results,indent=2))
print('Discovered original pairs:',sum(m['original_pair_count'] for m in manifest))