Home > samsung-code > detInit.m

detInit

PURPOSE ^

DETINIT loads and initializes the detection model

SYNOPSIS ^

function det_model = detInit( caffe_use_gpu, caffe_batch_size, model_dir )

DESCRIPTION ^

 DETINIT loads and initializes the detection model
   paths for dependency is also added
 
 Intro:
   The detection model consists of the following components:
     - the CNN feature extraction model
     - the linear/structured SVM classifier
     - the bounding box regression model
     - the GP-based FTS model

 Usage:

   det_model = detInit( caffe_use_gpu, caffe_batch_size, model_dir )
 
 Input:

   caffe_use_gpu: can be 0 or 1 (default) to indicate whether the Caffe 
       toolbox should use CPU or GPU
   
   caffe_batch_size: can be an integer >=1
       Default value: 32
       Caffe toolbox process input in batch. A proper batch number can get
       a good balance between the efficiency and memory consumption

   model_dir: specify the root directory of the detection model
       Default value: ./models_svm_struct
                   (The trained structured SVM model)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function det_model = detInit( caffe_use_gpu, caffe_batch_size, model_dir )
0002 % DETINIT loads and initializes the detection model
0003 %   paths for dependency is also added
0004 %
0005 % Intro:
0006 %   The detection model consists of the following components:
0007 %     - the CNN feature extraction model
0008 %     - the linear/structured SVM classifier
0009 %     - the bounding box regression model
0010 %     - the GP-based FTS model
0011 %
0012 % Usage:
0013 %
0014 %   det_model = detInit( caffe_use_gpu, caffe_batch_size, model_dir )
0015 %
0016 % Input:
0017 %
0018 %   caffe_use_gpu: can be 0 or 1 (default) to indicate whether the Caffe
0019 %       toolbox should use CPU or GPU
0020 %
0021 %   caffe_batch_size: can be an integer >=1
0022 %       Default value: 32
0023 %       Caffe toolbox process input in batch. A proper batch number can get
0024 %       a good balance between the efficiency and memory consumption
0025 %
0026 %   model_dir: specify the root directory of the detection model
0027 %       Default value: ./models_svm_struct
0028 %                   (The trained structured SVM model)
0029 %
0030 
0031 
0032 %% init param
0033 
0034 TOOLBOX_ROOT_DIR = fileparts(which(mfilename('fullpath')));
0035 addpath( genpath( fullfile( TOOLBOX_ROOT_DIR, 'dependency' ) ) );
0036 addpath( genpath( fullfile( TOOLBOX_ROOT_DIR, 'caffe/matlab/caffe' ) ) );
0037 addpath( TOOLBOX_ROOT_DIR );
0038 
0039 % ---------------------
0040 compile_dependency
0041 % --------------------
0042 
0043 
0044 if ~exist( 'caffe_use_gpu', 'var' ) || isempty(caffe_use_gpu)
0045     caffe_use_gpu = 1;
0046 end
0047 
0048 if ~exist( 'caffe_batch_size', 'var' ) || isempty(caffe_batch_size)
0049    caffe_batch_size = 32;
0050 end
0051 
0052 if ~exist( 'model_dir', 'var' ) || isempty(model_dir)
0053    model_dir = fullfile( fileparts( which( mfilename('fullpath') ) ), 'models_svm_struct' );
0054 end
0055 
0056 det_model = struct();
0057 det_model.model_dir = model_dir;
0058 
0059 %% load classification model for 20 category, i.e., CNN network
0060 
0061 if caffe_use_gpu<0
0062     fprintf( 'WARNING: CAFFE is not set up, you can only run the cached demo for VOC2007' );
0063 else
0064 
0065 % check whether matlab wrapper is there
0066 if exist( 'caffe','file' ) ~= 3
0067     error( 'FATAL: caffe matlab wrapper was not set up' );
0068 end
0069 
0070 try
0071     if caffe('get_version') ~= 20140912
0072         error( 'FATAL: wrong version for caffe matlab wrapper' );
0073     end
0074 catch
0075     error( 'FATAL: please use the customized matlab wrapper' );
0076 end
0077 
0078 % initialize caffe model
0079 matcaffe_init1( fullfile(det_model.model_dir, 'cnn/deploy.prototxt'), ...
0080     fullfile(det_model.model_dir, 'cnn/caffe_model'), caffe_use_gpu, caffe_batch_size );
0081 % figure out the canonical patch size
0082 caffe_input_response_ids = caffe( 'get_input_response_ids' );
0083 caffe_response_info = caffe('get_response_info');
0084 canonical_patchsize = caffe_response_info(caffe_input_response_ids).size;
0085 canonical_patchsize = canonical_patchsize([2 1 3]);
0086 % load input mean
0087 MAT_CONTENT = load( fullfile(det_model.model_dir, 'cnn/input_mean.mat') );
0088 caffe_input_mean = MAT_CONTENT.image_mean;
0089 clear MAT_CONTENT;
0090 % set up the feat_func (to classification layer)
0091 feat_func = @(patches) matcaffe_run_wrapper( single(patches), caffe_input_mean, {'fc7','pool5'} );
0092 
0093 det_model.cnn.feat_func = feat_func;
0094 det_model.cnn.canonical_patchsize = canonical_patchsize;
0095 det_model.cnn.padding       = 16;
0096 det_model.cnn.batch_size    = caffe_batch_size;
0097 det_model.cnn.max_batch_num = 5;
0098 
0099 end
0100 
0101 %% load category list
0102 MAT_CONTENT = load( fullfile(det_model.model_dir, 'categ_list.mat') );
0103 det_model.categ_list = MAT_CONTENT.CategList;
0104 clear MAT_CONTENT
0105 
0106 %% load gp model
0107 det_model.gp = cell(length(det_model.categ_list),1);
0108 for c = 1:length(det_model.categ_list)
0109     det_model.gp{c} = load( fullfile(det_model.model_dir, 'gp', [det_model.categ_list{c} '.mat']) );
0110 end
0111 det_model.gp = cell2mat(det_model.gp);
0112 
0113 %% load regression model for 20 categories
0114 det_model.bboxreg = GetRegressor( det_model.categ_list, fullfile(det_model.model_dir, 'bboxreg') ) ;
0115 
0116 %% load classifier model for 20 categories
0117 det_model.classifier = GetClassifier( det_model.categ_list, fullfile(det_model.model_dir, 'classifier') ) ;
0118 
0119 %% initiaize region proposal model
0120 
0121 SelectiveSearchInit();
0122 rp_func = @(im) SelectiveSearchOnOneImage( im, 'ijcv_fast' );
0123 
0124 det_model.rp_func = rp_func;
0125

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