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 ...
随机推荐
- 使用wireshark 抓取 http https tcp ip 协议进行学习
使用wireshark 抓取 http https tcp ip 协议进行学习 前言 本节使用wireshark工具抓包学习tcp ip http 协议 1. tcp 1.1 tcp三次握手在wire ...
- FreeBSD 13 显卡支持
On FreeBSD 13, using drm-devel-kmod, support is the same as on Linux 5.4. This includes support for ...
- 主成分分析 | Principal Components Analysis | PCA
理论 仅仅使用基本的线性代数知识,就可以推导出一种简单的机器学习算法,主成分分析(Principal Components Analysis, PCA). 假设有 $m$ 个点的集合:$\left\{ ...
- filecoin今日价格,filecoin币价估值,filecoin币会涨到多少钱
filecoin今日价格,截止 2021 年 3 月 17 日 9 时,filecoin价格为 63.8939 美元,约合人民币 415.69 元.流通市值约 416.2 亿人民币,总市值达到 831 ...
- 【原创】Linux虚拟化KVM-Qemu分析(十一)之virtqueue
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- JVMGC+Spring Boot生产部署和调参优化
一.微服务开发完成,IDEA进行maven clean和package 出现BUILD SUCCESS说明打包成功 二.要求微服务启动时,配置JVM GC调优参数 p.p1 { margin: 0; ...
- java面试-生产环境出现CPU占用过高,谈谈你的分析思路和定位
思路:结合Linux和JDK命令一起分析 1.用top命令找出CPU占比最高的进程 2.ps -ef|grep java|grep -v grep 或者jps -l进一步定位,得知是怎样一个后台程序惹 ...
- python3美化表格数据输出结果
技术背景 在前面一篇博客中我们介绍过关于python的表格数据处理方案,这其中的工作重点就是对表格类型的数据进行梳理.计算和展示,本文重点介绍展示这个方面的工作.首先我们看一个案例,定义一个数组形式的 ...
- 05_pytorch的Tensor操作
05_pytorch的Tensor操作 目录 一.引言 二.tensor的基础操作 2.1 创建tensor 2.2 常用tensor操作 2.2.1 调整tensor的形状 2.2.2 添加或压缩t ...
- Seata搭建与分布式事务入门
在单体架构下,我们大多使用的是单体数据库,通过数据库的ACID特性支持,实现了本地事务.但是在微服务架构下复杂的业务关系中,分布式事务是不可避免的问题之一.Seata是Spring Cloud Ali ...