-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar_generate_training_images.py
398 lines (261 loc) · 15.7 KB
/
cifar_generate_training_images.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
path_list = str(os.path.abspath(__file__)).split("/")
add_path = os.path.join(*path_list[1:-2])
# import sys
# sys.path.insert(0, '../')
from modules.sequential import Sequential
from modules.linear import Linear
from modules.softmax import Softmax
from modules.relu import Relu
from modules.tanh import Tanh
from modules.convolution import Convolution
from modules.avgpool import AvgPool
from modules.maxpool import MaxPool
from modules.utils import Utils, Summaries, plot_relevances, produce_relevance_image
import modules.render as render
import input_data
import tensorflow as tf
import numpy as np
import pdb
import scipy.io as sio
import foolbox
from foolbox.models import TensorFlowModel
from foolbox.criteria import TargetClassProbability
from foolbox.attacks import LBFGSAttack, DeepFoolAttack, GradientAttack, BoundaryAttack
from PIL import Image
from cifar_util import LoadCifarDataFromImages,LoadGSCifarData
print(foolbox.__file__)
flags = tf.flags
logging = tf.logging
flags.DEFINE_integer("max_steps", 1,'Number of steps to run trainer.')
flags.DEFINE_integer("batch_size", 10000,'Number of steps to run trainer.')
flags.DEFINE_integer("test_every", 500,'Number of steps to run trainer.')
flags.DEFINE_float("learning_rate", 0.01,'Initial learning rate')
flags.DEFINE_float("dropout", 0.9, 'Keep probability for training dropout.')
flags.DEFINE_string("data_dir", 'data','Directory for storing data')
flags.DEFINE_string("summaries_dir", 'mnist_convolutional_logs','Summaries directory')
flags.DEFINE_boolean("relevance", False,'Compute relevances')
flags.DEFINE_string("relevance_method", 'simple','relevance methods: simple/epsilon/ww/flat/alphabeta')
flags.DEFINE_boolean("save_model", False,'Save the trained model')
flags.DEFINE_boolean("reload_model", False,'Restore the trained model')
flags.DEFINE_string("checkpoint_dir", 'cifar_convolutional_model','Checkpoint dir')
flags.DEFINE_string("checkpoint_reload_dir", 'cifar_convolutional_model','Checkpoint dir')
FLAGS = flags.FLAGS
### functions that create LRP capable neural networks using LRP wrapper functions for tensorflow layer construction
def nn():
return Sequential([Convolution(output_depth=10,input_depth=num_channels,batch_size=FLAGS.batch_size, input_dim=32, act ='relu', stride_size=1, pad='VALID'),
AvgPool(),
Convolution(output_depth=25,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=4,output_depth=100,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID')
])
def nn2():
return Sequential([Convolution(output_depth=32,input_depth=num_channels,batch_size=FLAGS.batch_size, input_dim=32, act ='relu', stride_size=1, pad='VALID'),
AvgPool(),
Convolution(output_depth=25,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=4,output_depth=100,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID')
])
def vgg_like_nn():
return Sequential([Convolution(output_depth=64,input_depth=num_channels,batch_size=FLAGS.batch_size, input_dim=32, act ='relu', stride_size=1, pad='VALID'),
Convolution(output_depth=64,input_depth=num_channels,batch_size=FLAGS.batch_size, input_dim=32, act ='relu', stride_size=1, pad='VALID'),
MaxPool(),
Convolution(output_depth=128,stride_size=1, act ='relu', pad='VALID'),
Convolution(output_depth=128,stride_size=1, act ='relu', pad='VALID'),
MaxPool(),
# Linear(output_dim=1000),
# Linear(output_dim=10, input_dim = 1000),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID')
])
def cuda_conv_like_nn():
return Sequential([Convolution(output_depth=64,input_depth=num_channels,batch_size=FLAGS.batch_size, input_dim=32, act ='relu', stride_size=1, pad='VALID'),
AvgPool(),
Convolution(output_depth=64,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
#Convolution(kernel_size=1, output_depth=10,stride_size=1, pad='VALID'),
Linear(output_dim=1000),
Linear(output_dim=10, input_dim = 1000),
])
use_grayscale = True
if(use_grayscale):
num_channels = 1
data_function = LoadGSCifarData
else:
num_channels = 3
data_function = LoadCifarDataFromImages
model_to_use = nn2
#model_to_use = vgg_like_nn
#model_to_use = cuda_conv_like_nn
def feed_dict(train=False):
if(train):
k = 0.5
else:
k = 1.0
xs, ys = data_function(FLAGS.batch_size,train)
return xs, ys, k
config = tf.ConfigProto(allow_soft_placement = True)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
### Input tensorflow placeholders
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [None, 32,32,num_channels], name='absolute_input_x')
y_ = tf.placeholder(tf.float32, [None, 10], name='absolute_output_y')
keep_prob = tf.placeholder(tf.float32)
### network layers and output layver variable
with tf.variable_scope('model'):
net = model_to_use()
inp = tf.pad(tf.reshape(x, [1,32,32,num_channels]), [[0,0],[2,2],[2,2],[0,0]], name='absolute_input')
op = net.forward(inp)
y = tf.squeeze(op, name='absolute_output')
trainer = net.fit(output=y,ground_truth=y_,loss='softmax_crossentropy',optimizer='adam', opt_params=[FLAGS.learning_rate])
### relevancy map set up
with tf.variable_scope('relevance'):
LRP = net.lrp(op, FLAGS.relevance_method, 1e-3)
relevance_layerwise = []
R = op
for layer in net.modules[::-1]:
R = net.lrp_layerwise(layer, R, FLAGS.relevance_method, 1e-3)
relevance_layerwise.append(R)
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)), tf.float32))
tf.global_variables_initializer().run()
### Start the main program flow
### load from pretrained model MNIST model
reload_using_checkpoint = True
if(reload_using_checkpoint):
utils = Utils(sess, FLAGS.checkpoint_reload_dir)
utils.reload_model()
else:
tvars = tf.trainable_variables()
npy_files = np.load('cifar_convolutional_model/model.npy', encoding='bytes')
[sess.run(tv.assign(npy_files[tt])) for tt,tv in enumerate(tvars)]
d = feed_dict(False) #load the data
### initialisation of the adversarial attack variables
model = TensorFlowModel(x, y, bounds=(-1, 1)) #model used by the attacks
attacks = []
#attacks.append(("LBFGSAttack", LBFGSAttack(model)))
attacks.append(("DeepFoolAttack", DeepFoolAttack(model)))
# attacks.append(("BoundaryAttack", BoundaryAttack(model))) # this attack currently doesn't work
attacks.append(("GradientAttack", GradientAttack(model)))
### set up the output path
training_data_path = "cifar_training_data"
if not os.path.exists(training_data_path):
os.mkdir(training_data_path);
image_stats_dict = {} #tracks the number of correctly classified images that will be used for creating training data.
for attack_t in attacks[:]: #generate data for each attack
image_stats_dict[attack_t[0]] = {"correct_classification":0,"total_images":0}
print("Generating data for: "+str(attack_t[0]))
### set up of output paths for this attack
attack_path = os.path.join(training_data_path,attack_t[0])
raw_image_path = os.path.join(attack_path,"input_images")
rel_image_path = os.path.join(attack_path,"rel_images")
image_dir = os.path.join(raw_image_path,"0")
adv_image_dir = os.path.join(raw_image_path,"1")
rel_image_dir = os.path.join(rel_image_path,"0")
adv_rel_image_dir = os.path.join(rel_image_path,"1")
check_paths = [attack_path,raw_image_path,rel_image_path,image_dir,adv_image_dir,rel_image_dir,adv_rel_image_dir]
for check_path in check_paths:
if not os.path.exists(check_path):
os.mkdir(check_path);
attack = attack_t[1] #move the attack instance from the attack tuple to a variable named attack (for legacy reasons)
no_adversarial_list = [] #list of images that an adversarial attack could not be generated for
ground_truths = [] #contains the training data to be output to a csv file
rel_layer_index = 1
normal_final_layer_rels = [] #contains relvancy vectors for the final layer of the task generated when the network processes 'normal' images
attack_final_layer_rels = [] #contains relvancy vectors for the final layer of the task generated when the network processes adversarial images
for i in range(len(d[0][:])): #for each MNIST image in the batch loaded in d[0]
image_stats_dict[attack_t[0]]["total_images"] += 1
###fetch and save the image from MNIST
image = d[0][i]
save_im = image.reshape(32,32)
save_im = (255.0 / save_im.max() * (save_im - save_im.min())).astype(np.uint8)
im = Image.fromarray(save_im)
label = np.argmax(model.batch_predictions([image])) #get the class prediciton from the model
gt = np.argmax(d[1][i])
if(label != gt): #if the TASK classifier makes an inccorect prediction, do not generate adversarial training data from this image.
continue
else:
image_stats_dict[attack_t[0]]["correct_classification"] += 1
### save the regular image to the training data
im_output_path = os.path.join(image_dir,"test_"+str(i)+"_"+str(label)+".jpeg")
im.save(im_output_path)
### create and save the adversarial image
adversarial = attack(image, label=label)
if(adversarial is None):
print("no adversarial attack found")
no_adversarial_list.append(i)
continue
save_im = adversarial.reshape(32,32)
save_im = (255.0 / save_im.max() * (save_im - save_im.min())).astype(np.uint8)
im = Image.fromarray(save_im)
adv_label = np.argmax(model.batch_predictions([adversarial])) #get the class prediction for the adversarial image
adv_im_output_path = os.path.join(adv_image_dir,"adv_test_"+str(i)+"_"+str(label)+"_"+str(adv_label)+".jpeg")
im.save(adv_im_output_path)
### relevancy map generation and save
rel_inp = image.reshape(1,32,32,num_channels) # reshape to how tensorflow expects
test_inp = {x:rel_inp, y_: d[1][i:i+1], keep_prob: d[2]} #form feed dict
y1, relevance_test, rel_layer= sess.run([y, LRP, relevance_layerwise], feed_dict=test_inp) #make class prediciton
normal_final_layer_rels.append(np.array(rel_layer[rel_layer_index]).reshape(rel_layer[rel_layer_index].size)) #store the final layer's relevancy map
relevance_test = relevance_test[:,2:34,2:34,:]
images = test_inp[x]
relevance_image = produce_relevance_image(relevance_test.reshape([1,32,32,num_channels]) )
save_im = relevance_image.reshape(32,32,3)
save_im = (255.0 / save_im.max() * (save_im - save_im.min())).astype(np.uint8)
im = Image.fromarray(save_im, 'RGB')
rel_im_output_path = os.path.join(rel_image_dir,"rel_test_"+str(i)+"_"+str(label)+"_"+str(adv_label)+".jpeg")
im.save(rel_im_output_path)
### adversarial relevancy map generation and save
adv_inp = adversarial.reshape(1,32,32,num_channels)
test_inp = {x:adv_inp, y_: d[1][i:i+1], keep_prob: d[2]}
y1, relevance_test, rel_layer= sess.run([y, LRP, relevance_layerwise], feed_dict=test_inp)
attack_final_layer_rels.append(np.array(rel_layer[rel_layer_index]).reshape(rel_layer[rel_layer_index].size)) #store the final layer's relevancy map
relevance_test = relevance_test[:,2:34,2:34,:]
images = test_inp[x]
relevance_image = produce_relevance_image(relevance_test.reshape([1,32,32,num_channels]) )
save_im = relevance_image.reshape(32,32,3)
save_im = (255.0 / save_im.max() * (save_im - save_im.min())).astype(np.uint8)
im = Image.fromarray(save_im, 'RGB')
adv_rel_im_output_path = os.path.join(adv_rel_image_dir,"adv_rel_test_"+str(i)+"_"+str(label)+"_"+str(adv_label)+".jpeg")
im.save(adv_rel_im_output_path)
print("original label: "+str(label))
print("adversarial label: "+str(adv_label))
ground_truths.append( (str(i),str(im_output_path),str(adv_im_output_path),str(rel_im_output_path),str(adv_rel_im_output_path),str(label),str(adv_label)) )
rel_output_string = ""
rel_gt_string = ""
for rel in normal_final_layer_rels:
rel_output_string+= ",".join([str(val) for val in list(rel)]) + "\n"
rel_gt_string += "0\n"
for rel in attack_final_layer_rels:
rel_output_string+= ",".join([str(val) for val in list(rel)]) + "\n"
rel_gt_string += "1\n"
rel_output_path = os.path.join(training_data_path,attack_t[0]+"_rels.csv")
with open(rel_output_path,"w") as f:
f.write(rel_output_string)
rel_gt_output_path = os.path.join(training_data_path,attack_t[0]+"_rels_gt.csv")
with open(rel_gt_output_path,"w") as f:
f.write(rel_gt_string)
print("Num of no adversarials:")
print(len(no_adversarial_list))
### output training data to CSV
ground_truth_output_string = "i,img_path,adv_img_path,rel_path,adv_rel_path,label,adv_label\n"
for gt in ground_truths:
ground_truth_output_string += gt[0] + "," + gt[1] + "," + gt[2] + "," + gt[3] + "," + gt[4] + "," + gt[5]+ "," + gt[6]+ "\n"
ground_truth_output_path = os.path.join(training_data_path,attack_t[0]+"_adv_gt.csv")
with open(ground_truth_output_path, "w") as f:
f.write(ground_truth_output_string)
print(image_stats_dict)