Skip to content

Commit 1a0db4b

Browse files
authored
Merge pull request #1097 from Trusted-AI/development_iris_162
Fix failing download of Iris dataset in tests
2 parents 72e66c1 + fa7d501 commit 1a0db4b

File tree

5 files changed

+68
-75
lines changed

5 files changed

+68
-75
lines changed

art/utils.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -844,22 +844,14 @@ def load_iris(raw: bool = False, test_set: float = 0.3) -> DATASET_TYPE:
844844
:param test_set: Proportion of the data to use as validation split. The value should be between 0 and 1.
845845
:return: Entire dataset and labels.
846846
"""
847-
# Download data if needed
848-
path = get_file(
849-
"iris.data",
850-
path=config.ART_DATA_PATH,
851-
extract=False,
852-
url="https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
853-
)
854-
855-
data = np.loadtxt(path, delimiter=",", usecols=(0, 1, 2, 3), dtype=config.ART_NUMPY_DTYPE)
856-
labels = np.loadtxt(path, delimiter=",", usecols=4, dtype=str)
847+
from sklearn.datasets import load_iris as load_iris_sk
857848

858-
# Preprocess
849+
iris = load_iris_sk()
850+
data = iris.data
859851
if not raw:
860-
label_map = {"Iris-setosa": 0, "Iris-versicolor": 1, "Iris-virginica": 2}
861-
labels = np.array([label_map[labels[i]] for i in range(labels.size)], dtype=np.int32)
862-
data, labels = preprocess(data, labels, nb_classes=3)
852+
data /= np.amax(data)
853+
labels = to_categorical(iris.target, 3)
854+
863855
min_, max_ = np.amin(data), np.amax(data)
864856

865857
# Split training and test sets
@@ -883,7 +875,7 @@ def load_iris(raw: bool = False, test_set: float = 0.3) -> DATASET_TYPE:
883875
data[50 + split_index : 100],
884876
data[100 + split_index :],
885877
)
886-
)
878+
).astype(np.float32)
887879
y_test = np.vstack(
888880
(
889881
labels[split_index:50],
@@ -899,7 +891,7 @@ def load_iris(raw: bool = False, test_set: float = 0.3) -> DATASET_TYPE:
899891

900892
# Shuffle training set
901893
random_indices = np.random.permutation(len(y_train))
902-
x_train, y_train = x_train[random_indices], y_train[random_indices]
894+
x_train, y_train = x_train[random_indices].astype(np.float32), y_train[random_indices]
903895

904896
return (x_train, y_train), (x_test, y_test), min_, max_
905897

tests/attacks/inference/attribute_inference/test_black_box.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def transform_feature(x):
8181
# check accuracy
8282
train_acc = np.sum(inferred_train == x_train_feature.reshape(1, -1)) / len(inferred_train)
8383
test_acc = np.sum(inferred_test == x_test_feature.reshape(1, -1)) / len(inferred_test)
84-
assert train_acc == pytest.approx(0.8285, abs=0.09)
85-
assert test_acc == pytest.approx(0.8888, abs=0.09)
84+
assert train_acc == pytest.approx(0.8285, abs=0.12)
85+
assert test_acc == pytest.approx(0.8888, abs=0.12)
8686

8787
except ARTTestException as e:
8888
art_warning(e)
@@ -202,8 +202,8 @@ def transform_feature(x):
202202
# check accuracy
203203
train_acc = np.sum(np.all(inferred_train == train_one_hot, axis=1)) / len(inferred_train)
204204
test_acc = np.sum(np.all(inferred_test == test_one_hot, axis=1)) / len(inferred_test)
205-
assert pytest.approx(0.9145, abs=0.03) == train_acc
206-
assert pytest.approx(0.9333, abs=0.03) == test_acc
205+
assert pytest.approx(0.8666, abs=0.03) == train_acc
206+
assert pytest.approx(0.8888, abs=0.03) == test_acc
207207

208208
except ARTTestException as e:
209209
art_warning(e)
@@ -270,7 +270,7 @@ def transform_feature(x):
270270
# train attack model
271271
attack.fit(x_train)
272272
# infer attacked feature
273-
values = [[-0.6324555, 1.5811388], [-0.4395245, 2.2751858], [-1.1108746, 0.9001915]]
273+
values = [[-0.559017, 1.7888544], [-0.47003216, 2.127514], [-1.1774395, 0.84930056]]
274274
inferred_train = attack.infer(x_train_for_attack, x_train_predictions, values=values)
275275
inferred_test = attack.infer(x_test_for_attack, x_test_predictions, values=values)
276276
# check accuracy
@@ -280,8 +280,8 @@ def transform_feature(x):
280280
test_acc = np.sum(
281281
np.all(np.around(inferred_test, decimals=3) == np.around(test_one_hot, decimals=3), axis=1)
282282
) / len(inferred_test)
283-
assert pytest.approx(0.9145, abs=0.03) == train_acc
284-
assert pytest.approx(0.9333, abs=0.03) == test_acc
283+
assert pytest.approx(0.8666, abs=0.05) == train_acc
284+
assert pytest.approx(0.8666, abs=0.05) == test_acc
285285

286286
except ARTTestException as e:
287287
art_warning(e)

tests/attacks/test_elastic_net.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def test_8_keras_iris_clipped(self):
422422
classifier = get_tabular_classifier_kr()
423423
attack = ElasticNet(classifier, targeted=False, max_iter=10, verbose=False)
424424
x_test_adv = attack.generate(self.x_test_iris)
425-
expected_x_test_adv = np.asarray([0.85931635, 0.44633555, 0.65658355, 0.23840423])
425+
expected_x_test_adv = np.asarray([0.860373, 0.455002, 0.654925, 0.240258])
426426
np.testing.assert_array_almost_equal(x_test_adv[0, :], expected_x_test_adv, decimal=6)
427427
self.assertLessEqual(np.amax(x_test_adv), 1.0)
428428
self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
@@ -470,7 +470,7 @@ def test_8_keras_iris_clipped(self):
470470
1,
471471
1,
472472
2,
473-
0,
473+
2,
474474
2,
475475
2,
476476
1,
@@ -491,7 +491,7 @@ def test_9_keras_iris_unbounded(self):
491491
classifier = KerasClassifier(model=classifier._model, use_logits=False, channels_first=True)
492492
attack = ElasticNet(classifier, targeted=False, max_iter=10, verbose=False)
493493
x_test_adv = attack.generate(self.x_test_iris)
494-
expected_x_test_adv = np.asarray([0.85931635, 0.44633555, 0.65658355, 0.23840423])
494+
expected_x_test_adv = np.asarray([0.860373, 0.455002, 0.654925, 0.240258])
495495
np.testing.assert_array_almost_equal(x_test_adv[0, :], expected_x_test_adv, decimal=6)
496496
predictions_adv = np.argmax(classifier.predict(x_test_adv), axis=1)
497497
np.testing.assert_array_equal(
@@ -537,7 +537,7 @@ def test_9_keras_iris_unbounded(self):
537537
1,
538538
1,
539539
2,
540-
0,
540+
2,
541541
2,
542542
2,
543543
1,
@@ -557,7 +557,7 @@ def test_3_tensorflow_iris(self):
557557
# Test untargeted attack
558558
attack = ElasticNet(classifier, targeted=False, max_iter=10, verbose=False)
559559
x_test_adv = attack.generate(self.x_test_iris)
560-
expected_x_test_adv = np.asarray([0.8479195, 0.42525578, 0.70166135, 0.28664514])
560+
expected_x_test_adv = np.asarray([0.852286, 0.434626, 0.703376, 0.293738])
561561
np.testing.assert_array_almost_equal(x_test_adv[0, :], expected_x_test_adv, decimal=6)
562562
self.assertLessEqual(np.amax(x_test_adv), 1.0)
563563
self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
@@ -596,13 +596,13 @@ def test_3_tensorflow_iris(self):
596596
2,
597597
2,
598598
2,
599-
2,
599+
1,
600600
2,
601601
1,
602602
2,
603603
1,
604604
0,
605-
2,
605+
1,
606606
2,
607607
1,
608608
2,
@@ -622,7 +622,7 @@ def test_3_tensorflow_iris(self):
622622
targets = random_targets(self.y_test_iris, nb_classes=3)
623623
attack = ElasticNet(classifier, targeted=True, max_iter=10, verbose=False)
624624
x_test_adv = attack.generate(self.x_test_iris, **{"y": targets})
625-
expected_x_test_adv = np.asarray([0.885649, 0.51815695, 0.5026782, 0.0558902])
625+
expected_x_test_adv = np.asarray([0.892806, 0.531875, 0.501707, 0.059951])
626626
np.testing.assert_array_almost_equal(x_test_adv[0, :], expected_x_test_adv, decimal=6)
627627
self.assertLessEqual(np.amax(x_test_adv), 1.0)
628628
self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
@@ -692,7 +692,7 @@ def test_5_pytorch_iris(self):
692692
classifier = get_tabular_classifier_pt()
693693
attack = ElasticNet(classifier, targeted=False, max_iter=10, verbose=False)
694694
x_test_adv = attack.generate(self.x_test_iris.astype(np.float32))
695-
expected_x_test_adv = np.asarray([0.8479194, 0.42525578, 0.70166135, 0.28664517])
695+
expected_x_test_adv = np.asarray([0.852286, 0.434626, 0.703376, 0.293738])
696696
np.testing.assert_array_almost_equal(x_test_adv[0, :], expected_x_test_adv, decimal=6)
697697
self.assertLessEqual(np.amax(x_test_adv), 1.0)
698698
self.assertGreaterEqual(np.amin(x_test_adv), 0.0)
@@ -731,13 +731,13 @@ def test_5_pytorch_iris(self):
731731
2,
732732
2,
733733
2,
734-
2,
734+
1,
735735
2,
736736
1,
737737
2,
738738
1,
739739
0,
740-
2,
740+
1,
741741
2,
742742
1,
743743
2,

tests/estimators/classification/test_scikitlearn.py

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ def test_type(self):
203203

204204
def test_predict(self):
205205
y_predicted = self.classifier.predict(self.x_test_iris[0:1])
206-
y_expected = np.asarray([[0.07809449, 0.36258262, 0.55932295]])
206+
y_expected = np.asarray([[0.07997696, 0.36272544, 0.5572976]])
207207
np.testing.assert_array_almost_equal(y_predicted, y_expected, decimal=4)
208208

209209
def test_class_gradient_none_1(self):
210210
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=None)
211211
grad_expected = [
212212
[
213-
[-1.97934151, 1.36346793, -6.29719639, -2.61386204],
214-
[-0.56940532, -0.71100581, -1.00625587, -0.68006182],
215-
[0.64548057, 0.27053964, 1.5315429, 0.80580771],
213+
[-1.98016214, 1.35561633, -6.28256321, -2.60129547],
214+
[-0.56317347, -0.70493793, -0.98908591, -0.67106223],
215+
[0.65072, 0.2642768, 1.54536295, 0.81007898],
216216
]
217217
]
218218
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
@@ -221,45 +221,46 @@ def test_class_gradient_none_2(self):
221221
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=None)
222222
grad_expected = [
223223
[
224-
[-1.97934151, 1.36346793, -6.29719639, -2.61386204],
225-
[-0.56940532, -0.71100581, -1.00625587, -0.68006182],
226-
[0.64548057, 0.27053964, 1.5315429, 0.80580771],
224+
[-1.98016214, 1.35561633, -6.28256369, -2.60129547],
225+
[-0.56317353, -0.70493793, -0.98908603, -0.67106229],
226+
[0.65071994, 0.2642768, 1.54536283, 0.81007892],
227227
],
228228
[
229-
[-1.92147708, 1.3512013, -6.13324356, -2.53924561],
230-
[-0.51154077, -0.72327244, -0.84230322, -0.60544527],
231-
[0.70334512, 0.25827295, 1.69549561, 0.88042426],
229+
[-1.92221594, 1.34292829, -6.11845303, -2.5268743],
230+
[-0.50522733, -0.71762598, -0.82497525, -0.59664112],
231+
[0.70866615, 0.25158882, 1.70947361, 0.88450009],
232232
],
233233
]
234234
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
235235

236236
def test_class_gradient_int_1(self):
237237
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=1)
238-
grad_expected = [[[-0.56940532, -0.71100581, -1.00625587, -0.68006182]]]
238+
grad_expected = [[[-0.56317347, -0.70493793, -0.98908591, -0.67106223]]]
239239

240240
for i_shape in range(4):
241241
self.assertAlmostEqual(grad_predicted[0, 0, i_shape], grad_expected[0][0][i_shape], 3)
242242

243243
def test_class_gradient_int_2(self):
244244
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=1)
245245
grad_expected = [
246-
[[-0.56940532, -0.71100581, -1.00625587, -0.68006182]],
247-
[[-0.51154077, -0.72327244, -0.84230322, -0.60544527]],
246+
[[-0.56317353, -0.70493793, -0.98908603, -0.67106229]],
247+
[[-0.50522733, -0.71762598, -0.82497525, -0.59664112]],
248248
]
249249
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
250250

251251
def test_class_gradient_list_1(self):
252252
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=[1])
253-
grad_expected = [[[-0.56940532, -0.71100581, -1.00625587, -0.68006182]]]
253+
grad_expected = [[[-0.56317347, -0.70493793, -0.98908591, -0.67106223]]]
254+
print(grad_predicted)
254255

255256
for i_shape in range(4):
256257
self.assertAlmostEqual(grad_predicted[0, 0, i_shape], grad_expected[0][0][i_shape], 3)
257258

258259
def test_class_gradient_list_2(self):
259260
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=[1, 2])
260261
grad_expected = [
261-
[[-0.56940532, -0.71100581, -1.00625587, -0.68006182]],
262-
[[0.70334512, 0.25827295, 1.69549561, 0.88042426]],
262+
[[-0.56317353, -0.70493793, -0.98908603, -0.67106229]],
263+
[[0.70866615, 0.25158882, 1.70947361, 0.88450009]],
263264
]
264265
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
265266

@@ -274,7 +275,7 @@ def test_class_gradient_label_wrong_type(self):
274275

275276
def test_loss_gradient(self):
276277
grad_predicted = self.classifier.loss_gradient(self.x_test_iris[0:1], self.y_test_iris[0:1])
277-
grad_expected = np.asarray([[-0.21516019, -0.09017988, -0.51051431, -0.26860258]])
278+
grad_expected = np.asarray([[-0.21690667, -0.08809228, -0.51512096, -0.27002633]])
278279
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
279280

280281

@@ -304,7 +305,7 @@ def test_type(self):
304305
def test_class_gradient(self):
305306
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=None)
306307
grad_expected = np.asarray(
307-
[[[-0.1428355, 0.12111039, -0.45059183, -0.17579888], [0.1428355, -0.12111039, 0.45059183, 0.17579888]]]
308+
[[[-0.14551339, 0.12298754, -0.45839342, -0.17835225], [0.14551339, -0.12298754, 0.45839342, 0.17835225]]]
308309
)
309310
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=3)
310311

@@ -318,7 +319,7 @@ def test_loss_gradient(self):
318319
][:, [0, 1]]
319320

320321
grad_predicted = self.classifier.loss_gradient(x_test_binary[0:1], y_test_binary[0:1])
321-
grad_expected = np.asarray([[-0.37461641, 0.31763777, -1.18177287, -0.4610699]])
322+
grad_expected = np.asarray([[-0.3771413, 0.31875887, -1.18806318, -0.46225301]])
322323
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
323324

324325

@@ -342,16 +343,16 @@ def test_predict(self):
342343

343344
def test_loss_gradient(self):
344345
grad_predicted = self.classifier.loss_gradient(self.x_test_iris[0:1], self.y_test_iris[0:1])
345-
grad_expected = np.asarray([[-2.8753524, 0.31140438, -7.889445, -3.8314016]])
346+
grad_expected = np.asarray([[-2.9100819, 0.3048792, -7.935282, -3.840562]])
346347
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
347348

348349
def test_class_gradient_none_1(self):
349350
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=None)
350351
grad_expected = [
351352
[
352-
[-1.52398277, 1.6984953, -6.05832438, -2.45788848],
353-
[-0.43530558, -1.38692786, 0.41607214, -0.15109791],
354-
[1.95928835, -0.31156744, 5.64225224, 2.6089864],
353+
[-1.5939425, 1.67301144, -6.15095666, -2.4862934],
354+
[-0.40469415, -1.37572607, 0.46867108, -0.13317975],
355+
[1.99863665, -0.29728537, 5.68228559, 2.61947315],
355356
]
356357
]
357358
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
@@ -360,41 +361,41 @@ def test_class_gradient_none_2(self):
360361
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=None)
361362
grad_expected = [
362363
[
363-
[-1.52398277, 1.6984953, -6.05832438, -2.45788848],
364-
[-0.43530558, -1.38692786, 0.41607214, -0.15109791],
365-
[1.95928835, -0.31156744, 5.64225224, 2.6089864],
364+
[-1.5939425, 1.67301144, -6.15095666, -2.4862934],
365+
[-0.40469415, -1.37572607, 0.46867108, -0.13317975],
366+
[1.99863665, -0.29728537, 5.68228559, 2.61947315],
366367
],
367368
[
368-
[-1.52592969, 1.67535916, -6.2138464, -2.57887417],
369-
[-0.43875292, -1.38381583, 0.48486185, -0.1011285],
370-
[1.96468261, -0.29154334, 5.72898455, 2.68000267],
369+
[-1.5962279, 1.64964639, -6.30453897, -2.60572715],
370+
[-0.40788449, -1.37232544, 0.53680777, -0.08368929],
371+
[2.00411239, -0.27732096, 5.7677312, 2.68941644],
371372
],
372373
]
373374
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
374375

375376
def test_class_gradient_int_1(self):
376377
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=1)
377-
grad_expected = [[[-0.43530558, -1.38692786, 0.41607214, -0.15109791]]]
378+
grad_expected = [[[-0.40469415, -1.37572607, 0.46867108, -0.13317975]]]
378379
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
379380

380381
def test_class_gradient_int_2(self):
381382
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=1)
382383
grad_expected = [
383-
[[-0.43530558, -1.38692786, 0.41607214, -0.15109791]],
384-
[[-0.43875292, -1.38381583, 0.48486185, -0.1011285]],
384+
[[-0.40469415, -1.37572607, 0.46867108, -0.13317975]],
385+
[[-0.40788449, -1.37232544, 0.53680777, -0.08368929]],
385386
]
386387
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
387388

388389
def test_class_gradient_list_1(self):
389390
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=[1])
390-
grad_expected = [[[-0.43530558, -1.38692786, 0.41607214, -0.15109791]]]
391+
grad_expected = [[[-0.40469415, -1.37572607, 0.46867108, -0.13317975]]]
391392
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
392393

393394
def test_class_gradient_list_2(self):
394395
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:2], label=[1, 2])
395396
grad_expected = [
396-
[[-0.43530558, -1.38692786, 0.41607214, -0.15109791]],
397-
[[1.96468261, -0.29154334, 5.72898455, 2.68000267]],
397+
[[-0.40469415, -1.37572607, 0.46867108, -0.13317975]],
398+
[[2.00411239, -0.27732096, 5.7677312, 2.68941644]],
398399
]
399400
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
400401

@@ -427,16 +428,16 @@ def test_predict(self):
427428

428429
def test_loss_gradient(self):
429430
grad_predicted = self.classifier.loss_gradient(self.x_test_iris[0:1], self.y_test_iris[0:1])
430-
grad_expected = np.asarray([[0.38537693, 0.5659405, -3.600912, -2.338979]])
431+
grad_expected = np.asarray([[0.38021886, 0.57562107, -3.599666, -2.3177252]])
431432
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)
432433

433434
def test_class_gradient(self):
434435
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=None)
435436
grad_expected = [
436437
[
437-
[-0.34997019, 1.61489704, -3.49002061, -1.46298544],
438-
[-0.11249995, -2.52947052, 0.7052329, -0.44872424],
439-
[-0.3853818, -0.5659519, 3.60090744, 2.33898192],
438+
[-0.34648966, 1.63777444, -3.51845999, -1.4609451],
439+
[-0.11198028, -2.51565392, 0.71538245, -0.44830889],
440+
[-0.38021886, -0.57562105, 3.59966607, 2.31772514],
440441
]
441442
]
442443
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ def test_preprocess(self):
327327
def test_iris(self):
328328
(x_train, y_train), (x_test, y_test), min_, max_ = load_iris()
329329

330-
self.assertTrue((min_ == 0).all())
331-
self.assertTrue((max_ == 1).all())
330+
self.assertAlmostEqual(min_, 0.012658227848101266, places=6)
331+
self.assertEqual(max_, 1.0)
332332
self.assertEqual(x_train.shape[0], y_train.shape[0])
333333
self.assertEqual(x_test.shape[0], y_test.shape[0])
334334
train_labels = np.argmax(y_train, axis=1)

0 commit comments

Comments
 (0)