MNIST数据库_jsjsdzd的博客-程序员宅基地_mnist数据库

一、mnist数据库特点
MNIST数据库介绍:
MNIST是一个手写数字数据库,它是NIST数据库的一个共有7万张图片。其中6万张用于训练神经网络,1万张用于测试神经网络,每张图片是一个28*28像素点的图片。
黑底白字,黑底用0表示,白字用0~1之间的浮点数表示,越接近1,颜色越百。

二、原理
1不同的方法来处理 mnist数据集 : MNIST手写数字体分类–KNN matlab实现11
2用Decision Tree对MNIST数据集进行实验
3libsvm对MNIST数据集进行实验

三、数值化结果
#include “funset.hpp”
#include
#include
#include
#include <opencv2/opencv.hpp>

static int 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;
}

static void read_Mnist(std::string filename, std::vectorcv::Mat &vec)
{
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
file.read((char*)&magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
file.read((char*)&number_of_images, sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);
file.read((char*)&n_rows, sizeof(n_rows));
n_rows = ReverseInt(n_rows);
file.read((char*)&n_cols, sizeof(n_cols));
n_cols = ReverseInt(n_cols);
for (int i = 0; i < number_of_images; ++i) {
cv::Mat tp = cv::Mat::zeros(n_rows, n_cols, CV_8UC1);
for (int r = 0; r < n_rows; ++r) {
for (int c = 0; c < n_cols; ++c) {
unsigned char temp = 0;
file.read((char*)&temp, sizeof(temp));
tp.at(r, c) = (int)temp;
}
}
vec.push_back(tp);
}
}
}

static void read_Mnist_Label(std::string filename, std::vector &vec)
{
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
int magic_number = 0;
int number_of_images = 0;
int n_rows = 0;
int n_cols = 0;
file.read((char*)&magic_number, sizeof(magic_number));
magic_number = ReverseInt(magic_number);
file.read((char*)&number_of_images, sizeof(number_of_images));
number_of_images = ReverseInt(number_of_images);

	for (int i = 0; i < number_of_images; ++i) {
		unsigned char temp = 0;
		file.read((char*)&temp, sizeof(temp));
		vec[i] = (int)temp;
	}
}

}

static std::string GetImageName(int number, int arr[])
{
std::string str1, str2;
for (int i = 0; i < 10; i++) {
if (number == i) {
arr[i]++;
str1 = std::to_string(arr[i]);
if (arr[i] < 10) {
str1 = “0000” + str1;
} else if (arr[i] < 100) {
str1 = “000” + str1;
} else if (arr[i] < 1000) {
str1 = “00” + str1;
} else if (arr[i] < 10000) {
str1 = “0” + str1;
}
break;
}
}
str2 = std::to_string(number) + “_” + str1;
return str2;
}

int MNISTtoImage()
{
// reference: http://eric-yuan.me/cpp-read-mnist/
// test images and test labels
// read MNIST image into OpenCV Mat vector
std::string filename_test_images = “E:/GitCode/NN_Test/data/database/MNIST/t10k-images.idx3-ubyte”;
int number_of_test_images = 10000;
std::vectorcv::Mat vec_test_images;
read_Mnist(filename_test_images, vec_test_images);
// read MNIST label into int vector
std::string filename_test_labels = “E:/GitCode/NN_Test/data/database/MNIST/t10k-labels.idx1-ubyte”;
std::vector vec_test_labels(number_of_test_images);
read_Mnist_Label(filename_test_labels, vec_test_labels);
if (vec_test_images.size() != vec_test_labels.size()) {
std::cout << “parse MNIST test file error” << std::endl;
return -1;
}
// save test images
int count_digits[10];
std::fill(&count_digits[0], &count_digits[0] + 10, 0);
std::string save_test_images_path = “E:/GitCode/NN_Test/data/tmp/MNIST/test_images/”;
for (int i = 0; i < vec_test_images.size(); i++) {
int number = vec_test_labels[i];
std::string image_name = GetImageName(number, count_digits);
image_name = save_test_images_path + image_name + “.jpg”;
cv::imwrite(image_name, vec_test_images[i]);
}
// train images and train labels
// read MNIST image into OpenCV Mat vector
std::string filename_train_images = “E:/GitCode/NN_Test/data/database/MNIST/train-images.idx3-ubyte”;
int number_of_train_images = 60000;
std::vectorcv::Mat vec_train_images;
read_Mnist(filename_train_images, vec_train_images);
// read MNIST label into int vector
std::string filename_train_labels = “E:/GitCode/NN_Test/data/database/MNIST/train-labels.idx1-ubyte”;
std::vector vec_train_labels(number_of_train_images);
read_Mnist_Label(filename_train_labels, vec_train_labels);
if (vec_train_images.size() != vec_train_labels.size()) {
std::cout << “parse MNIST train file error” << std::endl;
return -1;
}
// save train images
std::fill(&count_digits[0], &count_digits[0] + 10, 0);
std::string save_train_images_path = “E:/GitCode/NN_Test/data/tmp/MNIST/train_images/”;
for (int i = 0; i < vec_train_images.size(); i++) {
int number = vec_train_labels[i];
std::string image_name = GetImageName(number, count_digits);
image_name = save_train_images_path + image_name + “.jpg”;
cv::imwrite(image_name, vec_train_images[i]);
}
// save big imags
std::string images_path = “E:/GitCode/NN_Test/data/tmp/MNIST/train_images/”;
int width = 28 * 20;
int height = 28 * 10;
cv::Mat dst(height, width, CV_8UC1);
for (int i = 0; i < 10; i++) {
for (int j = 1; j <= 20; j++) {
int x = (j-1) * 28;
int y = i * 28;
cv::Mat part = dst(cv::Rect(x, y, 28, 28));
std::string str = std::to_string(j);
if (j < 10)
str = “0000” + str;
else
str = “000” + str;
str = std::to_string(i) + “_” + str + “.jpg”;
std::string input_image = images_path + str;
cv::Mat src = cv::imread(input_image, 0);
if (src.empty()) {
fprintf(stderr, “read image error: %s\n”, input_image.c_str());
return -1;
}
src.copyTo(part);
}
}
std::string output_image = images_path + “result.png”;
cv::imwrite(output_image, dst);
return 0;
}
在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jsjsdzd/article/details/90743483

智能推荐

随便推点