MNIST是一个标准的手写字符测试集。
Mnist数据集对应四个文件:
train-images-idx3-ubyte: training set images
train-labels-idx1-ubyte: training set labels
t10k-images-idx3-ubyte: test set images
t10k-labels-idx1-ubyte: test set labels
训练数据集包含60000幅图片,测试集包含10000幅图片。
文件格式:
TRAINING SET LABEL FILE (train-labels-idx1-ubyte):
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (MSB first)
0004 32 bit integer 60000 number of items
0008 unsigned byte ?? label
0009 unsigned byte ?? label
........ xxxx unsigned byte ?? label
The labels values are 0 to 9.
TRAINING SET IMAGE FILE (train-images-idx3-ubyte):
[offset] [type] [value] [description]0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 60000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
Pixels are organized row-wise. Pixel values are 0 to 255. 0 means background (white), 255 means foreground (black).
TEST SET LABEL FILE (t10k-labels-idx1-ubyte):
[offset] [type] [value] [description]0000 32 bit integer 0x00000801(2049) magic number (MSB first)
0004 32 bit integer 10000 number of items
0008 unsigned byte ?? label
0009 unsigned byte ?? label
........
xxxx unsigned byte ?? label
The labels values are 0 to 9.
TEST SET IMAGE FILE (t10k-images-idx3-ubyte):
[offset] [type] [value] [description]0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 10000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
Pixels are organized row-wise. Pixel values are 0 to 255. 0 means background (white), 255 means foreground (black).
#include <iostream> #include <fstream> #include <string> #include <vector> 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_Label(string filename, vector<double>&labels) { ifstream file(filename, 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); cout << "magic number = " << magic_number << endl; cout << "number of images = " << number_of_images << endl; for (int i = 0; i < number_of_images; i++) { unsigned char label = 0; file.read((char*)&label, sizeof(label)); labels.push_back((double)label); } } } void read_Mnist_Images(string filename, vector<vector<double>>&images) { 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; 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); cout << "magic number = " << magic_number << endl; cout << "number of images = " << number_of_images << endl; cout << "rows = " << n_rows << endl; cout << "cols = " << n_cols << endl; for (int i = 0; i < number_of_images; i++) { vector<double>tp; 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)); tp.push_back(image); } } images.push_back(tp); } } } int main() { /* vector<double>labels; read_Mnist_Label("t10k-labels.idx1-ubyte", labels); for (auto iter = labels.begin(); iter != labels.end(); iter++) { cout << *iter << " "; } */ vector<vector<double>>images; read_Mnist_Images("t10k-images.idx3-ubyte", images); for (int i = 0; i < images.size(); i++) { for (int j = 0; j < images[0].size(); j++) { cout << images[i][j] << " "; } } return 0; }