Home > samsung-code > detVOC2007.m

detVOC2007

PURPOSE ^

DETVOC2007 is the internal function for benchmarking the existing detection models on PASCAL VOC2007 test set

SYNOPSIS ^

function [RECALL, PREC, AP, categ_list] = detVOC2007(model_type, gp_enabled, use_gpu, CategOfInterest4GP )

DESCRIPTION ^

 DETVOC2007 is the internal function for benchmarking the existing detection models on PASCAL VOC2007 test set

 Usage:
   [RECALL, PREC, AP, categ_list]  = detVOC2007( model_type, gp_enabled, use_gpu, CategOfInterest4GP )
   benchmark using specific classifier model with/without the Gaussian 
   process (GP) based fine-grained target search (FTS). More options can
   be specified.

  INPUT:
   model_type: can be a string naming the classifier model type:
       'struct' - (linear) structured SVM, 
                  use the models in ./models_svm_linear
       'linear' - ordinary linear SVM
                  use the models in ./models_svm_struct

   gp_enabled: can be 0 or 1 (default)
       0 - Only selective search is used to propose regions
       1 - GP-based FTS will be applied afterward.

   use_gpu: can be 0 or 1 (default)
       0 - the Caffe toolbox will run on CPU only.
       1 - the Caffe toolbox will mainly run on GPU
     Remark: GPU device id can be specified by caffe('set_device',gpu_id)

   CategOfInterest4GP: can be a string cell array indicating which
     categories FTS should be applied. It is only useful when gp_enable==1
     By default FTS is applied to all the categories.
     E.g. CategOfInterest4GP = {'aeroplane','cow'}
     Remark: Whatever CategOfInterest4GP is specified, detection is done
     for all the categories.

  OUTPUT: Let N be the number of categories
   RECALL, REC : N-d cell arrays. RECALL{i},REC{i} together form the precision-recall curve for the i-th category  
   AP          : a N-d vector of the average precisions of all the categories
   categ_list  : a N-d string cell array for the categories names

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [RECALL, PREC, AP, categ_list] = detVOC2007(...
0002     model_type, gp_enabled, use_gpu, CategOfInterest4GP )
0003 % DETVOC2007 is the internal function for benchmarking the existing detection models on PASCAL VOC2007 test set
0004 %
0005 % Usage:
0006 %   [RECALL, PREC, AP, categ_list]  = detVOC2007( model_type, gp_enabled, use_gpu, CategOfInterest4GP )
0007 %   benchmark using specific classifier model with/without the Gaussian
0008 %   process (GP) based fine-grained target search (FTS). More options can
0009 %   be specified.
0010 %
0011 %  INPUT:
0012 %   model_type: can be a string naming the classifier model type:
0013 %       'struct' - (linear) structured SVM,
0014 %                  use the models in ./models_svm_linear
0015 %       'linear' - ordinary linear SVM
0016 %                  use the models in ./models_svm_struct
0017 %
0018 %   gp_enabled: can be 0 or 1 (default)
0019 %       0 - Only selective search is used to propose regions
0020 %       1 - GP-based FTS will be applied afterward.
0021 %
0022 %   use_gpu: can be 0 or 1 (default)
0023 %       0 - the Caffe toolbox will run on CPU only.
0024 %       1 - the Caffe toolbox will mainly run on GPU
0025 %     Remark: GPU device id can be specified by caffe('set_device',gpu_id)
0026 %
0027 %   CategOfInterest4GP: can be a string cell array indicating which
0028 %     categories FTS should be applied. It is only useful when gp_enable==1
0029 %     By default FTS is applied to all the categories.
0030 %     E.g. CategOfInterest4GP = {'aeroplane','cow'}
0031 %     Remark: Whatever CategOfInterest4GP is specified, detection is done
0032 %     for all the categories.
0033 %
0034 %  OUTPUT: Let N be the number of categories
0035 %   RECALL, REC : N-d cell arrays. RECALL{i},REC{i} together form the precision-recall curve for the i-th category
0036 %   AP          : a N-d vector of the average precisions of all the categories
0037 %   categ_list  : a N-d string cell array for the categories names
0038 %
0039 
0040 %% initial data and models
0041 if ~exist('use_gpu','var') || isempty(use_gpu)
0042     use_gpu = 1;
0043 end
0044 
0045 if ~exist( 'model_type', 'var' ) || isempty(model_type)
0046     model_type = 'struct';
0047 end
0048 if ~ismember( model_type, {'linear','struct'} )
0049     error( 'Unknown model_type' );
0050 end
0051 
0052 if ~exist('gp_enabled','var') || isempty(gp_enabled)
0053     gp_enabled = 1;
0054 end
0055 
0056 if ~exist('CategOfInterest4GP','var') || isempty(CategOfInterest4GP)
0057     CategOfInterest4GP = [];
0058 end
0059 
0060 fprintf( 'Initialize model: ' ); tic
0061 det_model = detInit(use_gpu,[],['models_svm_' model_type]);
0062 toc
0063 
0064 TOOLBOX_ROOT_DIR = fileparts(which(mfilename('fullpath')));
0065 addpath( fullfile( TOOLBOX_ROOT_DIR, 'voc2007/VOCdevkit/VOCcode' )  );
0066 
0067 gp_flags_str = { 'with-gp', 'no-gp' };
0068 full_tag = [model_type '_' gp_flags_str{2-gp_enabled}];
0069 RESULT_CACHE_DIR = fullfile(TOOLBOX_ROOT_DIR,'voc2007_results_cache', full_tag );
0070 mkdir_p(RESULT_CACHE_DIR);
0071 
0072 FEATURE_CACHE_DIR = fullfile(TOOLBOX_ROOT_DIR,'voc2007_feature_cache');
0073 
0074 fprintf( 'Initialize VOC2007 dataset: ' ); tic
0075 VOCinit
0076 toc
0077 % load test set ('val' for development kit)
0078 
0079 fprintf( 'Load image list: ' ); tic
0080 [ids,gt]=textread(sprintf(VOCopts.imgsetpath,VOCopts.testset),'%s %d');
0081 toc
0082 
0083 imgN = length(ids);
0084 
0085 fprintf( 'Try to load bounding box cache: ' ); tic
0086 if exist( 'voc2007_test_bbox_cache.mat', 'file' )
0087     load voc2007_test_bbox_cache.mat
0088     toc
0089 else
0090     boxes = cell(imgN,1);
0091     fprintf( 'Cannot find. Ignored\n' );
0092 end
0093 
0094 %% run detection for every image
0095 
0096 fprintf( '====== Do detection on VOC2007 images =======\n' );
0097 
0098 for i = 1:imgN
0099 
0100     fprintf( '%d / %d (%s): ', i, imgN, ids{i} ); tic
0101     
0102     resultFn = fullfile( RESULT_CACHE_DIR, [ ids{i} '.mat' ] );
0103     
0104     if exist( resultFn, 'file' )
0105         fprintf( 'existed\n' );
0106         continue;
0107     end
0108     fprintf('\n')
0109     
0110     I = imread(sprintf(VOCopts.imgpath,ids{i}));
0111 
0112     try
0113         Fcls0 = GetProposedFeature( 'test', ids{i}, fullfile( FEATURE_CACHE_DIR, 'cls') );
0114         Freg0 = GetProposedFeature( 'test', ids{i}, fullfile( FEATURE_CACHE_DIR, 'bboxreg') );
0115         bboxes0 = [ boxes(i), Fcls0, Freg0 ];
0116     catch
0117         bboxes0 = boxes{i};
0118     end
0119     
0120     [Bs, Ss] = detSingle( I, det_model, gp_enabled , -100, -1, bboxes0, CategOfInterest4GP); 
0121     for c=1:VOCopts.nclasses
0122         [~,sidx] = sort( Ss{c}, 'descend' );
0123         sidx = sidx(1:min(40,end));
0124         Ss{c}= Ss{c}(sidx); 
0125         Bs{c}= Bs{c}(sidx,:); 
0126     end 
0127 
0128     save( resultFn, 'Bs', 'Ss' );
0129     toc
0130     
0131 end
0132 
0133 clear Bs Ss
0134 
0135 %% consolidate results
0136 
0137 fprintf( '==============================================\n' );
0138 fprintf( 'Consolidate the results: \n' ); tic
0139 
0140 fidC = zeros(VOCopts.nclasses,1);
0141 for c=1:VOCopts.nclasses
0142     cls = VOCopts.classes{c};
0143     fidC(c)=fopen(sprintf(VOCopts.detrespath,full_tag,cls),'w');
0144 end
0145 
0146 for i = 1:imgN
0147     tic_toc_print( '%d / %d \n', i, imgN );
0148     resultFn = fullfile( RESULT_CACHE_DIR, [ ids{i} '.mat' ] );
0149     R = load( resultFn );
0150     for c=1:VOCopts.nclasses
0151         for j=1:length(R.Ss{c})
0152             fprintf(fidC(c),'%s %f %f %f %f %f\n', ...
0153                     ids{i},R.Ss{c}(j),R.Bs{c}(j,[2 1 4 3]));
0154         end
0155     end
0156 end
0157 
0158 for c=1:VOCopts.nclasses
0159     fclose( fidC(c) );
0160 end
0161 
0162 toc
0163 
0164 %% PR, AP for results
0165 
0166 fprintf( 'Compute statistics (PR, AP): \n' );
0167 
0168 RECALL = cell(VOCopts.nclasses,1);
0169 PREC   = cell(VOCopts.nclasses,1);
0170 AP     = zeros(VOCopts.nclasses,1);
0171 for c=1:VOCopts.nclasses
0172     cls = VOCopts.classes{c};
0173     [RECALL{c},PREC{c},AP(c)]=VOCevaldet(VOCopts,full_tag,cls,false);
0174 end
0175 
0176 categ_list = VOCopts.classes;
0177 
0178 end
0179

Generated on Thu 18-Dec-2014 22:27:44 by m2html © 2005