Skip to content

Commit 30888fa

Browse files
Irina NicolaeIrina Nicolae
authored andcommitted
Rename norm param in univ perturbation attack for consistency
1 parent d6c9373 commit 30888fa

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

art/attacks/saliency_map.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SaliencyMapMethod(Attack):
1212
"""
1313
attack_params = ['theta', 'gamma']
1414

15+
# TODO Add parameter logits?
1516
def __init__(self, classifier, theta=0.1, gamma=1.):
1617
"""
1718
Create a SaliencyMapMethod instance.
@@ -46,7 +47,7 @@ def generate(self, x, **kwargs):
4647
:rtype: `np.ndarray`
4748
"""
4849
# Parse and save attack-specific parameters
49-
assert self.set_params(**kwargs)
50+
self.set_params(**kwargs)
5051
clip_min, clip_max = self.classifier.clip_values
5152

5253
# Initialize variables
@@ -118,7 +119,7 @@ def set_params(self, **kwargs):
118119
# Save attack-specific parameters
119120
super(SaliencyMapMethod, self).set_params(**kwargs)
120121

121-
if self.gamma < 0 or self.gamma > 1:
122+
if self.gamma <= 0 or self.gamma > 1:
122123
raise ValueError("The total perturbation percentage `gamma` must be between 0 and 1.")
123124

124125
return True

art/attacks/universal_perturbation.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class UniversalPerturbation(Attack):
1818
'jsma': 'art.attacks.saliency_map.SaliencyMapMethod',
1919
'vat': 'art.attacks.virtual_adversarial.VirtualAdversarialMethod'
2020
}
21-
attack_params = ['attacker', 'attacker_params', 'delta', 'max_iter', 'eps', 'p']
21+
attack_params = ['attacker', 'attacker_params', 'delta', 'max_iter', 'eps', 'norm']
2222

2323
def __init__(self, classifier, attacker='deepfool', attacker_params=None, delta=0.2, max_iter=20, eps=10.0,
24-
p=np.inf):
24+
norm=np.inf):
2525
"""
2626
:param classifier: A trained model.
2727
:type classifier: :class:`Classifier`
@@ -36,16 +36,16 @@ def __init__(self, classifier, attacker='deepfool', attacker_params=None, delta=
3636
:type max_iter: `int`
3737
:param eps: Attack step size (input variation)
3838
:type eps: `float`
39-
:param p: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
40-
:type p: `int`
39+
:param norm: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
40+
:type norm: `int`
4141
"""
4242
super(UniversalPerturbation, self).__init__(classifier)
4343
kwargs = {'attacker': attacker,
4444
'attacker_params': attacker_params,
4545
'delta': delta,
4646
'max_iter': max_iter,
4747
'eps': eps,
48-
'p': p
48+
'norm': norm
4949
}
5050
self.set_params(**kwargs)
5151

@@ -66,8 +66,8 @@ def generate(self, x, **kwargs):
6666
:type max_iter: `int`
6767
:param eps: Attack step size (input variation)
6868
:type eps: `float`
69-
:param p: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
70-
:type p: `int`
69+
:param norm: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
70+
:type norm: `int`
7171
:return: An array holding the adversarial examples.
7272
:rtype: `np.ndarray`
7373
"""
@@ -108,7 +108,7 @@ def generate(self, x, **kwargs):
108108
v += adv_xi - xi
109109

110110
# Project on L_p ball
111-
v = self._clip_perturbation(v, self.eps, self.p)
111+
v = self._clip_perturbation(v, self.eps, self.norm)
112112
nb_iter += 1
113113

114114
# Compute the error rate
@@ -137,8 +137,8 @@ def set_params(self, **kwargs):
137137
:type max_iter: `int`
138138
:param eps: Attack step size (input variation)
139139
:type eps: `float`
140-
:param p: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
141-
:type p: `int`
140+
:param norm: Order of the norm. Possible values: np.inf, 2 (default is np.inf)
141+
:type norm: `int`
142142
"""
143143
super(UniversalPerturbation, self).set_params(**kwargs)
144144

@@ -153,25 +153,25 @@ def set_params(self, **kwargs):
153153

154154
return True
155155

156-
def _clip_perturbation(self, v, eps, p):
156+
def _clip_perturbation(self, v, eps, norm):
157157
"""
158158
Clip the values in v if their L_p norm is larger than eps.
159159
160160
:param v: array of perturbations to clip.
161161
:type v: `np.ndarray`
162162
:param eps: maximum norm allowed.
163163
:type eps: `float`
164-
:param p: L_p norm to use for clipping. Only p = 2 and p = Inf supported for now.
165-
:type p: `int`
164+
:param norm: L_p norm to use for clipping. Only `norm == 2` and `norm == np.inf` supported for now.
165+
:type norm: `int`
166166
:return: clipped values of v
167167
:rtype: `np.ndarray`
168168
"""
169-
if p == 2:
169+
if norm == 2:
170170
v *= min(1., eps/np.linalg.norm(v, axis=(1, 2)))
171-
elif p == np.inf:
171+
elif norm == np.inf:
172172
v = np.sign(v) * np.minimum(abs(v), eps)
173173
else:
174-
raise NotImplementedError('Values of p different from 2 and Inf are currently not supported.')
174+
raise NotImplementedError('Values of `norm` different from 2 and `np.inf` are currently not supported.')
175175

176176
return v
177177

0 commit comments

Comments
 (0)