关于MNIST数据集的处理

本文介绍如何使用TensorFlow加载MNIST数据集,并通过Python的matplotlib和numpy库展示部分样本图像及其标签。同时,文章还探讨了不同图像保存方法及其结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

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)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值