% ================================================
% Adaboost Experiment
% ================================================

clear all

mex adaboostLearning.c
mex adaboostTesting.c
mex Cmax.c

addpath('../');

templateSize = [120 150];
numGridPoint = 50;

subsampleStep = 2; % sub sample rate that weak classifiers go over
numWeakClassifiers = 150;
scale = .7;  % scale of Gabors
norient = 15;  % number of orientations
allfilter = makefilter(scale, norient);  % generate Gabor filters 
h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
Lrange = 3; % allowed shifting in location (times subsampleStep pixels)
Orange = 1; % allowed shifting in orientation

for iRun = 1:25
    tic;
    load(sprintf('../RandomSplits/split%d.mat',iRun),'posTrainImages','negTrainImages');
    posTrainImagePaths = cell(1,length(posTrainImages));
    for i = 1:length(posTrainImages)
        posTrainImagePaths{i} = sprintf('../posAll/%s',posTrainImages{i});
    end
    posMaps = getGaborMaps(posTrainImagePaths,templateSize,allfilter); % compute Sum1 maps
    nimage = length(posTrainImages);
    Cmax(nimage, norient, posMaps, templateSize(1), templateSize(2), Lrange, Orange, subsampleStep); % compute Max1 maps
    
    negTrainImagePaths = cell(1,length(negTrainImages));
    for i = 1:length(negTrainImages)
        negTrainImagePaths{i} = sprintf('../negAll/%s',negTrainImages{i});  
    end
    negMaps = getGaborMaps(negTrainImagePaths,templateSize,allfilter); % compute Sum1 maps
    nimage = length(negTrainImages);
    Cmax(nimage, norient, negMaps, templateSize(1), templateSize(2), Lrange, Orange, subsampleStep); % compute Max1 maps
    
    % initial data weights: balanced

    dataWeights = 0.5 * [ ones(1, length(posTrainImages)) / length(posTrainImages), ...
        ones(1,length(negTrainImages)) / length(negTrainImages) ];
    
    [selectedXs selectedYs selectedOs selectedThresholds selectedPolars lambdas]...
        = adaboostLearning(posMaps,negMaps,...
        dataWeights,numGridPoint,h,subsampleStep,numWeakClassifiers);

    disp(['training time: ' num2str(toc) ' seconds']);
    clear posTrainImages
    clear negTrainImages
    clear posMaps
    clear negMaps
    
    % Testing
    tic
    load(sprintf('../RandomSplits/split%d.mat',iRun),'posTestImages','negTestImages');
    posTestImagePaths = cell(1,length(posTestImages));
    for i = 1:length(posTestImages)
        posTestImagePaths{i} = sprintf('../posAll/%s',posTestImages{i});
    end
    posMaps = getGaborMaps(posTestImagePaths,templateSize,allfilter); % compute Sum1 maps
    nimage = length(posTestImages);
    Cmax(nimage, norient, posMaps, templateSize(1), templateSize(2), Lrange, Orange, subsampleStep); % compute Max1 maps
    scoresPos = adaboostTesting(posMaps,selectedXs,selectedYs,selectedOs,selectedThresholds,selectedPolars,lambdas);
    clear posMaps
    
    negTestImagePaths = cell(1,length(negTestImages));
    for i = 1:length(negTestImages)
        negTestImagePaths{i} = sprintf('../negAll/%s',negTestImages{i});
    end
    negMaps = getGaborMaps(negTestImagePaths,templateSize,allfilter); % compute Sum1 maps
    nimage = length(negTestImages);
    Cmax(nimage, norient, negMaps, templateSize(1), templateSize(2), Lrange, Orange, subsampleStep); % compute Max1 maps
    scoresNeg = adaboostTesting(negMaps,selectedXs,selectedYs,selectedOs,selectedThresholds,selectedPolars,lambdas);
    clear negMaps
    
    % Evaluate strong classifiers with 1,2,3... weak classifiers
    [ap testAUC tp fp] = evaluate(scoresPos,scoresNeg);
    
    save(sprintf('out_adaboost_run%d.mat',iRun),'testAUC','tp','fp','ap',...
        'selectedXs','selectedYs','selectedOs','selectedThresholds',...
        'selectedPolars','lambdas','scoresPos','scoresNeg','numGridPoint','templateSize');
    
    disp(['testing time: ' num2str(toc) ' seconds']);
end

