Я пытаюсь использовать метод карты значимости с использованием умных людей.
Моя модель должна быть последовательной в keras, поэтому по этой причине я искал и нашел cleverhans.utils_keras, в Sequential используется KerasModelWrapper. Но по какой-то причине я все-таки получаю именно умную модель. Вот трассировка стека
TypeError Traceback (последний вызов был последним)
в
2 # https://github.com/tensorflow/cleverhans/blob/master/cleverhans/utils_keras.py
3
----> 4 jsma = SalacityMapMethod (модель, 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 \ attack__init __. Py в init (self, model, sess, dtypestr, ** kwargs)
911 "" "
912
-> 913 супер (SalacityMapMethod, self). init (модель, sess, dtypestr, ** kwargs)
914
915 self.feedable_kwargs = ('y_target',)
c: \ users \ jeredriq \ appdata \ local \ Programs \ python \ python35 \ lib \ site-packages \ cleverhans \ attack__init __. Py в init (self, model, sess, dtypestr, ** kwargs)
55
56 если не isinstance (модель, модель):
---> 57 повысить TypeError («Аргумент модели должен быть экземпляром»
58 "the cleverhans.model.Model class.")
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 .