Download code/scripts/head2head.py from reneeice/sparse-adversarial-research-archive: direct link, hf CLI and curl.
- Browser
- Download file 2.29 kB
-
https://huggingface.co/datasets/reneeice/sparse-adversarial-research-archive/resolve/main/code/scripts/head2head.py
- Command line
-
hf download hf://datasets/reneeice/sparse-adversarial-research-archive/code/scripts/head2head.py
-
curl -L -o head2head.py https://huggingface.co/datasets/reneeice/sparse-adversarial-research-archive/resolve/main/code/scripts/head2head.py
2.29 kB
| """Head-to-head: run OUR pipeline on the exact rows the poller captured, compare to UID126's | |
| actual per-task result. Definitive measure of the real gap distribution.""" | |
| from __future__ import annotations | |
| import sys, time, json, base64, io | |
| from pathlib import Path | |
| REPO_ROOT = Path(__file__).resolve().parents[1] | |
| if str(REPO_ROOT) not in sys.path: sys.path.insert(0, str(REPO_ROOT)) | |
| import torch | |
| from miner_lib.victim import VictimModel | |
| from miner_lib.config import AttackConfig | |
| from miner_lib.runner import run_attack | |
| from perturbnet.imagenet100_bootstrap import load_imagenet100 | |
| from perturbnet.image_io import decode_image_b64 | |
| def pil_b64(img): | |
| b=io.BytesIO(); img.convert("RGB").save(b, format="PNG"); return base64.b64encode(b.getvalue()).decode() | |
| def main(): | |
| budget=float(sys.argv[1]) if len(sys.argv)>1 else 35.0 | |
| recs=[] | |
| jpath = sys.argv[2] if len(sys.argv)>2 else "/root/lb_data.jsonl" | |
| for line in open(jpath): | |
| line=line.strip() | |
| if line: recs.append(json.loads(line)) | |
| ds=load_imagenet100(split="train") | |
| victim=VictimModel(device="cuda" if torch.cuda.is_available() else "cpu") | |
| cfg=AttackConfig(time_budget_seconds=budget) | |
| print(f"n_rows={len(recs)} budget={budget}s") | |
| print(f"{'row':<9}{'our':<9}{'u126':<9}{'delta':<9}{'our_m':<7}{'u126_m':<8}{'our_rmse':<10}{'u126_rmse':<10}{'u126_dens'}") | |
| ours=[]; u126=[] | |
| for r in recs: | |
| row=r["row"] | |
| if row>=ds.num_rows: continue | |
| u=r.get("uid126") | |
| if not u: continue | |
| x=decode_image_b64(pil_b64(ds[row]["image"])).to(victim.device) | |
| t0=time.time() | |
| o=run_attack(x_clean=x, cfg=cfg, net=victim.net, device=victim.device, task_id=f"r{row}") | |
| os_=o.validator_score if o.flipped else 0.0 | |
| ours.append(os_); u126.append(u["score"]) | |
| print(f"{row:<9}{os_:<9.4f}{u['score']:<9.4f}{os_-u['score']:<+9.4f}{o.margin:<7.2f}{u['margin']:<8.2f}{o.rmse:<10.6f}{u['rmse']:<10.6f}{u['dens']*100:.1f}%") | |
| if ours: | |
| import statistics | |
| print(f"\nOUR mean={statistics.mean(ours):.4f} UID126 mean={statistics.mean(u126):.4f} delta={statistics.mean(ours)-statistics.mean(u126):+.4f}") | |
| print(f"we beat UID126 on {sum(1 for a,b in zip(ours,u126) if a>b)}/{len(ours)} rows") | |
| if __name__=="__main__": main() | |