dramatic speedup of SVM::predict in the case of linear SVM
This commit is contained in:
parent
79e0e948a7
commit
a06af5ca25
@ -534,6 +534,8 @@ protected:
|
|||||||
virtual void write_params( CvFileStorage* fs ) const;
|
virtual void write_params( CvFileStorage* fs ) const;
|
||||||
virtual void read_params( CvFileStorage* fs, CvFileNode* node );
|
virtual void read_params( CvFileStorage* fs, CvFileNode* node );
|
||||||
|
|
||||||
|
void optimize_linear_svm();
|
||||||
|
|
||||||
CvSVMParams params;
|
CvSVMParams params;
|
||||||
CvMat* class_labels;
|
CvMat* class_labels;
|
||||||
int var_all;
|
int var_all;
|
||||||
|
@ -1517,6 +1517,7 @@ bool CvSVM::do_train( int svm_type, int sample_count, int var_count, const float
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optimize_linear_svm();
|
||||||
ok = true;
|
ok = true;
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
@ -1524,6 +1525,59 @@ bool CvSVM::do_train( int svm_type, int sample_count, int var_count, const float
|
|||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CvSVM::optimize_linear_svm()
|
||||||
|
{
|
||||||
|
// we optimize only linear SVM: compress all the support vectors into one.
|
||||||
|
if( params.kernel_type != LINEAR )
|
||||||
|
return;
|
||||||
|
|
||||||
|
int class_count = class_labels ? class_labels->cols :
|
||||||
|
params.svm_type == CvSVM::ONE_CLASS ? 1 : 0;
|
||||||
|
|
||||||
|
int i, df_count = class_count > 1 ? class_count*(class_count-1)/2 : 1;
|
||||||
|
CvSVMDecisionFunc* df = decision_func;
|
||||||
|
|
||||||
|
for( i = 0; i < df_count; i++ )
|
||||||
|
{
|
||||||
|
int sv_count = df[i].sv_count;
|
||||||
|
if( sv_count != 1 )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if every decision functions uses a single support vector;
|
||||||
|
// it's already compressed. skip it then.
|
||||||
|
if( i == df_count )
|
||||||
|
return;
|
||||||
|
|
||||||
|
int var_count = get_var_count();
|
||||||
|
int sample_size = (int)(var_count*sizeof(sv[0][0]));
|
||||||
|
float** new_sv = (float**)cvMemStorageAlloc(storage, df_count*sizeof(new_sv[0]));
|
||||||
|
|
||||||
|
for( i = 0; i < df_count; i++ )
|
||||||
|
{
|
||||||
|
new_sv[i] = (float*)cvMemStorageAlloc(storage, sample_size);
|
||||||
|
float* dst = new_sv[i];
|
||||||
|
memset(dst, 0, sample_size);
|
||||||
|
int j, k, sv_count = df[i].sv_count;
|
||||||
|
for( j = 0; j < sv_count; j++ )
|
||||||
|
{
|
||||||
|
const float* src = class_count > 1 ? sv[df[i].sv_index[j]] : sv[j];
|
||||||
|
double a = df[i].alpha[j];
|
||||||
|
for( k = 0; k < var_count; k++ )
|
||||||
|
dst[k] = (float)(dst[k] + src[k]*a);
|
||||||
|
}
|
||||||
|
df[i].sv_count = 1;
|
||||||
|
df[i].alpha[0] = 1.;
|
||||||
|
if( class_count > 1 )
|
||||||
|
df[i].sv_index[0] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
sv = new_sv;
|
||||||
|
sv_total = df_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool CvSVM::train( const CvMat* _train_data, const CvMat* _responses,
|
bool CvSVM::train( const CvMat* _train_data, const CvMat* _responses,
|
||||||
const CvMat* _var_idx, const CvMat* _sample_idx, CvSVMParams _params )
|
const CvMat* _var_idx, const CvMat* _sample_idx, CvSVMParams _params )
|
||||||
{
|
{
|
||||||
@ -2516,6 +2570,7 @@ void CvSVM::read( CvFileStorage* fs, CvFileNode* svm_node )
|
|||||||
CV_NEXT_SEQ_ELEM( df_node->data.seq->elem_size, reader );
|
CV_NEXT_SEQ_ELEM( df_node->data.seq->elem_size, reader );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optimize_linear_svm();
|
||||||
create_kernel();
|
create_kernel();
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
|
@ -691,7 +691,10 @@ int build_svm_classifier( char* data_filename )
|
|||||||
CvMat *result = cvCreateMat(1, nsamples_all - ntrain_samples, CV_32FC1);
|
CvMat *result = cvCreateMat(1, nsamples_all - ntrain_samples, CV_32FC1);
|
||||||
|
|
||||||
printf("Classification (may take a few minutes)...\n");
|
printf("Classification (may take a few minutes)...\n");
|
||||||
|
double t = (double)cvGetTickCount();
|
||||||
svm.predict(&sample, result);
|
svm.predict(&sample, result);
|
||||||
|
t = (double)cvGetTickCount() - t;
|
||||||
|
printf("Prediction type: %gms\n", t/(cvGetTickFrequency()*1000.));
|
||||||
|
|
||||||
int true_resp = 0;
|
int true_resp = 0;
|
||||||
for (int i = 0; i < nsamples_all - ntrain_samples; i++)
|
for (int i = 0; i < nsamples_all - ntrain_samples; i++)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user