@@ -38,6 +38,53 @@ class TestCarliniL2(unittest.TestCase):
3838 """
3939 A unittest class for testing the Carlini2 attack.
4040 """
41+ def test_failure_attack (self ):
42+ """
43+ Test the corner case when attack is failed.
44+ :return:
45+ """
46+ # Build a TFClassifier
47+ # Define input and output placeholders
48+ self ._input_ph = tf .placeholder (tf .float32 , shape = [None , 28 , 28 , 1 ])
49+ self ._output_ph = tf .placeholder (tf .int32 , shape = [None , 10 ])
50+
51+ # Define the tensorflow graph
52+ conv = tf .layers .conv2d (self ._input_ph , 4 , 5 , activation = tf .nn .relu )
53+ conv = tf .layers .max_pooling2d (conv , 2 , 2 )
54+ fc = tf .contrib .layers .flatten (conv )
55+
56+ # Logits layer
57+ self ._logits = tf .layers .dense (fc , 10 )
58+
59+ # Train operator
60+ self ._loss = tf .reduce_mean (tf .losses .softmax_cross_entropy (logits = self ._logits , onehot_labels = self ._output_ph ))
61+ optimizer = tf .train .AdamOptimizer (learning_rate = 0.01 )
62+ self ._train = optimizer .minimize (self ._loss )
63+
64+ # Tensorflow session and initialization
65+ self ._sess = tf .Session ()
66+ self ._sess .run (tf .global_variables_initializer ())
67+
68+ # Get MNIST
69+ batch_size , nb_train , nb_test = 100 , 1000 , 10
70+ (x_train , y_train ), (x_test , y_test ), _ , _ = load_mnist ()
71+ x_train , y_train = x_train [:nb_train ], y_train [:nb_train ]
72+ x_test , y_test = x_test [:nb_test ], y_test [:nb_test ]
73+
74+ # Train the classifier
75+ tfc = TFClassifier ((0 , 1 ), self ._input_ph , self ._logits , self ._output_ph ,
76+ self ._train , self ._loss , None , self ._sess )
77+ tfc .fit (x_train , y_train , batch_size = batch_size , nb_epochs = 2 )
78+
79+ # Failure attack
80+ cl2m = CarliniL2Method (classifier = tfc , targeted = True , max_iter = 0 , binary_search_steps = 0 ,
81+ learning_rate = 2e-2 , initial_const = 3 , decay = 1e-2 )
82+ params = {'y' : random_targets (y_test , tfc .nb_classes )}
83+ x_test_adv = cl2m .generate (x_test , ** params )
84+ self .assertTrue ((x_test_adv <= 1 ).all ())
85+ self .assertTrue ((x_test_adv >= 0 ).all ())
86+ np .testing .assert_almost_equal (x_test , x_test_adv , 3 )
87+
4188 def test_tfclassifier (self ):
4289 """
4390 First test with the TFClassifier.
@@ -82,6 +129,8 @@ def test_tfclassifier(self):
82129 params = {'y' : random_targets (y_test , tfc .nb_classes )}
83130 x_test_adv = cl2m .generate (x_test , ** params )
84131 self .assertFalse ((x_test == x_test_adv ).all ())
132+ self .assertTrue ((x_test_adv <= 1 ).all ())
133+ self .assertTrue ((x_test_adv >= 0 ).all ())
85134 target = np .argmax (params ['y' ], axis = 1 )
86135 y_pred_adv = np .argmax (tfc .predict (x_test_adv ), axis = 1 )
87136 self .assertTrue ((target == y_pred_adv ).all ())
@@ -92,6 +141,8 @@ def test_tfclassifier(self):
92141 params = {'y' : random_targets (y_test , tfc .nb_classes )}
93142 x_test_adv = cl2m .generate (x_test , ** params )
94143 self .assertFalse ((x_test == x_test_adv ).all ())
144+ self .assertTrue ((x_test_adv <= 1 ).all ())
145+ self .assertTrue ((x_test_adv >= 0 ).all ())
95146 target = np .argmax (params ['y' ], axis = 1 )
96147 y_pred_adv = np .argmax (tfc .predict (x_test_adv ), axis = 1 )
97148 self .assertTrue ((target != y_pred_adv ).all ())
@@ -102,6 +153,8 @@ def test_tfclassifier(self):
102153 params = {}
103154 x_test_adv = cl2m .generate (x_test , ** params )
104155 self .assertFalse ((x_test == x_test_adv ).all ())
156+ self .assertTrue ((x_test_adv <= 1 ).all ())
157+ self .assertTrue ((x_test_adv >= 0 ).all ())
105158 y_pred = np .argmax (tfc .predict (x_test ), axis = 1 )
106159 y_pred_adv = np .argmax (tfc .predict (x_test_adv ), axis = 1 )
107160 self .assertTrue ((y_pred != y_pred_adv ).all ())
@@ -141,6 +194,8 @@ def test_krclassifier(self):
141194 params = {'y' : random_targets (y_test , krc .nb_classes )}
142195 x_test_adv = cl2m .generate (x_test , ** params )
143196 self .assertFalse ((x_test == x_test_adv ).all ())
197+ self .assertTrue ((x_test_adv <= 1 ).all ())
198+ self .assertTrue ((x_test_adv >= 0 ).all ())
144199 target = np .argmax (params ['y' ], axis = 1 )
145200 y_pred_adv = np .argmax (krc .predict (x_test_adv ), axis = 1 )
146201 self .assertTrue ((target == y_pred_adv ).any ())
@@ -151,6 +206,8 @@ def test_krclassifier(self):
151206 params = {'y' : random_targets (y_test , krc .nb_classes )}
152207 x_test_adv = cl2m .generate (x_test , ** params )
153208 self .assertFalse ((x_test == x_test_adv ).all ())
209+ self .assertTrue ((x_test_adv <= 1 ).all ())
210+ self .assertTrue ((x_test_adv >= 0 ).all ())
154211 target = np .argmax (params ['y' ], axis = 1 )
155212 y_pred_adv = np .argmax (krc .predict (x_test_adv ), axis = 1 )
156213 self .assertTrue ((target != y_pred_adv ).all ())
@@ -161,6 +218,8 @@ def test_krclassifier(self):
161218 params = {}
162219 x_test_adv = cl2m .generate (x_test , ** params )
163220 self .assertFalse ((x_test == x_test_adv ).all ())
221+ self .assertTrue ((x_test_adv <= 1 ).all ())
222+ self .assertTrue ((x_test_adv >= 0 ).all ())
164223 y_pred = np .argmax (krc .predict (x_test ), axis = 1 )
165224 y_pred_adv = np .argmax (krc .predict (x_test_adv ), axis = 1 )
166225 self .assertTrue ((y_pred != y_pred_adv ).any ())
@@ -196,6 +255,8 @@ def test_ptclassifier(self):
196255 params = {'y' : random_targets (y_test , ptc .nb_classes )}
197256 x_test_adv = cl2m .generate (x_test , ** params )
198257 self .assertFalse ((x_test == x_test_adv ).all ())
258+ self .assertTrue ((x_test_adv <= 1 ).all ())
259+ self .assertTrue ((x_test_adv >= 0 ).all ())
199260 target = np .argmax (params ['y' ], axis = 1 )
200261 y_pred_adv = np .argmax (ptc .predict (x_test_adv ), axis = 1 )
201262 self .assertTrue ((target == y_pred_adv ).any ())
@@ -206,6 +267,8 @@ def test_ptclassifier(self):
206267 params = {'y' : random_targets (y_test , ptc .nb_classes )}
207268 x_test_adv = cl2m .generate (x_test , ** params )
208269 self .assertFalse ((x_test == x_test_adv ).all ())
270+ self .assertTrue ((x_test_adv <= 1 ).all ())
271+ self .assertTrue ((x_test_adv >= 0 ).all ())
209272 target = np .argmax (params ['y' ], axis = 1 )
210273 y_pred_adv = np .argmax (ptc .predict (x_test_adv ), axis = 1 )
211274 self .assertTrue ((target != y_pred_adv ).all ())
@@ -216,6 +279,8 @@ def test_ptclassifier(self):
216279 params = {}
217280 x_test_adv = cl2m .generate (x_test , ** params )
218281 self .assertFalse ((x_test == x_test_adv ).all ())
282+ self .assertTrue ((x_test_adv <= 1 ).all ())
283+ self .assertTrue ((x_test_adv >= 0 ).all ())
219284 y_pred = np .argmax (ptc .predict (x_test ), axis = 1 )
220285 y_pred_adv = np .argmax (ptc .predict (x_test_adv ), axis = 1 )
221286 self .assertTrue ((y_pred != y_pred_adv ).any ())
0 commit comments