转载自:https://www.2cto.com/kf/201701/587638.html
MNIST的images文件:
offset | type | value | description |
0000 | 32 bit integer | 0x00000803(2051) | magic number |
0004 | 32 bit integer | 60000 | 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 |
0018 | unsigned byte | ?? | pixel |
...... | |||
xxxx | unsigned byte | ?? | pixel |
代码中_read32()的定义在第33行,作用是从文件流中动态读取4位数据并转换为uint32的数据。
image文件的前四位为魔术码(magic number),只有检测到这4位数据的值和2051相等时,才代表这是正确的image文件,才会继续往下读取。接下来继续读取之后的4位,代表着image文件中,所包含的图片的数量(num_images)。再接着读4位,为每一幅图片的行数(rows),再后4位,为每一幅图片的列数(cols)。最后再读接下来的rows * cols * num_images位,即为所有图片的像素值。最后再将读取到的所有像素值装换为[index, rows, cols, depth]的4D矩阵。这样就将全部的image数据读取了出来。
同理,对于MNIST的labels文件:
offset | type | value | description |
0000 | 32 bit integer | 0x00000801(2049) | magic number |
0004 | 32 bit integer | 60000 | number of items |
0008 | unsigned byte | ?? | label |
0009 | unsigned byte | ?? | label |
...... | |||
xxxx | unsigned byte | ?? | label |