C++读mnist数据

2020-12-25 13:11

阅读:586

标签:col   ever   push   pre   names   eve   end   ima   void   

#include 
#include 
#include string>
#include 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, vectordouble> &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 //        cout 


        for (int i = 0; i ) {
            unsigned char label = 0;
            file.read((char *) &label, sizeof(label));
            labels.push_back((double) label);
        }

    }
}

void read_Mnist_Images(string filename, vectordouble>> &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 //        cout //        cout //        cout 

        for (int i = 0; i ) {
            vectordouble> tp;
            for (int r = 0; r ) {
                for (int c = 0; c ) {
                    unsigned char image = 0;
                    file.read((char *) &image, sizeof(image));
                    tp.push_back(image);
                }
            }
            images.push_back(tp);
        }
    }
}

//int main01() {
//    vectorlabels;
//    read_Mnist_Label("train-labels.idx1-ubyte", labels);
//    for (auto iter = labels.begin(); iter != labels.end(); iter++)
//    {
//        cout //    }
//
//    vector> images;
//    read_Mnist_Images("train-images.idx3-ubyte", images);
//    cout //    for (int i = 0; i //        for (int j = 0; j //            cout //        }
//    }
//
//    return 0;
//}

 

C++读mnist数据

标签:col   ever   push   pre   names   eve   end   ima   void   

原文地址:https://www.cnblogs.com/niubidexiebiao/p/13039478.html


评论


亲,登录后才可以留言!