http://stackoverflow.com/questions/34644779/how-to-use-the-network-trained-using-cnn-mnist-example-in-matconvnet

 

 

 

I've trained a cnn through the provided code cnn_mnist. After that, I tryed to to classify an image but I don't understand the reason why I got this error after the following code:

    [net, info] = cnn_mnist
net = 
    layers: {1x8 cell}
info = 
    train: [1x1 struct]
      val: [1x1 struct]
f=fopen(fullfile('.\data\mnist\', 't10k-images-idx3-ubyte'),'r') ;
x2=fread(f,inf,'uint8');
fclose(f) ;
x2=permute(reshape(x2(17:end),28,28,10e3),[2 1 3]) ;
im = x2(:,:,1); 
im = single(im);
 res = vl_simplenn(net,im);
Reference to non-existent field 'class'.
Error in vl_simplenn (line 163)
      res(i+1).x = vl_nnsoftmaxloss(res(i).x, l.class) ; 

-----------------------------------------------------

 

 

i'm not sure why your code won't run. I belive the problem is in your last layer. by default it is defined by softmaxloss and you should change it to softmax. try to add this line before calling vl_simplenn:

net.layers{end}.type = 'softmax';

anyway try this code that works for me (you should run it from main MatConvNet folder):

%%%%%%%%%%% run this lines only once at matlab startup
run matlab/vl_compilenn
run matlab/vl_setupnn
%%%%%%%%%%%

load('examples\mnist\data\mnist-bnorm\net-epoch-20.mat');  % files from training
net.layers{end}.type = 'softmax';
im = imread('Capture.jpg') ; % an image of a handwritten number
im = rgb2gray(im);
im_ = single(im) ; % note: 0-255 range
im_ = imresize(im_, net.meta.inputSize(1:2)+1) ;
load('examples\mnist\data\mnist-bnorm\imdb.mat')
im_ = im_ - images.data_mean;

% run the CNN
res = vl_simplenn(net, im_) ;

% show the classification result
scores = squeeze(gather(res(end).x)) ;
[bestScore, best] = max(scores) ;
best = best - 1;  % shift the score from 1:10 to 0:9
figure(1) ; clf ; imshow(im) ;
title(sprintf('The number is %s, score %.1f%%',...
net.meta.classes.name{best+1}-1, bestScore * 100)) ;
Posted by uniqueone
,