File size: 2,007 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
"""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())))