In [1]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
In [2]:
def build_graph():
"""build the same graph as previous dumped model
Args:
None
Returns:
sess : tf.InteractiveSession()
x : tf.placeholder()
y_ : tf.placeholder()
y_pred, : tf.Variable()
keep_prob, : tf.placeholder()
cross_entropy : tf.Variable()
Example:
>>> build_graph()
"""
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
def weight_variable(shape):
"""Create a weight variable with appropriate initialization."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""Create a bias variable with appropriate initialization."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
"""simple conv2d layer"""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
"""a simple 2x2 max pool layer"""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
# First conv layer with a pool layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
# Second conv layer with a pool layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
# First Full-connect layer
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# Second Full-connect layer
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
# output layer
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
y_pred = tf.nn.softmax(y_conv)
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
sess = tf.InteractiveSession()
return sess, x, y_, y_pred, keep_prob, cross_entropy
def generate_adversarial(model_path, img_list, target_class, eta=0.001,
threshold=0.99, save_path=None, file_name='adversarial', verbose=0):
"""generate adversarial images, note that gradient and some parts of
graph are needed during iterations, hence I decide not to pack some codes
into helper function
Args:
tensor_in: `Tensor`, input tensor.
other_tensor_in: `Tensor`, same shape as `tensor_in`, other input tensor.
my_param: `float`, coefficient for `tensor_in`.
other_param: `float`, coefficient for `other_tensor_in`.
output_collections: `tuple` of `string`s, name of the collection to
collect result of this op.
name: `string`, name of the operation.
model_path: `string`, the path to previous model
img_list: `string`, the img list that need to generate adversarial images
target_class: `int`, the wanted label
eta: `float`, learning rate (or step size), default: 0.001
threshold: `float`, the confidence we want to fool, default: 0.99 (99%)
save_path: `string`, the path to img/ folder
file_name: `string`, the name for saving file, default:'adversarial'
verbose: `int`, verbose=0, omit the training graphs, default: 0
Returns:
`np.array`: the final adversarial image for each img in img_list
Example:
>>> generate_adversarial(model_path='../model/MNIST.ckpt',
img_list=img_list, target_class=6, eta=0.01, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=1)
np.ndarray(...)
"""
sess, x, y_, y_pred, keep_prob, cross_entropy = build_graph()
sess.run(tf.global_variables_initializer())
tf.train.Saver().restore(sess, model_path)
print('load model from', model_path)
prediction=tf.argmax(y_pred,1)
probabilities=y_pred
img_gradient = tf.gradients(cross_entropy, x)[0]
adversarial_img_list = list()
# generate versus figure
sns.set_style('white')
versus_fig = plt.figure(figsize=(9, 40))
for img_index in range(0, img_list.shape[0]):
adversarial_img = img_list[img_index: img_index+1].copy()
adversarial_label = np.zeros((1, 10))
adversarial_label[:, target_class] = 1
confidence = 0
iter_num = 0
prob_history = list()
while confidence < threshold:
probabilities_val = probabilities.eval(feed_dict=
{x: adversarial_img, keep_prob: 1.0}, session=sess)
confidence = probabilities_val[:, 6]
prob_history.append(probabilities_val[0])
gradient = img_gradient.eval(
{x: adversarial_img, y_: adversarial_label, keep_prob: 1.0})
adversarial_img -= eta * gradient
iter_num += 1
print('generate adversarial image after', iter_num, 'iterations')
# generate versus figure
ax1 = versus_fig.add_subplot(10, 3, 3*img_index+1)
ax1.axis('off')
ax1.imshow(img_list[img_index].reshape([28, 28]),
interpolation=None, cmap=plt.cm.gray)
ax1.title.set_text(
'Confidence for 2: ' + '{:.4f}'.format(prob_history[0][2])
+ '\nConfidence for 6: ' + '{:.4f}'.format(prob_history[0][6]))
ax2 = versus_fig.add_subplot(10, 3, 3*img_index+2)
ax2.axis('off')
ax2.imshow((adversarial_img - img_list[img_index]).reshape([28, 28]),
interpolation=None, cmap=plt.cm.gray)
ax2.title.set_text('Delta')
ax3 = versus_fig.add_subplot(10, 3, 3*img_index+3)
ax3.axis('off')
ax3.imshow((adversarial_img).reshape([28, 28]),
interpolation=None, cmap=plt.cm.gray)
ax3.title.set_text(
'Confidence for 2: ' + '{:.4f}'.format(prob_history[-1][2])
+ '\nConfidence for 6: ' + '{:.4f}'.format(prob_history[-1][6]))
print("Difference Measure:",
np.sum((adversarial_img - img_list[img_index]) ** 2))
adversarial_img_list.append(adversarial_img)
if verbose != 0:
sns.set_style('whitegrid')
colors_list = sns.color_palette("Paired", 10)
# generate Iteration figure
prob_history = np.array(prob_history)
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
for i, record in enumerate(prob_history.T):
plt.plot(record, color=colors_list[i])
ax.legend([str(x) for x in range(0, 10)],
loc='center left', bbox_to_anchor=(1.01, 0.5), fontsize=14)
ax.set_xlabel('Iteration')
ax.set_ylabel('Prediction Confidence')
fig.savefig(save_path + file_name + str(img_index) + '_iter.png')
versus_fig.tight_layout()
versus_fig.savefig(save_path + file_name + '_versus.png')
return np.array(adversarial_img_list)
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/tensorflow/mnist/input_data', one_hot=True)
In [4]:
%matplotlib inline
In [5]:
index_mask = np.where(mnist.test.labels[:, 2])[0]
subset_mask = np.random.choice(index_mask, 10)
origin_images = mnist.test.images[subset_mask]
origin_labels = mnist.test.labels[subset_mask]
In [6]:
ad_img = generate_adversarial(model_path='../model/MNIST.ckpt', img_list=origin_images, target_class=6, eta=0.01, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=0)
In [3]:
from sklearn import svm, metrics
In [4]:
train_images = mnist.train.images[:]
train_labels = mnist.train.labels[:]
test_images = mnist.test.images[:]
test_labels = mnist.test.labels[:]
In [8]:
train_labels = np.apply_along_axis(lambda x: np.where(x)[0][0], 1, train_labels)
test_labels = np.apply_along_axis(lambda x: np.where(x)[0][0], 1, test_labels)
In [10]:
classifier = svm.SVC(probability=True, verbose=True)
In [15]:
classifier.fit(train_images[0: 10000], train_labels[0: 10000])
Out[15]:
In [33]:
pred_labels = classifier.predict(test_images)
print("Confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, pred_labels))
In [34]:
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(test_labels, pred_labels)))
In [27]:
pred_labels = classifier.predict(np.squeeze(ad_img))
In [28]:
pred_labels
Out[28]:
In [35]:
from sklearn.ensemble import RandomForestClassifier
In [36]:
classifier = RandomForestClassifier(n_estimators=200)
In [39]:
classifier.fit(train_images, train_labels)
Out[39]:
In [40]:
pred_labels = classifier.predict(test_images)
print("Confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, pred_labels))
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(test_labels, pred_labels)))
In [41]:
pred_labels = classifier.predict(np.squeeze(ad_img))
In [42]:
pred_labels
Out[42]:
In [7]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
In [18]:
# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
input_shape = (img_rows, img_cols, 1)
batch_size = 128
nb_classes = 10
nb_epoch = 50
In [19]:
train_images = mnist.train.images.reshape((55000, 28, 28, 1))
train_labels = mnist.train.labels
test_images = mnist.test.images.reshape((10000, 28, 28, 1))
test_labels = mnist.test.labels
valid_images = mnist.validation.images.reshape((5000, 28, 28, 1))
valid_labels = mnist.validation.labels
In [20]:
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
model.fit(train_images, train_labels, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=0, validation_data=(valid_images, valid_labels))
Out[20]:
In [21]:
score = model.evaluate(test_images, test_labels, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
In [22]:
model.predict(ad_img.reshape((10, 28, 28, 1)))
Out[22]:
In [23]:
model.predict_classes(ad_img.reshape((10, 28, 28, 1)))
Out[23]: