python读取二进制mnist实例详解

yipeiwu_com5年前Python基础

python读取二进制mnist实例详解

training data 数据结构:

<br>[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
........
xxxx   unsigned byte  ??        pixel
 

  将整个文件读入:

filename = 'train-images.idx3-ubyte'
binfile = open(filename , 'rb')
buf = binfile.read()

读取头四个32bit的interger:

index = 0
magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)
index += struct.calcsize('>IIII')

读取一个图片,784=28*28 :

im = struct.unpack_from('>784B' ,buf, index)
index += struct.calcsize('>784B')
 
im = np.array(im)
im = im.reshape(28,28)
 
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im , cmap='gray')
plt.show()

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

python实现旋转和水平翻转的方法

如下所示: # coding=utf-8 import glob import os from PIL import Image def rotate_270(imgae):...

一张图带我们入门Python基础教程

一张图带我们入门Python基础教程

啄木鸟社区上原始翻译后绘制的,最早这个图是出现在(链接已失效) “这个图太棒了,有编程基础的人一下子就了解 Python 的用法了。真正的 30 分钟上手。”Buzz by http:/...

详细探究Python中的字典容器

dictionary 我们都曾经使用过语言词典来查找不认识的单词的定义。语言词典针对给定的单词(比如 python)提供一组标准的信息。这种系统将定义和其他信息与实际的单词关联(映射)起...

在macOS上搭建python环境的实现方法

在macOS上搭建python环境的实现方法

今天刚接触python,查看了一些环境建立的文章,可能是年代久远很多都不适用,现在mac搭建python环境变得更简单。大神勿喷。 首先去python官网下载anaconda,我下载的是...

跟老齐学Python之类的细节

这几天和几个朋友以各种途径讨论过OOP的相关问题,他们是:令狐虫、Frank、晋剑、小冯 大家对OOP有不同看法,所谓工程派和学院派看法不一致。从应用的角度看,工程派的观点是值得推荐的,...