使用 Cleverhans 的 CarliniWagnerL2 攻击 Tensorflow 模型导致 NotImplementedError

Attacking Tensorflow model with Cleverhans' CarliniWagnerL2 resulting in NotImplementedError

我正在尝试熟悉 tensorflow 和 cleverhans。但似乎我把功能搞混了。

我用 tensorflow 建立了一个简单的模型,训练它,然后想用 cleverhans 的 CarliniWagnerL2-attack 制作一个对抗图像。我通读了 tensorflows 的代码和 cleverhans 的文档,并试图了解发生了什么,但我只是不知道我必须使用哪个库中的哪个函数。

这是我简化的示例代码。据我所知,我必须使用 CallableModelWrapper 将可调用对象转换为有效函数。那正确吗?或者我的模型不可调用?是否真的可以使用 tensorflow 训练模型,然后使用 cleverhans 对其进行攻击?当我尝试生成对抗图像时出现错误。

# 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 ! :)

提前致谢。

滚动

编辑

这是我的回溯:

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或我替换了我的内部目录结构。

您共享的代码片段使用 Keras 定义和训练模型,因此可以更轻松地使用我们为 Keras 模型提供的特定 KerasModelWrapper。您可以找到相关教程 here.