In [1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from skimage import io
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

MNIST dataset


In [2]:
mnist = input_data.read_data_sets("../../data/MNIST", one_hot=True)


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

Train


In [42]:
mnist.train.num_examples


Out[42]:
55000

Validation


In [43]:
mnist.validation.num_examples


Out[43]:
5000

Test


In [44]:
mnist.test.num_examples


Out[44]:
10000

Load images and labels


In [45]:
# Train images 
train_images = mnist.train.images
print(type(train_images))
print(train_images.shape)


<class 'numpy.ndarray'>
(55000, 784)

In [46]:
# Train labels 
train_labels = mnist.train.labels
print(type(train_labels))
print(train_labels.shape)


<class 'numpy.ndarray'>
(55000, 10)

Show examples


In [51]:
num_examples = 8
# Randomize list
images = np.random.permutation(train_images)
batch_images = images[:num_examples]
res_images = np.reshape(batch_images, (num_examples, 28, 28))
res_images.shape


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

In [52]:
# Stack them horizontally
height, width = res_images.shape[1:]
final_width, final_height = num_examples * width, height
final_img = np.zeros((final_height, final_width))

for i in range(num_examples):
    final_img[:, i*width:(i+1)*width] = res_images[i]
final_img.shape


Out[52]:
(28, 224)

In [53]:
plt.figure(figsize=(15, 8))
io.imshow(final_img)


Out[53]:
<matplotlib.image.AxesImage at 0x7f8bdbd245c0>

In [ ]: