我正在尝试熟悉tensorflow和cleverhans。但是似乎我把功能混在一起了。
我用张量流建立了一个简单的模型,对其进行训练,然后想用聪明人的CarliniWagnerL2攻击来制作对抗图像。我通读了tensorflows和cleverhans的文档代码,试图了解正在发生的事情,但是我只是不知道必须使用哪个库中的哪个函数。
这是我的简化示例代码。据我了解,我必须使用CallableModelWrapper将可调用函数转换为有效函数。那正确吗?还是我的模特无法通话?实际上是否可以使用张量流训练模型,然后用聪明人攻击它?当我尝试生成对抗图像时会发生错误。
# TensorFlow and tf.keras
import tensorflow as tf
# Cleverhans
import cleverhans as ch
from cleverhans import attacks
from cleverhans import model
# Others
import numpy as np
sess = tf.Session()
# load data set
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
class_names = ['0', '1', '2', '3', '4',
'5', '6', '7', '8', '9']
train_images = train_images / 255.0
test_images = test_images / 255.0
#set up model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# train
model.fit(train_images, train_labels, epochs=3)
# wrap
wrap = ch.model.CallableModelWrapper(model, 'probs')
cw = ch.attacks.CarliniWagnerL2(wrap, sess=sess)
#set params and targeted image
cw_params = {'batch_size': 1,
'confidence': 10,
'learning_rate': 0.1,
'binary_search_steps': 5,
'max_iterations': 1000,
'abort_early': True,
'initial_const': 0.01,
'clip_min': 0,
'clip_max': 1}
image = np.array([test_images[0]])
# and here i get the error!!!
adv_cw = cw.generate_np(image, **cw_params)
我实际上想获得对抗性图像,但是无论尝试如何,我似乎都同时使用了这两个库,但它们并不能很好地结合在一起。我得到:
NotImplementedError:必须实现get_logits
或必须在fprop
中定义logits输出
有人可以帮忙吗?
基本上,我只想了解我可以用于cleverhans.attacks的模型! :)
谢谢。
Rolle
修改
这是我的回溯:
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/<me>/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/home/<me>/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/<path_to_project>/tensorflow/untitled/minexample.py", line 57, in <module>
adv_cw = cw.generate_np(image, **cw_params)
File "/home/<me>/.local/lib/python3.6/site-packages/cleverhans/attacks/__init__.py", line 189, in generate_np
self.construct_graph(fixed, feedable, x_val, hash_key)
File "/home/<me>/.local/lib/python3.6/site-packages/cleverhans/attacks/__init__.py", line 161, in construct_graph
x_adv = self.generate(x, **new_kwargs)
File "/home/<me>/.local/lib/python3.6/site-packages/cleverhans/attacks/__init__.py", line 1196, in generate
x.get_shape().as_list()[1:])
File "/home/<me>/.local/lib/python3.6/site-packages/cleverhans/attacks_tf.py", line 628, in __init__
self.output = model.get_logits(self.newimg)
File "/home/<me/.local/lib/python3.6/site-packages/cleverhans/model.py", line 70, in get_logits
" output in `fprop`")
NotImplementedError: <class 'cleverhans.model.CallableModelWrapper'>must implement `get_logits` or must define a logits output in `fprop`
我分别用path_to_project或我替换了内部目录结构。
答案 0 :(得分:0)
您共享的代码段使用Keras定义和训练了模型,因此使用我们为Keras模型提供的特定KerasModelWrapper
会更容易。您可以找到有关完成此操作的教程here。