% ========================================================================
% Relaxed Collaborative Representation, Version 1.0
% Copyright(c) 2012 Meng Yang, Lei Zhang, and David Zhang
% All Rights Reserved.
%
% ----------------------------------------------------------------------
% Permission to use, copy, or modify this software and its documentation
% for educational and research purposes only and without fee is here
% granted, provided that this copyright notice and the original authors'
% names appear on all copies and supporting documentation. This program
% shall not be used, rewritten, or adapted as the basis of a commercial
% software or hardware product without first obtaining permission of the
% authors. The authors make no representations about the suitability of
% this software for any purpose. It is provided "as is" without express
% or implied warranty.
%----------------------------------------------------------------------
%
% This is an implementation of the algorithm (version of column generation)
% for ObjectCategorization on oxford flower 17.
%
% Please refer to the following paper
%
% Meng Yang, Lei Zhang, David Zhang, and Shenlong Wang, "Relaxed Collaborative
% Representation for Patter Classification," In Proc. CVPR 2012.
% 
% Contact: {csmyang,cslzhang}@comp.polyu.edu.hk
%----------------------------------------------------------------------

clear all;
clc;

%----RCR's parameter setting---- %
lambda            =   0.0001;
tau               =   0.1;
weight_v          =   1; % 1: weight version; 0:equal weight

%Other parameters
fea_names = {'hog','hsv','siftint','siftbdy','colourgc','shapegc','texturegc'};
if weight_v
   weight = [1 1 1 1 0.9 1 1];
else
   weight = [1 1 1 1 1 1 1];
end

fprintf('--------- Task (feature) confidence --------- \n');
for i = 1:size(weight,2)
   weight_task{i} = weight(i); 
   fprintf('%s : %f/7 \n', fea_names{i}, weight_task{i});
end

cr_rate = [];
for (split_id =[1:3])

    load(['..\data\tasks\task_',int2str(split_id),'.mat']);
    
    % offline computing
    ww = size(X_task{1},2);
    Sum_inv     = single(zeros(ww));
    for bi = 1:size(X_task,1)
        tem = inv(X_task{bi}'*X_task{bi} + (lambda + tau*weight(bi))*eye(ww));
        BlkD(bi).P = single(tem*X_task{bi}');
        Sum_inv    = single(Sum_inv + tau*weight(bi)^2*tem/sum(weight));
    end
    Proj = [];
    for bi = 1:size(X_task,1)
        tem = X_task{bi}'*X_task{bi} + (lambda + tau*weight(bi))*eye(ww);
        Proj(bi).P = single(inv((eye(ww)-Sum_inv)*tem));
    end
    
    fprintf('-------------train/test split #%d:------------- \n', split_id);
    fprintf('Number of test images processed: ');
        
    K = length(Q);
    testConf = zeros(numClasses);
    for (l=1:size(Y_task_total{1},2))
        
        Y_task = cell(K,1);
        for (k=1:K)
            Y_task{k} = Y_task_total{k}(:,l);  
        end     
        
        [predClass] = JCRDF (BlkD,Proj,Y_task, X_task,group_index_task,weight,tau);
        gtClass = gnd_Test(l);
        testConf(gtClass, predClass) = testConf(gtClass, predClass) + 1;
       
       if (mod(l,10)==0)
            fprintf('%d ', l);
            if (mod(l,200)==0)
                fprintf('\n \f');
            end
        end
    end
    
    testConf = testConf./ (sum(testConf,2) * ones(1, numClasses)) ;
    
    cr_rate = [cr_rate, mean(diag(testConf))];
    fprintf('\n Accuracy: %f \n \f',mean(diag(testConf)));
end

fprintf('Mean: %1.4f \n', mean(cr_rate));
fprintf('Variance: %1.4f \n', std(cr_rate));