function [err, score, gt] = slvcrf_testing(model, featureSet, gtLabelSet, testList, result_dir, pars)
% test slv-crf model 
% written by Jimei Yang @UCM, Oct 2011
numTests = length(testList);
bp_pars.maximize = 0;
bp_pars.max_iters = 200;
bp_pars.tol = 1e-3;
gamma = pars.gamma;
lambda = pars.lambda;
err = [];
score = cell(numTests,1);
gt = cell(numTests,1);
GT = cell(numTests,1);
for ii = 1 : numTests
    ipath = featureSet.path{testList(ii)};
    [dirstr, namestr, ext] = fileparts(ipath);
    load(featureSet.path{testList(ii)});
    load(gtLabelSet.path{testList(ii)});
    label = gtLabel.gt;
    L = gtLabel.GT;
    rows = length(unique(feaSet.y));
    cols = length(unique(feaSet.x));
    
    % build the graph
    graph = StarEdge_MakeEdgeNums_Lattice2(rows, cols);
    [aff, dist] = BuildGraph(feaSet.feaArr, rows, cols);
    graph.A = aff; graph.D = dist; graph.rows = rows; graph.cols = cols;
    % sparse coding
    S = L1QP_FeatureSign_Set(feaSet.feaArr, model.D, gamma, lambda);
    
    [label_hat, energy] = crf_infer_gc(model, S, graph);
%     [label_hat, belief] = crf_infer_lbp(model, S, graph, [], bp_pars);
%     saliency = reshape(belief(:,1),rows,cols);
%     saliency = imresize(saliency,[feaSet.height,feaSet.width]);
%     score{ii} = saliency(:);
%     imwrite(uint8(255*saliency),sprintf('%s/%s_saliency.png',result_dir, namestr), 'png');
    imwrite(uint8(255*reshape(label_hat,rows,cols)),sprintf('%s/%s.png',result_dir, namestr), 'png');
    imwrite(uint8(255*reshape(label,rows,cols)),sprintf('%s/%s_gt.png',result_dir, namestr), 'png');
%     gt{ii} = label(:);
%     GT{ii} = double(L(:)==0);
    err = [err; sum(label~=label_hat)/length(label)];
end

gt = cell2mat(gt);
score = cell2mat(score);