在做 TensorFlow和Python实现神经网络的时候,需要利用到一个MNIST数据集,数据集的格式是以.idx1-ubyte后缀,包含60000个训练图像。将这些图像展示出来,需要利用到[struct模块] iii.run。

下载MNIST训练数据集

手动下载

mark

使用tensorflow自带下载

可以看到,这个地方是有监督学习 (有label这个东西嘛)from tensorflow.examples.tutorials.mnist import input_data# 下载mnist数据集mnist = input_data.read_data_sets('/tmp/', one_hot=True)# 数字(label)只能是0-9,神经网络使用10个出口节点就可以编码表示0-9;# 1 -> [0,1.0,0,0,0,0,0,0,0] one_hot表示只有一个出口节点是hot# 2 -> [0,0.1,0,0,0,0,0,0,0]# 5 -> [0,0,0,0,0,1.0,0,0,0]# /tmp是macOS的临时目录,重启系统数据丢失; Linux的临时目录也是/tmp

详细步骤

读取文件with open(filename ,'rb') as f1:

buf1 = f1.read()

还有另外一种常用的方法,两个方法目前来看没有什么区别。f1 = open(filename , 'rb')

buf = binfile.read() # 先使用二进制方式把文件都读进来

跨过头部区域

train-images-idx3-ubyteTRAINING SET IMAGE FILE (train-images-idx3-ubyte):

[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

可以看到头部有4个integer 类型,设置image_index += struct.calcsize('>IIII')计算4个integer 值的位置,然后image_index 直接跳过去。至于为什么用IIII,愿意的话可以点击了解。temp = struct.unpack_from('>784B', buf1, image_index)

# '>784B'的意思就是用大端法读取784( 28*28 )个unsigned byteim = np.reshape(temp,(28,28))

最后那句np.reshape(temp,(28,28))是以下两句的缩写im = np.array(im)

im = im.reshape(28,28)

train-labels-idx1-ubyte

可以看到头部有2个integer 类型,同理,label_index 直接跳过去。TRAINING SET LABEL FILE (train-labels-idx1-ubyte):[offset] [type] [value] [description] 0000 32 bit integer 0x00000801(2049) magic number (MSB first)

0004 32 bit integer 60000 number of items 0008 unsigned byte ?? label 0009 unsigned byte ?? label ........

xxxx unsigned byte ?? labelThe labels values are 0 to 9.

显示图片plt.imshow(im , cmap='gray')

应该就可以看到图片了,是一张5, 当然头部文件还是要有的%matplotlib inlineimport numpy as npimport structimport matplotlib.pyplot as plt

path = 'E:\\Machine Learning\\train-images.idx3-ubyte'with open(path,'rb') as f1:

buf1 = f1.read()

image_index = 0image_index += struct.calcsize('>IIII')

temp = struct.unpack_from('>784B', buf1, image_index)

# '>784B'的意思就是用大端法读取784( 28*28 )个unsigned byteim = np.reshape(temp,(28,28))

plt.imshow(im , cmap='gray')

give me 5

多张图片读取

多张图片import numpy as npimport structimport matplotlib.pyplot as pltdef readfile():

with open('E:\\Machine Learning\\train-images.idx3-ubyte','rb') as f1:

buf1 = f1.read() with open('E:\\Machine Learning\\train-labels.idx1-ubyte','rb') as f2:

buf2 = f2.read() return buf1, buf2def get_image(buf1):

image_index = 0

image_index += struct.calcsize('>IIII')

im = [] for i in range(9):

temp = struct.unpack_from('>784B', buf1, image_index) # '>784B'的意思就是用大端法读取784个unsigned byte

im.append(np.reshape(temp,(28,28)))

image_index += struct.calcsize('>784B') # 每次增加784B

return imdef get_label(buf2): # 得到标签数据

label_index = 0

label_index += struct.calcsize('>II') return struct.unpack_from('>9B', buf2, label_index)if __name__ == "__main__":

image_data, label_data = readfile()

im = get_image(image_data)

label = get_label(label_data) for i in range(9):

plt.subplot(3, 3, i + 1)

title = u"标签对应为:"+ str(label[i])

plt.title(title, fontproperties='SimHei')

plt.imshow(im[i], cmap='gray')

plt.show()

遇到的一些坑:中文标题乱码的问题plt.title(title, fontproperties='SimHei') # 后边这个字体**SimHei**加上就好了标题内部不能用+

在外部加好之后,赋值给新变量,然后放进title即可

作者:mmmwhy

链接:https://www.jianshu.com/p/81f8ca1b722f

python3怎么使用mnist_loader_Python读取mnist相关推荐

  1. TensorFlow读取MNIST数据集错误的问题

    TensorFlow读取mnist数据集错误的问题 运行程序出现"URLError"错误的问题 可能是服务器或路径的原因,可以自行下载数据集后,将数据集放到代码所在的文件夹下,并将 ...

  2. 【机器学习】MATLAB读取mnist数据库

    原文出处:http://blog.csdn.net/tracer9/article/details/51253604 最近要做<优化理论基础>的课程大作业,需要用到mnist这个手写识别数 ...

  3. 读取mnist数据集方法大全(train-images-idx3-ubyte.gz,train-labels.idx1-ubyte等)(python读取gzip文件)

    文章目录 gzip包 keras读取mnist数据集 本地读取mnist数据集 下载数据集 解压读取 方法一 方法二 gzip包读取 读取bytes数据 注:import导入的包如果未安装使用pip安 ...

  4. 读取MNIST数据集并显示数据集图片 完全解析

    # coding: utf-8 import sys, os sys.path.append(os.pardir) # 为了导入父目录的文件而进行的设定 import numpy as np from ...

  5. (超详细)读取mnist数据集并保存成图片

    mnist数据集介绍.读取.保存成图片 1.mnist数据集介绍: MNIST数据集是一个手写体数据集,简单说就是一堆这样东西  MNIST的官网地址是 MNIST; 通过阅读官网我们可以知道,这个数 ...

  6. python 读取 MNIST 数据集,并解析为图片文件

    python 读取 MNIST 数据集,并解析为图片文件 MNIST 是 Yann LeCun 收集创建的手写数字识别数据集,训练集有 60,000 张图片,测试集有 10,000 张图片.数据集链接 ...

  7. 通过MATLAB读取mnist数据库

    mnist (手写字符识别) 的数据集下载地: http://yann.lecun.com/exdb/mnist/ MNIST是在机器学习领域中的一个经典问题.该问题解决的是把28x28像素的灰度手写 ...

  8. MNIST手写数字数据集格式,如何读取MNIST数据集?

    数据集下载地址:http://yann.lecun.com/exdb/mnist/ TRAINING SET LABEL FILE (train-labels-idx1-ubyte):[offset] ...

  9. Python读取MNIST数据集

    MNIST数据集下载地址:http://yann.lecun.com/exdb/mnist/ 读取MINST数据集第一张图像并显示 # coding=utf-8 import numpy as np ...

最新文章

  1. 为什么 Python 没有 main 函数?
  2. 三列浮动中间列宽度自适应
  3. cisco 交换机 定期 自动 备份配置 -linux,交换机定时自动备份配置文件的方法
  4. 【Linux入门基础知识】Linux 脚本编写基础
  5. mysql递归查询所有上下节点_非递归打印二叉树的所有路径,保存父节点和孩子节点到底有啥差别...
  6. 面向对象 —— 结构与设计
  7. 分布式存储系统学习笔记(一)—什么是分布式系统(2)—数据分布
  8. 百度地图依赖包php,调用百度地图
  9. Notepad++常用插件
  10. 转载--卷积神经网络卷积层池化层输出计算公式
  11. python访问字符串中的部分字符的操作_小白学Python-13(字符串基础与简单操作)...
  12. linux磁盘管理——quota磁盘配额GPT分区
  13. android 系统重新安装,一招学会安卓手机系统重装教程
  14. 个人最喜欢的几款火狐扩展
  15. phpstudy 本地配置url重写
  16. 让NodeJS出错退出之后自动重启的办法
  17. MacBook Pro 13 A1502 更换电池
  18. Android 界面设计尺寸规范
  19. LOL vs DOTA2,撕了这么多年终于有结论了
  20. 【汇正财经】红筹股和H股有什么区别?

热门文章

  1. reentrantlock原理_你必须要知道的热门 ReentrantLock 及 AQS 的实现原理
  2. 今夕何夕影迷小伙伴,喜欢的壁纸都在高图网
  3. 电商移动促销页面设计素材PSD分层模板,轻松出稿稿
  4. 民航飞行学院计算机学院院长,中国民航飞行学院计算机学院领导及老师到访四川华迪开展教研活动...
  5. linux显卡驱动未加载,Linux下无显卡驱动的解决办法
  6. oracle给日期加特定天数,oracle按照日期求连续天数的数据sql
  7. 知云文献阅读器_知云文献翻译
  8. 新高考改革选计算机专业要学什么,2020高考改革后考生如何选科与选专业?
  9. SU数据新旧格式转换问题
  10. Linux环境使用命名空间编写一个简单的容器应用程序:namespace,container,cgroups