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微信库itchat实现微信自动回复功能

前言 在论坛上看到了用Python登录微信并实现自动签到,才了解到一个新的Python库: itchat 利用Python 微信库itchat,可以实现自动回复等多种功能,好玩到根本停不...

TensorFlow 滑动平均的示例代码

滑动平均会为目标变量维护一个影子变量,影子变量不影响原变量的更新维护,但是在测试或者实际预测过程中(非训练时),使用影子变量代替原变量。 1、滑动平均求解对象初始化 ema = tf...

使用python获取电脑的磁盘信息方法

使用Python获取电脑的磁盘信息需要借助于第三方的模块psutil,这个模块需要自己安装,纯粹的CPython下面不具备这个功能。 在iPython交互界面中进行如下演示: 查看电脑的...

基于Django静态资源部署404的解决方法

一. 静态资源static文件放在app中 确认django.contrib.staticfiles包含在INSTALLED_APPS中。 在settings文件中定义STATIC_UR...

Python3爬楼梯算法示例

本文实例讲述了Python3爬楼梯算法。分享给大家供大家参考,具体如下: 假设你正在爬楼梯。需要 n 步你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼...