【问题标题】:Neural network attack foolbox (FGSM)神经网络攻击傻瓜盒(FGSM)
【发布时间】: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


【解决方案1】:

我今天遇到了这个问题,很遗憾看到这里没有答案。但我想通了,所以我们开始吧。

是的,这是由于您使用的傻瓜版本。

在版本 2.4.0 和版本 3.0.0b1 之间,傻瓜将错误分类从没有参数的函数更改为需要“标签”参数的函数。使用foolbox 2.4.0 应该可以解决这个问题。

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 2017-02-19
    • 2014-01-15
    • 1970-01-01
    • 2015-12-03
    • 2018-12-16
    • 2011-08-23
    • 2018-03-21
    相关资源
    最近更新 更多