reneeice's picture
full research archive: code, models, datasets, experiments, logs
152228b verified
Raw History Blame Contribute Delete
2.95 kB
"""DECISIVE control: on val[:24], take the COMPETITOR's EXACT support and measure margin under
different sign sources / converge budgets. Determines whether the wall is SUPPORT SELECTION (their
support + our signs -> ~10) or SIGNS/CONVERGE (their support + our signs -> ~1)."""
import os, sys, json, time
sys.path.insert(0, "/root/Perturb"); sys.path.insert(0, "/root/Perturb/scripts")
import numpy as np, torch
from PIL import Image
from miner_lib.victim import VictimModel
while int(time.time()) % 120 < 50:
time.sleep(2)
dev = "cuda:0"; COND = "/root/supgen4/cond"
v = VictimModel(device=dev); eps = 1/255.
def marg(x0f, delta, ti):
xi = ((x0f+delta).clamp(0,1)*255).round().long()
return float(v.evaluate(xi.float()/255., ti)[1])
def converge(x0f, ti, tg, mask, seed, steps):
delta = eps*seed*mask.float(); gm = torch.zeros_like(delta)
for _ in range(steps):
d = delta.detach().requires_grad_(True)
row = v.net(v._differentiable_preprocess((x0f+d).clamp(0,1).unsqueeze(0)))[0]
g = torch.autograd.grad(row[tg]-row[ti], d)[0]
with torch.no_grad():
gm = 0.9*gm+g; delta = (delta+eps*0.25*torch.sign(gm)).clamp(-eps,eps)*mask.float()
return marg(x0f, delta, ti)
va = [json.loads(l) for l in open("/root/supgen3/val.jsonl")]
va = [it for it in va if os.path.exists(f"{COND}/{it['row']}.npz")][:24]
D, A25, A60, C25 = [], [], [], []
for it in va:
if int(time.time()) % 120 < 50: time.sleep(max(1, 51-int(time.time())%120))
clean = np.asarray(Image.open(it["clean"]).convert("RGB")).astype(np.float32)
adv = np.asarray(Image.open(it["adv"]).convert("RGB")).astype(np.float32)
if adv.shape != clean.shape: continue
x = torch.from_numpy(clean/255.).permute(2,0,1).to(dev)
dpix = torch.from_numpy((adv-clean)/255.).permute(2,0,1).to(dev) # competitor delta (units)
mask = (dpix != 0)
cond = np.load(f"{COND}/{it['row']}.npz")
gcl = torch.from_numpy(cond["gcl"].astype(np.float32)).to(dev); ti, tg = int(cond["ti"]), int(cond["tg"])
if int(mask.sum()) < 8: continue
# D: competitor's EXACT delta, no reconverge (sanity -> should be high)
D.append(marg(x, dpix, ti))
# A: their support, seed sign(gcl), converge 25 / 60
A25.append(converge(x, ti, tg, mask, torch.sign(gcl), 25))
A60.append(converge(x, ti, tg, mask, torch.sign(gcl), 60))
# C: their support, seed their OWN signs, converge 25
C25.append(converge(x, ti, tg, mask, torch.sign(dpix), 25))
print(f"n={len(D)}")
print(f"D competitor EXACT delta (sanity) margin = {np.mean(D):.2f}")
print(f"A25 their support + sign(gcl) + converge25 margin = {np.mean(A25):.2f}")
print(f"A60 their support + sign(gcl) + converge60 margin = {np.mean(A60):.2f}")
print(f"C25 their support + their signs + converge25 margin = {np.mean(C25):.2f}")
print("If D~10 but A/C~1 -> converge/signs wall (NOT support selection). If A/C~10 -> support-selection wall.")