import os import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.examples.tutorials.mnist import input_data %matplotlib inline LOG_DIR = 'F:\temp\log\' SPRITE_FILE = 'mnist_sprite.jpg' META_FIEL = "mnist_meta.tsv"
def create_sprite_image(images): """Returns a sprite image consisting of images passed as argument. Images should be count x width x height""" if isinstance(images, list): images = np.array(images) img_h = images.shape[1] img_w = images.shape[2] n_plots = int(np.ceil(np.sqrt(images.shape[0]))) spriteimage = np.ones((img_h * n_plots ,img_w * n_plots )) for i in range(n_plots): for j in range(n_plots): this_filter = i * n_plots + j if(this_filter < images.shape[0]): this_img = images[this_filter] spriteimage[i * img_h:(i + 1) * img_h,j * img_w:(j + 1) * img_w] = this_img return spriteimage mnist = input_data.read_data_sets("F:\TensorFlowGoogle\201806-github\datasets\MNIST_data", one_hot=False) to_visualise = 1 - np.reshape(mnist.test.images,(-1,28,28)) sprite_image = create_sprite_image(to_visualise) path_for_mnist_sprites = os.path.join(LOG_DIR, SPRITE_FILE) plt.imsave(path_for_mnist_sprites,sprite_image,cmap='gray') plt.imshow(sprite_image,cmap='gray')