CNN for nonMNIST Data Set

To get a better handle on deep CNN, I am implementing a TensorFlow based algorithm to train and test over the nonMNIST data.


In [1]:
import tensorflow as tf
import numpy as np
import scipy as sp
from scipy import ndimage
import matplotlib.pyplot as plt
import os
import sys
import cPickle as pickle
import sklearn.preprocessing as skproc
import tfhelpers as tfh
import random as rnd

In [2]:
# Functions
def next_batch(xs, ys, batch_size):
    xs_shape = np.shape(xs)
#     ys_shape = np.shape(ys)
    idxs = rnd.sample(range(0,xs_shape[0]), batch_size)
    xs_rand = xs[idxs, :]
    ys_rand = ys[idxs,:]
    return xs_rand, ys_rand 

def load_all_data(file_str):
    curr_dir = os.getcwd()
    data_file = open(os.path.join(curr_dir, file_str))
    nonMnist_all = pickle.load(data_file)
    # Set up the one-hot encoding...
    train_ys = nonMnist_all['train_labels'].reshape(-1,1)
    test_ys = nonMnist_all['test_labels'].reshape(-1,1)
    enc = skproc.OneHotEncoder()
    enc.fit(test_ys)
    train_data = {'x': nonMnist_all['train_dataset'], 'y' : enc.transform(train_ys)}
    test_data = {'x' : nonMnist_all['test_dataset'], 'y' : enc.transform(test_ys)}
    return train_data, test_data

def run_nonmnist():
    print 'Running nonMNIST CNN task.'
    print 'Load the nonMNIST data...'
    train_data, test_data = load_all_data('notMNIST_sanitized.pickle')
    print 'nonMNIST batch:'
    print next_batch(train_data['x'], train_data['y'], 10)
    
    return train_data, test_data

In [3]:
train_data, test_data = load_all_data('notMNIST_sanitized.pickle')

In [8]:
xs, ys = next_batch(train_data['x'], train_data['y'], 5)
np.shape(xs)


Out[8]:
(5, 28, 28)

In [13]:
# TF setup: the placeholders for the input data (28x28x3 image) and output (10x1 vector)
x = tf.placeholder(tf.float32, shape=[None,784])
x_img = tf.reshape(x,[-1,28,28,1])

y_targ = tf.placeholder(tf.float32, shape=[None,10])

sess = tf.Session()

In [14]:
# conv layer 1
Wc1 = tfh.weight_variable([5,5,1,32])
bc1 = tfh.bias_variable([32])

conv1 = tfh.conv2d(x_img, Wc1) + bc1
relu1 = tf.nn.relu(conv1)
pool1 = tfh.max_pool_2x2(relu1)

In [16]:
ximg = tf.placeholder(tf.float32, shape=[None,28,28,1])

In [17]:



---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-c4e90cf99ecb> in <module>()
----> 1 ximg.eval()

/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in eval(self, feed_dict, session)
    553 
    554     """
--> 555     return _eval_using_default_session(self, feed_dict, self.graph, session)
    556 
    557 

/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _eval_using_default_session(tensors, feed_dict, graph, session)
   3482     session = get_default_session()
   3483     if session is None:
-> 3484       raise ValueError("Cannot evaluate tensor using `eval()`: No default "
   3485                        "session is registered. Use `with "
   3486                        "sess.as_default()` or pass an explicit session to "

ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

In [ ]: