In [1]:
# first, we need to ad the path-to-AdversarialMNIST-folder into sys.path
%matplotlib inline
import sys
sys.path.append('../')
from AdversarialMNIST.AdversarialMNIST import AdversarialMNIST
import numpy as np
model = AdversarialMNIST()
In [2]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/tensorflow/mnist/input_data', one_hot=True)
In [ ]:
model.fit(learning_rate=1e-4, max_steps=20000, dropout=0.5, model_path='../model/MNIST.ckpt')
In [20]:
model.load_model(model_path='../model/MNIST.ckpt')
In [4]:
data = mnist.test.next_batch(10)
x = data[0]
y = data[1]
Once we have loaded or trained a model, we can use predict() to make prediction
In [5]:
result = model.predict(x, True)
In [6]:
result[0]
Out[6]:
In [7]:
np.array(list(map(lambda x: np.max(x), result[1])))
Out[7]:
Similar to previous version, set verbose=1, the function will generate all figures to target path
In [21]:
ad_img = model.generate_adversarial(img_list=x, target_class=7, eta=0.01, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=1)
In [8]:
ad_images = model.generate_adversarial(img_list=mnist.train.images[0: 3000], target_class=7, eta=0.1, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=0)
In [9]:
new_train_images = np.concatenate((ad_images, mnist.train.images[0: 3000]))
new_train_labels = np.concatenate((mnist.train.labels[0: 3000], mnist.train.labels[0: 3000]))
In [10]:
from sklearn.utils import shuffle
new_train_images, new_train_labels = shuffle(new_train_images, new_train_labels)
In [11]:
model.fit(x=new_train_images, y=new_train_labels, learning_rate=1e-4,
max_steps=5000, dropout=0.5, model_path='../model/MNIST_AD.ckpt')
In [12]:
ad_images = model.generate_adversarial(img_list=mnist.train.images[1000: 1010], target_class=7, eta=0.1, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=0)
In [13]:
result = model.predict(ad_images)
In [14]:
result[0]
Out[14]:
In [15]:
ad_images = model.generate_adversarial(img_list=mnist.train.images[4000: 4010], target_class=7, eta=0.1, threshold=0.99,
save_path='../img/', file_name='adversarial', verbose=0)
In [16]:
result = model.predict(ad_images)
In [17]:
result[0]
Out[17]: