Несовпадение рангов формы при активации FGSM в Keras ResNet - PullRequest
1 голос
/ 19 апреля 2019

Я пытаюсь активировать FGSM с ResNet 50 с keras, но получаю ошибку:

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

1 Ответ

0 голосов
/ 19 апреля 2019

Оказывается, мне нужно было изменить 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)
...