C++基于文件流和armadillo读取mnist
发现网上大把都是用python读取mnist的,用C++大都是用opencv读取的,但我不怎么用opencv,因此自己摸索了个使用文件流读取mnist的方法,armadillo仅作为储存矩阵的一种方式。
1. mnist文件
首先避坑,这些文件要解压。

官网截图可知,文件头很简单,只有若干个32位整数,MSB,像素和标签均是无符号字节(即unsigned char)可以先读取文件头,再读取剩下的部分。
2. 读取文件头
我觉得没什么必要啊,直接跳过不行吗
文件头都是32位,那就整四个unsigned char呗。
uchar a, b, c, d;
File >> a >> b >> c >> d;
这样a、b、c、d就保存了一个整数。
x = ((((a * 256) + b) * 256) + c) * 256 + d;
然后就得到了呗。
看每个文件有多少文件头,就操作几次(并可以顺便与官方的magic number进行对比),剩下的就是文件的内容了。
3. 读取内容
这部分可以依照之前的方法,一次读取一个字符,再保存至矩阵当中。例如:
uchar a;
mat image(28, 28, fill::zeros); // 这是个矩阵!
for(int i = 0; i < 28; i++) //28行28列的图像懒得改了
for(int j = 0; j < 28; j++)
{
File >> a;
image(i, j) = double(a);
}
这样就读取了一张图片。其余以此类推吧。
4. 完整代码
可以复制,可以修改,也可以用于商用和学术,但是请标注原作者(就是我)。
mnist.h
#ifndef MNIST_H
#define MNIST_H
#include<iostream>
#include<fstream>
#include<armadillo>
#define uchar unsigned char
using namespace std;
using namespace arma;
//小端存储转换
int reverseInt(uchar a, uchar b, uchar c, uchar d);
//读取image数据集信息
mat read_mnist_image(const string fileName);
//读取label数据集信息
mat read_mnist_label(const string fileName);
#endif
mnist.cpp
//mnist.cpp
//作者:C艹
#include "mnist.h"
int reverseInt(uchar a, uchar b, uchar c, uchar d)
{
return ((((a * 256) + b) * 256) + c) * 256 + d;
}
mat read_mnist_image(const string fileName)
{
fstream File;
mat image;
File.open(fileName);
if (!File.is_open()) // cannot open file
{
cout << "文件打不开啊" << endl;
return mat(0, 0, fill::zeros);
}
uchar a, b, c, d;
File >> a >> b >> c >> d;
int magic = reverseInt(a, b, c, d);
if (magic != 2051) //magic number wrong
{
cout << magic;
return mat(0, 0, fill::zeros);
}
File >> a >> b >> c >> d;
int num_img = reverseInt(a, b, c, d);
File >> a >> b >> c >> d;
int num_row = reverseInt(a, b, c, d);
File >> a >> b >> c >> d;
int num_col = reverseInt(a, b, c, d);
// 文件头读取完毕
image = mat(num_img, num_col * num_row, fill::zeros);
for(int i = 0; i < num_img; i++)
for (int j = 0; j < num_col * num_row; j++)
{
File >> a;
image(i, j) = double(a);
}
return image;
}
mat read_mnist_label(const string fileName)
{
fstream File;
mat label;
File.open(fileName);
if (!File.is_open()) // cannot open file
{
cout << "文件打不开啊" << endl;
return mat(0, 0, fill::zeros);
}
uchar a, b, c, d;
File >> a >> b >> c >> d;
int magic = reverseInt(a, b, c, d);
if (magic != 2051) //magic number wrong
{
cout << magic;
return mat(0, 0, fill::zeros);
}
File >> a >> b >> c >> d;
int num_lab = reverseInt(a, b, c, d);
// 文件头读取完毕
label = mat(num_lab, 10, fill::zeros);
for (int i = 0; i < num_lab; i++)
{
File >> a;
label(i, int(a)) = 1;
}
return label;
}
C++基于文件流和armadillo读取mnist的更多相关文章
- JS通过使用PDFJS实现基于文件流的预览功能
需求: 使用JS实现PDF文件预览功能 备选方案: 使用ViewerJS,官网 http://viewerjs.org/ 使用PDFJS,官网 https://mozilla.github.io/ ...
- C#循环读取文件流,按行读取
public Dictionary<string,string> GetSourceDisksElements(String section) { section = "[&qu ...
- 使用C#处理基于比特流的数据
使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...
- c语言中的文件流
一.打开和关闭文件 #include int main( void ) { FILE* pReadFile = fopen( "E:\\mytest.txt", "r&q ...
- Linux 文件流管理
1. 打开/关闭文件 1). 打开文件 / fopen 作用: 打开一个文件,将其与文件流联系起来,方便后续的操作 头文件: #include <stdio.h> 函数原型: FILE * ...
- 第十四周总结 Io之文件流
I/O相关 输入/输出 流(数据流动) 数据流动的方向 读数据(输入input) 写数据(输出output) 文件流 字符流 数据流 对象流 网络流.... 1.什么叫文件 一种电脑的存储方式 文件有 ...
- koa2基于stream(流)进行文件上传和下载
阅读目录 一:上传文件(包括单个文件或多个文件上传) 二:下载文件 回到顶部 一:上传文件(包括单个文件或多个文件上传) 在之前一篇文章,我们了解到nodejs中的流的概念,也了解到了使用流的优点,具 ...
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
- FileStream文件流的读取和写入(为以后聊天工具的设计基础)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
随机推荐
- WPF 基础 - 启动与退出及异常捕获
1. 若需要控制 exe 实例数量 bool ret; mutex = new System.Threading.Mutex(true, exename, out ret); if (!ret) { ...
- python常见的错误异常
1.AssertionError 该异常在assert()语句运行失败时输出 2.AttributeError 该异常在参考或设置属性失败时输出 eg:class Gs: pass g = Gs() ...
- Linux 用户登陆提示This account is currently not available
使用 su 切换到用户 hdfs 时提示:This account is currently not available,使用 hdfs 用户登陆会直接退出 ssh 窗口. 此时可以尝试检查文件 /e ...
- 后台开发-核心技术与应用实践--TCP协议
网络模型 为使不同计算机厂家的计算机能够互相通信,国际标准化组织 ISO 1981 年正式推荐了一个网络系统结构一一七层参考模型,也叫作开放系统互连模型. ISO 七层网络模型及其功能展示: 这个七层 ...
- java例题_46 两个字符串拼接问题!
1 /*46 [程序 46 字符串连接] 2 题目:两个字符串连接程序,将两个字符串拼接在一起 3 */ 4 5 /*分析 6 * 两个字符串的拼接方法 7 * concat方式 8 * 当两个量都为 ...
- 七大php主流框架,你用过几个?
在php中,我们最常用的框架有七个,分别是Yii2.Laravel.Yaf.Thinkphp.Code Igniter.Zend Framework以及CakePHP,今天我们就来详细介绍一下他们.一 ...
- Apache Hudi 0.8.0版本重磅发布
1. 重点特性 1.1 Flink集成 自从Hudi 0.7.0版本支持Flink写入后,Hudi社区又进一步完善了Flink和Hudi的集成.包括重新设计性能更好.扩展性更好.基于Flink状态索引 ...
- OOUML系列总结及终章回顾
盼望着,盼望着,OO课程终于结束了,但是,此刻的我却感到一丝失落,甚至想着再来一单元岂不妙哉? 目录 总结本单元三次作业架构 四个单元中架构设计及OO方法理解的演进 四个单元中测试理解与实践的演进 课 ...
- 中小型前端团队代码规范工程化最佳实践 - ESLint
前言 There are a thousand Hamlets in a thousand people's eyes. 一千个程序员,就有一千种代码风格.在前端开发中,有几个至今还在争论的代码风格差 ...
- Oracle-buffer cache过小导致SQL执行时间长
一.问题:客户反馈在生产库和测试库执行相同SQL,测试库执行比生产库慢一倍 问题摆在这里,需要进行分析? 啥??? 版本11.2.0.4,都是单实例,主机系统硬件配置差不多. 二.对比SQL的执行效率 ...