-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathattacks.py
59 lines (55 loc) · 2.86 KB
/
attacks.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
import numpy as np
def fgsm_targeted(x, predictions, x_original, eps, back='tf', clip_min=None, clip_max=None, target_class = 0):
if back == 'tf':
# Compute FGSM using TensorFlow
from attacks_tf import fgsm_targeted as fgsm_targeted_tf
return fgsm_targeted_tf(x, predictions, x_original, eps, clip_min=clip_min, clip_max=clip_max, target_class = target_class)
def fgsm(x, predictions, eps, back='tf', clip_min=None, clip_max=None):
"""
A wrapper for the Fast Gradient Sign Method.
It calls the right function, depending on the
user's backend.
:param x: the input
:param predictions: the model's output
:param eps: the epsilon (input variation parameter)
:param back: switch between TensorFlow ('tf') and
Theano ('th') implementation
:param clip_min: optional parameter that can be used to set a minimum
value for components of the example returned
:param clip_max: optional parameter that can be used to set a maximum
value for components of the example returned
:return: a tensor for the adversarial example
"""
if back == 'tf':
# Compute FGSM using TensorFlow
from attacks_tf import fgsm as fgsm_tf
return fgsm_tf(x, predictions, eps, clip_min=clip_min, clip_max=clip_max)
elif back == 'th':
raise NotImplementedError("Theano jsma not implemented.")
def jsma(sess, x, predictions, grads, sample, target, theta, gamma=np.inf, increase=True, back='tf', clip_min=None, clip_max=None):
"""
A wrapper for the Jacobian-based saliency map approach.
It calls the right function, depending on the
user's backend.
:param sess: TF session
:param x: the input
:param predictions: the model's symbolic output (linear output, pre-softmax)
:param sample: (1 x 1 x img_rows x img_cols) numpy array with sample input
:param target: target class for input sample
:param theta: delta for each feature adjustment
:param gamma: a float between 0 - 1 indicating the maximum distortion percentage
:param increase: boolean; true if we are increasing pixels, false otherwise
:param back: switch between TensorFlow ('tf') and
Theano ('th') implementation
:param clip_min: optional parameter that can be used to set a minimum
value for components of the example returned
:param clip_max: optional parameter that can be used to set a maximum
value for components of the example returned
:return: an adversarial sample
"""
if back == 'tf':
# Compute Jacobian-based saliency map attack using TensorFlow
from attacks_tf import jsma_tf
return jsma_tf(sess, x, predictions, grads, sample, target, theta, gamma, increase, clip_min, clip_max)
elif back == 'th':
raise NotImplementedError("Theano jsma not implemented.")