In [1]:
from scipy.io import loadmat
import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools
import numpy as np
import tensorbuilder
from tensorbuilder import tensorbuilder as tb
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder
from datetime import datetime

reload(tensorbuilder)

py.init_notebook_mode()


/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

In [2]:
mat = loadmat('mnist.mat')
x_data = mat['X']
y_data = mat['y']

x_data = x_data.reshape([5000, 20, 20, 1])
x_data = np.transpose(x_data, [0, 2, 1, 3]) #transpose
x_data = x_data[:,::-1, :, :] #flip

y_data = y_data % 10
y_data = OneHotEncoder().fit_transform(y_data).todense()

data = tb.data(
    x=x_data, 
    y=y_data
)

[training_data, test_data] = data.split(0.8, 0.2)

In [3]:
axis_ops = dict(
    autorange=True,
    showgrid=False,
    zeroline=False,
    showline=False,
    autotick=True,
    ticks='',
    showticklabels=False
)
layout = go.Layout(xaxis=axis_ops, yaxis=axis_ops)
fig = tools.make_subplots(rows=2, cols=5, print_grid=False)
def heatmap(z, smooth='best', reversescale=True):
    return go.Heatmap(
        z=z,
        colorscale='Greys',
        reversescale=reversescale,
        showscale=False,
        showlegend=False,
        zsmooth=smooth
    )
def printmat(x):
    print(np.array2string(np.abs(x[::-1, :]), formatter={'float_kind':'{0:.1f}'.format}))

for row in range(1, 3):
    for col in range(1, 6):
        i = 500 * ((row - 1) * 5 + (col - 1)) + 250
        trace = heatmap(x_data[i][:, :, 0])
        fig.append_trace(trace, row, col)

for i in range(10):
    fig['layout']['xaxis{0}'.format(i+1)].update(**axis_ops)
    fig['layout']['yaxis{0}'.format(i+1)].update(**axis_ops)


py.iplot(fig)



In [4]:
sample_indexes = [ 500 * i + 250 for i in range(10) ]

sample_features = x_data[sample_indexes]
sample_labels = y_data[sample_indexes]

sample = sample_features[0][:, 2:-2, 0]

hmap = heatmap(sample)
hlayout = layout.copy()
hlayout.update(width=700,height=700)

py.iplot(go.Figure(data=[hmap], layout=hlayout))
printmat(sample)


[[0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.3 0.4 0.2 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.7 0.9 1.0 1.0 0.6 0.1 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.3 1.0 1.0 1.0 0.9 1.0 0.7 0.1 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.1 0.7 1.0 1.0 0.9 0.2 0.6 1.1 0.5 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.6 1.0 0.8 0.5 0.2 0.1 0.4 1.1 0.6 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.3 1.0 0.8 0.1 0.0 0.0 0.0 0.2 0.9 0.6 0.0 0.0]
 [0.0 0.0 0.0 0.1 0.8 1.0 0.4 0.0 0.0 0.0 0.0 0.1 0.8 0.6 0.0 0.0]
 [0.0 0.0 0.0 0.5 1.0 0.7 0.0 0.0 0.0 0.0 0.0 0.2 1.0 0.6 0.0 0.0]
 [0.0 0.0 0.2 0.9 1.0 0.2 0.0 0.0 0.0 0.0 0.0 0.6 1.0 0.3 0.0 0.0]
 [0.0 0.0 0.6 1.0 0.6 0.0 0.0 0.0 0.0 0.0 0.4 1.0 0.7 0.0 0.0 0.0]
 [0.0 0.0 0.6 0.9 0.2 0.0 0.0 0.0 0.0 0.3 1.0 0.9 0.2 0.0 0.0 0.0]
 [0.0 0.0 0.7 0.8 0.1 0.0 0.0 0.1 0.4 1.0 0.9 0.2 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.8 0.9 0.3 0.2 0.4 0.8 1.0 0.8 0.2 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.5 1.0 1.0 1.0 1.0 0.9 0.5 0.1 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.4 0.7 0.7 0.5 0.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]]

In [14]:
graph = tf.Graph()
with graph.as_default():
    
    with tf.name_scope('inputs'):
        x = tf.placeholder('float', shape=[None, 20, 20, 1], name='x')
        y = tf.placeholder('float', shape=[None, 10], name='y')
        learning_rate = tf.placeholder('float', name='learning_rate')
        keep_prob = tf.placeholder('float', name='keep_prob')
        
    
    #output builders
    
#     with tf.device('/gpu:0'):
    [h, trainer, conv1, conv2, loss] = tb(
        x,
        tb
        .convolution2d(16, 3).on('conv1') #, scope='conv2d_16_relu_3x3')
        .max_pool2d(2)

        .convolution2d(32, 3).on('conv2') #, scope='conv2d_32_relu_3x3')
        .max_pool2d(2)

        .flatten()

        .relu_layer(1024)

        .dropout(keep_prob)
        
        .linear_layer(10), #, scope='logits'),
        [
            tb.softmax(name='h').on('h')
        ,
            (
                tb.With( tf.name_scope('loss'),
                    tb.softmax_cross_entropy_with_logits(y).reduce_mean().make_scalar_summary('loss').on('loss')
                ),
                tb.minimize(tf.train.AdadeltaOptimizer(learning_rate)).on('trainer')
            )
        ],
        ['h', 'trainer', 'conv1', 'conv2', 'loss']
    )
    
    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(h, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.scalar_summary('accuracy', accuracy)
    
    summaries = tf.merge_all_summaries()
    saver = tf.train.Saver()
    
    
    logs_dir = "/logs/" + datetime.now().strftime('%m_%d_%Y_%H_%M_%S') 
    writter = tf.train.SummaryWriter(logs_dir, graph=graph)

    
def feed_dict(data, rate, prob):
    return {
        x: data.x,
        y: data.y,
        learning_rate: rate,
        keep_prob: prob
    }

In [15]:
model_dir = 'mnist.model'
    
with tf.Session(graph=graph, config=tf.ConfigProto(log_device_placement=True)) as sess:
    #sess.run(tf.initialize_all_variables()); 
    best = 0.0
    rate = 0.01
    saver.restore(sess, model_dir)
    
    for step, datum in training_data.epochs(20000).batches_of(500).enumerated():
        
        
        [_, _summaries] = sess.run(
            [trainer, summaries], 
            feed_dict=feed_dict(datum, rate, 0.4)
        )
        
        if step % 5 == 0:
            writter.add_summary(_summaries, step)
        
        
        if step % 200 == 0:
            [test_loss, test_accuracy] = sess.run(
                [loss, accuracy],
                feed_dict=feed_dict(test_data, rate, 1.0)
            )
            [train_loss, train_accuracy] = sess.run(
                [loss, accuracy],
                feed_dict=feed_dict(training_data, rate, 1.0)
            )
            
            print "Step ", step
            print "[Test]  loss: {0}, accuracy: {1}".format(test_loss, test_accuracy)
            print "[Train] loss: {0}, accuracy: {1}".format(train_loss, train_accuracy)
            
            if test_accuracy > best:
                print "Saving"
                saver.save(sess, model_dir)
                best = test_accuracy
                
            print ""


Step  0
[Test]  loss: 0.350817501545, accuracy: 0.899999976158
[Train] loss: 0.320192694664, accuracy: 0.913999974728
Saving

Step  200
[Test]  loss: 0.341511100531, accuracy: 0.903999984264
[Train] loss: 0.310906678438, accuracy: 0.917500019073
Saving

Step  400
[Test]  loss: 0.334638237953, accuracy: 0.907999992371
[Train] loss: 0.302221089602, accuracy: 0.920249998569
Saving

Step  600
[Test]  loss: 0.327766656876, accuracy: 0.907999992371
[Train] loss: 0.29401576519, accuracy: 0.92150002718

Step  800
[Test]  loss: 0.322609633207, accuracy: 0.908999979496
[Train] loss: 0.28636932373, accuracy: 0.92325001955
Saving

Step  1000
[Test]  loss: 0.31638354063, accuracy: 0.911000013351
[Train] loss: 0.279112279415, accuracy: 0.926249980927
Saving

Step  1200
[Test]  loss: 0.31067559123, accuracy: 0.910000026226
[Train] loss: 0.272260844707, accuracy: 0.928250014782

Step  1400
[Test]  loss: 0.30617415905, accuracy: 0.911000013351
[Train] loss: 0.265767097473, accuracy: 0.929000020027

Step  1600
[Test]  loss: 0.299727648497, accuracy: 0.912999987602
[Train] loss: 0.259457826614, accuracy: 0.930999994278
Saving

Step  1800
[Test]  loss: 0.294294416904, accuracy: 0.913999974728
[Train] loss: 0.253737151623, accuracy: 0.933250010014
Saving


KeyboardInterruptTraceback (most recent call last)
<ipython-input-15-ce0639b4db69> in <module>()
     12         [_, _summaries] = sess.run(
     13             [trainer, summaries],
---> 14             feed_dict=feed_dict(datum, rate, 0.4)
     15         )
     16 

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    715     try:
    716       result = self._run(None, fetches, feed_dict, options_ptr,
--> 717                          run_metadata_ptr)
    718       if run_metadata:
    719         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    913     if final_fetches or final_targets:
    914       results = self._do_run(handle, final_targets, final_fetches,
--> 915                              feed_dict_string, options, run_metadata)
    916     else:
    917       results = []

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
    963     if handle is None:
    964       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 965                            target_list, options, run_metadata)
    966     else:
    967       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
    970   def _do_call(self, fn, *args):
    971     try:
--> 972       return fn(*args)
    973     except errors.OpError as e:
    974       message = compat.as_text(e.message)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
    952         return tf_session.TF_Run(session, options,
    953                                  feed_dict, fetch_list, target_list,
--> 954                                  status, run_metadata)
    955 
    956     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [8]:
image_index = 7

with tf.Session(graph=graph) as sess:
    saver.restore(sess, model_dir)
    
    with tf.variable_scope("Conv", reuse=True):
        conv_weights = tf.get_variable("weights").eval().reshape([16, 3, 3])
        
    [_conv1, _conv2] = sess.run([conv1, conv2], feed_dict={x: sample_features[image_index:image_index+1, :, :, :]})
    
        

# weight plots
fig = tools.make_subplots(rows=4, cols=4, print_grid=False)

for row in range(4):
    for col in range(4):
        i = 4 * row + col
        trace = heatmap(conv_weights[i], smooth=None, reversescale=False)
        fig.append_trace(trace, row+1, col+1)

for i in range(16):
    fig['layout']['xaxis{0}'.format(i+1)].update(**axis_ops)
    fig['layout']['yaxis{0}'.format(i+1)].update(**axis_ops)


py.iplot(fig)

# conv1 plots
hmap = heatmap(sample_features[image_index, :, :, 0])
py.iplot(go.Figure(data=[hmap], layout=hlayout))

fig = tools.make_subplots(rows=4, cols=4, print_grid=False)

for row in range(4):
    for col in range(4):
        i = 4 * row + col
        trace = heatmap(_conv1[0, :, :, i], smooth=None)
        fig.append_trace(trace, row+1, col+1)

for i in range(16):
    fig['layout']['xaxis{0}'.format(i+1)].update(**axis_ops)
    fig['layout']['yaxis{0}'.format(i+1)].update(**axis_ops)
    
fig['layout'].update(width=1000,height=1000)


py.iplot(fig)

# conv2 plots
fig = tools.make_subplots(rows=8, cols=4, print_grid=False)

for row in range(8):
    for col in range(4):
        i = 4 * row + col
        trace = heatmap(_conv2[0, :, :, i], smooth=None)
        fig.append_trace(trace, row+1, col+1)

for i in range(32):
    fig['layout']['xaxis{0}'.format(i+1)].update(**axis_ops)
    fig['layout']['yaxis{0}'.format(i+1)].update(**axis_ops)
    
fig['layout'].update(width=1000,height=1000)


py.iplot(fig)



In [9]:
_conv1.shape


Out[9]:
(1, 20, 20, 16)

In [11]:
extra_answers = [4, 6 , 8]
extra_samples = np.array([loadmat('num_{0}.mat'.format(n))['im'][::-1, :] for n in extra_answers])
extra_samples = extra_samples.reshape([3, 20, 20, 1])

answers = range(10) + extra_answers
samples = np.vstack((sample_features, extra_samples))

with tf.Session(graph=graph) as sess:
    saver.restore(sess, model_dir)
    predictions = sess.run(h, feed_dict={x: samples, keep_prob: 1.0})
    prediction_vals = np.argmax(predictions, 1)
    
    
for answer, sample, prediction, prediction_val in zip(answers, samples, predictions, prediction_vals):
    fig = tools.make_subplots(
        rows=1, cols=2, print_grid=False,
        subplot_titles=(
            'Answer {0}'.format(answer),
            'Prediction {0} - {1}'.format(prediction_val, prediction_val == answer)
        )
    )

    heatmap_trace = heatmap(sample[:, :, 0])
    bar_trace = go.Bar(
        x=range(10),
        y=prediction
    )

    fig.append_trace(heatmap_trace, 1, 1)
    fig.append_trace(bar_trace, 1, 2)

    for i in range(2):
        fig['layout']['xaxis{0}'.format(i+1)].update(**axis_ops)
        fig['layout']['yaxis{0}'.format(i+1)].update(**axis_ops)


    py.iplot(fig)



In [ ]: