# adaboostLearn

## PURPOSE

LearnAdaboostTemplate - Learns an adaboost "template", i.e. strong classifier.

## SYNOPSIS

function adaboostLearn(category,posTrainImagePaths,negTrainImagePaths,numWeakClassifiers,templateSize,scale)

## DESCRIPTION

     LearnAdaboostTemplate - Learns an adaboost "template", i.e. strong classifier.

     category: the category of training images
     imagepaths: path of training images
     scale: scale of Gabors

## CROSS-REFERENCE INFORMATION

This function calls:

- displaySelectedFilters displaySelectedFilters - For display of selected weak classifiers by adaboost.
- getGaborMaps getGaborMaps - Compute S1 maps from images whose paths are specified in .
- makefilter makefilter - Make Gabor filters at fixed scale with norient orientations

This function is called by:

- LearnAdaboostTemplate LearnAdaboostTemplate - Learns an adaboost "template", i.e. strong classifier.

## SOURCE CODE

    0001 function adaboostLearn(category,posTrainImagePaths,negTrainImagePaths,numWeakClassifiers,templateSize,scale)
    0002 % LearnAdaboostTemplate - Learns an adaboost "template", i.e. strong classifier.
    0003 %
    0004 % category: the category of training images
    0005 % imagepaths: path of training images
    0006 % scale: scale of Gabors
    0007
    0008 tic
    0009 addpath('../common');
    0010 % negTrainImagePaths = negTrainImagePaths(1:50); !!! remember to remove this
    0011 negTrainImagePaths = negTrainImagePaths(1:100);
    0012
    0013 if nargin < 4
    0014     numWeakClassifiers = 30;
    0015 end
    0016
    0017 if nargin < 5
    0018     templateSize = [120 120];
    0019 end
    0020
    0021 if nargin < 6
    0022     scale = .7;
    0023 end
    0024
    0025 numGridPoint = 50;
    0026 subsampleStep = 2; % sub sample rate that weak classifiers go over
    0027 norient = 16;  % number of orientations
    0028 [allfilter allsymbol] = makefilter(scale, norient);  % generate Gabor filters
    0029 h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
    0030 Lrange = 6; % allowed shifting in location (times subsampleStep pixels)
    0031 Orange = 1; % allowed shifting in orientation
    0032
    0033
    0034 posMaps = getGaborMaps(posTrainImagePaths,templateSize,allfilter); % compute Sum1 maps
    0035 %     Cmax(nimage, norient, posMaps, templateSize(1), templateSize(2), Lrange, Orange, subsampleStep); % compute Max1 maps
    0036 n = length(posTrainImagePaths);
    0037 M1TraceMap = cell(n,norient);
    0038 subsampleM1 = 1;
    0039 for i = 1:n
    0040     [posMaps(i,:) M1TraceMap(i,:) rowShifts colShifts oriShifts] = ...
    0041         CcomputeMAX1(norient,posMaps(i,:),double(Lrange)/(2*h+1),Orange,h*2+1,subsampleM1);
    0042 end
    0043
    0044 negMaps = getGaborMaps(negTrainImagePaths,templateSize,allfilter); % compute Sum1 maps
    0045 n = length(negTrainImagePaths);
    0046 for i = 1:n
    0047     negMaps(i,:) = CcomputeMAX1(norient,negMaps(i,:),double(Lrange)/(2*h+1),Orange,h*2+1,subsampleM1);
    0048 end
    0049
    0050 % initial data weights: balanced
    0051 dataWeights = 0.5 * [ ones(1, length(posTrainImagePaths)) / length(posTrainImagePaths), ...
    0052     ones(1,length(negTrainImagePaths)) / length(negTrainImagePaths) ];
    0053
    0054 disp(sprintf('mex-C adaboost learning ----------- #pos: %d, #neg: %d',length(posTrainImagePaths),length(negTrainImagePaths)));
    0055 [selectedXs selectedYs selectedOs selectedThresholds selectedPolars lambdas]...
    0056     = adaboostLearning(posMaps,negMaps,...
    0057     dataWeights,numGridPoint,h,subsampleStep,numWeakClassifiers);
    0058
    0059 scoresPos = adaboostTesting(posMaps,selectedXs,selectedYs,selectedOs,selectedThresholds,selectedPolars,lambdas);
    0060 scoresNeg = adaboostTesting(negMaps,selectedXs,selectedYs,selectedOs,selectedThresholds,selectedPolars,lambdas);
    0061
    0062 disp(['training time: ' num2str(toc) ' seconds']);
    0063 clear posTrainImages
    0064 clear negTrainImages
    0065 clear negMaps
    0066
    0067 % obtain the symbolic template and deformed symbolic templates
    0068 n = length(posTrainImagePaths);
    0069 symbolicTemplate = displaySelectedFilters(allsymbol,lambdas,selectedPolars,selectedXs,selectedYs,selectedOs,templateSize);
    0070 deformedTemplate = cell(n,1);
    0071 for i = 1:n
    0072     xlist = zeros(numWeakClassifiers,1);
    0073     ylist = zeros(numWeakClassifiers,1);
    0074     olist = zeros(numWeakClassifiers,1);
    0075     on_off_vec = zeros(numWeakClassifiers,1);
    0076     for j = 1:numWeakClassifiers
    0077         x = selectedXs(j);
    0078         y = selectedYs(j);
    0079         o = selectedOs(j);
    0080         shiftInd = M1TraceMap{i,o+1}(x+1,y+1)+1;
    0081         if shiftInd <= 0 || shiftInd > length(rowShifts{1})
    0082             disp(shiftInd);
    0083         end
    0084         xlist(j) = x + rowShifts{1}(shiftInd);
    0085         ylist(j) = y + colShifts{1}(shiftInd);
    0086         olist(j) = o + oriShifts{1}(shiftInd);
    0087         while olist(j) < 0
    0088             olist(j) = olist(j) + norient;
    0089         end
    0090         while olist(j) >= norient
    0091             olist(j) = olist(j) - norient;
    0092         end
    0093         on_off_vec(j) = selectedPolars(j) == (posMaps{i,olist(j)+1}(xlist(j)+1,ylist(j)+1) > selectedThresholds(j));
    0094     end
    0095
    0096     deformedTemplate{i} = displaySelectedFilters(allsymbol,lambdas(:) .* double(on_off_vec(:)),selectedPolars,xlist,ylist,olist,templateSize);
    0097 end
    0098
    0099 clear posMaps
    0100 rootFolder = '..';
    0101 mkdir(sprintf('%s/result/%s',rootFolder,category));
    0102 save(sprintf('%s/result/%s/adaboost_model.mat',rootFolder,category),...
    0103     'selectedXs','selectedYs','selectedOs','selectedThresholds',...
    0104     'selectedPolars','lambdas','scoresPos','scoresNeg','numGridPoint',...
    0105     'templateSize','symbolicTemplate','deformedTemplate');
    0106
    0107
    0108