python读取mnist
python读取mnist
其实就是python怎么读取binnary file
mnist的结构如下,选取train-images
TRAINING 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个 32 bit integer
试过很多方法,觉得最方便的,至少对我来说还是使用
struct.unpack_from()
filename = 'train-images.idx3-ubyte'binfile = open(filename , 'rb')buf = binfile.read() |
先使用二进制方式把文件都读进来
index = 0magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)index += struct.calcsize('>IIII') |
然后使用struc.unpack_from
'>IIII'是说使用大端法读取4个unsinged int32
然后读取一个图片测试是否读取成功
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() |
'>784B'的意思就是用大端法读取784个unsigned byte
完整代码如下
import numpy as npimport structimport matplotlib.pyplot as pltfilename = 'train-images.idx3-ubyte'binfile = open(filename , 'rb')buf = binfile.read()index = 0magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)index += struct.calcsize('>IIII')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读取mnist的更多相关文章
- Python读取MNIST数据集
MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...
- python读取,显示,保存mnist图片
python处理二进制 python的struct模块可以将整型(或者其它类型)转化为byte数组.看下面的代码. # coding: utf-8 from struct import * # 包装成 ...
- C++基于文件流和armadillo读取mnist
发现网上大把都是用python读取mnist的,用C++大都是用opencv读取的,但我不怎么用opencv,因此自己摸索了个使用文件流读取mnist的方法,armadillo仅作为储存矩阵的一种方式 ...
- python读取excel一例-------从工资表逐行提取信息
在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...
- python读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
- [转] Windows下使用Python读取Excel表格数据
http://www.python-excel.org/这个网站罗列了很多关于在Python下操作Excel文件的信息,这里选择了其介绍的第一个模块xlrd . xlrd 0.9.2版本跨平台同时支持 ...
- Python读取txt文件
Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...
- Python读取Yaml文件
近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...
- python读取中文文件编码问题
python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...
随机推荐
- [转]IOS, xib和storyboard的混用
1. 从xib的viewcontroll中启动storyboard 或者 从一个storyboard切换到另一个storyboard: [objc]– (IBAction)openStoryboard ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
- 第三篇、image 设置圆角的几种方式
第一种: 就拿view来举例 view.layer.masksToBounds=YES; //设置为yes,就可以使用圆角 view.layer.cornerRadius= ; //设置它的圆角大小 ...
- 第五十九篇、OC录制小视频
用 AVCaptureSession + AVCaptureMovieFileOutput 来录制视频,并通过AVAssetExportSeeion 手段来压缩视频并转换为 MP4 格 AVFound ...
- 从客户端中检测到有潜在危险的 request
如题,当遇到这种情况该怎么办呢? 通常情况下一下2种解决方案就可以解决问题了: 解决方案一: 在.aspx文件头中加入这句: <%@ Page validateReques ...
- jQuery父级以及同级元素查找介绍
父级以及同级元素的查找在使用过程中还是蛮频繁的,下面为大家介绍下jQuery是如何实现的,感兴趣的朋友可以参考下: jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$ ...
- hash桶
#include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...
- C# Winform程序打包
1.新建一个安装项目,起名“Setup3” 2.点击应用程序文件夹 3.在右侧右键点击添加文件 注意:是Debug文件夹下所有程序 接着在添加你的应用程序项目的时候,多添加一个msiexec.exe进 ...
- 数据库(MSSQLServer,Oracle,DB2,MySql)常见语句以及问题
创建数据库表 create table person ( FName varchar(), FAge int, FRemark varchar(), primary key(FName) ) 基本sq ...
- 定时备份服务器数据库(借助windows任务计划以及mysqldump)
最近社区多了,考虑到数据的安全性,要每天备份一次数据库,以防万一: linux目前还不是很了解,先用windows的计划任务吧: 大体思路就是 借用windows的计划任务来执行备份远程数据库到本地: ...