OpenCV 3.1.0: Save and load trained SVMs
<p>At the moment I am trying to train different SVMs for the recognition of different emotions. So for example to recognize the emotion happy I train a SVM with images of happy people as positives and images where people express other emotions such as anger, fear, disgust, ... as negatives. The images are stored in a database that I have partitioned in a training section and testing section.</p>

<p>When I have trained the SVMs, I immediatly use them to test the accuracy on the test images of the database and this works fine. But I also save the trained SVMs because I would like to use them in another programm and don't want to retrain them every time I start the other program.</p>

<p>Thus I loaded the SVMs in the other program but the results were very bad. The accuracy was near zero percent. So I tried to save and immedialty load the SVMs in the training program but also here the accuracy was now near zero percent. </p>

<p>After searching for a while I found out that if I have loaded the SVMs and I print the SVM type, kernel type and supportvectors that they are the same as in the SVM .xml file. So I think that the problem is that the prediction is not executed in the right way. I also don't know if I save my SVMs and load them in the appropriate way. </p>

<p>At the moment I have tried searching for a solution but without any success. Some of the links that I have tried are:</p>

<p><a href="https://stackoverflow.com/questions/34951872/train-svm-and-save-it-with-opencv-3-0">Train SVM and save it with OpenCV 3.0</a></p>

<p><a href="https://stackoverflow.com/questions/24135372/how-to-load-previously-stored-svm-classifier">How to load previously stored svm classifier?</a></p>

<p><a href="https://stackoverflow.com/questions/35732489/load-trained-svm-emgu-cv">Load Trained SVM – Emgu CV</a></p>

<p><a href="https://stackoverflow.com/questions/32863392/opencv-3-c-auto-trained-svm-loading-issue">opencv 3 (C++) auto trained SVM loading issue</a></p>

<p>The code that I use to train the SVMs and to test them immediately without loading them again is:</p>

<pre><code>trainData = ml::TrainData::create(training_mat, ROW_SAMPLE, label_mat);
svm = SVM::create();
svm-&gt;setType(SVM::C_SVC);
svm-&gt;setKernel(SVM::RBF);
svm-&gt;trainAuto(trainData);
svm-&gt;save(svmSaveNames[i]);

// Test SVMs
data_file.open(filenameLabelsTestingImages[i]);
data_file &lt;&lt; "Number\n";
startTest = stopTest;
stopTest  = startTest + emotionCountersTesting[i];
int numberRightClassified = 0;
int numberClassified = 0;

for (int j = 0; j &lt; numberOfTestImg; j++)
{
    cv::Mat testing_one_image_mat(1, numberOfFeatures, CV_32F);
    for (int k = 0; k &lt; numberOfFeatures; k++)
    {
        testing_one_image_mat.at&lt;float&gt;(0, k) = testing_mat.at&lt;float&gt;(j, k);
    }

    int value_svm = svmNew-&gt;predict(testing_one_image_mat);

    if (value_svm == 1)
    {   
        if (j &gt;= startTest &amp;&amp; j &lt; stopTest)
        {
            numberRightClassified++;
        }
        numberClassified++;
    }
    data_file &lt;&lt; value_svm &lt;&lt; endl;
}
data_file.close();
</code></pre>

<p>So this works fine until I change the code to save the SVMs first and then load them again for prediction as follow</p>

<pre><code>trainData = ml::TrainData::create(training_mat, ROW_SAMPLE, label_mat);
svm = SVM::create();
svm-&gt;setType(SVM::C_SVC);
svm-&gt;setKernel(SVM::RBF);
svm-&gt;trainAuto(trainData);
svm-&gt;save(svmSaveNames[i]);

Ptr&lt;SVM&gt; svmNew = SVM::create();
svmNew = SVM::load&lt;SVM&gt;(svmSaveNames[i]);
//cout &lt;&lt; "The type is " &lt;&lt; svmNew-&gt;getType() &lt;&lt; endl;
//cout &lt;&lt; "The kernel type is " &lt;&lt; svmNew-&gt;getKernelType() &lt;&lt; endl;
//cout &lt;&lt; "The support vectors are " &lt;&lt; svmNew-&gt;getSupportVectors() &lt;&lt; endl;

// Test SVMs
data_file.open(filenameLabelsTestingImages[i]);
data_file &lt;&lt; "Number\n";
startTest = stopTest;
stopTest  = startTest + emotionCountersTesting[i];
int numberRightClassified = 0;
int numberClassified = 0;

for (int j = 0; j &lt; numberOfTestImg; j++)
{
    cv::Mat testing_one_image_mat(1, numberOfFeatures, CV_32F);
    for (int k = 0; k &lt; numberOfFeatures; k++)
    {
        testing_one_image_mat.at&lt;float&gt;(0, k) = testing_mat.at&lt;float&gt;(j, k);
    }

    //int value_svm = svm -&gt; predict(testing_one_image_mat);
    int value_svm = svmNew-&gt;predict(testing_one_image_mat);

    if (value_svm == 1)
    {   
        if (j &gt;= startTest &amp;&amp; j &lt; stopTest)
        {
            numberRightClassified++;
        }
        numberClassified++;
    }
    data_file &lt;&lt; value_svm &lt;&lt; endl;
}
data_file.close();
</code></pre>

<p>The array svmSaveNames contains strings with names for saving the different SVMs like svm_anger.xml, svm_contempt.xml, ...</p>

<p>I use the variable data_file to create a .txt file for every SVM that is tested. So first I train and test the SVM to recognize for example the emotion anger and while testing this SVM I use all the test images. So the prediction of all those images (1 = positive / -1 = negative) is written to a textfile.</p>

<p>The parameters startTest and stopTest are used to verify if the positive images, prediction gives value 1, is in the range of the images that are needed to be recognized as positive. In the testing map of the database I ordered all the images by there emotion so first anger then contempt,...</p>

<p>The 2D matrix testing_mat contains the data from all the test images that is given to the SVM to predict the emotion.</p>

<p>So my problem is that after I have loaded the SVMs they don't give me the right prediction.</p>