rasmusbergpalm/DeepLearnToolbox

Predicting one image

RafaRuiz opened this issue · 4 comments

Hello, I'm really new with CNN.

I've got my Network trained with N images and now I would like to ask for 1 image and see what label is predicted. How could I make this?

Thank you in advance.

For example, if 'cnn' is your trained model, and 'img' is your image, do like this:

net = cnnff(cnn,img);
[~, h] = max(net.o);

Then 'h' will be the predict label.

I tried this with the following code:

% after running test_example_CNN.m
img = squeeze(test_x(:,:,1));
net = cnnff(cnn,img);

And got the following error:

Matrix dimensions must agree.
Error in cnnff (line 11)
z = zeros(size(net.layers{l - 1}.a{1}) - [net.layers{l}.kernelsize - 1 net.layers{l}.kernelsize - 1 0]);

Any suggestions on how to fix this?

The problem is because cnnff is looking for a 3rd dimension, and the single image case yields 2 dimensions only. As far as I can see, you can't artificially add a singleton 3rd dimension, so I figured out a hacky fix which involves changing two lines in cnnff to handle single image cases:

    if size(x,3) == 1
         z = zeros([size(net.layers{l - 1}.a{1}) 1] - [net.layers{l}.kernelsize - 1 net.layers{l}.kernelsize - 1 0]);
    else
         z = zeros(size(net.layers{l - 1}.a{1}) - [net.layers{l}.kernelsize - 1 net.layers{l}.kernelsize - 1 0]);
    end

and

    if size(x,3) == 1            
        net.fv = [net.fv; reshape(net.layers{n}.a{j}, sa(1) * sa(2), 1)];
    else
        net.fv = [net.fv; reshape(net.layers{n}.a{j}, sa(1) * sa(2), sa(3))];
    end
Sehla commented

Hello,
I am new new with CNN. My project is to train the CNN with a image dataset and test the prediction with new image. I already download the caltech-101image dataset but how can i launch the training step?
Thanks