Skip to content

Commit

Permalink
Update workflows for TensorFlow/Keras 2.14
Browse files Browse the repository at this point in the history
Signed-off-by: Beat Buesser <[email protected]>
  • Loading branch information
beat-buesser committed Nov 15, 2023
1 parent 68bc1cc commit f45b268
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/attacks/test_adversarial_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_4_pytorch(self):

self.assertAlmostEqual(patch_adv[0, 8, 8], 0.5, delta=0.05)
self.assertAlmostEqual(patch_adv[0, 14, 14], 0.5, delta=0.05)
self.assertAlmostEqual(float(np.sum(patch_adv)), 371.88014772999827, delta=4.0)
self.assertAlmostEqual(float(np.sum(patch_adv)), 367.6218066346819, delta=4.0)

mask = np.ones((1, 28, 28)).astype(bool)
attack_ap.apply_patch(x=x_train, scale=0.1, mask=mask)
Expand Down
4 changes: 2 additions & 2 deletions tests/classifiersFrameworks/test_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_binary_keras_instantiation_and_attack_pgd(art_warning):
]
)
model.summary()
model.compile(optimizer=tf.optimizers.Adam(), loss="binary_crossentropy", metrics=["accuracy"])
model.compile(optimizer=tf.optimizers.legacy.Adam(), loss="binary_crossentropy", metrics=["accuracy"])
classifier = KerasClassifier(model=model)
classifier.fit(train_x, train_y, nb_epochs=5)
pred = classifier.predict(test_x)
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_binary_keras_instantiation_and_attack_pgd(art_warning):
# ]
# )
# model.summary()
# model.compile(optimizer=tf.optimizers.Adam(), loss="binary_crossentropy", metrics=["accuracy"])
# model.compile(optimizer=tf.optimizers.legacy.Adam(), loss="binary_crossentropy", metrics=["accuracy"])
# classifier = art.estimators.classification.TensorFlowV2Classifier(model=model)
# classifier.fit(train_x, train_y, nb_epochs=5)
# pred = classifier.predict(test_x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_adversarial_trainer_awppgd_pytorch_fit_and_predict(get_adv_trainer_awpp
else:
accuracy = np.sum(predictions == y_test_mnist) / x_test_mnist.shape[0]

trainer.fit(x_train_mnist, y_train_mnist, nb_epochs=20)
trainer.fit(x_train_mnist, y_train_mnist, nb_epochs=40)
predictions_new = np.argmax(trainer.predict(x_test_mnist), axis=1)

if label_format == "one_hot":
Expand Down
2 changes: 1 addition & 1 deletion tests/estimators/certification/test_macer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _get_classifier():
import tensorflow as tf

classifier, _ = get_image_classifier_tf()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, name="SGD", decay=5e-4)
optimizer = tf.keras.optimizers.legacy.SGD(learning_rate=0.01, momentum=0.9, name="SGD", decay=5e-4)
scheduler = tf.keras.optimizers.schedules.PiecewiseConstantDecay([250, 400], [0.01, 0.001, 0.0001])
rs = TensorFlowV2MACER(
model=classifier.model,
Expand Down
2 changes: 1 addition & 1 deletion tests/estimators/certification/test_smooth_adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _get_classifier():
import tensorflow as tf

classifier, _ = get_image_classifier_tf()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, name="SGD", decay=1e-4)
optimizer = tf.keras.optimizers.legacy.SGD(learning_rate=0.01, momentum=0.9, name="SGD", decay=1e-4)
scheduler = tf.keras.optimizers.schedules.PiecewiseConstantDecay([50, 100], [0.01, 0.001, 0.0001])
rs = TensorFlowV2SmoothAdv(
model=classifier.model,
Expand Down
10 changes: 9 additions & 1 deletion tests/estimators/classification/test_scikitlearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ def test_class_gradient_none_2(self):

def test_class_gradient_int_1(self):
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=1)
grad_expected = [[[-0.56322294, -0.70427608, -0.98874801, -0.67053026]]]
grad_expected = [[[-0.56322294, -0.70493763, -0.98874801, -0.67053026]]]

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

def test_class_gradient_int_2(self):
Expand All @@ -311,13 +312,16 @@ def test_class_gradient_int_2(self):
[[-0.56322294, -0.70427608, -0.98874801, -0.67053026]],
[[-0.50528532, -0.71700042, -0.82467848, -0.59614766]],
]
print("grad_predicted")
print(grad_predicted)
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)

def test_class_gradient_list_1(self):
grad_predicted = self.classifier.class_gradient(self.x_test_iris[0:1], label=[1])
grad_expected = [[[-0.56322294, -0.70427608, -0.98874801, -0.67053026]]]

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

def test_class_gradient_list_2(self):
Expand All @@ -326,6 +330,8 @@ def test_class_gradient_list_2(self):
[[-0.56322294, -0.70427608, -0.98874801, -0.67053026]],
[[0.70875132, 0.25104877, 1.70929277, 0.88410652]],
]
print("grad_predicted")
print(grad_predicted)
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)

def test_class_gradient_label_wrong_type(self):
Expand All @@ -340,6 +346,8 @@ def test_class_gradient_label_wrong_type(self):
def test_loss_gradient(self):
grad_predicted = self.classifier.loss_gradient(self.x_test_iris[0:1], self.y_test_iris[0:1])
grad_expected = np.asarray([[-0.21693791, -0.08792436, -0.51507443, -0.26990796]])
print("grad_predicted")
print(grad_predicted)
np.testing.assert_array_almost_equal(grad_predicted, grad_expected, decimal=4)

def test_save(self):
Expand Down

0 comments on commit f45b268

Please sign in to comment.