python读取二进制mnist实例详解

yipeiwu_com6年前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代理工具mitmproxy使用指南

python代理工具mitmproxy使用指南

前言 mitmproxy 是 man-in-the-middle proxy 的简称,译为中间人代理工具,可以用来拦截、修改、保存 HTTP/HTTPS 请求。以命令行终端形式呈现,操作...

R语言 vs Python对比:数据分析哪家强?

R语言 vs Python对比:数据分析哪家强?

什么是R语言? R语言,一种自由软件编程语言与操作环境,主要用于统计分析、绘图、数据挖掘。R本来是由来自新西兰奥克兰大学的罗斯·伊哈卡和罗伯特·杰特曼开发(也因此称为R),现在...

利用python打开摄像头及颜色检测方法

最近两周由于忙于个人项目,一直未发言了,实在是太荒凉了。。。。,上周由于项目,见到Python的应用极为广泛,用起来也特别顺手,于是小编也开始着手学习Python,…下面我就汇报下今天的...

详解如何用python实现一个简单下载器的服务端和客户端

话不多说,先看代码: 客户端: import socket def main(): #creat: download_client=socket.socket(socket...

Django + Uwsgi + Nginx 实现生产环境部署的方法

Django + Uwsgi + Nginx 实现生产环境部署的方法

如何在生产上部署Django? Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。 uwsgi介绍 uWSGI是一个Web服务器,它实现...