-
Notifications
You must be signed in to change notification settings - Fork 12
/
nontargeted_attack.py
264 lines (232 loc) · 13 KB
/
nontargeted_attack.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import time
start_time = time.time()
import multiprocessing as mp
mp.set_start_method('fork')
import ctypes as c
import argparse
import os
import numpy as np
import pandas as pd
from PIL import Image
import image_generators
import task_utils
task_utils.start_time = start_time
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--input_dir', required=True, type=str, \
help='Input directory with images.')
parser.add_argument('--output_dir', required=True, type=str, \
help='Output directory with images.')
parser.add_argument('--max_epsilon', default=16.0, type=float, \
help='Maximum size of adversarial perturbation.')
parser.add_argument('--input_dir_mode', default='test', type=str, \
help='Either flat or hierarchy, how the input dir is organised')
parser.add_argument('--meta', default='/dev/shm/dev_dataset.csv', type=str, \
help='True labels for dev set')
parser.add_argument('--num_samples', default=-1, type=int, \
help='Number of samples, -1 for all samples')
parser.add_argument('--train_batch_size', default=24, type=int, \
help='How many images process at one time.')
args = parser.parse_args()
input_dir_abs = os.path.abspath(args.input_dir)
if args.input_dir_mode == 'flat':
df_meta = pd.read_csv(args.meta)
df_meta = df_meta[['ImageId', 'TrueLabel']]
df_meta['ImageId'] = os.path.abspath(args.input_dir) + '/' + df_meta['ImageId'] + '.png'
df_meta['TrueLabel'] = df_meta['TrueLabel'] - 1
df_meta = df_meta.set_index('ImageId')
meta_dict = df_meta.to_dict()['TrueLabel']
images_info = image_generators.get_names_and_labels(\
args.input_dir, mode='flat', meta_dict=meta_dict)
elif args.input_dir_mode == 'flat_targeted':
df_meta = pd.read_csv(args.meta)
df_meta = df_meta[['ImageId', 'TrueLabel', 'TargetClass']]
df_meta['ImageId'] = os.path.abspath(args.input_dir) + '/' + df_meta['ImageId'] + '.png'
df_meta['TrueLabel'] = df_meta['TrueLabel'] - 1
df_meta['TargetClass'] = df_meta['TargetClass'] - 1
df_meta = df_meta.set_index('ImageId')
meta_dict = df_meta.to_dict()['TrueLabel']
meta_target_dict = df_meta.to_dict()['TargetClass']
images_info = image_generators.get_names_and_labels(\
args.input_dir, mode='flat_targeted', meta_dict=meta_dict, meta_target_dict=meta_target_dict)
elif args.input_dir_mode == 'test':
images_info = image_generators.get_names_and_labels(\
args.input_dir, mode='test')
elif args.input_dir_mode == 'test_targeted':
df_meta = pd.read_csv(os.path.join(os.path.abspath(args.input_dir), 'target_class.csv'), \
header=None, names=['ImageId', 'TargetClass'])
df_meta['TargetClass'] = df_meta['TargetClass'] - 1
df_meta = df_meta.set_index('ImageId')
meta_target_dict = df_meta.to_dict()['TargetClass']
images_info = image_generators.get_names_and_labels(\
args.input_dir, mode='test_targeted', meta_target_dict=meta_target_dict)
else:
images_info = image_generators.get_names_and_labels(\
args.input_dir, mode='hierarchy')
image_generators.initialize_hierarchy(args.output_dir)
print('Total images:', len(images_info))
eps = args.max_epsilon
if args.num_samples < 0:
num_samples = len(images_info)
else:
num_samples = args.num_samples
is_targeted = ('targeted' in args.input_dir_mode)
print('Is targeted', is_targeted)
use_avg_pred = True
print('Loading images')
# each image_data element: filename, img, lbl
image_data = image_generators.load_images_into_batches(\
images_info, args.train_batch_size, \
max_samples=num_samples, to_buffer=True)
print('Generating placeholders')
placeholders = {}
for i, data in enumerate(image_data):
imgmat_size = (len(data[0]), 299, 299, 3)
lblmat_size = (len(data[0]), 1000)
data_min = task_utils.get_raw_array(init=np.clip(task_utils.get_array(data[1], imgmat_size)-eps, 0, None))
data_max = task_utils.get_raw_array(init=np.clip(task_utils.get_array(data[1], imgmat_size)+eps, None, 255))
data_actual = data[1]
grad_mat = task_utils.get_raw_array(dims=imgmat_size)
lbls_mat = task_utils.get_raw_array(dims=lblmat_size) if not is_targeted else data[3]
# data, min, max, grad, lbl, pseudo-label matrix
placeholders[i] = (\
data[0], \
data_actual, \
data_min, \
data_max, \
grad_mat, \
data[2], \
lbls_mat)
print('Preparing functions')
source_models = ['incresv2ensadv','resnet50', 'inceptionv3adv', 'inceptionv3']
pred_models = ['incresv2ensadv','resnet50', 'inceptionv3adv']
step_size = eps
noise_size = eps*0.2
grad_aug_scale = 0.02
min_step = 0.
min_noise = 0.
plan = [\
(('RST', 'ir2ea-i3a-i3-r50-nt', source_models), \
# ((source_models, pred_models), \
(('resnet50', 2), ('resnet50', 2), step_size, noise_size), \
(('inceptionv3adv', 2), ('incresv2ensadv', 2), step_size, noise_size), \
(('inceptionv3adv', 2), ('incresv2ensadv', 2), step_size*0.5, noise_size), \
(('inceptionv3adv', 2), ('incresv2ensadv', 2), step_size*0.5, noise_size), \
(('inceptionv3adv', 2), ('incresv2ensadv', 2), step_size*0.5, noise_size)), \
(('RST', 'i3-x-ir2-nt', ['inceptionv3','incresv2','xception']), \
# ((['inceptionv3','incresv2','xception'], None), \
(('inceptionv3', 2), ('incresv2', 2), step_size*0.5, noise_size), \
(('inceptionv3', 2), ('xception', 2), step_size*0.5, noise_size), \
(('incresv2', 2), ('xception', 2), step_size*0.5, noise_size), \
(('inceptionv3', 2), ('xception', 2), step_size*0.5, noise_size)), \
(('RST', 'ir2ea-r101-i3a-nt', ['incresv2ensadv', 'resnet101', 'inceptionv3adv']), \
# ((['incresv2ensadv', 'resnet101', 'inceptionv3adv'], None), \
(('incresv2ensadv', 2), ('resnet101', 2), step_size*0.2, noise_size*0.5), \
(('inceptionv3adv', 2), ('resnet101', 2), step_size*0.2, noise_size*0.5), \
(('inceptionv3adv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.5)), \
(('inceptionv3adv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.5)), \
(('inceptionv3adv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2)), \
(('incresv2ensadv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2)), \
(('incresv2ensadv', 1), ('resnet101', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2)), \
(('incresv2ensadv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2)), \
(('incresv2ensadv', 1), ('incresv2ensadv', 1), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2)), \
(('inceptionv3adv', 2), ('incresv2ensadv', 2), max(min_step, step_size*0.1), max(min_noise, noise_size*0.2))), \
]
if not task_utils.verify_plan(source_models, pred_models, plan):
return
task_list = []
phase_info = {}
next_task = {}
for i_phase, phase in enumerate(plan):
last_task_in_batch = {}
if phase[0][0] == 'RST':
task = ('phase {0} restore from {1} model {2}'.format(i_phase, phase[0][1], phase[0][2]), \
(), 7, (phase[0][1],))
task_list.append(task)
phase_info[i_phase] = (len(task_list)-1, set(), set())
# COPIED
if (not is_targeted) and (i_phase==0):
for phid, (fname_list, imgs, imgs_min, imgs_max, grads, lbls, lbls_mat) in placeholders.items():
task = ('batch {0}, fname [{1}], pred'.format(phid, ' '.join([os.path.split(f)[1][:-4] for f in fname_list])), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),3,(None,))
task_list.append(task)
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
phase_info[i_phase][2].add(len(task_list)-1)
else:
if i_phase:
task = ('phase {0} reload {1}'.format(i_phase, phase[0]), (), 5, \
(phase[0][0], use_avg_pred, is_targeted))
task_list.append(task)
# id of loading task, all tasks other than load, first tasks after load
phase_info[i_phase] = (len(task_list)-1, set(), set())
else:
task = ('phase {0} initial load {1} pred {2}'.format(i_phase, phase[0][0], phase[0][1]), (), 6, \
(phase[0][0], phase[0][1], use_avg_pred, is_targeted))
task_list.append(task)
phase_info[0] = (len(task_list)-1, set(), set())
if not is_targeted:
for phid, (fname_list, imgs, imgs_min, imgs_max, grads, lbls, lbls_mat) in placeholders.items():
task = ('batch {0}, fname [{1}], pred'.format(phid, ' '.join([os.path.split(f)[1][:-4] for f in fname_list])), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),3,(None,))
task_list.append(task)
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
phase_info[i_phase][2].add(len(task_list)-1)
for phid, (fname_list, imgs, imgs_min, imgs_max, grads, lbls, lbls_mat) in placeholders.items():
for i_pl, pl in enumerate(phase[1:]):
if pl[-1]>1e-5:
task = ('phase {0} batch {1} step {2}, noise {3:.2f}'.format(i_phase, phid, i_pl, pl[-1]), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),4,\
(pl[-1],))
task_list.append(task)
# COPIED
if phid not in last_task_in_batch:
phase_info[i_phase][2].add(len(task_list)-1)
else:
next_task[last_task_in_batch[phid]] = len(task_list)-1
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
for j_stp, stp in enumerate(pl[:-2]):
task = ('phase {0} batch {1} step {2}, grad {3} repeat {4}'.format(i_phase, phid, i_pl, stp[0], stp[1]), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),0,\
(None, stp[0], grad_aug_scale, stp[1]))
task_list.append(task)
# COPIED
if phid not in last_task_in_batch:
phase_info[i_phase][2].add(len(task_list)-1)
else:
next_task[last_task_in_batch[phid]] = len(task_list)-1
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
task = ('phase {0} batch {1} step {2}, FGSM step, step size {3:.2f}'.format(i_phase, phid, i_pl, pl[-2]), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),1,\
(pl[-2],))
task_list.append(task)
# COPIED
if phid not in last_task_in_batch:
phase_info[i_phase][2].add(len(task_list)-1)
else:
next_task[last_task_in_batch[phid]] = len(task_list)-1
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
task = ('phase {0} batch {1}, finish'.format(i_phase, phid), \
(fname_list,imgs,None,grads,imgs_min,imgs_max,lbls_mat),2,(len(input_dir_abs), args.output_dir))
task_list.append(task)
# COPIED
if phid not in last_task_in_batch:
phase_info[i_phase][2].add(len(task_list)-1)
else:
next_task[last_task_in_batch[phid]] = len(task_list)-1
last_task_in_batch[phid] = len(task_list)-1
phase_info[i_phase][1].add(len(task_list)-1)
print('Preparation finished', time.time()-start_time)
print(len(task_list), 'tasks')
task_utils.run_tasks(task_list, phase_info, next_task, verbose=False, cpu_worker=1)
print('Time:', time.time()-start_time)
print('Total images:', len(images_info))
print('Augmentation:', grad_aug_scale)
print('\n'.join(str(u) for u in plan))
print()
if __name__ == '__main__':
main()