【发布时间】:2021-05-31 00:09:15
【问题描述】:
我正在尝试攻击位于文件 (model.h5) 中的 keras 神经网络模型,根据傻瓜的文档,TensorFlowModel 支持 keras 模型。但是,当我将其应用于我的 keras 模型时,出现错误。不知道是不是因为我用的是傻瓜版?
代码:
import foolbox
import numpy as np
import tensorflow as tf
from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification
############## Loading the model and preprocessing #####################
tf.keras.backend.set_learning_phase(False)
model = tf.keras.load_model("model.h5")
_, (images, labels) = tf.keras.datasets.mnist.load_data()
images = images.reshape(images.shape[0], 28, 28, 1)
images = images / 255
images = images.astype(np.float32)
######################### Attacking the model ##########################
fmodel = foolbox.models.TensorFlowModel(model, bounds=(0, 1))
attack = foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
adversarial = np.array([attack(images[0], label=labels[0])])
model_predictions = model.predict(adversarial)
print('real label: {}, label prediction; {}'.format(
labels[0], np.argmax(model_predictions)))
错误:
TypeError Traceback (most recent call last)
<ipython-input-28-29f5e9da9c7e> in <module>()
27 ######################### Attacking the model ##########################
28
---> 29 attack = foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
30 adversarial = np.array([attack(images[0], label=labels[0])])
31
TypeError: __init__() missing 1 required positional argument: 'labels'
【问题讨论】:
-
错误信息很清楚:
Misclassification()期望传递一个参数labels。 -
@sara HHJ,您是否按照上述建议尝试通过
labels?
标签: python tensorflow keras neural-network