gpt4 book ai didi

c++ - 如何在 C++ 中读取 MNIST 数据?

转载 作者:IT王子 更新时间:2023-10-28 23:28:08 28 4
gpt4 key购买 nike

我无法阅读 MNIST database of handwritten digits在 C++ 中。

它是二进制格式,我知道如何阅读,但我不知道 MNIST 的确切格式。

所以想请教一下看过MNIST数据的人,关于MNIST数据的格式,你们对如何用C++读取这些数据有什么建议吗?

最佳答案

int reverseInt (int i) 
{
unsigned char c1, c2, c3, c4;

c1 = i & 255;
c2 = (i >> 8) & 255;
c3 = (i >> 16) & 255;
c4 = (i >> 24) & 255;

return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
void read_mnist(/*string full_path*/)
{
ifstream file (/*full_path*/"t10k-images-idx3-ubyte.gz");
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)
{
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));

}
}
}
}
}

关于c++ - 如何在 C++ 中读取 MNIST 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8286668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com