In [1]:
#
# COMMENTS TO DO
#

%matplotlib inline
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

def plot(samples, w, h, fw, fh, iw=28, ih=28):
    fig = plt.figure(figsize=(fw, fh))
    gs = gridspec.GridSpec(w, h)
    gs.update(wspace=0.05, hspace=0.05)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample.reshape(iw, ih), cmap='Greys_r')

    return fig

    
DATA_PATH = "../DATASETS/"
# Train Set
############

filename_queue = tf.train.string_input_producer([DATA_PATH + "MNIST_LITE/mnist_train_lite.csv"])

reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

record_defaults = [[] for i in range(785)]

sample = tf.decode_csv(value, name="reader", record_defaults=record_defaults, field_delim=" ")

single_image, single_label = tf.cast(sample[:-1], tf.float32), tf.cast(sample[-1], tf.int32)

NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 60000

NUM_PREPROCESS_THREADS = 2

min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN * min_fraction_of_examples_in_queue)

min_queue_examples = 100

BATCH_SIZE = 64

images, labels = tf.train.shuffle_batch(
            [single_image, single_label],
            batch_size=BATCH_SIZE,
            num_threads=NUM_PREPROCESS_THREADS,
            capacity=min_queue_examples + 3 * BATCH_SIZE,
            min_after_dequeue=min_queue_examples)

image_batch = tf.placeholder_with_default(images, shape=(None, 784))

label_batch = tf.placeholder_with_default(labels, shape=(None,))

label_one_hot_batch = tf.one_hot(label_batch, depth=10)

#####
# Test set
#####

filename_queue_test = tf.train.string_input_producer([DATA_PATH + "MNIST_LITE/mnist_test_lite.csv"])

reader_test = tf.TextLineReader()
key_test, value_test = reader_test.read(filename_queue_test)

sample_test = tf.decode_csv(value_test, name="reader", record_defaults=record_defaults, field_delim=" ")

single_image_test, single_label_test = tf.cast(sample_test[:-1], tf.float32), tf.cast(sample_test[-1], tf.int32)

NUM_EXAMPLES_PER_EPOCH_FOR_TEST = 25

NUM_PREPROCESS_THREADS = 2

min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN * min_fraction_of_examples_in_queue)

min_queue_examples = 25

BATCH_SIZE_TEST = 25

images_test, labels_test = tf.train.shuffle_batch(
            [single_image_test, single_label_test],
            batch_size=BATCH_SIZE_TEST,
            num_threads=NUM_PREPROCESS_THREADS,
            capacity=min_queue_examples + 3 * BATCH_SIZE_TEST,
            min_after_dequeue=min_queue_examples)

label_one_hot_batch_test = tf.one_hot(labels_test, depth=10)

with tf.Session() as sess:

    # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    
    tf.global_variables_initializer().run()
    
    out_images, out_labels = sess.run([image_batch, label_batch])
    
    fig=plot(out_images, w=8, h=8, fw=10, fh=10)
    fig.suptitle('Train samples', fontsize=20)
    
    out_images, out_labels = sess.run([images_test, labels_test])
    
    fig=plot(out_images, w=5, h=5, fw=10, fh=10)
    fig.suptitle('Test samples', fontsize=20)
    
    f = open(DATA_PATH + "MNIST_LITE/mnist_test_lite.csv", 'r')
    
    lines = f.readlines()
    
    temp_array = np.zeros((BATCH_SIZE_TEST, 784), np.float32)
    
    for i, line in enumerate(lines[:BATCH_SIZE_TEST]):
        tokens=line.split()
        temp_array[i,:] = tokens[:-1]
        
    out_images = sess.run(image_batch, feed_dict={image_batch:temp_array})
    
    fig=plot(temp_array, w=5, h=5, fw=10, fh=10)
    fig.suptitle('From File', fontsize=20)
    
    fig=plot(out_images, w=5, h=5, fw=10, fh=10)
    fig.suptitle('With default placeholder', fontsize=20)
    
    coord.request_stop()
    coord.join(threads)