-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.py
159 lines (124 loc) · 5.08 KB
/
show.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import matplotlib.pyplot as plt
import numpy as np
import os
ROOT = 'result'
DATASET = 'imagenet'
MODELS = ['alexnet', 'googlenet', 'inception', 'mobilenet', 'resnet']
MODEL_ATTR = 'vgg'
BASELINES = ['onepixel', 'pixle', 'square']
PLOT_NAMES = ['Onepixel', 'Pixle', 'Square', 'TETRADAT']
CLASSES = 1000
SEED = 42
def check_image(num, model, bs=None):
return os.path.isfile(get_image_path(num, model, bs))
def get_image(num, model, bs=None):
if check_image(num, model, bs):
return plt.imread(get_image_path(num, model, bs))
def get_image_nums(model, bs=None):
nums = []
fpath = f'{ROOT}/{DATASET}-{model}/attack-'
fpath += f'attr-{MODEL_ATTR}' if bs is None else f'bs_{bs}-{MODEL_ATTR}'
fpath += '/img'
for num in os.listdir(fpath):
if num.isnumeric():
nums.append(num)
return nums
def get_image_path(num, model, bs=None):
fpath = f'{ROOT}/{DATASET}-{model}/attack-'
fpath += f'attr-{MODEL_ATTR}' if bs is None else f'bs_{bs}-{MODEL_ATTR}'
fpath += f'/img/{num}/changed.png'
return fpath
def load_data(model, bs=None):
fpath = f'{ROOT}/{DATASET}-{model}/attack-'
fpath += f'attr-{MODEL_ATTR}' if bs is None else f'bs_{bs}-{MODEL_ATTR}'
fpath += f'/result.npz'
return np.load(fpath, allow_pickle=True).get('result').item()
def plot(num_total=5, dpi=150, bs_ref='onepixel'):
print(f'\n\nFigures >>>')
for model in MODELS:
for _ in range(100000000):
# Note that we select "num_total" random images from the of
# successful attacks with "onepixel" method (see "bs_ref"),
# since this method gives the lowest percentage of asr:
nums = get_image_nums(model, bs_ref)
nums = np.random.choice(nums, num_total, replace=False)
is_ok = True
for bs in BASELINES:
is_ok_curr = [check_image(num, model, bs) for num in nums]
if False in is_ok_curr:
is_ok = False
break
is_ok_curr = [check_image(num, model) for num in nums]
if False in is_ok_curr:
is_ok = False
if is_ok:
break
if not is_ok:
raise ValueError('Can not find {num_total} images')
def build_title(item):
t = item['l_new']
for _ in range(3):
if len(t) > 30 and ', ' in t:
t = ', '.join(t.split(', ')[:-1])
return t
images = []
titles = []
for bs in BASELINES:
result = load_data(model, bs)
images.append([get_image(num, model, bs) for num in nums])
titles.append([build_title(result[int(num)]) for num in nums])
result = load_data(model)
images.append([get_image(num, model) for num in nums])
titles.append([build_title(result[int(num)]) for num in nums])
print(f'Model {model} | Selected random images: {nums}')
fig = plt.figure(figsize=(14, 12))
plt.subplots_adjust(wspace=0.01, hspace=0.2)
cnt = 0
text_positions = [420, 340, 365, 420]
for j, method in enumerate(BASELINES + ['TETRADAT']):
for i, num in enumerate(range(num_total)):
cnt += 1
fig.add_subplot(4, num_total, cnt)
plt.imshow(images[j][i])
plt.title(titles[j][i], fontsize=9, color='#8b1d1d')
plt.axis('off')
if i == 0:
plt.text(-150, text_positions[j], PLOT_NAMES[j],
rotation='vertical',
fontfamily='monospace', fontsize=25,
color='#000099' if j == 3 else '#000099',
fontweight=1000 if j == 3 else 500,
horizontalalignment='left')
fname = f'{model}'
os.makedirs(f'{ROOT}/_show', exist_ok=True)
plt.savefig(f'{ROOT}/_show/{fname}.png', bbox_inches='tight', dpi=dpi)
plt.close(fig)
#break
def show():
print(f'\n\nResults >>>')
for model in MODELS:
for num, bs in enumerate(BASELINES):
show_method(model, bs, title=(num==0))
show_method(model)
def show_method(model, bs=None, title=False):
result = load_data(model, bs)
succ = np.sum([r['success'] for r in result.values() if r])
full = len(result.keys())
dx0 = np.mean([r['changes'] for r in result.values() if r['success']])
dx1 = np.mean([r['dx1'] for r in result.values() if r['success']])
dx2 = np.mean([r['dx2'] for r in result.values() if r['success']])
name = 'tetradat' if bs is None else f'{bs}'
text = ''
if title:
text += f'\n\n{model} (attr: {MODEL_ATTR}) | (total images {full})\n'
text += name + ' '*max(0, 10-len(name)) + ' >>> '
text += f'asr: {succ/full*100:-6.2f}% | '
text += f'total: {full} | '
text += f'changes: {dx0:-6.0f} | '
text += f'dx1: {dx1:-8.1f} | '
text += f'dx2: {dx2:-8.1f}'
print(text)
if __name__ == '__main__':
np.random.seed(SEED)
show()
plot()