In [1]:
import sys; print('Python \t\t{0[0]}.{0[1]}'.format(sys.version_info))
import tensorflow as tf; print('Tensorflow \t{}'.format(tf.__version__))
import keras; print('Keras \t\t{}'.format(keras.__version__))


Python 		3.6
Tensorflow 	1.0.0
Using TensorFlow backend.
Keras 		2.0.3

In [2]:
%matplotlib inline 

import numpy as np
import matplotlib.pyplot as plt

In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../mnist-data/", one_hot=True)


Extracting ../mnist-data/train-images-idx3-ubyte.gz
Extracting ../mnist-data/train-labels-idx1-ubyte.gz
Extracting ../mnist-data/t10k-images-idx3-ubyte.gz
Extracting ../mnist-data/t10k-labels-idx1-ubyte.gz

In [4]:
mnist.train.images.shape


Out[4]:
(55000, 784)

In [5]:
plt.figure(figsize=(15,5))
for i in list(range(10)):
    plt.subplot(1, 10, i+1)
    pixels = mnist.test.images[i+100]
    pixels = pixels.reshape((28, 28))
    plt.imshow(pixels, cmap='gray_r')
plt.show()



In [6]:
import tensorflow as tf
import tensorlayer as tl

In [7]:
# Set parameters
n_epoch = 5
batch_size = 250
print_freq = 1

In [8]:
# TF graph input
x = tf.placeholder('float', [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder('float', [None,10]) # 0-9 digits recognition => 10 classes

In [9]:
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.ReshapeLayer(network, [-1, 28, 28, 1])

network = tl.layers.Conv2dLayer(network,act = tf.nn.relu,shape = [3, 3, 1, 32],  name ='cnn_32_1')
network = tl.layers.Conv2dLayer(network,act = tf.nn.relu,shape = [3, 3, 32, 32], name ='cnn_32_2')
network = tl.layers.MaxPool2d(network, (2,2), name='cnn_maxpool_1')
        
network = tl.layers.Conv2dLayer(network,act = tf.nn.relu,shape = [3, 3, 32, 64], name ='cnn_64_1')
network = tl.layers.Conv2dLayer(network,act = tf.nn.relu,shape = [3, 3, 64, 64], name ='cnn_64_2')
network = tl.layers.MaxPool2d(network, (2,2), name='cnn_maxpool_2')

network = tl.layers.FlattenLayer(network, name='flatten')

network = tl.layers.DropoutLayer(network, keep=0.75, name='drop1')
network = tl.layers.DenseLayer(network, n_units=256,act = tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.75, name='drop2')
network = tl.layers.DenseLayer(network, n_units=10, name='output_layer')

y_hat = network.outputs
y_out  = tf.nn.softmax(network.outputs)


  [TL] InputLayer  input_layer: (?, 784)
  [TL] ReshapeLayer reshape_layer: (?, 28, 28, 1)
  [TL] Conv2dLayer cnn_32_1: shape:[3, 3, 1, 32] strides:[1, 1, 1, 1] pad:SAME act:relu
  [TL] Conv2dLayer cnn_32_2: shape:[3, 3, 32, 32] strides:[1, 1, 1, 1] pad:SAME act:relu
  [TL] PoolLayer   cnn_maxpool_1: ksize:[1, 2, 2, 1] strides:[1, 2, 2, 1] padding:SAME pool:max_pool
  [TL] Conv2dLayer cnn_64_1: shape:[3, 3, 32, 64] strides:[1, 1, 1, 1] pad:SAME act:relu
  [TL] Conv2dLayer cnn_64_2: shape:[3, 3, 64, 64] strides:[1, 1, 1, 1] pad:SAME act:relu
  [TL] PoolLayer   cnn_maxpool_2: ksize:[1, 2, 2, 1] strides:[1, 2, 2, 1] padding:SAME pool:max_pool
  [TL] FlattenLayer flatten: 3136
  [TL] DropoutLayer drop1: keep:0.750000 is_fix:False
  [TL] DenseLayer  relu1: 256 relu
  [TL] DropoutLayer drop2: keep:0.750000 is_fix:False
  [TL] DenseLayer  output_layer: 10 identity

In [10]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_hat))

In [11]:
train_params = network.all_params
with tf.name_scope("train") as scope:
    # Gradient descent
    optimizer = tf.train.AdamOptimizer()
    learn     = optimizer.minimize(cost, var_list=train_params)

In [12]:
# Test the model, Calculate accuracy
predictions = tf.equal(tf.argmax(y_out, 1), tf.argmax(y, 1))
acc = tf.reduce_mean(tf.cast(predictions, "float"))

In [13]:
# Initializing the variables
init = tf.global_variables_initializer()

# Merge all summaries into a single operator
merged_summary_op = tf.summary.merge_all()

In [14]:
# Launch the graph
sess = tf.InteractiveSession()

# Logs and graph for tensorboard
summary_writer = tf.summary.FileWriter('./tensorboard/tensorlayer', graph=sess.graph)

# Init the session
sess.run(init)

In [15]:
# train the network
tl.utils.fit(sess, network, learn, cost, mnist.train.images, mnist.train.labels, x, y,
             acc=acc, batch_size=batch_size, n_epoch=n_epoch, print_freq=print_freq, 
             X_val=mnist.test.images, y_val=mnist.test.labels, eval_train=False)


Start training the network ...
Epoch 1 of 5 took 170.754563s
   val loss: 0.069441
   val acc: 0.975700
Epoch 2 of 5 took 156.410212s
   val loss: 0.045561
   val acc: 0.984800
Epoch 3 of 5 took 164.174852s
   val loss: 0.032850
   val acc: 0.988800
Epoch 4 of 5 took 157.983728s
   val loss: 0.032882
   val acc: 0.988100
Epoch 5 of 5 took 159.763808s
   val loss: 0.025896
   val acc: 0.991300
Total training time: 863.797987s

In [16]:
# evaluation
tl.utils.test(sess, network, acc, mnist.test.images, mnist.test.labels, x, y, batch_size=None)


Start testing the network ...
   test acc: 0.991300

In [17]:
dp_dict = tl.utils.dict_to_one( network.all_drop )
dp_dict


Out[17]:
{<tf.Tensor 'Placeholder_2:0' shape=<unknown> dtype=float32>: 1,
 <tf.Tensor 'Placeholder_3:0' shape=<unknown> dtype=float32>: 1}

In [18]:
# test item #100 is a "six"
pixels = mnist.test.images[100]

feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)

result = sess.run(y_out, feed_dict=feed_dict)
dict(zip(range(10), result[0]))


Out[18]:
{0: 8.1228354e-06,
 1: 1.3522767e-08,
 2: 8.6567997e-09,
 3: 6.3256472e-10,
 4: 1.2988748e-07,
 5: 1.2763636e-06,
 6: 0.99999011,
 7: 7.4560502e-10,
 8: 3.5295832e-07,
 9: 2.9802494e-10}

In [19]:
def test_render(pixels, result, truth):
    #pixels, result and truth are np vectors
    plt.figure(figsize=(10,5))
    plt.subplot(1, 2, 1)
    pixels = pixels.reshape((28, 28))
    plt.imshow(pixels, cmap='gray_r')

    plt.subplot(1, 2, 2)
    
    #index, witdh
    ind = np.arange(len(result))
    width = 0.4

    plt.barh(ind,result, width, color='gray')
    plt.barh(ind+width,truth,width, color='green')
    plt.yticks(ind+width, range(10))
    plt.margins(y=0)

    plt.show()

In [20]:
import random
i = random.randint(0,mnist.test.images.shape[0])

pixels = mnist.test.images[i]
truth  = mnist.test.labels[i]

feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)[0]

test_render(pixels, result, truth)



In [21]:
### What went wrong?
pixels = mnist.test.images
truth = mnist.test.labels

feed_dict = {x:pixels}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)

In [22]:
acc = result.argmax(axis=1) == truth.argmax(axis=1)
incorrect = np.argwhere(acc==False).flatten()

print("Incorrect predictions: {}".format(len(incorrect)))


Incorrect predictions: 87

In [23]:
plt.figure(figsize=(20,5))
plt_idx = 1
for i in list(incorrect[:16]):
    plt.subplot(1, 16, plt_idx)
    pixels = mnist.test.images[i]
    pixels = pixels.reshape((28, 28))
    plt.imshow(pixels, cmap='gray_r')
    plt_idx += 1
plt.show()



In [24]:
i = random.choice(list(incorrect))

pixels = mnist.test.images[i]
truth  = mnist.test.labels[i]

feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)[0]

test_render(pixels, result, truth)



In [25]:
# Close the Session when we're done.
sess.close()

In [ ]: