我正在尝试使用 Cleverhans (FGSM) 教程生成对抗性示例Keras
,但到目前为止我一直没有成功。我正在使用 keras 生成器功能来检索数据集图像。(Python 3.7)
我在工作方面相当新,Cleverhans
我无法弄清楚为什么我会收到这个错误。我正在关注 Cleverhans Mnist 教程,所以我不确定为什么会出现此错误。
from __future__ import print_function
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import os
from cleverhans.cleverhans.utils_tf import model_eval
#from cleverhans.utils_tf import model_train
from cleverhans.cleverhans.utils import AccuracyReport
report = AccuracyReport()
batch_size = 1
path = "/home/Downloads/dataset/"
path_test = os.path.join(path, "test/")
path_train = os.path.join(path, "train/")
# create a data generator
datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
# load and iterate test dataset
itr = test_it = datagen.flow_from_directory(path_test, target_size=(150, 150), class_mode='categorical', batch_size=1)
itr2 = train_it = datagen.flow_from_directory(path_train, target_size=(150, 150), class_mode='categorical', batch_size=1)
X, Y = itr.next()
W, Z = itr2.next()
###############################################################################
# create tensorflow session
sess = tf.Session()
from tensorflow.keras import backend as K
K.set_session(sess)
from tensorflow.keras.models import load_model
model = load_model('/home/saved_models/model.100.h5')
from cleverhans.cleverhans.utils_keras import KerasModelWrapper
wrap = KerasModelWrapper(model)
x = tf.placeholder(tf.float32, shape=(1, 150, 150, 3))
y = tf.placeholder(tf.float32, shape=(1, 2))
from cleverhans.cleverhans.attacks import FastGradientMethod
fgsm = FastGradientMethod(wrap, sess=sess)
fgsm_params = {'eps': .007,'clip_min': -1.,'clip_max': 1.}
adv_x = fgsm.generate(x, **fgsm_params)
adv_x = tf.stop_gradient(adv_x)
adv_prob = model(adv_x)
# Evaluate the accuracy of the model on adversarial examples
eval_par = {'batch_size': batch_size}
acc = model_eval(sess, x, y, adv_prob, X_test=X, Y_test=Y, args=eval_par)
print('Test accuracy on adversarial examples: %0.4f\n' % acc)
report.clean_train_adv_eval = acc
# Calculating train error
eval_par = {'batch_size': batch_size}
acc = model_eval(sess, x, y, adv_prob, X, Y, args=eval_par)
report.train_clean_train_adv_eval = acc
InvalidArgumentError:找到 2 个根错误。(0) 无效参数:断言失败:[] [Condition x >= y did not hold element-wise:x (Placeholder_539:0) = ] [[[[-59.939003 -72.779 -79.68]]]...] [ y (Cast/x:0) = ] [-1] [[node assert_greater_equal/Assert/AssertGuard/Assert (定义在/home/Downloads/cleverhans/cleverhans/utils_tf.py:749)]] (1) 无效参数:断言失败:[] [Condition x >= y did not hold element-wise:x (Placeholder_539:0) = ] [[[[-59.939003 -72.779 -79.68]]]...] [y (Cast/x: 0) = ] [-1] [[节点assert_greater_equal/Assert/AssertGuard/Assert(定义在/home/Downloads/cleverhans/cleverhans/utils_tf.py:749)]] [[GroupCrossDeviceControlEdges_0/model_1_1/conv1_pad/Pad/_3698] ] 0 次成功操作。0 派生错误被忽略。