0

我正在尝试熟悉 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或必须定义 logits 输出fprop

任何人都可以帮忙吗?

基本上我只是想了解我可以将哪些模型用于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 或 me 替换了我的内部目录结构。

4

1 回答 1

0

您共享的代码片段使用 Keras 定义和训练模型,因此更容易使用KerasModelWrapper我们为 Keras 模型提供的特定内容。你可以在这里找到一个教程。

于 2019-06-09T01:02:29.533 回答