vlfeat / matconvnet

MatConvNet: CNNs for MATLAB
Other
1.4k stars 753 forks source link

Error in training CNN for binary classification #849

Open Addhi86 opened 7 years ago

Addhi86 commented 7 years ago

Hello Just to get familiar with the CNN I have prepared the code for the binary classification Aircraft (760 images) or non-aircraft (750).

Here is my code

Npos =  numel(possitive_regions);
Nneg =  numel(negative_regions);

Npos_train = floor(0.25* Npos); 
Npos_val = floor(0.25*Npos);
Npos_test = floor(0.50*Npos);

Nneg_train = floor(0.25*Nneg); 
Nneg_val = floor(0.25*Nneg);
Nneg_test = floor(0.50*Nneg);

for i=1:Npos
    im= imresize (single(possitive_regions{i,:}),[50,50]);
    imdb.images.data(:,:,:, i) = im;
    imdb.images.labels(i) = 1;

        if i <= Npos_train
            imdb.images.set(i) = 1;
        elseif i <= Npos_train+Npos_val
            imdb.images.set(i) = 2;
        else
            imdb.images.set(i) = 3;
        end
end

% for negative samples
for i=1:Nneg
    im= imresize (single(negative_regions{i,:}),[50,50]);
    imdb.images.data(:,:,:, i+Npos) = im;
    imdb.images.labels(i+Npos) = 0;

        if i <= Nneg_train
            imdb.images.set(Npos+i) = 1;
        elseif i <= Nneg_train+Nneg_val
              imdb.images.set(Npos+i) = 2;
        else
              imdb.images.set(Npos+i) = 3;                       
        end
end
imdb.meta.sets = {'train', 'val', 'test'} ;
%% Network
opts.inputSize = [50 50 3] ;
opts.train.batchSize = 50;
opts.train.numEpochs = 10;
opts.train.continue = true;
% opts.train.useGpu = false;
opts.train.learningRate = 0.01;
% opts = vl_argparse(opts, []);
f = 0.01;

f=1/100 ;
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv','weights', {{f*randn(5,5,3,20, 'single'), zeros(1, 20, 'single')}},'stride', 1,'pad', 0);
net.layers{end+1} = struct('type', 'pool','method', 'max', 'pool', [2 2], 'stride', 2, 'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', 'weights', {{f*randn(5,5,20,50, 'single'),zeros(1,50,'single')}}, 'stride', 1, 'pad', 0) ;
net.layers{end+1} = struct('type', 'pool', 'method', 'max', 'pool', [2 2], 'stride', 2, 'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', 'weights', {{f*randn(4,4,50,500, 'single'),  zeros(1,500,'single')}}, 'stride', 1, 'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'conv', 'weights', {{f*randn(1,1,500,10, 'single'), zeros(1,10,'single')}}, 'stride', 1, 'pad', 0) ;
net.layers{end+1} = struct('type', 'softmaxloss') ;
disp( 'Net is Ok.' );
% [net, info] = trainfn(net, imdb, getBatch(opts), 'expDir', opts.expDir, net.meta.trainOpts,  opts.train, 'val', find(imdb.images.set == 3)) ;

[net, info] = cnn_train( net, imdb, @getBatch, opts.train, 'val', find( imdb.images.set == 2 ) ) ;

The Network part I took it from the MINST example. I have saved this file and getBatch function in the MatConvNet example folder. When I run the cnn_train i am getting this output and error.

image

Anyone, please help me to resolve this error.

SWICURays commented 7 years ago

Hi Addhi, the error you get is that you have probably not compiled the MEX files. The mex files are not compiled when you clone the repository.

Have a nice weekend, Johan

Addhi86 commented 7 years ago

@darthascaria Thanks yes I have figured that out. now from the above network if I choose learning rate 1e-2. I am getting this output image

and i reduce the error rate more image please help me to tell which learning rate is good and what these graphs are showing?

saskra commented 7 years ago

As there is no use for a top5err on two classes, first of all you have to change "opts.errorFunction" in cnn-Train.m to 'binary'.