Skip to content

Commit

Permalink
Add build os to docs yml
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 9, 2023
1 parent cf8295e commit 5314060
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
6 changes: 5 additions & 1 deletion art/attacks/evasion/auto_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ def generate(self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs) -> n
logger.warning("Error completing attack: %s}", str(error))

if self.parallel:
with multiprocess.get_context("spawn").Pool() as pool:
with multiprocess.get_context("spawn").Pool(processes=1) as pool:
# Results come back in the order that they were issued
results = pool.starmap(run_attack, args)
asdfdasd

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
perturbations = []
is_robust = []
for img_idx in range(len(x)):
Expand Down Expand Up @@ -364,6 +365,7 @@ def run_attack(
:param eps: Maximum perturbation that the attacker can introduce.
:return: An array holding the adversarial examples.
"""
eeeeee

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
# Attack only correctly classified samples
x_robust = x[sample_is_robust]
y_robust = y[sample_is_robust]
Expand Down Expand Up @@ -394,4 +396,6 @@ def run_attack(

sample_is_robust[sample_is_robust] = np.invert(sample_is_not_robust)

print("Attack done:", attack)

return x, sample_is_robust
2 changes: 1 addition & 1 deletion art/defences/detector/poison/clustering_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def analyze_by_silhouette_score(
logger.info("computed silhouette score: %s", silhouette_avg)
dict_i.update(suspicious=True)
else:
poison_clusters = (np.array([[]]), )
poison_clusters = (np.array([[]]),)
clean_clusters = np.where(percentages >= 0)
dict_i.update(suspicious=False)
else:
Expand Down
41 changes: 10 additions & 31 deletions tests/attacks/evasion/test_auto_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ def test_classifier_type_check_fail(art_warning):
art_warning(e)


@pytest.mark.skip_framework("tensorflow1", "tensorflow2v1", "keras", "non_dl_frameworks", "mxnet", "kerastf")
@pytest.mark.skip_framework(
"tensorflow1", "tensorflow2v1", "tensorflow2", "keras", "non_dl_frameworks", "mxnet", "kerastf"
)
def test_generate_parallel(art_warning, fix_get_mnist_subset, image_dl_estimator, framework):
try:
classifier, _ = image_dl_estimator(from_logits=True)
Expand Down Expand Up @@ -286,35 +288,12 @@ def test_generate_parallel(art_warning, fix_get_mnist_subset, image_dl_estimator
parallel=False,
)

from tensorflow.keras.utils import CustomObjectScope

# Copy
from tests.utils import _tf_weights_loader

_tf_initializer_W_CONV2D_MNIST = _tf_weights_loader("MNIST", "W", "CONV2D", 2)
# _tf_initializer_MNIST_W_CONV2D.__name__ = "_tf_initializer_MNIST_W_CONV2D"
_tf_initializer_B_CONV2D_MNIST = _tf_weights_loader("MNIST", "B", "CONV2D", 2)
# _tf_initializer_MNIST_B_CONV2D.__name__ = "_tf_initializer_MNIST_B_CONV2D"

_tf_initializer_W_DENSE_MNIST = _tf_weights_loader("MNIST", "W", "DENSE", 2)
# _tf_initializer_MNIST_W_DENSE.__name__ = "_tf_initializer_MNIST_W_DENSE"
_tf_initializer_B_DENSE_MNIST = _tf_weights_loader("MNIST", "B", "DENSE", 2)
# _tf_initializer_MNIST_B_DENSE.__name__ = "_tf_initializer_MNIST_B_DENSE"

custom_objects = {
"_tf_initializer_W_CONV2D_MNIST": _tf_initializer_W_CONV2D_MNIST,
"_tf_initializer_B_CONV2D_MNIST": _tf_initializer_B_CONV2D_MNIST,
"_tf_initializer_W_DENSE_MNIST": _tf_initializer_W_DENSE_MNIST,
"_tf_initializer_B_DENSE_MNIST": _tf_initializer_B_DENSE_MNIST,
}

with CustomObjectScope(custom_objects):
x_train_mnist_adv = attack.generate(x=x_train_mnist, y=y_train_mnist)
x_train_mnist_adv = attack.generate(x=x_train_mnist, y=y_train_mnist)

x_train_mnist_adv_nop = attack_noparallel.generate(x=x_train_mnist, y=y_train_mnist)

assert np.mean(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(0.0182, abs=0.105)
assert np.max(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(0.3, abs=0.05)
assert np.mean(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(expected=0.0182, abs=0.105)
assert np.max(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(expected=0.3, abs=0.05)

noparallel_perturbation = np.linalg.norm(x_train_mnist[[2]] - x_train_mnist_adv_nop[[2]])
parallel_perturbation = np.linalg.norm(x_train_mnist[[2]] - x_train_mnist_adv[[2]])
Expand All @@ -334,10 +313,10 @@ def test_generate_parallel(art_warning, fix_get_mnist_subset, image_dl_estimator
parallel=True,
)

with CustomObjectScope(custom_objects):
x_train_mnist_adv = attack.generate(x=x_train_mnist, y=y_train_mnist)
x_train_mnist_adv = attack.generate(x=x_train_mnist, y=y_train_mnist)

assert np.mean(x_train_mnist_adv - x_train_mnist) == pytest.approx(expected=0.0, abs=0.0075)
assert np.max(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(expected=eps, abs=0.005)

assert np.mean(x_train_mnist_adv - x_train_mnist) == pytest.approx(0.0, abs=0.0075)
assert np.max(np.abs(x_train_mnist_adv - x_train_mnist)) == pytest.approx(eps, abs=0.005)
except ARTTestException as e:
art_warning(e)
1 change: 1 addition & 0 deletions tests/defences/detector/poison/test_provenance_defence.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def setUpClass(cls):
eps=1.0,
x_val=valid_data,
y_val=valid_labels,
max_iter=100,
verbose=False,
)

Expand Down
74 changes: 51 additions & 23 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import warnings

import numpy as np
import tensorflow.python.keras.initializers.initializers_v2

from art.estimators.classification.tensorflow import TensorFlowV2Classifier
from art.estimators.encoding.tensorflow import TensorFlowEncoder
Expand Down Expand Up @@ -165,38 +166,61 @@ def is_valid_framework(framework):
return True


def _tf_weights_loader(dataset, weights_type, layer="DENSE", tf_version=1):
class _tf_weights_loader(tensorflow.keras.initializers.Initializer):

filename = str(weights_type) + "_" + str(layer) + "_" + str(dataset) + ".npy"
import tensorflow as tf

# pylint: disable=W0613
# disable pylint because of API requirements for function
if tf_version == 1:
def __init__(self, dataset, weights_type, layer="DENSE", tf_version=1):
self.dataset = dataset
self.weights_type = weights_type
self.layer = layer
self.tf_version = tf_version

def _tf_initializer(_, dtype, partition_info):
import tensorflow as tf
def get_config(self):
return {
"dataset": self.dataset,
"weights_type": self.weights_type,
"layer": self.layer,
"tf_version": self.tf_version,
}

weights = np.load(
os.path.join(os.path.dirname(os.path.dirname(__file__)), "utils/resources/models", filename)
)
return tf.constant(weights, dtype)
def __call__(self, shape, dtype=None, **kwargs):

elif tf_version == 2:
import tensorflow as tf

def _tf_initializer(_, dtype):
import tensorflow as tf
filename = str(self.weights_type) + "_" + str(self.layer) + "_" + str(self.dataset) + ".npy"

weights = np.load(
os.path.join(os.path.dirname(os.path.dirname(__file__)), "utils/resources/models", filename)
)
return tf.constant(weights, dtype)

else:
raise ValueError("The TensorFlow version tf_version has to be either 1 or 2.")
# pylint: disable=W0613
# disable pylint because of API requirements for function
# if self.tf_version == 1:
#
# def _tf_initializer(_, dtype, partition_info):
# import tensorflow as tf
#
# weights = np.load(
# os.path.join(os.path.dirname(os.path.dirname(__file__)), "utils/resources/models", filename)
# )
# return tf.constant(weights, dtype)

# elif self.tf_version == 2:
#
# def _tf_initializer(_, dtype):
# import tensorflow as tf
#
# weights = np.load(
# os.path.join(os.path.dirname(os.path.dirname(__file__)), "utils/resources/models", filename)
# )
# return tf.constant(weights, dtype)
#
# else:
# raise ValueError("The TensorFlow version tf_version has to be either 1 or 2.")
#
# _tf_initializer.__name__ = "_tf_initializer_" + str(self.weights_type) + "_" + str(self.layer) + "_" + str(self.dataset)

_tf_initializer.__name__ = "_tf_initializer_" + str(weights_type) + "_" + str(layer) + "_" + str(dataset)
weights = np.load(os.path.join(os.path.dirname(os.path.dirname(__file__)), "utils/resources/models", filename))
return tf.convert_to_tensor(weights, dtype)

return _tf_initializer
# return _tf_initializer


def _kr_weights_loader(dataset, weights_type, layer="DENSE"):
Expand Down Expand Up @@ -468,6 +492,10 @@ def get_image_classifier_tf_v2(from_logits=False):
_tf_initializer_W_DENSE_MNIST = _tf_weights_loader("MNIST", "W", "DENSE", 2)
_tf_initializer_B_DENSE_MNIST = _tf_weights_loader("MNIST", "B", "DENSE", 2)

print("W", _tf_initializer_W_CONV2D_MNIST)
print("B", _tf_initializer_B_CONV2D_MNIST)
# sdf

model = Sequential()
model.add(
Conv2D(
Expand Down

0 comments on commit 5314060

Please sign in to comment.