% SHARED SKETCH ALGORITHM FOR LEARNING ACTIVE BASIS -- piecewise version 
clear all; close all; 
disp(['===============> Learning the active basis model from positive training images']);
%% Load in training images
load working/trainingImage; 
allSizex = zeros(1, numImage)+sizex; 
allSizey = zeros(1, numImage)+sizey; 
%% learning over multiple scales
load working/parameter; 
for (s = 1 : numScale)
  storeName = ['working/storedExponentialModel' num2str(s)];   
  load(storeName); 
  numElement = floor(numElement1*scales(1)/scales(s)); 
%% Compute SUM1 maps by Gabor filtering
disp(['start filtering training images at Gabor scale ' num2str(s)]); tic
SUM1map = ApplyFilterfftSame(I, allFilter, localOrNot, localHalfx, localHalfy, -1); % SUM1 maps by Gabor filtering
disp(['filtering time: ' num2str(toc) ' seconds']);
%% Compute MAX1 maps and record the track maps
Csigmoid(numImage, allSizex, allSizey, numOrient, saturation, SUM1map);
MAX1map = cell(numImage, numOrient); 
trackMap = cell(numImage, numOrient); 
for (img = 1:numImage)
    for (orient = 1:numOrient)
        MAX1map{img, orient} = single(zeros(floor(sizex/subsample), floor(sizey/subsample))); 
        trackMap{img, orient} = single(zeros(floor(sizex/subsample), floor(sizey/subsample))); 
    end
end
disp(['start maxing training images at Gabor scale ' num2str(s)]); tic
CgetMAX1track(numImage, allSizex, allSizey, numOrient, locationShiftLimit, orientShiftLimit, subsample, ...
              SUM1map, MAX1map, trackMap); 
disp(['maxing time: ' num2str(toc) ' seconds']);
%% 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, numImage); % templates for training images 
for (img = 1 : numImage)
    deformedTemplate{img} = single(zeros(sizex, sizey));  
end
SUM2score = zeros(numImage, 1); % template matching scores for training images 
%% Learning by shared sketch algorithm
disp(['start mex-C learning of template at Gabor scale ' num2str(s)]); tic
CsharedSketch(numOrient, locationShiftLimit, orientShiftLimit, saturation, subsample, ... % about active basis  
       numElement, numImage, sizex, sizey, ...
       SUM1map, MAX1map, trackMap, ... % about training images 
       halfFilterSize, Correlation, allSymbol(1, :), ... % about filters
       numStoredPoint, storedlambda, storedExpectation, storedLogZ, ... % about exponential model 
       selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, SUM2score, ... % learned parameters
       commonTemplate, deformedTemplate); % learned templates 
disp(['mex-C learning time: ' num2str(toc) ' seconds']);
%% Display and save results
OutLearningResult; 
Outepsgif(-commonTemplate, ['output/learnedTemplateUsingGaborScale' num2str(s)]); % output learned template
templateName = ['working/learnedTemplate' num2str(s)]; 
save(templateName, 'numElement', 'selectedOrient', 'selectedx', 'selectedy', 'selectedlambda', 'selectedLogZ', ...
                   'templateHalfx', 'templateHalfy', 'sizeTemplatex', 'sizeTemplatey'); 
end