MNIST data set into two-dimensional image implementation example


This paper introduces the implementation example of MNIST data set into two-dimensional image, which is shared with you as follows:

#coding: utf-8
from tensorflow.examples.tutorials.mnist import input_data
import scipy.misc
import os

#Read MNIST dataset. If it doesn't exist, it will be downloaded in advance.
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

#We save the original image in MNIST_ Data / raw / folder
#If not, the folder will be created automatically
save_dir = 'MNIST_data/raw/'
if os.path.exists(save_dir) is False:

#Save the first 20 pictures
for i in range(20):
  #Please note, mnist.train.images [I,:] means the i-th picture (serial number starts from 0)
  image_array = mnist.train.images[i, :]
  #The MNIST image in tensorflow is a 784 dimensional vector. We restore it to a 28x28 dimensional image.
  image_array = image_array.reshape(28, 28)
  #Save the file in MNIST format_ train_ 0.jpg, mnist_ train_ 1.jpg, ... ,mnist_ train_ 19.jpg
  filename = save_dir + 'mnist_train_%d.jpg' % i
  #Image_ Save array as picture
  #Use it first scipy.misc.toimage Convert to image, and then call save to save directly.
  scipy.misc.toimage(image_array, cmin=0.0, cmax=1.0).save(filename)

print('Please check: %s ' % save_dir)

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.