@@ -36,3 +36,38 @@ def _apply(self, image_label, epsilons=1000):
36
36
37
37
38
38
FGSM = GradientSignAttack
39
+
40
+
41
+ class IteratorGradientSignAttack (Attack ):
42
+ """
43
+ This attack was originally implemented by Alexey Kurakin(Google Brain).
44
+ Paper link: https://arxiv.org/pdf/1607.02533.pdf
45
+ """
46
+ def _apply (self , image_label , epsilons = 100 , steps = 10 ):
47
+ """
48
+ Apply the iterative gradient sign attack.
49
+ Args:
50
+ image_label(list): The image and label tuple list of one element.
51
+ epsilons(list|tuple|int): The epsilon (input variation parameter).
52
+ steps(int): The number of iterator steps.
53
+ Return:
54
+ numpy.ndarray: The adversarail sample generated by the algorithm.
55
+ """
56
+ assert len (image_label ) == 1
57
+ pre_label = np .argmax (self .model .predict (image_label ))
58
+ gradient = self .model .gradient (image_label )
59
+ min_ , max_ = self .model .bounds ()
60
+
61
+ if not isinstance (epsilons , Iterable ):
62
+ epsilons = np .linspace (0 , 1 , num = epsilons + 1 )
63
+
64
+ for epsilon in epsilons :
65
+ adv_img = image_label [0 ][0 ].reshape (gradient .shape )
66
+ for _ in range (steps ):
67
+ gradient = self .model .gradient ([(adv_img , image_label [0 ][1 ])])
68
+ gradient_sign = np .sign (gradient ) * (max_ - min_ )
69
+ adv_img = adv_img + epsilon * gradient_sign
70
+ adv_img = np .clip (adv_img , min_ , max_ )
71
+ adv_label = np .argmax (self .model .predict ([(adv_img , 0 )]))
72
+ if pre_label != adv_label :
73
+ return adv_img
0 commit comments