本文首发在我的个人博客:https://jlice.top/p/7hnmu/。欢迎大家前去参观,么么哒~
从MNIST数据集官网可以下载MNIST数据集。
MNIST数据集以.gz格式压缩,Python可以直接读取而不需要解压缩:
import gzip
with gzip.open('t10k-images-idx3-ubyte.gz') as f:
buf = f.read()
MNIST数据集使用二进制文件,而不是常规的图片文件格式。以t10k-images-idx3-ubyte为例,在官网有其结构说明:
[offset] [type] [value] [description]
0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 10000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
先解压并查看t10k-images-idx3-ubyte的内容:
$ gzip -d -k t10k-images-idx3-ubyte.gz
$ xxd t10k-images-idx3-ubyte | head
00000000: 0000 0803 0000 2710 0000 001c 0000 001c ......'.........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ....