注意:我已经尝试过不同 SO 问题的解决方案,但没有成功,详细信息如下。
我正在学习克莱尔汉斯Python 教程,重点关注this https://github.com/tensorflow/cleverhans/blob/master/cleverhans_tutorials/mnist_tutorial_keras_tf.py代码(keras 模型案例)。
我有基本的 keras 知识,但我刚刚开始使用 Tensorflow(完全新手)。
我正在尝试可视化这段代码中生成的对抗性图像(引用链接克莱尔汉斯来源):
# Initialize the Fast Gradient Sign Method (FGSM) attack object and graph
fgsm = FastGradientMethod(wrap, sess=sess)
fgsm_params = {'eps': 0.3,
'clip_min': 0.,
'clip_max': 1.}
adv_x = fgsm.generate(x, **fgsm_params)
# Consider the attack to be constant
adv_x = tf.stop_gradient(adv_x)
preds_adv = model(adv_x)
据我了解,adv_x
应该包含生成的对抗图像,我尝试将张量转换为ndarray
为了将其形象化matplot
。我之前和之后都尝试过以下操作model(adv_x)
:
1) adv_x.eval()
2) adv_x.eval(sess)
3) sess.run(adv_x)
4) ..and minor changes
没有任何事情按预期工作,我收到各种错误:
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`
and
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,28,28,1]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
and
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,28,28,1]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: strided_slice/_115 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_152_strided_slice", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
也尝试过with sess.as_default():
没有成功。
Type of adv_x
is <class 'tensorflow.python.framework.ops.Tensor'>
,其形状为TensorShape([Dimension(None), Dimension(28), Dimension(28), Dimension(1)])
。
在调试控制台中编写 adv_x ,我得到:<tf.Tensor 'StopGradient_4:0' shape=(?, 28, 28, 1) dtype=float32>
我还尝试过研究张量的一部分adv_x[0]
,没有成功。
我有点迷失,我想我错过了一些 TensorFlow 基础知识,或者我误解了教程(adv_x 是否有效地填充了数据?)。
我如何转换adv_x
to ndarray
类型?任何提示表示赞赏
Regards