File size: 2,945 Bytes
152228b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""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.")