• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C++基于文件流与armadillo读取mnist示例详解

c++ 搞代码 4年前 (2022-01-06) 38次浏览 已收录 0个评论

这篇文章主要给大家介绍了关于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 > a; image(i, j) = double(a); } 

这样就读取了一张图片。其余以此类推吧。

4. 完整代码

可以复制,可以修改,也可以用于商用和学术,但是请标注原作者(就是我)。

mnist.h

 #ifndef MNIST_H #define MNIST_H #include #include #include #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 << "文件打不开啊" <> a >> b >> c >> d; int magic = reverseInt(a, b, c, d); if (magic != 2051) //magic number wrong <strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>{ cout <> 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 > 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 << "文件打不开啊" <> a >> b >> c >> d; int magic = reverseInt(a, b, c, d); if (magic != 2049) //magic number wrong { cout <> a >> b >> c >> d; int num_lab = reverseInt(a, b, c, d); // 文件头读取完毕 label = mat(num_lab, 10, fill::zeros); for (int i = 0; i > a; label(i, int(a)) = 1; } return label; } 

总结

以上就是C++基于文件流与armadillo读取mnist示例详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++基于文件流与armadillo读取mnist示例详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址