Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
super(TestFastGradientMethod, self).setUp()
self.attack = FastGradientMethod(self.model, sess=self.sess)
feed={model.g['train_flag']: False}, args=eval_par)
aer = 100 * (1-acc)
results['normal_aer'] = aer
# print('Test AER on normal examples: {:0.4f} %'.format(aer))
if p.ord == 'inf':
import numpy as np
ord = np.inf
elif p.ord == '1':
ord = 1
else:
ord = 2
fgsm_params = {'eps': 0.3, 'ord': ord}
fgsm = FastGradientMethod(model, sess=sess)
adv_x = fgsm.generate(x, **fgsm_params)
adv_x = tf.stop_gradient(adv_x)
preds_adv = model.get_probs(adv_x)
# Evaluate the accuracy of the MNIST model on adversarial examples
acc = model_eval(sess, x, y, preds_adv, X_test, Y_test,
feed={model.g['train_flag']: False},
args=eval_par)
aer = 100 * (1 - acc)
results['adv_aer'] = aer
# print('Test AER on adversarial examples: {:0.4f} %'.format(aer))
print(results['checkpoint'], results['normal_aer'], results[
'adv_aer'], sep=',')
return results
tfe = tf.contrib.eager
x = tfe.Variable(x_val)
adv_x = self.generate(x, **kwargs)
return adv_x.numpy()
def construct_variables(self, kwargs):
"""
Construct the inputs to the attack graph.
Is inherited from the attack class, is overloaded
to raise an error.
"""
error = "This method is not required for eager execution."
raise AttributeError(error)
class FastGradientMethod(Attack, attacks.FastGradientMethod):
"""
Inherited class from Attack and cleverhans.attacks.FastGradientMethod.
This attack was originally implemented by Goodfellow et al. (2015) with the
infinity norm (and is known as the "Fast Gradient Sign Method"). This
implementation extends the attack to other norms, and is therefore called
the Fast Gradient Method.
Paper link: https://arxiv.org/abs/1412.6572
"""
def __init__(self, model, dtypestr='float32', **kwargs):
"""
Creates a FastGradientMethod instance in eager execution.
:model: cleverhans.model.Model
:dtypestr: datatype in the string format.
"""
test_start=test_start,
test_end=test_end)
assert Y_train.shape[1] == 10
# NOTE: for compatibility with Madry Lab downloadable checkpoints,
# we cannot enclose this in a scope or do anything else that would
# change the automatic naming of the variables.
model = MadryMNIST()
x_input = tf.placeholder(tf.float32, shape=[None, 784])
x_image = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
y = tf.placeholder(tf.float32, shape=[None, 10])
if FLAGS.attack_type == 'fgsm':
fgsm = FastGradientMethod(model)
fgsm_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.}
adv_x = fgsm.generate(x_image, **fgsm_params)
elif FLAGS.attack_type == 'bim':
bim = BasicIterativeMethod(model)
bim_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.,
'nb_iter': 50,
'eps_iter': .01}
adv_x = bim.generate(x_image, **bim_params)
else:
raise ValueError(FLAGS.attack_type)
preds_adv = model.get_probs(adv_x)
saver = tf.train.Saver()
with tf.Session() as sess:
# Restore the checkpoint
labels_sub,
nb_classes, nb_epochs_s, batch_size,
learning_rate, data_aug, lmbda, rng=rng,
substitute_model=sub_model,
)
accuracies['sub'] = 0
# Initialize the Fast Gradient Sign Method (FGSM) attack object.
fgsm_par = {
'eps': FLAGS.fgsm_eps, 'ord': np.inf, 'clip_min': 0., 'clip_max': 1.
}
if gan:
if gan.dataset_name == 'celeba':
fgsm_par['clip_min'] = -1.0
fgsm = FastGradientMethod(model_sub, sess=sess)
# Craft adversarial examples using the substitute.
eval_params = {'batch_size': batch_size}
x_adv_sub = fgsm.generate(images_tensor, **fgsm_par)
if FLAGS.debug and gan is not None: # To see some qualitative results.
reconstructed_tensors = gan.reconstruct(x_adv_sub, batch_size=batch_size,
reconstructor_id=2)
x_rec_orig = gan.reconstruct(images_tensor, batch_size=batch_size,
reconstructor_id=3)
x_adv_sub_val = sess.run(x_adv_sub,
feed_dict={images_tensor: x_debug_test,
K.learning_phase(): 0})
sess.run(tf.local_variables_initializer())
x_rec_debug_val, x_rec_orig_val = sess.run(
with sess.as_default():
save("clean_model.joblib", model)
print("Now that the model has been saved, you can evaluate it in a"
" separate process using `evaluate_pickled_model.py`. "
"You should get exactly the same result for both clean and "
"adversarial accuracy as you get within this program.")
# Calculate training error
if testing:
do_eval(preds, x_train, y_train, 'train_clean_train_clean_eval')
# Initialize the Fast Gradient Sign Method (FGSM) attack object and
# graph
fgsm = FastGradientMethod(model, sess=sess)
adv_x = fgsm.generate(x, **fgsm_params)
preds_adv = model.get_logits(adv_x)
# Evaluate the accuracy of the MNIST model on adversarial examples
do_eval(preds_adv, x_test, y_test, 'clean_train_adv_eval', True)
# Calculate training error
if testing:
do_eval(preds_adv, x_train, y_train, 'train_clean_train_adv_eval')
print('Repeating the process, using adversarial training')
# Create a new model and train it to be robust to FastGradientMethod
model2 = make_basic_picklable_cnn()
# Tag the model so that when it is saved to disk, future scripts will
# be able to tell what data it was trained on
update_eps_expr = eps.assign(tf.cond(update_eps_ph >= 0, lambda: update_eps_ph, lambda: eps))
act = U.function(inputs=[observations_ph, stochastic_ph, update_eps_ph],
outputs=output_actions,
givens={update_eps_ph: -1.0, stochastic_ph: True},
updates=[update_eps_expr])
# Load model before attacks graph construction so that TF won't
# complain can't load parameters for attack
U.load_state(model_path)
if attack != None:
if attack == 'fgsm':
def wrapper(x):
return q_func(x, num_actions, scope="q_func", reuse=True, concat_softmax=True)
adversary = FastGradientMethod(CallableModelWrapper(wrapper, 'probs'), sess=U.get_session())
adv_observations = adversary.generate(observations_ph.get(), eps=1.0/255.0,
clip_min=0, clip_max=1.0) * 255.0
elif attack == 'iterative':
def wrapper(x):
return q_func(x, num_actions, scope="q_func", reuse=True, concat_softmax=True)
adversary = BasicIterativeMethod(CallableModelWrapper(wrapper, 'probs'), sess=U.get_session())
adv_observations = adversary.generate(observations_ph.get(), eps=1.0/255.0,
clip_min=0, clip_max=1.0) * 255.0
elif attack == 'cwl2':
def wrapper(x):
return q_func(x, num_actions, scope="q_func", reuse=True)
adversary = CarliniWagnerL2(CallableModelWrapper(wrapper, 'logits'), sess=U.get_session())
cw_params = {'binary_search_steps': 1,
'max_iterations': 100,
'learning_rate': 0.1,
'initial_const': 10,
model.add_rec_model(gan, z_init_val, batch_size)
min_val = 0.0
if gan:
if gan.dataset_name == 'celeba':
min_val = -1.0
if 'rand' in FLAGS.attack_type:
test_images = np.clip(
test_images + args.alpha * np.sign(np.random.randn(*test_images.shape)),
min_val, 1.0)
eps -= args.alpha
if 'fgsm' in FLAGS.attack_type:
attack_params = {'eps': eps, 'ord': np.inf, 'clip_min': min_val, 'clip_max': 1.}
attack_obj = FastGradientMethod(model, sess=sess)
elif FLAGS.attack_type == 'cw':
attack_obj = CarliniWagnerL2(model, back='tf', sess=sess)
attack_iterations = 100
attack_params = {'binary_search_steps': 1,
'max_iterations': attack_iterations,
'learning_rate': 10.0,
'batch_size': batch_size,
'initial_const': 100,
'feed': {K.learning_phase(): 0}}
adv_x = attack_obj.generate(images_pl, **attack_params)
eval_par = {'batch_size': batch_size}
if FLAGS.defense_type == 'defense_gan':
preds_adv = model.get_probs(adv_x)
num_dims = len(images_pl.get_shape())
Y_train, evaluate=evaluate, args=args)
# Initialize the Fast Gradient Sign Method (FGSM) attack object and graph
fgsm = FastGradientMethod(model, back='th')
adv_x = fgsm.generate(x, params={'eps': 0.3})
# Evaluate the accuracy of the MNIST model on adversarial examples
accuracy = th_model_eval(x, y, model(adv_x), X_test, Y_test, args=args)
print('Test accuracy on adversarial examples: ' + str(accuracy))
print("Repeating the process, using adversarial training")
# Redefine Theano model graph
model_2 = cnn_model()
model_2.build(x_shape)
preds_2 = model_2(x)
fgsm = FastGradientMethod(model_2, back='th')
preds_2_adv = model_2(fgsm.generate(x, params={'eps': 0.3}))
def evaluate_2():
# Evaluate the accuracy of the adversarialy trained MNIST model on
# legitimate test examples
accuracy = th_model_eval(x, y, preds_2, X_test, Y_test, args=args)
print('Test accuracy on legitimate test examples: ' + str(accuracy))
# Evaluate the accuracy of the adversarially trained MNIST model on
# adversarial examples
acc_adv = th_model_eval(x, y, preds_2_adv, X_test, Y_test, args=args)
print('Test accuracy on adversarial examples: ' + str(acc_adv))
# Perform adversarial training
th_model_train(x, y, preds_2, model_2.trainable_weights, X_train, Y_train,
predictions_adv=preds_2_adv, evaluate=evaluate_2, args=args)
# Images for inception classifier are normalized to be in [-1, 1] interval,
# eps is a difference between pixels so it should be in [0, 2] interval.
# Renormalizing epsilon from [0, 255] to [0, 2].
eps = 2.0 * FLAGS.max_epsilon / 255.0
batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
nb_classes = 1001
tf.logging.set_verbosity(tf.logging.INFO)
with tf.Graph().as_default():
# Prepare graph
x_input = tf.placeholder(tf.float32, shape=batch_shape)
model = InceptionModel(nb_classes)
fgsm = FastGradientMethod(model)
x_adv = fgsm.generate(x_input, eps=eps, clip_min=-1., clip_max=1.)
# Run computation
saver = tf.train.Saver(slim.get_model_variables())
session_creator = tf.train.ChiefSessionCreator(
scaffold=tf.train.Scaffold(saver=saver),
checkpoint_filename_with_path=FLAGS.checkpoint_path,
master=FLAGS.master)
with tf.train.MonitoredSession(session_creator=session_creator) as sess:
for filenames, images in load_images(FLAGS.input_dir, batch_shape):
adv_images = sess.run(x_adv, feed_dict={x_input: images})
save_images(adv_images, filenames, FLAGS.output_dir)