这个博客是留给自己备用的

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <eigen3/Eigen/Core>class MnistData
{
public:MnistData(std::string train_img_filename, std::string train_label_filename,std::string test_img_filename, std::string test_label_filename);void getMnistTrainData(Eigen::MatrixXd &_data);void getMnistTrainLabel(Eigen::MatrixXd &_data);void getMnistTestData(Eigen::MatrixXd &_data);void getMnistTestLabel(Eigen::MatrixXd &_data);private:void read_Mnist_Images(const std::string &filename, Eigen::MatrixXd &_data);void read_Mnist_Label(const std::string &filename, Eigen::MatrixXd &_data);int ReverseInt (int i);private:std::vector<std::string> imgDataPath;// {train image, train label, test image, test laebl}
};MnistData::MnistData(std::string train_img_filename,std::string train_label_filename,std::string test_img_filename,std::string test_label_filename)
{imgDataPath.push_back(train_img_filename);imgDataPath.push_back(train_label_filename);imgDataPath.push_back(test_img_filename);imgDataPath.push_back(test_label_filename);
}void MnistData::getMnistTrainData(Eigen::MatrixXd &_data)
{read_Mnist_Images(imgDataPath[0], _data);
}void MnistData::getMnistTrainLabel(Eigen::MatrixXd &_data)
{read_Mnist_Label(imgDataPath[1], _data);
}void MnistData::getMnistTestData(Eigen::MatrixXd &_data)
{read_Mnist_Images(imgDataPath[2], _data);
}void MnistData::getMnistTestLabel(Eigen::MatrixXd &_data)
{read_Mnist_Label(imgDataPath[3], _data);
}void MnistData::read_Mnist_Images(const std::string &filename, Eigen::MatrixXd &_data)
{std::ifstream file;file.open(filename.c_str(), std::ios::in | std::ios::binary);if (file.is_open()){int magic_number = 0;int number_of_images = 0;int n_rows = 0;int n_cols = 0;unsigned char label;file.read((char*)&magic_number, sizeof(magic_number));file.read((char*)&number_of_images, sizeof(number_of_images));file.read((char*)&n_rows, sizeof(n_rows));file.read((char*)&n_cols, sizeof(n_cols));magic_number = ReverseInt(magic_number);number_of_images = ReverseInt(number_of_images);n_rows = ReverseInt(n_rows);n_cols = ReverseInt(n_cols);std::cout << "magic number = " << magic_number << std::endl;std::cout << "number of images = " << number_of_images << std::endl;std::cout << "rows = " << n_rows << std::endl;std::cout << "cols = " << n_cols << std::endl;_data.resize(n_rows * n_cols, number_of_images);for (int i = 0; i < number_of_images; i++){Eigen::MatrixXd img(n_rows * n_cols, 1);for (int r = 0; r < n_rows; r++){for (int c = 0; c < n_cols; c++){unsigned char image = 0;file.read((char*)&image, sizeof(image));img(n_rows * r + c, 0) = (double)image / 255.0;}}_data.col(i) = img;}}if (file.is_open())file.close();
}void MnistData::read_Mnist_Label(const std::string &filename, Eigen::MatrixXd &_data)
{std::ifstream file(filename.c_str(), std::ios::in | std::ios::binary);if (file.is_open()){int magic_number = 0;int number_of_images = 0;file.read((char*)&magic_number, sizeof(magic_number));file.read((char*)&number_of_images, sizeof(number_of_images));magic_number = ReverseInt(magic_number);number_of_images = ReverseInt(number_of_images);std::cout << "magic number = " << magic_number << std::endl;std::cout << "number of images = " << number_of_images << std::endl;_data.resize(10, number_of_images);_data.setZero();for (int i = 0; i < number_of_images; i++){unsigned char label = 0;file.read((char*)&label, sizeof(label));_data((int)label, i) = 1;}}if (file.is_open())file.close();
}int MnistData::ReverseInt (int i)
{unsigned char ch1, ch2, ch3, ch4;ch1=i&255;ch2=(i>>8)&255;ch3=(i>>16)&255;ch4=(i>>24)&255;return((int)ch1<<24)+((int)ch2<<16)+((int)ch3<<8)+ch4;
}

MnistData的读取相关推荐

  1. tensorflow随笔-保存与读取使用模型

    1.MNIST是深度学习的经典入门demo,他是由6万张训练图片和1万张测试图片构成的,每张图片都是2828大小(如下图),而且都是黑白色构成(这里的黑色是一个0-1的浮点数,黑色越深表示数值越靠近1 ...

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

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

  3. golang通过RSA算法生成token,go从配置文件中注入密钥文件,go从文件中读取密钥文件,go RSA算法下token生成与解析;go java token共用

    RSA算法 token生成与解析 本文演示两种方式,一种是把密钥文件放在配置文件中,一种是把密钥文件本身放入项目或者容器中. 下面两种的区别在于私钥公钥的初始化, init方法,需要哪种取哪种. 通过 ...

  4. 在kotlin companion object中读取Bean,注入Bean对象

    在kotlin companion object中读取Bean,注入Bean对象 在使用kotlin时,或多或少地会使用到一些公共组件,如 http. mongo. redis相关的组件.   使用组 ...

  5. 在kotlin companion object中读取spring boot配置文件,静态类使用@Value注解配置

    在kotlin companion object中读取配置文件 静态类使用@Value注解配置 class Config {@Value("\${name}")fun setNam ...

  6. 基于Golang的监听读取配置文件的程序包开发——simpleConfig_v1

    基于Golang的监听&读取配置文件的程序包开发--simpleConfig_v1 [阅读时间:约10分钟] 一.配置文件概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 ...

  7. OpenCV 笔记(03)— 读取视频、通过摄像头采集视频、采集视频 canny 边缘检测

    我们本节学习如何利用 OpenCV 中的 VideoCapture 类,来对视频进行读取显示,以及调用摄像头. VideoCapture 它提供了从摄像机或视频文件捕获视频的 C++ 接口, 作用是从 ...

  8. Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)

    1. 命令行参数 向 shell 脚本传递数据的最基本方法是使用命令行参数.命令行参数允许在运行脚本时向命令行添加数据. $ ./addem 10 30 本例向脚本 addem 传递了两个命令行参数( ...

  9. Python 标准库之 os (获取当前目录、读取/设置环境变量、重命名文件、运行shell命令、创建/删除/查看目录文件、判断目录/文件/存在、获取绝对路径、获取文件名、获取换行符、获取路径分隔符)

    1. os与sys模块的官方解释如下: os This module provides a portable way of using operating system dependent funct ...

最新文章

  1. 遥控车_vijos1458_纪中1724_水
  2. patch成为了ALL You Need?挑战ViT、MLP-Mixer的简单模型来了
  3. 烂泥:ubuntu 14.04搭建Open***服务器
  4. 什么是1+N模式的新一代城市大脑建设方案
  5. asp.net用户登录 用户验证
  6. linux内核端口绑定,linux 多网卡bonding 绑定 端口聚合
  7. 渗透测试入门14之渗透测试工具1
  8. JUnit5 @AfterEach注解示例
  9. 08_提升方法Boosting2_统计学习方法
  10. jira7.3.6的安装步骤
  11. JavaScript中Ajax
  12. tensorflow在文本处理中的使用——Word2Vec预测
  13. 从4千/平到4.5万/平!南京房价10年血泪史 看哭所有人!
  14. 用JS逐步分解实现放大镜(看完就有收获)
  15. 阿里达摩院发布并开源“通义”大模型,AI底座之上促场景创新
  16. 福建选择阿里云服务器地域(华南/华东/华北)哪个更好?
  17. iOS生成gif图片
  18. winform直接控制云台_比 2 代便宜的灵眸手机云台 3,竟然还多了 15 条新亮点!...
  19. 组会 | RELAXLOSS: DEFENDING MEMBERSHIP INFERENCE ATTACKS WITHOUT LOSING UTILITY
  20. 浅析某城商行手机银行水平授权漏洞问题

热门文章

  1. 【DTCC2016】平安科技汪洋畅谈与数据库的不解之缘
  2. 关于 DellEMC 安装系统时找不到系统硬盘的问题
  3. KDE的网络管理工具networkmanagement
  4. 交通违章6分怎么处理
  5. 锥体区块链白皮书V1.0
  6. 同惠LCR测试仪TH2829产品技术参数
  7. 《乡土中国》 费孝通
  8. 硬件工程师的真实发展前途是怎么样的?
  9. [展览人周刊]华展云20170710期
  10. 详解音视频直播平台搭建中的低延时