如何查看mnist数据集的图片

import numpy as np
import matplotlib.pyplot as plt


def read_mnist_images(filename):
    with open(filename, 'rb') as f:
        # 读取魔术数字、图像数量、行数、列数
        magic_number = int.from_bytes(f.read(4), 'big')
        number_of_images = int.from_bytes(f.read(4), 'big')
        rows = int.from_bytes(f.read(4), 'big')
        cols = int.from_bytes(f.read(4), 'big')

        # 读取图像数据
        images = np.frombuffer(f.read(), dtype=np.uint8)
        images = images.reshape((number_of_images, rows, cols))
    return images


# 修改为您的路径
filename = './data/MNIST/raw/train-images-idx3-ubyte'

images = read_mnist_images(filename)

# 显示第一张图像
plt.imshow(images[0], cmap='gray')
plt.title("First Image in MNIST Dataset")
plt.show()

把最后第三行的0,修改成别的数字,就会显示某张数字。 

运行之后,结果如下

 

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 07:58:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 07:58:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 07:58:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 07:58:02       20 阅读

热门阅读

  1. shell学习

    2024-03-14 07:58:02       18 阅读
  2. SpringBoot 中使用自定义参数解析器修改请求对象

    2024-03-14 07:58:02       18 阅读
  3. 浏览器文件下载

    2024-03-14 07:58:02       17 阅读
  4. IO进程线程day8

    2024-03-14 07:58:02       16 阅读
  5. 你知道数据库有哪些约束吗?

    2024-03-14 07:58:02       19 阅读
  6. Ribbon跟Nginx实现负载均衡的区别!

    2024-03-14 07:58:02       21 阅读
  7. 【分布式websocket】聊天系统消息加密如何做

    2024-03-14 07:58:02       25 阅读