function ABlearn(category,imagepaths,Totalsketch,templateSize,scale)
% ABlearn - Learns an active basis template from images.
%
% category: the category name of training images
% imagepaths: path of training images
% templateSize: [height width] (default 120 * 120)
% scale: scale of Gabor filter (default 0.7)
% Totalsketch: number of allowed Gabor elements (default 30)

if nargin < 3
    Totalsketch = 30;
end

if nargin < 4
    templateSize = [120 120];
end

if nargin < 5
    scale = .7; % Gabor scale
end

rootFolder = '..';

% load natural statistics
load(sprintf('%s/result/natural_stat.mat',rootFolder),'norient','binnum','M','lam','e','lz','Upperbound');

% CONSTANTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
epsilon = .1;  % allowed correlation between selected Gabors
sub = 2;    % subsampling rate for Gabor
Lrange = 3; % allowed shifting in location (times sub pixels)
Orange = 1; % allowed shifting in orientation
%%% Note: Lrange*sub is the pixels allowed to shift along normal
%%%       Lrange*sub should be the same in learning and testing
SHUTUP = 0.; % SHUTUP is to neglect the positions with low average responses

nimage = length(imagepaths);
n = nimage;

% TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ==================================================================
%  input images from a folder
% ==================================================================

% FILTERING IMAGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
disp('start filtering');
addpath('../common');
[allfilter, allsymbol] = makefilter(scale, norient);  % generate Gabor filters
allfiltered = getGaborMaps(imagepaths,templateSize,allfilter); % compute Sum1 maps
Ctransform(nimage, norient, allfiltered, ones(1,nimage)*templateSize(1)...
    , ones(1,nimage)*templateSize(2), Upperbound); % sigmoid transform
h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
M1Map = cell(n,norient);
M1TraceMap = cell(n,norient);
subM1 = 1;
for i = 1:n
    [M1Map(i,:) M1TraceMap(i,:) rowShifts colShifts oriShifts] = CcomputeMAX1(norient,allfiltered(i,:),Lrange,Orange,h*2+1,subM1);
end
C = corr(allfilter, epsilon);    % generate the inhibition maps between Gabors
disp(['filtering time: ' num2str(toc) ' seconds']);

% VARIABLES FOR MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mi = zeros(1, Totalsketch);  % orientation of selected Gabor
Mx = zeros(1, Totalsketch);  % position of selected Gabor
My = zeros(1, Totalsketch); 
Mm = zeros(1, Totalsketch);  % lambda
Mm1 = zeros(1, Totalsketch);  % logZ
sym = zeros(templateSize);  % symbolic plot of selected Gabor
Asym = cell(1, nimage);   % symbolic plot for each image with active Gabor
for i = 1 : nimage
    Asym{i} = zeros(templateSize);  
end
tss = zeros(nimage, 1);  % total scores for testing images
gain = zeros(Totalsketch, 1);  % coding gain for selected Gabor

binnum = 50; % number of bins
% MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('start learning');
tic
Clearn(nimage, norient, allfiltered, C, h, templateSize(1), templateSize(2),...
    Mi, Mx, My, Mm, Mm1, allsymbol(1, :), sym, Asym,...
    Lrange, Orange, sub, Totalsketch, Upperbound,...
    tss, gain, SHUTUP, binnum, M, lam, e, lz);
disp(['mex-C learning time: ' num2str(toc) ' seconds']);
SUM2score = tss;

mkdir(sprintf('%s/result/%s',rootFolder,category));
save(sprintf('%s/result/%s/activebasis_model.mat',rootFolder,category),...
    'Mi','Mx','My', 'Mm','Mm1','C','h','Lrange','Orange','sub','scale',...
    'norient','allfilter','allsymbol','Asym','Upperbound',...
    'Totalsketch','M','lam', 'e', 'lz', 'sym', 'templateSize',...
    'M1TraceMap','rowShifts','colShifts','oriShifts','SUM2score');

