Download code/scripts/edge_analyze.py from reneeice/sparse-adversarial-research-archive: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/datasets/reneeice/sparse-adversarial-research-archive/resolve/main/code/scripts/edge_analyze.py
- Command line
-
hf download hf://datasets/reneeice/sparse-adversarial-research-archive/code/scripts/edge_analyze.py
-
curl -L -o edge_analyze.py https://huggingface.co/datasets/reneeice/sparse-adversarial-research-archive/resolve/main/code/scripts/edge_analyze.py
2.01 kB
| """Diff collected competitor outputs vs the clean ImageNet-100 input to reveal their technique: | |
| amplitude (max|diff|), density, per-channel support, spatial clustering. CPU-only.""" | |
| import sys, json, io, base64 | |
| from pathlib import Path | |
| from collections import defaultdict | |
| import numpy as np | |
| sys.path.insert(0, "/root/Perturb") | |
| from perturbnet.imagenet100_bootstrap import load_imagenet100 | |
| EDGE = Path("/root/edge_cases") | |
| ds = load_imagenet100(split="train") | |
| def clean_np(row): | |
| return np.asarray(ds[row]["image"].convert("RGB")).astype(np.int16) | |
| recs=[json.loads(l) for l in open(EDGE/"manifest.jsonl")] | |
| byuid=defaultdict(list) | |
| for r in recs: byuid[str(r["uid"])].append(r) | |
| # analyze the strongest competitors on the biggest gaps | |
| from PIL import Image | |
| print(f"{'uid':<5}{'row':<8}{'their_sc':<9}{'our_sc':<8}{'amp':<5}{'dens%':<8}{'chR':<7}{'chG':<7}{'chB':<7}") | |
| rows_done=0 | |
| for r in sorted(recs, key=lambda r: -( r['score']-r['our_score'])): | |
| uid=str(r["uid"]); row=r["row"] | |
| p=EDGE/uid/f"{row}.png" | |
| if not p.exists(): continue | |
| try: | |
| adv=np.asarray(Image.open(p).convert("RGB")).astype(np.int16) | |
| cln=clean_np(row) | |
| if adv.shape!=cln.shape: continue | |
| except Exception: continue | |
| diff=adv-cln | |
| amp=int(np.abs(diff).max()) | |
| dens=float((diff!=0).mean()) | |
| ch=[int((diff[:,:,c]!=0).sum()) for c in range(3)] | |
| print(f"{uid:<5}{row:<8}{r['score']:<9.4f}{r['our_score']:<8.4f}{amp:<5}{dens*100:<8.1f}{ch[0]:<7}{ch[1]:<7}{ch[2]:<7}") | |
| rows_done+=1 | |
| if rows_done>=25: break | |
| # amplitude histogram across ALL collected | |
| amps=defaultdict(int) | |
| for r in recs: | |
| p=EDGE/str(r["uid"])/f"{r['row']}.png" | |
| if not p.exists(): continue | |
| try: | |
| adv=np.asarray(Image.open(p).convert("RGB")).astype(np.int16); cln=clean_np(r["row"]) | |
| if adv.shape!=cln.shape: continue | |
| amps[int(np.abs(adv-cln).max())]+=1 | |
| except Exception: continue | |
| print("\namplitude histogram across all collected competitor outputs:", dict(sorted(amps.items()))) | |