http://www.ninadthakoor.com/2016/07/25/getting-started-with-matconvnet/

 

Available at: http://www.vlfeat.org/matconvnet/

My goal is to use this toolbox to classify cars into four classes: Sedan, Minivan, SUV and pickup. I already have the data from my prior work (http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6558793).

My plan is to first use transfer learning i. e. to use one of the deep networks pre-trained with imagenet data to extract features and use an SVM classifier to do the actual classification. I plan to do this with Matlab 2016a on Windows 10 PC equipped with GeForce GTX 960. I am aware that Matlab also has deep learning support in its Neural Network toolbox, I am going with MatConNet in hopes that it will stay more cutting edge.

The code below is derived from http://www.mathworks.com/company/newsletters/articles/deep-learning-for-computer-vision-with-matlab.html and http://www.vlfeat.org/matconvnet/quick/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
clear; close all; clc;
%Load the pre-trained net
net = load('imagenet-vgg-f.mat');
net = vl_simplenn_tidy(net) ;
 
%Remove the last layer (softmax layer)
net.layers = net.layers(1 : end - 1);
 
%% This deals with reading the data and getting the ground truth class labels
 
%All files are are inside the root
root = 'C:\Users\ninad\Dropbox\Ninad\Lasagne\data\c200x200\';
Files = dir(fullfile(root, '*.bmp'));
 
%Load the map which stores the class information
load MakeModels.mat
for i = 1 : length(Files)
    waitbar (i/ length(Files));
    % Read class from the map
    Q = Files(i).name(end - 18 : end - 4);
    Qout = MakeModels(Q);
    Files(i).class = Qout.Type;
     
    % Preprocess the data and get it ready for the CNN
    im = imread(fullfile(root, Files(i).name));
    im_ = single(im); % note: 0-255 range
    im_ = imresize(im_, net.meta.normalization.imageSize(1:2));
    im_ = bsxfun(@minus, im_, net.meta.normalization.averageImage);
 
    % run the CNN to compute the features
    feats = vl_simplenn(net, im_) ;
    Files(i).feats = squeeze(feats(end).x);
end
 
%% Classifier training
 
%Select training data fraction
 
trainFraction = 0.5;
randomsort = randperm(length(Files));
trainSamples = randomsort(1 : trainFraction * length(Files));
testSamples = randomsort(trainFraction * length(Files)+1 : end);
 
Labels = [Files.class];
Features = [Files.feats];
 
trainingFeatures = Features( :, trainSamples);
trainingLabels = Labels( :, trainSamples);
 
classifier = fitcecoc(trainingFeatures', trainingLabels);
 
%% Carry out the validation with rest of the data
testFeatures = Features( :, testSamples);
testLabels = Labels( :, testSamples);
 
predictedLabels = predict(classifier, testFeatures');
 
confMat = confusionmat(testLabels, predictedLabels);
 
% Convert confusion matrix into percentage form
confMat = bsxfun(@rdivide,confMat,sum(confMat,2))
 
% Display the mean accuracy
mean(diag(confMat))

 

'Deep Learning > Matconvnet' 카테고리의 다른 글

How to generate confusion matrix in MatConvNet  (0) 2017.05.15
assertition failed vl_simplenn.m  (0) 2017.05.13
Posted by uniqueone
,

https://groups.google.com/forum/#!topic/matconvnet/m2BPIPVxB5o

 

 

I've used imagenet pretrained model for training my dataset. Here is my code:


    net = load('YourTrainedModel.mat');

 

    mdl.classes = net.net_classes;

    mdl.layers = net.net_layers;

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

 

    ds=load('YourDataSet.mat');

 

    trainSet = [ones(1,numel(ds.y2)) 3*ones(1,numel(ds.y2))];

    data = single(reshape(cat(4,ds.x1,ds.x2),224,224,3,[]));

    dataMean = mean(data(:,:,:,trainSet == 1), 4);

 

    testSize = size(ds.y2,2);

   

    confMat = zeros(numOfClasses, numOfClasses);

 

    i=1;

    while i<=testSize

        im=ds.x2(:,:,:,i);

        im_ = single(im);

        im_=im_-dataMean;

        res = vl_simplenn(mdl, im_) ;

        scores1= squeeze(gather(res(end).x)) ;

        [bestScore, best] = max(scores);

        confMat(ds.y2(1,i),best) = confMat(ds.y2(1,i),best)+1;

        i = i+1;

    end

 

    for i=1:numOfClasses

        s = sum(confMat(i,:));

        for j=1:numOfClasses

                 confMat(i,j) = confMat(i,j)/s*100;

        end

    end


 

Posted by uniqueone
,

https://github.com/vlfeat/matconvnet/issues/34

 

 

StevenLOL commented on Jan 1 2015

Hi,

After run cnn_mnist.m, I have few net-epoch-n.mat models.

To predict one image on mnist , following the example in http://www.vlfeat.org/matconvnet/pretrained

When calling

net=load('./data/mnist-baseline/net-epoch-5.mat');
res=vl_simplenn(net.net, im); % im is just one mnist image whose size is 28*28

I got the error:

Error using vl_nnsoftmaxloss (line 42) Assertion failed.
Error in vl_simplenn (line 164)
res(i+1).x = vl_nnsoftmaxloss(res(i).x, l.class) ;

Contributor
lenck commented on Jan 2 2015

Hi, when you train a model, as a last layer there is usually the softmaxloss which is a softmax followed by logistic loss - the loss which is being optimized. But in order to compute the loss, it needs the ground truth in net.layers{end}.class which is passed as the second parameter in vl_simplenn to vl_nnsoftmaxloss.

However, in general when you want to obtain only class probabilities, you simply change the last layer type to:

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

And now your call of vl_simplenn should work...

Thanks, the best CNN toolkit ever.

Posted by uniqueone
,