% This is jut an example of AR
% This function aims to calculate the Gabor feature of original occlusion
% matrix, and learn a dimensionality reduced gabor occlusion matrix.
%
% Copyright. Mike Yang, PolyU, Hong Kong.
% reference:
% M. Yang and L. Zhang.  Gabor feature based sparse representation
% for face recognition with Gabor occlusion dictionary. In ECCV 2010.
%
% for the original occlusion is very big. so this function is better to run
% alone
clear all;
clc;

setpara_occlu;

im_h           =    96; % for AR
im_w           =    84; % for AR

% create the identity matrix's gabor feature
% Because the original occlusion matrix may be too big. So we create its
% gabor feature by two step;

% occlu_gdat     =    [];

% occlu_dat      =    [eye(im_h*im_w/2,im_h*im_w/2);zeros(im_h*im_w/2,im_h*im_w/2)];
% oclu_ipts.dat  =    uint8(reshape(occlu_dat,[im_h im_w im_h*im_w/2]));
% [oclu_opts]    =    Create_GaborF (oclu_ipts, par);
% occlu_gdat     =    single([occlu_gdat oclu_opts.gdat]); 
% save('form_half_occlu_gdata','occlu_gdat');clear occlu_dat oclu_ipts oclu_opts occlu_gdat;

% occlu_dat      =    [zeros(im_h*im_w/2,im_h*im_w/2);eye(im_h*im_w/2,im_h*im_w/2)];
% oclu_ipts.dat  =    uint8(reshape(occlu_dat,[im_h im_w im_h*im_w/2]));
% [oclu_opts]    =    Create_GaborF (oclu_ipts, par); 
% oclu_opts.gdat =    single(oclu_opts.gdat);
% load form_half_occlu_gdata;
% occlu_gdat     =    [occlu_gdat oclu_opts.gdat]; clear occlu_dat oclu_ipts oclu_opts; 

% occlu_gdat     =    occlu_gdat./ repmat(sqrt(sum(occlu_gdat.*occlu_gdat)),[size(occlu_gdat,1) 1]); % unit norm 2
% save('occlu_gabor_dat', 'occlu_gdat');

load('occlu_gabor_dat');

% for the occlu_gdat matrix is too big, we learn the occlusion dictionary
% by two step(% using the following way); other you'd better learn the 
% occlusion dictionary as a whole

ncol_occg        =    size(occlu_gdat,2);
D_input          =    randn(size(occlu_gdat,1),par.dim/2); % Occlusion dictionary initialization
D_input          =    D_input./ repmat(sqrt(sum(D_input.*D_input)),[size(D_input,1) 1]); % unit norm 2
[D_Oclu1 error1] =    Occlusion_Dict_Learn (occlu_gdat(:,1:floor(ncol_occg/2)),D_input, par.dl_nIter, par.lambda_l);

D_input          =    randn(size(occlu_gdat,1),par.dim/2); % Occlusion dictionary initialization
D_input          =    D_input./ repmat(sqrt(sum(D_input.*D_input)),[size(D_input,1) 1]); % unit norm 2
[D_Oclu2 error2] =    Occlusion_Dict_Learn (occlu_gdat(:,floor(ncol_occg/2)+1:end),D_input, par.dl_nIter, par.lambda_l);

D_Oclu =[D_Oclu1 D_Oclu2];
save('Occlu_Lear_Dict','D_Oclu');