我正在尝试使用cleverhans做显着性图方法。
我的模型需要按顺序进行keras排序,因此出于这个原因,我搜索并找到了cleverhans.utils_keras,Sequential使用KerasModelWrapper。但是出于某种原因,我仍然认为它应该是聪明人模型。这是堆栈跟踪
TypeError跟踪(最近一次通话) 在 2#https://github.com/tensorflow/cleverhans/blob/master/cleverhans/utils_keras.py 3 ----> 4 jsma = SaliencyMapMethod(model,sess = sess) 5 jsma_params = {'theta':10.0,'gamma':0.15, 6'clip_min':0.,'clip_max':1。,
c:\ users \ jeredriq \ appdata \ local \ programs \ python \ python35 \ lib \ site-packages \ cleverhans \ attacks__init __。py in init ((自我,模型,sess,dtypestr,* * kwargs) 911“”“ 912 -> 913超级(SaliencyMapMethod,self)。初始化(模型,sess,dtypestr,** kwargs) 914 915 self.feedable_kwargs =('y_target',)
c:\ users \ jeredriq \ appdata \ local \ programs \ python \ python35 \ lib \ site-packages \ cleverhans \ attacks__init __。py in init ((自我,模型,sess,dtypestr,* * kwargs) 55 56如果不是isinstance(模型,模型): ---> 57raise TypeError(“模型参数应该是”的实例 58“ cleverhans.model.Model类。”) 59
TypeError:模型参数应该是cleverhans.model.Model类的实例。
这是我的代码
import numpy as np
from keras import backend
import tensorflow as tf
from keras.callbacks import ModelCheckpoint
from matplotlib import gridspec
from matplotlib import pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
from keras.datasets import mnist
from keras.layers import Dense, Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from cleverhans.attacks import FastGradientMethod
from cleverhans.attacks import BasicIterativeMethod
from cleverhans.attacks import SaliencyMapMethod
from cleverhans.attacks import DeepFool
from cleverhans.utils_keras import Sequential
sess = backend.get_session()
x = tf.placeholder(tf.float32, shape=(None, 28, 28, 1))
y = tf.placeholder(tf.float32, shape=(None, 10))
# Managing Mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
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_train.astype('float32')
X_test = X_test.astype('float32')
X_train/=255
X_test/=255
y_train_cat = np_utils.to_categorical(y_train)
y_test_cat = np_utils.to_categorical(y_test)
num_classes = y_test_cat.shape[1]
### Defining Model ###
model = Sequential() # <----- I use Sequential from CleverHans
model.add(Conv2D(32, (5, 5), input_shape=(28,28,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train_cat, epochs=10, batch_size=1024, verbose=1, validation_split=0.7)
### And the problem part ###
jsma = SaliencyMapMethod(model, sess=sess) # <---- Where I get the exception
jsma_params = {'theta': 10.0, 'gamma': 0.15,
'clip_min': 0., 'clip_max': 1.,
'y_target': None}
sample_size = 20
one_hot_target = np.zeros((sample_size, 10), dtype=np.float32)
one_hot_target[:, 1] = 1
jsma_params['y_target'] = one_hot_target
X_test_small = X_test[0:sample_size,:]
y_test_small = y_test[0:sample_size]
adv_x = jsma.generate_np(X_test_small, **jsma_params)
我在github上也有同样的问题。
答案 0 :(得分:1)
在Sequential
中定义的cleverhans.utils_keras
仍然是keras的Sequential
模型。需要的是cleverhans.model.Model
。可以使用KerasModelWrapper
类包装一个keras模型来提供这种行为。
替换
jsma = SaliencyMapMethod(model, sess=sess)
使用
jsma = SaliencyMapMethod(KerasModelWrapper(model), sess=sess)