Skip to content

Commit d7294d3

Browse files
Irina NicolaeIrina Nicolae
authored andcommitted
Merge Clever fix from dev
2 parents f12082e + 40c96d8 commit d7294d3

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

src/attacks/fast_gradient.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ def set_params(self, **kwargs):
211211
if self.ord not in [np.inf, int(1), int(2)]:
212212
raise ValueError("Norm order must be either np.inf, 1, or 2.")
213213

214-
if self.eps <= self.clip_min or self.eps > self.clip_max:
215-
raise ValueError('The amount of perturbation has to be in the data range.')
214+
if self.clip_min is not None and self.clip_max is not None:
215+
if self.eps <= self.clip_min or self.eps > self.clip_max:
216+
raise ValueError('The amount of perturbation has to be in the data range.')
216217

217218
return True

src/classifiers/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
from keras.models import model_from_json
88
from keras.optimizers import SGD
99

10-
from src.classifiers.cnn import CNN
11-
from src.classifiers.mlp import MLP
12-
from src.classifiers.resnet import ResNet
1310
from src.layers.activations import BoundedReLU
1411
from src.utils import make_directory
1512

@@ -55,6 +52,11 @@ def load_classifier(file_path, weights_name="weights.h5"):
5552
:param weights_name: name of the file containing the weights
5653
:return: Classifier
5754
"""
55+
from src.classifiers.bnn import BNN
56+
from src.classifiers.cnn import CNN
57+
from src.classifiers.mlp import MLP
58+
from src.classifiers.resnet import ResNet
59+
5860
# Load json and create model
5961
with open(os.path.join(file_path, "model.json"), "r") as json_file:
6062
model_json = json_file.read()

src/metrics.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Module implementing varying metrics for assessing model robustness. These fall mainly under two categories:
33
attack-dependent and attack-independent.
44
"""
5+
from __future__ import absolute_import, division, print_function, unicode_literals
56
import config
67

78
import numpy as np
@@ -170,10 +171,13 @@ def loss_sensitivity(x, classifier, sess):
170171
:return: The average loss sensitivity of the model
171172
:rtype: `float`
172173
"""
174+
from src.attacks.attack import class_derivative
175+
173176
x_op = tf.placeholder(dtype=tf.float32, shape=list(x.shape))
174177
y_pred = classifier.predict(x)
175178
indices = np.argmax(y_pred, axis=1)
176-
grads = [tf.gradients(classifier.model(x_op)[:, i], x_op) for i in range(10)]
179+
grads = class_derivative(classifier._get_predictions(x_op, log=True), x_op,
180+
classifier.model.get_output_shape_at(0)[1])
177181
res = sess.run(grads, feed_dict={x_op: x})
178182
res = np.asarray([r[0] for r in res])[indices, list(range(x.shape[0]))]
179183
res = la.norm(res.reshape(res.shape[0], -1), ord=2, axis=1)

src/metrics_unittest.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# from config import config_dict
1+
from __future__ import absolute_import, division, print_function, unicode_literals
2+
23
import unittest
34

4-
import keras.backend as K
5+
import keras.backend as k
56
import tensorflow as tf
67
import numpy as np
78

@@ -36,7 +37,7 @@ class TestMinimalPerturbation(unittest.TestCase):
3637

3738
def test_emp_robustness_mnist(self):
3839
session = tf.Session()
39-
K.set_session(session)
40+
k.set_session(session)
4041

4142
comp_params = {"loss": 'categorical_crossentropy',
4243
"optimizer": 'adam',
@@ -108,7 +109,7 @@ def test_clever_t_unit(self):
108109
print("Unit test for the targeted version with simplified data.")
109110
# Define session & params
110111
session = tf.Session()
111-
K.set_session(session)
112+
k.set_session(session)
112113

113114
# Get classifier
114115
classifier = TestClassifier()
@@ -129,7 +130,7 @@ def test_clever_u_unit(self):
129130
print("Unit test for the untargeted version with simplified data.")
130131
# Define session & params
131132
session = tf.Session()
132-
K.set_session(session)
133+
k.set_session(session)
133134

134135
# Get classifier
135136
classifier = TestClassifier()
@@ -150,7 +151,7 @@ def test_clever_t(self):
150151
print("Test if the targeted version works on a true classifier/data")
151152
# Define session & params
152153
session = tf.Session()
153-
K.set_session(session)
154+
k.set_session(session)
154155

155156
comp_params = {"loss": 'categorical_crossentropy', "optimizer": 'adam',
156157
"metrics": ['accuracy']}
@@ -178,7 +179,7 @@ def test_clever_u(self):
178179
print("Test if the untargeted version works on a true classifier/data")
179180
# Define session & params
180181
session = tf.Session()
181-
K.set_session(session)
182+
k.set_session(session)
182183

183184
comp_params = {"loss": 'categorical_crossentropy', "optimizer": 'adam',
184185
"metrics": ['accuracy']}

src/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Module providing convenience functions.
33
"""
4-
from __future__ import absolute_import, division, print_function
4+
from __future__ import absolute_import, division, print_function, unicode_literals
55

66
import argparse
77
import json

0 commit comments

Comments
 (0)