MNIST的数据集下载下来是不能直接看的,需要依据其给出的数据格式读取数据,然后用reshape生成28x28的图像。
四个文件:
train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)
我们以训练用图,也就是第一个文件为例。代码如下:
import struct
import numpy as np
import matplotlib.pyplot as plt
filename = r’C:\Users\dell\Desktop\Azure\MNIST\mnist\train-images.idx3-ubyte’
def read_train_images(filename):
binfile = open(filename, ‘rb’) #rb意思是以二进制的方式打开这个文件
buf = binfile.read() #.read表示从这个文件中读取多少个字节,缺省则表示读取所有字节
index = 0
magic,train_img_num, numRows, numColums = struct.unpack_from(’>iiii’, buf, index)
#struct.unpack_from:从buf中,以index为偏移量,按第一个参数指定的格式读取数据,返回值是一个元组。>IIII表示以大端模式读取四个int,大端模式是指二进制中最高位在左边。
print( magic,’ ‘, train_img_num, ’ ‘,numRows, ’ ‘, numColums)
index += struct.calcsize(’>IIII’) #c

本文介绍如何读取MNIST数据集,通过struct模块解析idx3-ubyte文件,展示了解析过程中的关键步骤,包括理解数据格式、使用struct进行解包,并通过numpy重塑数组生成图像。同时,提到了在处理gz压缩文件时可能出现的问题,即浏览器可能自动解压,导致读取错误,正确的做法是手动解压.gz文件并使用未压缩的idx3-ubyte文件。

672

被折叠的 条评论
为什么被折叠?



