gusucode.com > Matlab脸部识别程序源码 > code/code/vj_train.m

    function [adaboost_train accuracy] = vj_train(datatype,data,showfeatures,T)
%% [adaboost_train accuracy] = vj_train(datatype,data,showfeatures,T) 
%
%       Nathan Sutter, Fall 2007
%       Last Updated, 11/14/07
%--------------------------------------------------------------------------
%   Inputs:
%--------------------------------------------------------------------------
%   datatype    :   1 for actual image data, 2 for precomputed image
%                   features.
%   data        :   If datatype is set to 1 : 
%                   -------------------------
%                   data.images = cell of training images, data.label = -1/1
%                   for negative/positive.
%
%                   If datatype is set to 2 : 
%                   -------------------------
%                   precomp_data.imagefeatures = cell array of image
%                   features which is a cell array which has (in each
%                   element) a vector of features for each image.
%
%                   precomp_data.numfeat = number of features per image.
%                   precomp_data.labels = labels for the images.
%
%   showfeatures:   if you'd like to show the features chosen at the end of
%                   each iteration along with a training accuracy plot, set
%                   this to 1.
%
%   T           :   number of weak learners
%              
%--------------------------------------------------------------------------
%  Outputs:
%--------------------------------------------------------------------------
%  adaboost_train   :   Structure containing all sufficient statistics
%                       for perceptrons, as well as Adaboost learned parameters
%  accuracy         :   Vector of accuracy, Tx1
%--------------------------------------------------------------------------

%%--LOAD PRECOMPUTED FEATURES OR COMPUTE FEATURES FOR INPUT IMAGES---------
if datatype == 1
    data.images = uint8(data.images);
    images = data.images;
    labels = data.labels;
    numtotal = length(images);        %need a cell of training data here
    all_features = cell(numtotal,1);

    for i=1:numtotal
        if mod(i,10) == 0
            fprintf('Extracting from image %d\n',i);
        end
        [features] = extract_features(images(:,:,i),1,0);   
        numfeat = length(features);
        all_features{i} = features;
    end
    precomp_data.imagefeatures = all_features;
    precomp_data.numfeat = numfeat(1);
    precomp_data.labels = labels;
    save precomputed_features.mat precomp_data 
elseif datatype == 2
    fprintf('Loading pre-computed features...');
    load('../data/precomputed_features.mat');
    all_features = precomp_data.imagefeatures;
    numfeat = precomp_data.numfeat;
    labels = precomp_data.labels;
    [numtotal d] = size(all_features);
    fprintf('done\n');
end
m = length(find(labels == 1));           %find number of positive examples
l = length(find(labels == -1));          %find number of negative examples

numpos = m;
numneg = l;
numtotal = numpos+numneg;
w = zeros(T,numtotal);            %weights for each learner, for each image  

for i=1:numtotal                  %initialize the weights
    if labels(i) == 1
        w(1,i) = 1/(2*m);
    elseif labels(i) == -1
        w(1,i) = 1/(2*l);
    end
end

alllabels = [];

%--------------------------------------------------------------------------
numfeat = numfeat(1); 

y_weak = zeros(T,numfeat,numtotal);
fprintf('Training one classifier per feature for %d rounds\n----------------------------\n',T);
for t=1:T
    %normalize weights
    w(t,:) = w(t,:)./sum(w(t,:));
    epsilon = zeros(numfeat,1);
    fprintf('Boosting iteration %d:\n',t);

    %-TRAIN ONE CLASSIFIER FOR EACH FEATURE OVER ALL IMAGES
    for j=1:numfeat
        curdata = zeros(numtotal,1);
        labels = zeros(numtotal,1);
        for k=1:numtotal
            image_features = all_features{k};               %get the kth set of features (features from kth image)
            curdata(k) = image_features(j);                 %get the jth feature from the kth set of features.
            %labels(k) = data.labels(k);
        end
        if mod(j,1000) == 0
            fprintf('...Training classifier %d\n',j);
        end

        curdata = [curdata ones(numtotal,1)];                                       %pad with ones for the perceptron
        [weights,mse,acc] = learn_perceptron(curdata,labels,1e-9,1e-1,1,1234,0,0);  %learn on the data
        
        weak_learner(t,j).weights = weights;                                        %do book keeping for the learner stats
        weak_learner(t,j).mse = mse;
        weak_learner(t,j).acc = acc;
        weak_learner(t,j).classifications = perceptron(weights,curdata);
        
        for k=1:numtotal                                                            %update errors
            epsilon(j) = epsilon(j) + w(t,k)*abs(weak_learner(t,j).classifications(k) - labels(k));
        end
    end

    %3. choose the classifier with the lowest error
    [error(t) idx] = min(epsilon);
    %keyboard
    best_weak.learner(t) = weak_learner(t,idx);
    best_weak.feature(t) = idx;
    beta(t) = error(t)/(1-error(t));
    alpha(t) = log(1/(beta(t)+eps)); %log(1) - log(beta(t));
    
    if t+1 <= T
        for i=1:numtotal
            if (labels(i) == best_weak.learner(t).classifications(i))
                e_i = 1;
            else
                e_i = 0;
            end
            w(t+1,i) = w(t,i)*beta(t)^(1-e_i);                                      %update weights for data cases
            if e_i == 0

            end
        end
    end
    
    h = zeros(numtotal,1);                                                          %vector for global classifications
    for i=1:numtotal                                                                %do classification for each based on the ensemble
        insum = 0;
        for z=1:t
            insum = insum + alpha(z)*best_weak.learner(z).classifications(i);
        end
        if insum >= .5*sum(alpha)
            h(i) = 1;
        else
            h(i) = -1;
        end
    end
    
    numincorrect = length(find(h ~= labels));
    accuracy(t) = (numtotal-numincorrect)/numtotal;
    fprintf('Accuracy for round %d is %2.2f\n',t,accuracy(t));
    plot(1:t,accuracy)
    xlabel('Number of weak learners');
    ylabel('Accuracy in percent');
    pause(.05);
end
adaboost_train.weak_learners = best_weak;
adaboost_train.acc = accuracy;
adaboost_train.alphas = alpha;

save adaboost_train.mat adaboost_train;                                             %save these to disk as well as return them