import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import scipy.misc
import os
#读取MNIST数据集,如果不存在会进行下载
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
#把原始图片保存在MNIST_data/raw/文件下,该文件不存在则会自行创建
save_dir = 'G:/TensorFlow/MNIST_data/image/'
if os.path.exists(save_dir) is False:
os.makedirs(save_dir)
for i in range(20):
# i表示第i张图片
image_array = mnist.train.images[i,:]
#将MNIST的图像还原为28*28维
image_array = image_array.reshape(28,28)
#将图像保存为JPG
filename = save_dir + 'mnist_train_%d.jpg' %i
#先用scipy.misc.toimage转换为图像,再调用save直接保存
scipy.misc.toimage(image_array,cmin = 0.0,cmax= 1.0).save(filename)
# print(mnist.train.labels[0,:])
# print(mnist.train.images.shape,mnist.train.labels.shape)
# print(mnist.test.images.shape,mnist.test.labels.shape)
# print(mnist.validation.images.shape,mnist.validation.labels.shape)
# print(mnist.test.images.shape)
# print(mnist.test.labels.shape)
# print(mnist.train.images[0,:])
'''
返回训练集中前20个图片的标签
'''
for i in range(20):
one_hot_label = mnist.train.labels[i,:]
label = np.argmax(one_hot_label) #返回one_hot_label中最大值的索引
print('mnist_train_%d.jpg label: %d' %(i,label))
MNIST数据集简单练习
最新推荐文章于 2024-05-09 12:58:58 发布