checkpoint-selection script: 48-edit subset, base + 4 ckpts at 20 steps
Browse files- select_ckpt.py +114 -0
select_ckpt.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""Checkpoint selection for the orbit-alpha LoRA.
|
| 3 |
+
|
| 4 |
+
Fixed 48-edit subset (first 12 eval objects x 4 pairs) at 20 steps, for the
|
| 5 |
+
untouched base model AND every saved checkpoint, all through ONE pipeline load
|
| 6 |
+
(adapter unload/load between checkpoints). Choice rule per spec: best LPIPS
|
| 7 |
+
that does not lose alpha IoU vs the base model.
|
| 8 |
+
|
| 9 |
+
Usage: python select_ckpt.py --out /out/ckpt_select
|
| 10 |
+
Uploads summary + per-edit results to ysharma/gso-orbit-rgba/eval/ckpt_select/.
|
| 11 |
+
"""
|
| 12 |
+
import argparse, json, os, collections
|
| 13 |
+
import numpy as np
|
| 14 |
+
import eval_edit as ee
|
| 15 |
+
|
| 16 |
+
# 20-step selection pass, per spec (ee.run_pairs reads module-level STEPS)
|
| 17 |
+
ee.STEPS = 20
|
| 18 |
+
|
| 19 |
+
MODEL_REPO = 'ysharma/orbit-alpha-lora'
|
| 20 |
+
CKPT_DIR = 'checkpoints/steps2000res768/orbit_alpha_lora'
|
| 21 |
+
CKPTS = [
|
| 22 |
+
('ckpt500', f'{CKPT_DIR}/orbit_alpha_lora_000000500.safetensors'),
|
| 23 |
+
('ckpt1000', f'{CKPT_DIR}/orbit_alpha_lora_000001000.safetensors'),
|
| 24 |
+
('ckpt1500', f'{CKPT_DIR}/orbit_alpha_lora_000001500.safetensors'),
|
| 25 |
+
('ckpt2000', f'{CKPT_DIR}/orbit_alpha_lora.safetensors'),
|
| 26 |
+
]
|
| 27 |
+
DS_REPO = ee.DS_REPO
|
| 28 |
+
OUT_REPO_PATH = 'eval/ckpt_select'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def pick_subset(pairs, n_obj=12):
|
| 32 |
+
by_obj = collections.OrderedDict()
|
| 33 |
+
for p in pairs:
|
| 34 |
+
by_obj.setdefault(p['object_id'], []).append(p)
|
| 35 |
+
objs = list(by_obj)[:n_obj]
|
| 36 |
+
sel = [p for o in objs for p in by_obj[o]]
|
| 37 |
+
assert len(objs) == n_obj and all(len(by_obj[o]) == 4 for o in objs), \
|
| 38 |
+
f'expected {n_obj} objects x 4 pairs, got {len(objs)} objs'
|
| 39 |
+
return sel
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def swap_adapter(pipe, path):
|
| 43 |
+
try:
|
| 44 |
+
pipe.unload_lora_weights()
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f'[swap] unload_lora_weights failed ({e!r}); continuing with load', flush=True)
|
| 47 |
+
pipe.load_lora_weights(path)
|
| 48 |
+
print(f'[swap] loaded {path}', flush=True)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def agg_rows(rows, label):
|
| 52 |
+
a = float(np.mean([r['alpha_iou'] for r in rows]))
|
| 53 |
+
l = float(np.mean([r['lpips'] for r in rows]))
|
| 54 |
+
p = float(np.mean([r['psnr'] for r in rows]))
|
| 55 |
+
d = float(np.mean([r['dino_sim'] for r in rows]))
|
| 56 |
+
s = float(np.mean([r['s_edit'] for r in rows]))
|
| 57 |
+
print(f'{label}: n={len(rows)} aIoU={a:.3f} LPIPS={l:.4f} PSNR={p:.2f} DINO={d:.3f} s/edit={s:.1f}', flush=True)
|
| 58 |
+
return {'name': label, 'n': len(rows), 'alpha_iou': round(a, 4),
|
| 59 |
+
'lpips': round(l, 4), 'psnr': round(p, 2),
|
| 60 |
+
'dino_sim': round(d, 4), 's_edit': round(s, 2)}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def upload(local, remote):
|
| 64 |
+
from huggingface_hub import HfApi
|
| 65 |
+
HfApi().upload_file(path_or_fileobj=local, path_in_repo=remote,
|
| 66 |
+
repo_id=DS_REPO, repo_type='dataset',
|
| 67 |
+
commit_message=f'ckpt-select: {remote}')
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
ap = argparse.ArgumentParser()
|
| 72 |
+
ap.add_argument('--out', required=True)
|
| 73 |
+
args = ap.parse_args()
|
| 74 |
+
os.makedirs(args.out, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
pairs = pick_subset([json.loads(l) for l in open(ee.get_file('pairs_eval.jsonl'))])
|
| 77 |
+
print(f'subset: {len(pairs)} edits, objects={sorted({p["object_id"] for p in pairs})[:3]}...', flush=True)
|
| 78 |
+
|
| 79 |
+
pipe = ee.load_pipe()
|
| 80 |
+
m = ee.make_metrics()
|
| 81 |
+
|
| 82 |
+
summary = []
|
| 83 |
+
# base model at 20 steps first (reference point)
|
| 84 |
+
rows = ee.run_pairs(pipe, m, pairs, 'instruction', os.path.join(args.out, 'base20'))
|
| 85 |
+
summary.append(agg_rows(rows, 'base20'))
|
| 86 |
+
base_aiou = summary[0]['alpha_iou']
|
| 87 |
+
|
| 88 |
+
for name, path in CKPTS:
|
| 89 |
+
local = ee.hf_hub_download(repo_id=MODEL_REPO, repo_type='model', filename=path)
|
| 90 |
+
swap_adapter(pipe, local)
|
| 91 |
+
rows = ee.run_pairs(pipe, m, pairs, 'instruction', os.path.join(args.out, name))
|
| 92 |
+
r = agg_rows(rows, name)
|
| 93 |
+
r['kept_aiou'] = bool(r['alpha_iou'] >= base_aiou)
|
| 94 |
+
summary.append(r)
|
| 95 |
+
|
| 96 |
+
# choice rule: best LPIPS among checkpoints that do not lose alpha IoU
|
| 97 |
+
ok = [s for s in summary[1:] if s['kept_aiou']]
|
| 98 |
+
chosen = min(ok, key=lambda s: s['lpips']) if ok else None
|
| 99 |
+
if chosen is None:
|
| 100 |
+
chosen = min(summary[1:], key=lambda s: s['lpips'])
|
| 101 |
+
print('WARNING: no checkpoint kept base alpha IoU; fell back to best LPIPS overall', flush=True)
|
| 102 |
+
summary.append({'chosen': chosen['name']})
|
| 103 |
+
|
| 104 |
+
with open(os.path.join(args.out, 'summary.json'), 'w') as f:
|
| 105 |
+
json.dump(summary, f, indent=2)
|
| 106 |
+
print('SUMMARY ' + json.dumps(summary), flush=True)
|
| 107 |
+
upload(os.path.join(args.out, 'summary.json'), f'{OUT_REPO_PATH}/summary.json')
|
| 108 |
+
for name, _ in [('base20', None)] + CKPTS:
|
| 109 |
+
upload(os.path.join(args.out, name, 'results.jsonl'), f'{OUT_REPO_PATH}/{name}_results.jsonl')
|
| 110 |
+
print('DONE', flush=True)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == '__main__':
|
| 114 |
+
main()
|