MNIST手写体数字数据集
MNIST手写体数字数据集
本文从MNIST数据集的文件、文件格式、存储方式以及使用C++对其读取的流程(最终保存为图片)进行介绍。
-
MNIST原始数据文件
包含以上4个数据文件
-
文件格式
以下只以训练集图片文件为例说明:
<div<魔数,其实就是一个校验数,用来判断这个文件是不是MNIST里面的train-labels.idx1-ubyte文件;
3.数据集
训练样本:共60000个,
其中55000个用于训练,另外5000个用于验证(评估训练过程中的准确度);
测试样本:共10000个(评估最终模型的准确度);
所有数字图像已经进行尺寸归一化、数字居中处理,固定尺寸为28×28像素。
- MNIST大端存储方式
大端存储:高位字节放在内存低地址,
低位字节放在内存高地址;
区别于C/C++变量中的小端存储
小端存储:低位字节放在内存低地址,
高位字节放在内存高地址; - C++读取MNIST数据集读取流程
- 代码
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include<inttypes.h>
using namespace std;
using namespace cv;
//把大端数据转换为我们常用的小端数据
uint32_t swap_endian(uint32_t val)
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}
void readAndSave(const string& mnist_img_path, const string& mnist_label_path)
{
//以二进制格式读取mnist数据库中的图像文件和标签文件
ifstream mnist_image(mnist_img_path, ios::in | ios::binary);
ifstream mnist_label(mnist_label_path, ios::in | ios::binary);
if (mnist_image.is_open() == false || mnist_label.is_open() == false)
{
cout << "open mnist image or lable file error!" << endl;
return;
}
uint32_t magic;//文件中的魔术数(magic number)
uint32_t num_items;//mnist图像集文件中的图像数目
uint32_t num_label;//mnist标签集文件中的标签数目
uint32_t rows;//图像的行数
uint32_t cols;//图像的列数
//读魔术数
mnist_image.read(reinterpret_cast<char*>(&magic), 4);
magic = swap_endian(magic);
if (magic != 2051)
{
cout << "this is not the mnist image file" << endl;
return;
}
mnist_label.read(reinterpret_cast<char*>(&magic), 4);
magic = swap_endian(magic);
if (magic != 2049)
{
cout << "this is not the mnist label file" << endl;
return;
}
//读图像/标签数
mnist_image.read(reinterpret_cast<char*>(&num_items), 4);
num_items = swap_endian(num_items);
mnist_label.read(reinterpret_cast<char*>(&num_label), 4);
num_label = swap_endian(num_label);
//判断两种标签数是否相等
if (num_items != num_label)
{
cout << "the image file and label file are not a pair" << endl;
}
//读图像行数、列数
mnist_image.read(reinterpret_cast<char*>(&rows), 4);
rows = swap_endian(rows);
mnist_image.read(reinterpret_cast<char*>(&cols), 4);
cols = swap_endian(cols);
//读取图像
for (int i = 0; i != num_items; i++)
{
char* pixels = new char[rows * cols];
mnist_image.read(pixels, rows * cols);
char label;
mnist_label.read(&label, 1);
Mat image(rows, cols, CV_8UC1);
for (int m = 0; m != rows; m++)
{
uchar* ptr = image.ptr<uchar>(m);
for (int n = 0; n != cols; n++)
{
if (pixels[m * cols + n] == 0)
ptr[n] = 0;
else
ptr[n] = 255;
}
}
string saveFile = "E:\\visual studio 2015 Projects\\MNIST\\MNIST\\MNISTIMAGE\\" + to_string((unsigned int)label) + "_" + to_string(i) + ".jpg";
//文件保存路径
imwrite(saveFile, image);
}
}
int main()
{
readAndSave("E:\\visual studio 2015 Projects\\MNIST\\MNIST\\t10k-images.idx3-ubyte", "E:\\visual studio 2015 Projects\\MNIST\\MNIST\\t10k-labels.idx1-ubyte");
return 0;
}
以上就是关于MNIST手写体数字数据集的相关介绍,希望对你有用哦!