%% Initialize learning from the starting image 
numImage0 = 1; locationShiftLimit0 = 0; orientShiftLimit0 = 0; 
for (s = 1:numScale)
 learnedTemplateName = ['working/learnedTemplate' num2str(s)];  
 load(learnedTemplateName); 
 storeExponentialModelName = ['working/storedExponentialModel' num2str(s)]; 
 load(storeExponentialModelName); 
 SUM1MAX1mapName = ['working/SUM1MAX1map' 'image' num2str(starting) 'scale' num2str(s)];   
 load(SUM1MAX1mapName);  
 SUM1mapLearn0 = cell(1, numOrient); 
 MAX1mapLearn0 = cell(1, numOrient); 
 trackMapLearn0 = cell(1, numOrient); 
 sizex = allSizex(originalResolution); sizey = allSizey(originalResolution); 
 ave = averageMap{originalResolution}; 
 averageHere = ave(startx+halfTemplatex, starty+halfTemplatey); % average within local window
 for (orient = 1 : numOrient)      
       SUM1mapLearn0{1, orient} = single(zeros(sizeTemplatex, sizeTemplatey)); 
       Ccopy(SUM1mapLearn0{1, orient}, SUM1map{originalResolution, orient}, startx-1, starty-1, sizeTemplatex, sizeTemplatey, sizex, sizey, averageHere, saturation); 
       MAX1mapLearn0{1, orient} = single(zeros(sizeTemplatexSubsample, sizeTemplateySubsample)); 
       trackMapLearn0{1, orient} = single(zeros(sizeTemplatexSubsample, sizeTemplateySubsample)); 
 end
 CgetMAX1track(numImage0, allSizex0, allSizey0, numOrient, locationShiftLimit0, orientShiftLimit0, subsample, ...
                     SUM1mapLearn0, MAX1mapLearn0, trackMapLearn0);
 commonTemplate0 = single(zeros(sizeTemplatex, sizeTemplatey));               
 deformedTemplate0{1} = single(zeros(sizeTemplatex, sizeTemplatey));
 disp(['start from single image learning at Gabor length ' num2str(halfFilterSize*2+1)]); tic
 CsharedSketch(numOrient, locationShiftLimit0, orientShiftLimit0, subsample, ... % about active basis  
       numElement, numImage0, sizeTemplatex, sizeTemplatey, ...
       SUM1mapLearn0, MAX1mapLearn0, trackMapLearn0, ... % about training images 
       halfFilterSize, Correlation, allSymbol(1, :), ... % about filters
       numStoredPoint, storedlambda, storedExpectation, storedLogZ, ... % about exponential model 
       selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, ... % learned parameters
       commonTemplate0, deformedTemplate0); % learned templates 
 disp(['mex-C learning time: ' num2str(toc) ' seconds']);
 Outepsgif(-double(commonTemplate0), ['LNlion_sym0']); 
 learnedTemplateName = ['working/learnedTemplate' num2str(s)]; 
 save(learnedTemplateName, 'numElement', 'selectedOrient', 'selectedx', 'selectedy', 'selectedlambda', 'selectedLogZ'); 
end
