代码:
import tensorflow as tf
# 导入 MNIST 数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./MNIST_data/", one_hot=True)
运行结果:
Extracting ./MNIST_data/train-images-idx3-ubyte.gz
Extracting ./MNIST_data/train-labels-idx1-ubyte.gz
Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz
Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz
代码:
# 查看数据集形状
print(mnist.train.images.shape)
print(mnist.train.labels.shape)
print(mnist.test.images.shape)
print(mnist.test.labels.shape)
运行结果:
(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
(784,)
代码:
# 打印前10张图片
l = len(mnist.train.images[1])
# 开平方 l_为28
l_ = int(np.sqrt(l))
images = mnist.train.images
# 标签
labels = mnist.train.labels
for i in range(10):
plt.subplot(2,5,i+1)
# 显示图片 imshow
plt.imshow(images[i].reshape(l_,l_))
# 添加标题为对应的label
# np.where 返回对应的索引
plt.title(int(np.where(labels[i]==1)[0]))
# 保存 imsave
plt.imsave('./test_image/images_'+str(i+1)+'.jpg', images[i].reshape((l_,l_)))
plt.show()
运行结果:
代码:
# 读取保存的图片 imread
img = plt.imread('./test_image/images_1.jpg')
# 查看形状
# (28, 28, 4)
# 发现有4通道
# 第4通道的值全部为255
print(img.shape)
print('===' * 30)
print(img.T[:][:][3])
运行结果:
(28, 28, 4)
==========================================================================================
[[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255]]
代码:
# 第2种保存方式,保存后图片仍为原始尺寸
from scipy import misc
l = len(mnist.train.images[1])
# 开平方
l_ = int(np.sqrt(l))
images = mnist.train.images
labels = mnist.train.labels
for i in range(10):
plt.subplot(2,5,i+1)
plt.imshow(images[i].reshape(l_,l_))
plt.title(int(np.where(labels[i]==1)[0]))
misc.imsave('./test_image/images_'+str(i+10)+'.jpg', images[i].reshape((l_,l_)))
plt.show()
运行结果:
代码:
# 查看保存的结果
img = plt.imread('./test_image/images_11.jpg')
print(img.shape)
运行结果:
(28, 28)
代码:
# 第3种保存方式
import h5py
im_index = np.zeros((10, 28,28,1))
la_index = np.zeros((10,10))
for i in range(10):
f = h5py.File('train.h5', 'w')
im_index[i,:] = images[i].reshape((28,28,1))
la_index[i,:] = labels[i]
f['data'] = im_index
f['label'] = la_index
f.close()
代码:
rf = h5py.File('train.h5')
print(rf['data'])
print(rf['label'])
print(rf['data'].shape)
print(rf['label'].shape)
运行结果:
<HDF5 dataset "data": shape (10, 28, 28, 1), type "<f8">
<HDF5 dataset "label": shape (10, 10), type "<f8">
(10, 28, 28, 1)
(10, 10)