我正在尝试使用 generate_np 方法生成 MNIST 对抗图像。得到这个下面的异常。使用来自cleverhans 的默认 CNN 模型。
FailedPreconditionError:从容器读取资源变量dense_2/bias时出错:localhost。这可能意味着该变量未初始化。未找到:资源 localhost/dense_2/bias/N10tensorflow3VarE 不存在。[[节点模型_2_1/dense_2/BiasAdd/ReadVariableOp(定义在/anaconda3/envs/Python36/lib/python3.6/site-packages/cleverhans/utils_keras.py:228)]]
import tensorflow as tf
import keras
import numpy as np
from cleverhans.attacks import FastGradientMethod
from cleverhans.compat import flags
from cleverhans.dataset import MNIST
from cleverhans.utils import AccuracyReport
from cleverhans.utils_keras import cnn_model
from cleverhans.utils_keras import KerasModelWrapper
FLAGS = flags.FLAGS
train_start=0
train_end=6000
test_start=0
test_end=1000
nb_epochs=3
batch_size=128
learning_rate=.001
testing=False
label_smoothing=0.1
# Get MNIST test data
mnist = MNIST(train_start=train_start, train_end=train_end, test_start=test_start, test_end=test_end)
x_train, y_train = mnist.get_set('train')
x_test, y_test = mnist.get_set('test')
# Obtain Image Parameters
img_rows, img_cols, nchannels = x_train.shape[1:4]
nb_classes = y_train.shape[1]
# Label smoothing
y_train -= label_smoothing * (y_train - 1. / nb_classes)
print(x_train.shape)
# Set TF random seed to improve reproducibility
tf.set_random_seed(1234)
# Force TensorFlow to use single thread to improve reproducibility
config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
# Create TF session and set as Keras backend session
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
#keras.backend.set_image_data_format('channels_last')
keras.backend.set_session(sess)
# Define Keras model
model = cnn_model(img_rows=img_rows, img_cols=img_cols,
channels=nchannels, nb_filters=64,
nb_classes=nb_classes)
print("Defined Keras model.")
# Initialize the Fast Gradient Sign Method (FGSM) attack object
wrap = KerasModelWrapper(model)
fgsm = FastGradientMethod(wrap, sess=sess)
fgsm_params = {'eps': 0.3,
'clip_min': 0.,
'clip_max': 1.}
x_val = np.random.rand(100, 2)
x_val = np.array(x_val, dtype=np.float32)
adv_x = fgsm.generate_np(x_train, **fgsm_params)
print(model.input.shape)