I was reading the following question:
How to read MNIST data in C++?
and there was some C++ code for reading the MNIST database. Upon trying it I found out that it worked fine until the place where it started reading the data.
which is the following code:
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));
//cout<<(int)temp<<" "; //printing the pixel in integer format
}
}
}
I tried printing out the integer value of the variable “temp” however I didn’t get the correct number for the pixels(all of them were zero).
I’m not sure what’s wrong there, each pixel takes one bytes space and then I convert it to an int and it doesn’t work. Why does this happen? thank you in advance
When working with the MNIST data set, I had the same problem that you had. I could read the labels, but the training and test set images were mostly bogus; the training set was filled almost entirely with 175, and the testing set was filled almost entirely with 0s (except for the first 6 images). Rebooting did not fix the problem and I was unable to determine why the file reading was not working correctly.
For anyone with this same problem, I would suggest instead using the data files located at http://cis.jhu.edu/~sachin/digit/digit.html. The data is already organized by number (no label/image association required), and the arrays of pixel values are simply encoded one after the other. Knowing that each array is 28×28 and that there are 1000 images for each number, you can easily write code to input the individual image arrays of pixel values.