Deep Learning with TensorFlow

Credits: Forked from TensorFlow by Google

Setup

Refer to the setup instructions.

Exercise 1

The objective of this exercise is to learn about simple data curation practices, and familiarize you with some of the data we'll be reusing later.

This notebook uses the notMNIST dataset to be used with python experiments. This dataset is designed to look like the classic MNIST dataset, while looking a little more like real data: it's a harder task, and the data is a lot less 'clean' than MNIST.


In [1]:
print 'xxxx'


xxxx

In [1]:
# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
import matplotlib.pyplot as plt
import numpy as np
import os
import tarfile
import urllib
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
import cPickle as pickle

First, we'll download the dataset to our local machine. The data consists of characters rendered in a variety of fonts on a 28x28 image. The labels are limited to 'A' through 'J' (10 classes). The training set has about 500k and the testset 19000 labelled examples. Given these sizes, it should be possible to train models quickly on any machine.


In [3]:
url = 'http://yaroslavvb.com/upload/notMNIST/'

def maybe_download(filename, expected_bytes):
  """Download a file if not present, and make sure it's the right size."""
  if not os.path.exists(filename):
    filename, _ = urllib.urlretrieve(url + filename, filename)
  statinfo = os.stat(filename)
  if statinfo.st_size == expected_bytes:
    print 'Found and verified', filename
  else:
    raise Exception(
      'Failed to verify' + filename + '. Can you get to it with a browser?')
  return filename

train_filename = maybe_download('notMNIST_large.tar.gz', 247336696)
test_filename = maybe_download('notMNIST_small.tar.gz', 8458043)


Found and verified notMNIST_large.tar.gz
Found and verified notMNIST_small.tar.gz

Extract the dataset from the compressed .tar.gz file. This should give you a set of directories, labelled A through J.


In [4]:
num_classes = 10

def extract(filename):
#   tar = tarfile.open(filename)
#   tar.extractall()
#   tar.close()
  root = os.path.splitext(os.path.splitext(filename)[0])[0]  # remove .tar.gz
  data_folders = [os.path.join(root, d) for d in sorted(os.listdir(root))]
  if len(data_folders) != num_classes:
    raise Exception(
      'Expected %d folders, one per class. Found %d instead.' % (
        num_folders, len(data_folders)))
  print data_folders
  return data_folders

In [5]:
train_folders = extract(train_filename)
print train_folders


['notMNIST_large/A', 'notMNIST_large/B', 'notMNIST_large/C', 'notMNIST_large/D', 'notMNIST_large/E', 'notMNIST_large/F', 'notMNIST_large/G', 'notMNIST_large/H', 'notMNIST_large/I', 'notMNIST_large/J']
['notMNIST_large/A', 'notMNIST_large/B', 'notMNIST_large/C', 'notMNIST_large/D', 'notMNIST_large/E', 'notMNIST_large/F', 'notMNIST_large/G', 'notMNIST_large/H', 'notMNIST_large/I', 'notMNIST_large/J']

In [6]:
test_folders = extract(test_filename)
print test_folders


['notMNIST_small/A', 'notMNIST_small/B', 'notMNIST_small/C', 'notMNIST_small/D', 'notMNIST_small/E', 'notMNIST_small/F', 'notMNIST_small/G', 'notMNIST_small/H', 'notMNIST_small/I', 'notMNIST_small/J']
['notMNIST_small/A', 'notMNIST_small/B', 'notMNIST_small/C', 'notMNIST_small/D', 'notMNIST_small/E', 'notMNIST_small/F', 'notMNIST_small/G', 'notMNIST_small/H', 'notMNIST_small/I', 'notMNIST_small/J']

直接 Load


Problem 1

Let's take a peek at some of the data to make sure it looks sensible. Each exemplar should be an image of a character A through J rendered in a different font. Display a sample of the images that we just downloaded. Hint: you can use the package IPython.display.


Now let's load the data in a more manageable format.

We'll convert the entire dataset into a 3D array (image index, x, y) of floating point values, normalized to have approximately zero mean and standard deviation ~0.5 to make training easier down the road. The labels will be stored into a separate array of integers 0 through 9.

A few images might not be readable, we'll just skip them.


In [7]:
parent_folder = './'

In [8]:
all_train_filles = []
for i in range(10):
    folder = train_folders[i]
    flist =os.listdir(parent_folder+folder)
    for fname in flist :
        all_train_filles.append((i,parent_folder+folder+'/'+fname))

In [9]:
from random import shuffle

print all_train_filles[:10]
shuffle(all_train_filles)
print all_train_filles[:10]


[(0, './notMNIST_large/A/Q2hhcnRlckVGLUJvbGRJdGFsaWMub3Rm.png'), (0, './notMNIST_large/A/TW9kdWxhci5vdGY=.png'), (0, './notMNIST_large/A/UHlyaXRlLm90Zg==.png'), (0, './notMNIST_large/A/T2xpZ2FyY2gudHRm.png'), (0, './notMNIST_large/A/TmVvbmxpZ2h0REIgTm9ybWFsLnR0Zg==.png'), (0, './notMNIST_large/A/R2FyYW1vbmRNZWRpdSA0LnR0Zg==.png'), (0, './notMNIST_large/A/U3RhY2tjYXBzLnR0Zg==.png'), (0, './notMNIST_large/A/RnJpelF1YWRyYXRhRUYtQm9sZEl0YWxpYy5vdGY=.png'), (0, './notMNIST_large/A/WmVpdGdlaXN0TVQtQWx0ZXJuYXRpdmUub3Rm.png'), (0, './notMNIST_large/A/SkkgS2FsZWlkb3Njb3BlIEJhdHMgMi50dGY=.png')]
[(2, './notMNIST_large/C/T3B0YXNpYSBTU2kudHRm.png'), (9, './notMNIST_large/J/SkxSIEJhYnkudHRm.png'), (1, './notMNIST_large/B/R2FydG9uIFJlZ3VsYXIudHRm.png'), (2, './notMNIST_large/C/Rm9ybWF0YSBMaWdodCBDb25kZW5zZWQgU21hbGwgQ2FwcyAmIE9sZHN0eWxlIEZpZ3VyZXMudHRm.png'), (4, './notMNIST_large/E/QXN0ZXJzSGFuZCBSZWd1bGFyLnR0Zg==.png'), (0, './notMNIST_large/A/TmlmZSBGaXRlcy50dGY=.png'), (0, './notMNIST_large/A/V2FiZW5lLnR0Zg==.png'), (3, './notMNIST_large/D/RGlzdHJvIEJldi50dGY=.png'), (9, './notMNIST_large/J/TE1TIEkgSG9wZSBZb3UgRGFuY2UudHRm.png'), (0, './notMNIST_large/A/U2NhbGFTYW5zLVJlZ3VsYXJMRkl0YWxpYy5vdGY=.png')]

In [10]:
def getNext():
    import numpy as np
    s = 100
    batch_szie = 100
    image_size = 28
    partial_data = all_train_filles[s:s+batch_szie]

    lables = np.ndarray(shape=(batch_szie), dtype=np.int32)
    dataset = np.ndarray(
        shape=(batch_szie, image_size, image_size), dtype=np.float32)

    image_index = 0
    for label,fname in partial_data:

        image_data = (ndimage.imread(fname).astype(float) -
                          128 / 2) / 128
    #     print image_data
        if image_data.shape != (image_size, image_size):
          raise Exception('Unexpected image shape: %s' % str(image_data.shape))
        dataset[image_index,:,:] = image_data
        lables[image_index] = label
        image_index += 1
    return lables,dataset

In [11]:
image_size = 28  # Pixel width and height.
pixel_depth = 255.0  # Number of levels per pixel.

def load(data_folders, min_num_images, max_num_images):
  dataset = np.ndarray(
    shape=(max_num_images, image_size, image_size), dtype=np.float32)
  labels = np.ndarray(shape=(max_num_images), dtype=np.int32)
  label_index = 0
  image_index = 0
  for folder in data_folders:
    print folder
    for image in os.listdir(folder):
      if image_index >= max_num_images:
#         raise Exception('More images than expected: %d >= %d' % (
#           num_images, max_num_images))
        print "Too Many Images",folder
        break
      image_file = os.path.join(folder, image)
      try:
        image_data = (ndimage.imread(image_file).astype(float) -
                      pixel_depth / 2) / pixel_depth
        if image_data.shape != (image_size, image_size):
          raise Exception('Unexpected image shape: %s' % str(image_data.shape))
        dataset[image_index, :, :] = image_data
        labels[image_index] = label_index
        image_index += 1
      except IOError as e:
        print 'Could not read:', image_file, ':', e, '- it\'s ok, skipping.'
    label_index += 1
  num_images = image_index
  dataset = dataset[0:num_images, :, :]
  labels = labels[0:num_images]
  if num_images < min_num_images:
    raise Exception('Many fewer images than expected: %d < %d' % (
        num_images, min_num_images))
  print 'Full dataset tensor:', dataset.shape
  print 'Mean:', np.mean(dataset)
  print 'Standard deviation:', np.std(dataset)
  print 'Labels:', labels.shape
  return dataset, labels
train_dataset, train_labels = load(train_folders, 450000, 550000)


notMNIST_large/A
Could not read: notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png : cannot identify image file 'notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png' - it's ok, skipping.
Could not read: notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png : cannot identify image file 'notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png' - it's ok, skipping.
Could not read: notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png : cannot identify image file 'notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png' - it's ok, skipping.
notMNIST_large/B
Could not read: notMNIST_large/B/TmlraXNFRi1TZW1pQm9sZEl0YWxpYy5vdGY=.png : cannot identify image file 'notMNIST_large/B/TmlraXNFRi1TZW1pQm9sZEl0YWxpYy5vdGY=.png' - it's ok, skipping.
notMNIST_large/C
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-11-2233cae48054> in <module>()
     39   print 'Labels:', labels.shape
     40   return dataset, labels
---> 41 train_dataset, train_labels = load(train_folders, 450000, 550000)

<ipython-input-11-2233cae48054> in load(data_folders, min_num_images, max_num_images)
     18       image_file = os.path.join(folder, image)
     19       try:
---> 20         image_data = (ndimage.imread(image_file).astype(float) -
     21                       pixel_depth / 2) / pixel_depth
     22         if image_data.shape != (image_size, image_size):

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/scipy/ndimage/io.pyc in imread(fname, flatten, mode)
     22 def imread(fname, flatten=False, mode=None):
     23     if _have_pil:
---> 24         return _imread(fname, flatten, mode)
     25     raise ImportError("Could not import the Python Imaging Library (PIL)"
     26                       " required to load image files.  Please refer to"

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/scipy/misc/pilutil.pyc in imread(name, flatten, mode)
    152     """
    153 
--> 154     im = Image.open(name)
    155     return fromimage(im, flatten=flatten, mode=mode)
    156 

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/PIL/Image.pyc in open(fp, mode)
   2280 
   2281     if filename:
-> 2282         fp = builtins.open(filename, "rb")
   2283 
   2284     try:

KeyboardInterrupt: 

In [31]:
test_dataset, test_labels = load(test_folders, 18000, 20000)


notMNIST_small/A
Could not read: notMNIST_small/A/RGVtb2NyYXRpY2FCb2xkT2xkc3R5bGUgQm9sZC50dGY=.png : cannot identify image file 'notMNIST_small/A/RGVtb2NyYXRpY2FCb2xkT2xkc3R5bGUgQm9sZC50dGY=.png' - it's ok, skipping.
notMNIST_small/B
notMNIST_small/C
notMNIST_small/D
notMNIST_small/E
notMNIST_small/F
Could not read: notMNIST_small/F/Q3Jvc3NvdmVyIEJvbGRPYmxpcXVlLnR0Zg==.png : cannot identify image file 'notMNIST_small/F/Q3Jvc3NvdmVyIEJvbGRPYmxpcXVlLnR0Zg==.png' - it's ok, skipping.
notMNIST_small/G
notMNIST_small/H
notMNIST_small/I
notMNIST_small/J
Full dataset tensor: (18724, 28, 28)
Mean: -0.0746363
Standard deviation: 0.458622
Labels: (18724,)

Problem 2

Let's verify that the data still looks good. Displaying a sample of the labels and images from the ndarray. Hint: you can use matplotlib.pyplot.


Next, we'll randomize the data. It's important to have the labels well shuffled for the training and test distributions to match.


In [32]:
np.random.seed(133)
def randomize(dataset, labels):
  permutation = np.random.permutation(labels.shape[0])
  shuffled_dataset = dataset[permutation,:,:]
  shuffled_labels = labels[permutation]
  return shuffled_dataset, shuffled_labels
train_dataset, train_labels = randomize(train_dataset, train_labels)
test_dataset, test_labels = randomize(test_dataset, test_labels)

In [33]:
train_labels[:100]


Out[33]:
array([7, 7, 6, 7, 7, 5, 8, 7, 3, 6, 1, 1, 7, 3, 9, 0, 1, 0, 3, 4, 0, 8, 5,
       8, 1, 0, 3, 8, 3, 1, 3, 1, 5, 1, 6, 8, 5, 0, 5, 7, 9, 3, 9, 1, 3, 9,
       4, 7, 2, 3, 4, 9, 9, 9, 9, 2, 9, 1, 9, 7, 9, 4, 6, 2, 5, 3, 3, 9, 4,
       9, 9, 5, 7, 6, 4, 0, 1, 8, 2, 3, 8, 9, 2, 0, 8, 5, 7, 0, 5, 1, 8, 0,
       6, 1, 7, 4, 9, 2, 8, 2], dtype=int32)

Problem 3

Convince yourself that the data is still good after shuffling!



In [34]:
print 'train_dataset'
print train_dataset.shape
print train_dataset.mean()
print train_dataset.std()

print 'test_dataset'
print test_dataset.shape
print test_dataset.mean()
print test_dataset.std()


train_dataset
(529114, 28, 28)
-0.0816593
0.454233
test_dataset
(18724, 28, 28)
-0.0746363
0.458622

Problem 4

Another check: we expect the data to be balanced across classes. Verify that.



In [35]:
t_lable_count =[0]*10
for i in train_labels:
    t_lable_count[i] +=1

In [36]:
t_lable_count


Out[36]:
[52909, 52911, 52912, 52911, 52912, 52912, 52912, 52912, 52912, 52911]

In [37]:
test_lable_count =[0]*10
for i in test_labels:
    test_lable_count[i] +=1
test_lable_count


Out[37]:
[1872, 1873, 1873, 1873, 1873, 1872, 1872, 1872, 1872, 1872]

Prune the training data as needed. Depending on your computer setup, you might not be able to fit it all in memory, and you can tune train_size as needed.

Also create a validation dataset for hyperparameter tuning.


In [38]:
train_size = 200000
valid_size = 10000

valid_dataset = train_dataset[:valid_size,:,:]
valid_labels = train_labels[:valid_size]
train_dataset = train_dataset[valid_size:valid_size+train_size,:,:]
train_labels = train_labels[valid_size:valid_size+train_size]
print 'Training', train_dataset.shape, train_labels.shape
print 'Validation', valid_dataset.shape, valid_labels.shape


Training (200000, 28, 28) (200000,)
Validation (10000, 28, 28) (10000,)

Finally, let's save the data for later reuse:


In [39]:
pickle_file = 'notMNIST.pickle'

try:
  f = open(pickle_file, 'wb')
  save = {
    'train_dataset': train_dataset,
    'train_labels': train_labels,
    'valid_dataset': valid_dataset,
    'valid_labels': valid_labels,
    'test_dataset': test_dataset,
    'test_labels': test_labels,
    }
  pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
  f.close()
except Exception as e:
  print 'Unable to save data to', pickle_file, ':', e
  raise

In [40]:
statinfo = os.stat(pickle_file)
print 'Compressed pickle size:', statinfo.st_size


Compressed pickle size: 718193801

Load Dataset


In [2]:
pickle_file = 'notMNIST.pickle'

with open(pickle_file, 'rb') as f:
  save = pickle.load(f)
  train_dataset = save['train_dataset']
  train_labels = save['train_labels']
  valid_dataset = save['valid_dataset']
  valid_labels = save['valid_labels']
  test_dataset = save['test_dataset']
  test_labels = save['test_labels']
  del save  # hint to help gc free up memory
  print 'Training set', train_dataset.shape, train_labels.shape
  print 'Validation set', valid_dataset.shape, valid_labels.shape
  print 'Test set', test_dataset.shape, test_labels.shape


Training set (200000, 28, 28) (200000,)
Validation set (10000, 28, 28) (10000,)
Test set (18724, 28, 28) (18724,)

Problem 5

By construction, this dataset might contain a lot of overlapping samples, including training data that's also contained in the validation and test set! Overlap between training and test can skew the results if you expect to use your model in an environment where there is never an overlap, but are actually ok if you expect to see training samples recur when you use it. Measure how much overlap there is between training, validation and test samples. Optional questions:

  • What about near duplicates between datasets? (images that are almost identical)
  • Create a sanitized validation and test set, and compare your accuracy on those in subsequent exercises.


Problem 6

Let's get an idea of what an off-the-shelf classifier can give you on this data. It's always good to check that there is something to learn, and that it's a problem that is not so trivial that a canned solution solves it.

Train a simple model on this data using 50, 100, 1000 and 5000 training samples. Hint: you can use the LogisticRegression model from sklearn.linear_model.

Optional question: train an off-the-shelf model on all the data!



In [3]:
import tensorflow as tf

In [4]:
x = tf.placeholder(tf.float32,shape=[None,28*28],name='Input_X')
y = tf.placeholder(tf.float32,shape=[None,10],name='Input_Y')

In [5]:
W = tf.Variable(tf.truncated_normal(shape=[28*28,10]))
b = tf.Variable(tf.truncated_normal(shape=[10]))

In [6]:
xw = tf.matmul(x,W)
r = xw + b

cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(r,y))


a = tf.nn.softmax(r)
# cost_array = y*tf.log(a)
# cost = -1*tf.reduce_sum(cost_array )/1000

In [7]:
op = tf.train.GradientDescentOptimizer(0.5).minimize(cost)

In [8]:
init = tf.initialize_all_variables()


WARNING:tensorflow:From <ipython-input-8-0f50547c66e8>:1 in <module>.: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

In [17]:
session = tf.Session()

In [18]:
session.run(init)

In [19]:
batch_size =128
batch_list = []
for i in range(0,train_labels[:1024].shape[0]/batch_size):
#     print (i*batch_size,i*batch_size+batch_size)
    batch_list.append((i*batch_size,(i+1)*batch_size))
# batch_list.append(((i+1)*batch_size,train_labels.shape[0]-1))

In [20]:
# for start,end in batch_list:

start,end = batch_list[0]
print start,end
input_images = np.reshape(train_dataset[start:end],[batch_size,28*28])
output_labes = np.zeros([batch_size,10],dtype=np.int)
output_labes[100][9]


0 128
Out[20]:
0

In [21]:
for epcho in range(10000):
    for start,end in batch_list:
        for index,value in enumerate(train_labels[start:end]):
            output_labes[index][value] = 1
        session.run(op,feed_dict={x:input_images,y:output_labes})

    if epcho % 100 == 0 :
        print "epcho",epcho,
        print session.run(cost,feed_dict={x:input_images,y:output_labes})/batch_size,
        
        
        test_image = np.reshape(train_dataset[:1024],[-1,28*28])
        prediction = tf.arg_max(a,1)
        pr = session.run(prediction,feed_dict={x:test_image,})
        print np.sum(np.equal(pr,train_labels[:1024])*1)*1./test_image.shape[0]


epcho 0 0.4859572649 0.251953125
epcho 100 5.24954605103 0.25
epcho 200 10.2940673828 0.2490234375
epcho 300 15.3403844833 0.2509765625
epcho 400 20.3873672485 0.2509765625
epcho 500 25.4345016479 0.2509765625
epcho 600 30.481716156 0.251953125
epcho 700 35.5289268494 0.251953125
epcho 800 40.57629776 0.251953125
epcho 900 45.6237106323 0.251953125
epcho 1000 50.6712188721 0.251953125
epcho 1100 55.718662262 0.251953125
epcho 1200 60.7661056519 0.251953125
epcho 1300 65.8136520386 0.251953125
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-21-0e7bf48485f0> in <module>()
      3         for index,value in enumerate(train_labels[start:end]):
      4             output_labes[index][value] = 1
----> 5         session.run(op,feed_dict={x:input_images,y:output_labes})
      6 
      7     if epcho % 100 == 0 :

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    764     try:
    765       result = self._run(None, fetches, feed_dict, options_ptr,
--> 766                          run_metadata_ptr)
    767       if run_metadata:
    768         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    962     if final_fetches or final_targets:
    963       results = self._do_run(handle, final_targets, final_fetches,
--> 964                              feed_dict_string, options, run_metadata)
    965     else:
    966       results = []

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1012     if handle is None:
   1013       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1014                            target_list, options, run_metadata)
   1015     else:
   1016       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1019   def _do_call(self, fn, *args):
   1020     try:
-> 1021       return fn(*args)
   1022     except errors.OpError as e:
   1023       message = compat.as_text(e.message)

/net/account/pixuser/kent/ENV_mars/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1001         return tf_session.TF_Run(session, options,
   1002                                  feed_dict, fetch_list, target_list,
-> 1003                                  status, run_metadata)
   1004 
   1005     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [41]:
test_image = np.reshape(train_dataset,[-1,28*28])
prediction = tf.arg_max(a,1)
pr = session.run(prediction,feed_dict={x:test_image,})
np.sum(np.equal(pr,train_labels)*1)*1./test_image.shape[0]


Out[41]:
0.092685000000000003

In [162]:
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib
%matplotlib inline
import numpy

In [167]:
tmp2 = train_dataset[2222].reshape((28,28))

plt.imshow(tmp2, cmap = cm.Greys)
plt.show()



In [ ]: