Skip to content

Commit 1a66058

Browse files
Irina NicolaeIrina Nicolae
authored andcommitted
Change Classifier API to framework agnostic
2 parents c99094d + 068fba5 commit 1a66058

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2755
-3290
lines changed

.travis.yml

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
sudo: required
22
dist: trusty
33
language: python
4+
env:
45
matrix:
56
include:
67
- python: 2.7
7-
env:
8-
- KERAS_BACKEND=tensorflow
9-
- TENSORFLOW_V=1.4.1
8+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.5.1
109
- python: 2.7
11-
env:
12-
- KERAS_BACKEND=tensorflow
13-
- TENSORFLOW_V=1.1.0
10+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.6.0
11+
- python: 2.7
12+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.7.0
13+
- python: 3.5
14+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.5.1
1415
- python: 3.5
15-
env:
16-
- KERAS_BACKEND=tensorflow
17-
- TENSORFLOW_V=1.1.0
16+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.6.0
1817
- python: 3.5
19-
env:
20-
- KERAS_BACKEND=tensorflow
21-
- TENSORFLOW_V=1.4.1
18+
env: KERAS_BACKEND=tensorflow TENSORFLOW_V=1.7.0
19+
exclude:
20+
- env:
2221

2322
before_install:
2423
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
@@ -37,15 +36,7 @@ install:
3736
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy scipy matplotlib pandas h5py
3837
- source activate test-environment
3938
# Install TensorFlow
40-
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" && "$TENSORFLOW_V" == "1.4.1" ]]; then
41-
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.1-cp27-none-linux_x86_64.whl;
42-
elif [[ "$TRAVIS_PYTHON_VERSION" == "2.7" && "$TENSORFLOW_V" == "1.1.0" ]]; then
43-
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.1.0-cp27-none-linux_x86_64.whl;
44-
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.5" && "$TENSORFLOW_V" == "1.4.1" ]]; then
45-
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.1-cp35-cp35m-linux_x86_64.whl;
46-
elif [[ "$TRAVIS_PYTHON_VERSION" == "3.5" && "$TENSORFLOW_V" == "1.1.0" ]]; then
47-
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.1.0-cp35-cp35m-linux_x86_64.whl;
48-
fi
39+
- if [[ "$TENSORFLOW_V" != "" ]]; then pip install tensorflow==${TENSORFLOW_V}; fi
4940
- pip install keras
5041
- conda install libgcc
5142
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/travis/miniconda/envs/test-environment/lib

README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,10 @@ stl10_path=./data/stl-10
5555

5656
If the datasets are not present at the indicated path, loading them will also download the data.
5757

58-
## Running scripts
58+
## Running Nemesis
5959

60-
The library contains three main scripts for:
61-
* training a classifier using (`train.py`)
62-
* crafting adversarial examples on a trained model through (`generate_adversarial.py`)
63-
* testing model accuracy on different test sets using (`test_accuracies.py`)
60+
Some examples of how to use Nemesis when writing your own code can be found in the `examples` folder. See `examples/README.md` for more information about what each example does. To run an example, use the following command:
6461

65-
Detailed instructions for each script are available by typing
66-
```bash
67-
python3 <script_name> -h
68-
```
69-
70-
## Documentation
71-
Documentation is available [here](https://adversarial-robustness-toolbox.readthedocs.io/).
72-
73-
Some examples of how to use the toolbox when writing your own code can be found in the `examples` folder. See `examples/README.md` for more information about what each example does. To run an example, use the following command:
7462
```bash
7563
python3 examples/<example_name>.py
7664
```

art/attacks/attack.py

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@
2020
import abc
2121
import sys
2222

23-
import numpy as np
24-
import tensorflow as tf
25-
26-
27-
def clip_perturbation(v, eps, p):
28-
"""
29-
Clip the values in v if their L_p norm is larger than eps.
30-
:param v: array of perturbations to clip
31-
:param eps: maximum norm allowed
32-
:param p: L_p norm to use for clipping. Only p = 2 and p = Inf supported for now
33-
:return: clipped values of v
34-
"""
35-
if p == 2:
36-
v *= min(1., eps/np.linalg.norm(v, axis=(1, 2)))
37-
elif p == np.inf:
38-
v = np.sign(v) * np.minimum(abs(v), eps)
39-
else:
40-
raise NotImplementedError('Values of p different from 2 and Inf are currently not supported.')
41-
42-
return v
43-
44-
45-
def class_derivative(preds, x, num_labels=10):
46-
"""
47-
Computes per class derivatives.
48-
:param preds: the model's logits
49-
:param x: the input placeholder
50-
:param num_labels: the number of classes the model has
51-
:return: (list) class derivatives
52-
"""
53-
return [tf.gradients(preds[:, i], x) for i in range(num_labels)]
5423

5524
# Ensure compatibility with Python 2 and 3 when using ABCMeta
5625
if sys.version_info >= (3, 4):
@@ -63,67 +32,28 @@ class Attack(ABC):
6332
"""
6433
Abstract base class for all attack classes.
6534
"""
66-
attack_params = ['classifier', 'session']
35+
attack_params = ['classifier']
6736

68-
def __init__(self, classifier, sess=None):
37+
def __init__(self, classifier):
6938
"""
7039
:param classifier: A trained model.
7140
:type classifier: :class:`Classifier`
72-
:param sess: The session to run graphs in.
73-
:type sess: `tf.Session`
7441
"""
75-
7642
self.classifier = classifier
77-
self.model = classifier.model
78-
self.sess = sess
79-
self.inf_loop = False
80-
81-
def generate_graph(self, x, **kwargs):
82-
"""
83-
Generate the attack's symbolic graph for adversarial examples. This method should be overridden in any child
84-
class that implements an attack that is expressible symbolically. Otherwise, it will wrap the numerical
85-
implementation as a symbolic operator.
8643

87-
:param x: The model's symbolic inputs.
88-
:type x: `tf.Placeholder`
89-
:param kwargs: optional parameters used by child classes.
90-
:type kwargs: `dict`
91-
:return: A symbolic representation of the adversarial examples.
92-
:rtype: `tf.Tensor`
93-
"""
94-
if not self.inf_loop:
95-
self.inf_loop = True
96-
self.set_params(**kwargs)
97-
graph = tf.py_func(self.generate, [x], tf.float32)
98-
self.inf_loop = False
99-
return graph
100-
else:
101-
raise NotImplementedError("No symbolic or numeric implementation of attack.")
102-
103-
def generate(self, x_val, **kwargs):
44+
def generate(self, x, **kwargs):
10445
"""
105-
Generate adversarial examples and return them as a Numpy array. This method should be overridden in any child
106-
class that implements an attack that is not fully expressed symbolically.
46+
Generate adversarial examples and return them as an array. This method should be overridden by all concrete
47+
attack implementations.
10748
108-
:param x_val: An array with the original inputs to be attacked.
109-
:type x_val: `np.ndarray`
49+
:param x: An array with the original inputs to be attacked.
50+
:type x: `np.ndarray`
11051
:param kwargs: Attack-specific parameters used by child classes.
11152
:type kwargs: `dict`
11253
:return: An array holding the adversarial examples.
11354
:rtype: `np.ndarray`
11455
"""
115-
if not self.inf_loop:
116-
self.inf_loop = True
117-
self.set_params(**kwargs)
118-
input_shape = list(x_val.shape)
119-
input_shape[0] = None
120-
self._x = tf.placeholder(tf.float32, shape=input_shape)
121-
self._x_adv = self.generate_graph(self._x)
122-
self.inf_loop = False
123-
else:
124-
raise NotImplementedError("No symbolic or numeric implementation of attack.")
125-
126-
return self.sess.run(self._x_adv, feed_dict={self._x: x_val})
56+
raise NotImplementedError
12757

12858
def set_params(self, **kwargs):
12959
"""

0 commit comments

Comments
 (0)