python - 使用傻瓜盒对 CNN 进行攻击的代码,出了什么问题?

标签 python machine-learning deep-learning

我必须对卷积神经网络执行简单的 FSGM 攻击。 CNN 的代码工作正常,并且模型保存没有问题,但是当我尝试执行攻击时,会显示错误。

这是 CNN 的代码

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.utils import to_categorical
import json
import tensorflow as tf
#Using TensorFlow backend.

#download mnist data and split into train and test sets

(X_train, y_train), (X_test, y_test) = mnist.load_data()

#plot the first image in the dataset
plt.imshow(X_train[0])
#check image shape
X_train[0].shape
#reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)
#one-hot encode target column
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

y_train[0]
#create model
model = Sequential()
#add model layers
model.add(Conv2D(32, kernel_size=(5,5), activation='relu', input_shape= (28,28,1)))

model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, kernel_size=(5,5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
#compile model using accuracy as a measure of model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics= ['accuracy'])

#train model
model.fit(X_train, y_train,validation_data=(X_test, y_test), epochs=5)

json.dump({'model':model.to_json()},open("model.json", "w"))
model.save_weights("model_weights.h5")

然后我尝试使用以下代码执行攻击:

import json
import foolbox
import keras
import numpy as np
from keras import backend
from keras.models import load_model
from keras.datasets import mnist
from keras.utils import np_utils
from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification
from foolbox.distances import MeanSquaredDistance
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
import numpy as np
import tensorflow as tf
from keras.models import model_from_json
import os



############## Loading the model and preprocessing #####################

backend.set_learning_phase(False)

model = tf.keras.models.model_from_json(json.load(open("model.json"))["model"],custom_objects={})
model.load_weights("model_weights.h5")
fmodel = foolbox.models.KerasModel(model, bounds=(0,1))
_,(images, labels) = mnist.load_data()

images = images.reshape(10000,28,28)
images= images.astype('float32')
images /= 255

######################### Attacking the model ##########################

attack=foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
adversarial=attack(images[12],labels[12]) # for single image
adversarial_all=attack(images,labels) # for all the images
adversarial =adversarial.reshape(1,28,28,1) #reshaping it for model prediction

model_predictions = model.predict(adversarial)
print(model_predictions)


########################## Visualization ################################
images=images.reshape(10000,28,28)
adversarial =adversarial.reshape(28,28)

plt.figure()
plt.subplot(1,3,1)
plt.title('Original')
plt.imshow(images[12])
plt.axis('off')

plt.subplot(1, 3, 2)
plt.title('Adversarial')
plt.imshow(adversarial)
plt.axis('off')

plt.subplot(1, 3, 3)
plt.title('Difference')
difference = adversarial - images[124]
plt.imshow(difference / abs(difference).max() * 0.2 + 0.5)
plt.axis('off')
plt.show()

生成对抗性示例时会显示此错误:

    c_api.TF_GetCode(self.status.status)) 
InvalidArgumentError: Matrix size-incompatible: In[0]: [1,639232], In[1]: [1024,10]
[[{{node dense_4_5/MatMul}}]]
[[{{node dense_4_5/BiasAdd}}]]

可能是什么?

最佳答案

这是我的解决方案。

首先修改模型代码如下

import tensorflow as tf
import json
# download mnist data and split into train and test sets
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# reshape data to fit model
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train, X_test = X_train/255, X_test/255
# one-hot encode target column
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
# create model
model = tf.keras.models.Sequential()
# add model layers
model.add(tf.keras.layers.Conv2D(32, kernel_size=(5, 5),
                                 activation='relu', input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
# compile model using accuracy as a measure of model performance
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])

# train model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5)

json.dump({'model': model.to_json()}, open("model.json", "w"))
model.save_weights("model_weights.h5")

您只是忘记将每个像素除以 RGB 最大值 (255)

至于攻击者代码

import json
import foolbox
from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification
import numpy as np
import tensorflow as tf


############## Loading the model and preprocessing #####################
tf.enable_eager_execution()
tf.keras.backend.set_learning_phase(False)

model = tf.keras.models.model_from_json(
    json.load(open("model.json"))["model"], custom_objects={})
model.load_weights("model_weights.h5")
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])

_, (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)

fmodel = foolbox.models.TensorFlowEagerModel(model, bounds=(0, 1))


######################### Attacking the model ##########################

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)))

为了简单起见,我使用 TensorFlowEagerModel 而不是 KerasModel。您遇到的错误是由于 model.predict 在您传递 3d 矩阵时需要 4d 矩阵,因此我只是将对图像示例的攻击封装到 numpy 数组中以使其成为 4d。

希望对你有帮助

关于python - 使用傻瓜盒对 CNN 进行攻击的代码,出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953083/

相关文章:

python - 关闭 matplotlib 中的 Spanselector

python - 使用 iterrows 选择 pandas 数据框中的下 N 行

python - 使用keras的L2标准化输出

tensorflow - 使用 Keras Tuner 进行时间序列分割

python - Keras - 检查目标时出错

python - Django 找不到任何 View ,但索引

python - 在 Windows 上安装 Python 模块的最佳实践

python - 错误 : Shapes (1, 4) 和 (14,14) 未对齐

opencv - 使用 Azure 机器学习检测图像中的符号

machine-learning - 在 cifar-10 上的 Keras 中实现 AlexNet 的准确性较差