% LOCAL LEARNING OF ACTIVE BASIS
%% Load in exponential model, mex C codes, and set parameters
% ExponentialModel; % this line is not needed if 'storedExponentialModel' exists
clear; close all; 
load 'storedExponentialModel'; % load in exponential model 
mex CsharedSketch.c;  % learning from aligned images 
mex CgetSUM2.c; % code for finding nearest neighbors, without inhibition, so very fast
mex CgetMAX1.c; % local maximum pooling for MAX1 maps
mex Csigmoid.c; % sigmoid transformation 
mex Ccopy.c; % copy around detect location

epsilon = .1; % allowed correlation between selected Gabors 
subsample = 1; % subsample in computing MAX1 and SUM2 maps   
locationShiftLimit = 2; % shift in normal direction = locationShiftLimit*subsample pixels
orientShiftLimit = 1; % shift in orientation
Correlation = CorrFilter(allFilter, epsilon); % correlation between filters 
numNeighbor = 5; % number of nearest neighbors including self
numElement = 45; % number of Gabors in active basis   
numIteration = 3;  % number of iterations
%% Load in training images
%imageOption = 1; resizeFactor = .6; % resize images to specified ratio, and use the common upper-left portion 
%imageOption = 2; sizex = 120; sizey = 120; % resize images to specified common height and width
imageOption = 3; sizey = 120; % resize images to have common width without changing their aspect ratios, and use the common horizontal central line
imageFolder = 'positiveImage'; % folder of training images  
imageName = dir([imageFolder '/*.jpg']);
numImage = size(imageName, 1); % number of training images 
I = cell(1, numImage); 
LoadImage;  
allSizex = zeros(1, numImage) + sizex; 
allSizey = zeros(1, numImage) + sizey; 
%% Compute SUM1 maps and MAX1 maps
disp(['start filtering']); tic
if (imageOption == 3) 
    [SUM1map, Ifill] = ApplyFilterfftFill(I, allFilter, localOrNot, localHalfx, localHalfy, thresholdFactor); % SUM1 maps by Gabor filtering
else 
    SUM1map = ApplyFilterfftSame(I, allFilter, localOrNot, localHalfx, localHalfy, -1, thresholdFactor);% SUM1 maps by Gabor filtering
end
disp(['filtering time: ' num2str(toc) ' seconds']);
Csigmoid(numImage, allSizex, allSizey, numOrient, saturation, SUM1map);
MAX1map = cell(numImage, numOrient);
for (img = 1:numImage)
    for (orient = 1:numOrient) 
        MAX1map{img, orient} = single(zeros(floor(sizex/subsample), floor(sizey/subsample)));
    end
end
CgetMAX1(numImage, allSizex, allSizey, numOrient, locationShiftLimit, orientShiftLimit, subsample, ...
         SUM1map, MAX1map);           
SUM1mapLearn = cell(numNeighbor, numOrient); 
for (img = 1:numNeighbor)
    for (orient = 1:numOrient)
        SUM1mapLearn{img, orient} = single(zeros(sizex, sizey)); 
    end
end
%% Prepare output variables for learning
selectedOrient = zeros(1, numElement);  % orientation and location of selected Gabors
selectedx = zeros(1, numElement); 
selectedy = zeros(1, numElement); 
selectedlambda = zeros(1, numElement); % weighting parameter for scoring template matching
selectedLogZ = zeros(1, numElement); % normalizing constant
commonTemplate = single(zeros(sizex, sizey)); % template of active basis 
deformedTemplate = cell(1, numNeighbor); % templates for training images 
for (img = 1 : numNeighbor)
    deformedTemplate{img} = single(zeros(sizex, sizey));  
end
SUM2score = zeros(numImage, 1); % template matching scores for training images 
allSUM2score = zeros(numImage, numImage); % matching score starting from each image
meanSUM2score = zeros(numImage, 1); % average matching score starting from each image
neighbor = zeros(numImage, numNeighbor); % neighbors of each starting image
allSelectedOrient = zeros(numImage, numElement);  % orientation and location of selected Gabors
allSelectedx = zeros(numImage, numElement); 
allSelectedy = zeros(numImage, numElement); 
allSelectedlambda = zeros(numImage, numElement); % weighting parameter for scoring template matching
allSelectedLogZ = zeros(numImage, numElement); % normalizing constant
%% Iteration part 0: learning template from this image
locationShiftLimit0 = 0; orientShiftLimit0 = 0; 
numImage0 = 1; 
SUM1map0 = cell(1, numOrient); 
for (orient = 1 : numOrient)
       SUM1map0{1, orient} = single(zeros(sizex, sizey));
end
showon = -1; 
for (this = 1:numImage)
ABlearnThis; 
end  % end of this 
%% Output locally learned templates with inhibition 
numInhibit = 5; OutTemplateInhibit; 
showon = 1; showNum = 0; 
for (this = selected)
    showNum = showNum + 1; 
    ABlearnThis; 
end
% selecting templates according to meanSUM2score. Once a template is selected, it inhibits the first numInhibit neighbors, 
% so that templates learned from these neighbors will not be selected. 
save 'locallyLearnedTemplate' numImage sizex sizey allTemplate allSUM2score meanSUM2score ...
     allSelectedOrient allSelectedx allSelectedy allSelectedlambda allSelectedLogZ;

