我试着去熟悉tensorflow和cleverhans。但我似乎把功能搞混了。在
我用tensorflow建立了一个简单的模型,对其进行训练,然后想用cleverhans的carliniwagner2攻击来构建一个对抗性的图像。我通读了tensorflows的代码和cleverhans的文档,试图了解发生了什么,但我不知道我必须从哪个库中使用哪个函数。在
这是我的简化示例代码。据我所知,我必须使用CallableModelWrapper将callable转换为有效的函数。对吗?或者我的模型不可调用?使用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输出
有人能帮忙吗?在
我基本上只想用模型来做什么克利夫汉。攻击! :)
提前谢谢。在
罗尔
编辑
这是我的回溯:
^{pr2}$我将内部目录结构分别替换为path_to_project或me。在
您共享的代码片段使用Keras定义和训练模型,以便更容易使用我们为Keras模型提供的特定
KerasModelWrapper
。你可以找到一个教程来做这个here。在相关问题 更多 >
编程相关推荐