设为首页 加入收藏

TOP

MNIST数据库介绍及转换(一)
2015-11-13 01:24:22 来源: 作者: 【 】 浏览:57
Tags:MNIST 数据库 介绍 转换

MNIST数据库介绍:MNIST是一个手写数字数据库,它有60000个训练样本集和10000个测试样本集。它是NIST数据库的一个子集。


MNIST数据库官方网址为:http://yann.lecun.com/exdb/mnist/ ,也可以在windows下直接下载,train-images-idx3-ubyte.gz、train-labels-idx1-ubyte.gz等。下载四个文件,解压缩。解压缩后发现这些文件并不是标准的图像格式。这些图像数据都保存在二进制文件中。每个样本图像的宽高为28*28。


以下为将其转换成普通的jpg图像格式的代码:


#include
#include


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


using namespace std;


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


void read_Mnist(string filename, vector &vec)
{
?ifstream file (filename, 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);
? }
?}
}


void read_Mnist_Label(string filename, vector &vec)
{
?ifstream file (filename, 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;
? }
?}
}


string GetImageName(int number, int arr[])
{
?string str1, str2;


?for (int i = 0; i < 10; i++) {
? if (number == i) {
? ?arr[i]++;
? ?char ch1[10];?
? ?sprintf(ch1, "%d", arr[i]);?
? ?str1 = std::string(ch1);


? ?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;
? }
?}


?char ch2[10];
?sprintf(ch2, "%d", number);
?str2 = std::string(ch2);


?str2 = str2 + "_" + str1;


?return str2;
}


int main()
{
?//reference: http://eric-yuan.me/cpp-read-mnist/
?//test images and test labels
?//read MNIST image into OpenCV Mat vector
?string filename_test_images = "D:/Download/t10k-images-idx3-ubyte/t10k-images.idx3-ubyte";
?int number_of_test_images = 10000;
? ? vector vec_test_images;


? ? read_Mnist(filename_test_images, vec_test_images);


?//read MNIST label into int vector
? ? string filename_test_labels = "D:/Download/t10k-labels-idx1-ubyte/t10k-labels.idx1-ubyte";
? ? 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()) {
? cout<<"parse MNIST test file error"<? return -1;
?}


?//save test images
?int count_digits[10];
?for (int i = 0; i < 10; i++)
? count_digits[i] = 0;


?string save_test_images_path = "D:/Download/MNIST/test_images/";


?for (int i = 0; i < vec_test_images.size(); i++) {
? int number = vec_test_labels[i];
? string image_name = GetImageName(number, count_digits);
? image_name = save_test_images_path + image

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇CentOS 6.6安装Oralce 11gR2数据库 下一篇RHEL6.4 安装 MySQL 5.6.27

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: