Ask Your Question

Revision history [back]

To save a SVM use the save function:

svm.save( "my_svm.yml" ); // to save in YML
svm.save( "my_svm.xml" ); // to save in XML
svm.load( "my_svm.yml" ); // to load a trained svm from YML.
svm.load( "my_svm.xml" ); // to load a trained svm from XML.

These functions are inherited from CvStatModel, see the doc [here](http://docs.opencv.org/modules/ml/doc/statistical_models.html#void CvStatModel::save(const char* filename, const char* name)).

Train your SVM once, and save the results. After if you want to evaluate the SVM on other files, use the loading capability.

You could also save and load everything using the OpenCV FileStorage facilities. See samples and doc here. Therefore, you could save your features to test different SVM training, or store features of validation features to test different prediction threshold, etc.

To save a SVM use the save function:

svm.save( "my_svm.yml" ); // to save in YML
svm.save( "my_svm.xml" ); // to save in XML
svm.load( "my_svm.yml" ); // to load a trained svm from YML.
svm.load( "my_svm.xml" ); // to load a trained svm from XML.

These functions are inherited from CvStatModel, see the doc [here](http://docs.opencv.org/modules/ml/doc/statistical_models.html#void CvStatModel::save(const char* filename, const char* name)).

Train your SVM once, and save the results. After if you want to evaluate the SVM on other files, use the loading capability.

You could also save and load everything using the OpenCV FileStorage facilities. See samples and doc here. Therefore, you could save your features to test different SVM training, or store features of validation features to test different prediction threshold, etc.

[EDIT]

Simple trick for the SVM. For Bag of Words, the problem is there is no function to store it. You could store the vocabulary and used the BOWImgDescriptorExtractor setVocabulary function (as you did but with the loaded dictionary).

int main( int argc, char** argv )
{
    svm.load( "my_svm.yml" );
    if( !svm.get_support_vector_count() > 0 ) // model not loaded
    {
        [...]
        // Load a vocabulary.
        FileStorage fs( "vocabulary.yml", FileStorage::READ );
        if( fs.isOpened() )
        {
            fs[ "vocabulary" ] >> dictionary;
        }
        else
        {
            fs.Release();
            // Compute your vocabulary.
            [...]
            fs.open( "vocabulary.yml", FileStorage::WRITE );
            fs << "vocabulary" << dictionary;
        }
        fs.Release();
        // do your training here until
        printf("%s\n", "Training SVM classifier");
        bool res = svm.train(trainingData, labels, cv::Mat(), cv::Mat(), params);
        cout<<"Processing evaluation data..."<<endl;
        svm.save( "my_svm.yml" ); // Save your SVM!
    }
    // continue with your test here
}

You probably should cut your program in functions, each one for each step, and store the results in each function (or reload them if they exist).