Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Keras ResNet上激活FGSM时形状等级不匹配_Python_Keras_Cleverhans - Fatal编程技术网

Python 在Keras ResNet上激活FGSM时形状等级不匹配

Python 在Keras ResNet上激活FGSM时形状等级不匹配,python,keras,cleverhans,Python,Keras,Cleverhans,我正在尝试使用ResNet 50和keras激活FGSM,但出现错误: ValueError: Shape must be rank 4 but is rank 5 for 'model_1/conv1_pad/Pad' (op: 'Pad') with input shapes: [2,1,224,224,3], [4,2]. 我的代码是: from keras.applications.resnet50 import ResNet50 model = ResNet50(weights='i

我正在尝试使用ResNet 50和keras激活FGSM,但出现错误:

ValueError: Shape must be rank 4 but is rank 5 for 'model_1/conv1_pad/Pad' (op: 'Pad') with input shapes: [2,1,224,224,3], [4,2].
我的代码是:

from keras.applications.resnet50 import ResNet50
model = ResNet50(weights='imagenet')
images = ['images/dog1.jpg', 'images/image_0001.jpg']
for image_path in images:
    img = image.load_img(image_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    X.append(x)

wrap = KerasModelWrapper(model)

target = [np.zeros((1000,))]
target[0][0] = 1
target = np.repeat(target, len(X), axis=0)

fgsm_params = {
    'eps': 0.05,
    # 'clip_min': 0.,
    # 'clip_max': 1.,
    'y_target': target
}

X = np.array(X)

x_tensor = K.variable(X)
print(type(X))
print(X.shape)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    fgsm = FastGradientMethod(wrap, sess=sess)
    adv = fgsm.generate(x_tensor, **fgsm_params)
系统配置 -操作系统 -Python版本3.7
-TensorFlow 1.13版

证明我需要将我的
x
更改为介于0和1之间的浮动:

for image_path in file_list:
    img = image.load_img(image_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = x.astype('float32')
    x /= 255

    X.append(x)

结果我需要将我的
x
更改为介于0和1之间的浮动:

for image_path in file_list:
    img = image.load_img(image_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = x.astype('float32')
    x /= 255

    X.append(x)

此问题与您的模型体系结构有关。检查
conv1\u pad
图层尺寸。另外,添加代码片段进行详细分析。我不直接创建任何模型。我正在利用
ResNet50
prebuild此问题与您的模型体系结构有关。检查
conv1\u pad
图层尺寸。另外,添加代码片段进行详细分析。我不直接创建任何模型。我正在使用
ResNet50
prebuild