Little
Little

Reputation: 3477

how to generate an image in the format of the mnist database?

I need to make a handwritten image to be tested with a neural network in Matlab. When I see the data contained in the training images from the MNIST I see that it is an array of different gray scales like:

Columns 343 through 351

         0   -0.0240    0.4002    0.6555    0.0235   -0.0062         0         0         0

  Columns 352 through 360

         0         0         0         0         0         0         0         0         0

  Columns 361 through 369

         0         0         0   -0.0079    0.1266    0.3272   -0.0233    0.0005

corresponding to a 20x20 image, unrolled into a 1*400 dimensional array.

I have downloaded an image in jpeg format and did the following:

im=imread('image.jpg');
gi=rgb2gray(im);
gi=gi(:);
gi=gi';

that generates me an array gi that says <1*400 uint8>, the last part of uint8 does not appear in the MNIST samples when I put it in Matlab. When I check it up my array it appear the following values:

Columns 289 through 306

58  105  128  133  142  131   76   21    1    0    3    0    2    4   17   12    7    0

  Columns 307 through 324

    1   15   42   75   97  105   98   73   31    4    1    0    0    0    0    2    4    3

  Columns 325 through 342

    0    0    1    4   21   37   55   59   46   26    9    0    0    0    0    0    0    0

  Columns 343 through 360

    1    1    0    0    0    1    7   14   21   21   14    5    0    0    0    0    0    0

  Columns 361 through 378

    0    0    0    0    0    0    0    0    0    1    2    1    0    0    0    2    0    0

when I visualize them all is fine, but when I want to run my program the following message appears:

??? Error using ==> mtimes
MTIMES is not fully supported for integer classes. At least one input must be scalar.

Error in ==> predict at 15
h1 = sigmoid([ones(m, 1) X] * Theta1');

Error in ==> ex4 at 241
pred = predict(Theta1, Theta2, gi);

situation that does not occur when I test my program even with one random sample ofc the MNIST data; any help?

Upvotes: 2

Views: 4887

Answers (1)

Jvmann
Jvmann

Reputation: 11

You could try something like this:

imfile = 'image.jpg';
im = double(rgb2gray(imread(imfile))); % double and convert to grayscale
im = imresize(im,[20,20]);  % change to 20 by 20 dimension
im = im(:); % unroll matrix to vector
im = im./max(im); 

Note the MNIST dataset is intended to be a good dataset to require minimal preprocessing and the images were actually originally black and white (bilevel) whereas you are using color image. Also they do normalisation and other preprocessing to make nice 28 by 28 image dataset, my brief snippet of code above is unlikely to be anywhere near as good as MNIST dataset and is just intended to attempt to fix your error.

Your specific error is likely because you don't use double(). You may also get further errors because your code needs right dimensions, which can be achieved using imresize.

More information on MNIST dataset here: http://yann.lecun.com/exdb/mnist/

Upvotes: 1

Related Questions