How can I use a trained CNN to predict a new image label?

I was applying the following CNN fine-tuning example : https://www.mathworks.com/help/nnet/examples/transfer-learning-and-fine-tuning-of-convolutional-neural-networks.html
The example shows how to fine-tune a pre-trained CNN on letters to classify images of digits. Now i would like to use this new fine-tuned CNN on new images of digits that i have on my computer. How can I do that?

2 Comments

Has there been a solution to this since may? I'm facing the exact same problem here.

Sign in to comment.

 Accepted Answer

Hello Abich,
The example you linked shows how to train on a new set of images. I am not a neural networks expert, but if the output you are looking for is the same as the output from the retrained network (digits 0-9), I do not even think you need to replace the final three layers of the network. You can just create the image datastore for your new images, set any options you would like with "trainingOptions", and then call "trainNetwork".
Did you have some specific questions about a particular step?
-Cam

7 Comments

Thank you for answering. In fact, I just want to load one image, and call the network that is newly trained on digits, to predict its label.
I think you can just call "classify" with the image. It seems like it'll take images as well as image datastores as the "X" input.
Remember, the image needs to be the exact same dimensions as the images that the network was trained on. Size of input is very, very important in neural networks.
That's exactly what i tried to do. but it gives me an error. Here's what I typed : % Get size of existing image A. A = imread('knownimage.png'); [rowsA colsA numberOfColorChannelsA] = size(A); % Get size of existing image B. B = imread('myimage.PNG'); [rowsB colsB numberOfColorChannelsB] = size(B); % See if lateral sizes match. if rowsB ~= rowsA colsA ~= colsB % Size of B does not match A, so resize B to match A's size. B = imresize(B, [rowsA colsA]); end [Ypred,scores] = classify(netname,B)
where A is an image from the digitDataset the CNN was trained on and B is the image im trying to classify using the network.
In other words, I tried to match the size of my image with the size of the images from test dataset. But it still gives me the following error : Error using SeriesNetwork/predict (line 171) For an image input layer, the input data for predict must be a single image, a 4D array of images, or an imageDatastore with the correct size.
Does the "B" that is output from imresize have the same number of color channels as "A"? Also, it may be possible that the neural net is expecting a different image datatype (like double or uint8 rather than the other). Check that "A" and "B" (from imresize) are the same there too. If not, use im2double or im2uint8 to convert.
If that's still not the issue, I'd recommend contacting MathWorks Technical Support with your image and code. They'll better be able to look into what's going on.
-Cam
I just wanted to know did it work the classifier? I am having a problem trying to output the probability/predict the testing image.
Thanks and would appreciate reply.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!