# ABlearn

## PURPOSE

ABlearn - Learns an active basis template from images.

## SYNOPSIS

function ABlearn(category,imagepaths,Totalsketch,templateSize,scale)

## DESCRIPTION

     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)

## CROSS-REFERENCE INFORMATION

This function calls:

- corr corr - Compute inhibition map among filters
- getGaborMaps getGaborMaps - Compute S1 maps from images whose paths are specified in .
- makefilter makefilter - Make Gabor filters at fixed scale with norient orientations

This function is called by:

- LearnActiveBasisTemplate LearnActiveBasisTemplate - Learns an active basis template.

## SOURCE CODE

    0001 function ABlearn(category,imagepaths,Totalsketch,templateSize,scale)
    0002 % ABlearn - Learns an active basis template from images.
    0003 %
    0004 % category: the category name of training images
    0005 % imagepaths: path of training images
    0006 % templateSize: [height width] (default 120 * 120)
    0007 % scale: scale of Gabor filter (default 0.7)
    0008 % Totalsketch: number of allowed Gabor elements (default 30)
    0009
    0010 if nargin < 3
    0011     Totalsketch = 30;
    0012 end
    0013
    0014 if nargin < 4
    0015     templateSize = [120 120];
    0016 end
    0017
    0018 if nargin < 5
    0019     scale = .7; % Gabor scale
    0020 end
    0021
    0022 rootFolder = '..';
    0023
    0024 % load natural statistics
    0025 load(sprintf('%s/result/natural_stat.mat',rootFolder),'norient','binnum','M','lam','e','lz','Upperbound');
    0026
    0027 % CONSTANTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0028 epsilon = .1;  % allowed correlation between selected Gabors
    0029 sub = 2;    % subsampling rate for Gabor
    0030 Lrange = 3; % allowed shifting in location (times sub pixels)
    0031 Orange = 1; % allowed shifting in orientation
    0032 %%% Note: Lrange*sub is the pixels allowed to shift along normal
    0033 %%%       Lrange*sub should be the same in learning and testing
    0034 SHUTUP = 0.; % SHUTUP is to neglect the positions with low average responses
    0035
    0036 nimage = length(imagepaths);
    0037 n = nimage;
    0038
    0039 % TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0040 % ==================================================================
    0041 %  input images from a folder
    0042 % ==================================================================
    0043
    0044 % FILTERING IMAGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0045 tic
    0046 disp('start filtering');
    0047 addpath('../common');
    0048 [allfilter, allsymbol] = makefilter(scale, norient);  % generate Gabor filters
    0049 allfiltered = getGaborMaps(imagepaths,templateSize,allfilter); % compute Sum1 maps
    0050 Ctransform(nimage, norient, allfiltered, ones(1,nimage)*templateSize(1)...
    0051     , ones(1,nimage)*templateSize(2), Upperbound); % sigmoid transform
    0052 h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
    0053 M1Map = cell(n,norient);
    0054 M1TraceMap = cell(n,norient);
    0055 subM1 = 1;
    0056 for i = 1:n
    0057     [M1Map(i,:) M1TraceMap(i,:) rowShifts colShifts oriShifts] = CcomputeMAX1(norient,allfiltered(i,:),Lrange,Orange,h*2+1,subM1);
    0058 end
    0059 C = corr(allfilter, epsilon);    % generate the inhibition maps between Gabors
    0060 disp(['filtering time: ' num2str(toc) ' seconds']);
    0061
    0062 % VARIABLES FOR MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0063 Mi = zeros(1, Totalsketch);  % orientation of selected Gabor
    0064 Mx = zeros(1, Totalsketch);  % position of selected Gabor
    0065 My = zeros(1, Totalsketch);
    0066 Mm = zeros(1, Totalsketch);  % lambda
    0067 Mm1 = zeros(1, Totalsketch);  % logZ
    0068 sym = zeros(templateSize);  % symbolic plot of selected Gabor
    0069 Asym = cell(1, nimage);   % symbolic plot for each image with active Gabor
    0070 for i = 1 : nimage
    0071     Asym{i} = zeros(templateSize);
    0072 end
    0073 tss = zeros(nimage, 1);  % total scores for testing images
    0074 gain = zeros(Totalsketch, 1);  % coding gain for selected Gabor
    0075
    0076 binnum = 50; % number of bins
    0077 % MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0078 disp('start learning');
    0079 tic
    0080 Clearn(nimage, norient, allfiltered, C, h, templateSize(1), templateSize(2),...
    0081     Mi, Mx, My, Mm, Mm1, allsymbol(1, :), sym, Asym,...
    0082     Lrange, Orange, sub, Totalsketch, Upperbound,...
    0083     tss, gain, SHUTUP, binnum, M, lam, e, lz);
    0084 disp(['mex-C learning time: ' num2str(toc) ' seconds']);
    0085 SUM2score = tss;
    0086
    0087 mkdir(sprintf('%s/result/%s',rootFolder,category));
    0088 save(sprintf('%s/result/%s/activebasis_model.mat',rootFolder,category),...
    0089     'Mi','Mx','My', 'Mm','Mm1','C','h','Lrange','Orange','sub','scale',...
    0090     'norient','allfilter','allsymbol','Asym','Upperbound',...
    0091     'Totalsketch','M','lam', 'e', 'lz', 'sym', 'templateSize',...
    0092     'M1TraceMap','rowShifts','colShifts','oriShifts','SUM2score');
    0093