1

I have below code here. What I want to ask is why opencv SVM predict same object result is different after 2 times training ? Have any solution to combine the 2 times training result ? Any advice will be appreciated. Sorry for my poor English.

int main(int argc, char **argv){

    // Set up SVM's parameters
    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

    CvSVM SVM;
    int idx, idx2;

    // Set up test sample
    Mat test = Mat(1, 2, CV_32FC1);
    test.at<float>(0,0) = 1;
    test.at<float>(0,1) = 1;

    // Set up 1st training data
    float labels[4] = {0, 1, 2, 3};
    Mat labelsMat(4, 1, CV_32FC1, labels);

    float trainingData[4][2] = { {0, 0}, {1, 1}, {2, 2}, {3, 3} };
    Mat trainingDataMat(4, 2, CV_32FC1, trainingData);
    SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);

    // Predict sample
    idx = SVM.predict(test);

    // Set up 2nd training data
    float labels2[4] = {4, 5, 6, 7};
    Mat labelsMat2(4, 1, CV_32FC1, labels);

    float trainingData2[4][2] = { {4, 4}, {5, 5}, {6, 6}, {7, 7} };
    Mat trainingDataMat2(4, 2, CV_32FC1, trainingData2);
    SVM.train(trainingDataMat2, labelsMat2, Mat(), Mat(), params);

    // Predict sample
    idx2 = SVM.predict(test);

    printf("idx [%d] idx2 [%d]\n", idx, idx2);
    waitKey(0);
    return 1;
}

Above code output is idx [1] idx2 [0]. Why ?

QH. Zhu
  • 95
  • 1
  • 6
  • Because on the 2nd training you don't pass the training samples and labels you used in the 1st training. This error aside, it's normal that if you change your training set you get different prediction results. And also, you probably don't want to use different labels for each sample – Miki Sep 22 '16 at 11:17
  • Hi,Miki. Thanks for your reply. Understood. But I have a huge set of data need to train. And I want to train step by step. Not one time training. Maybe several times training. How to do ? Can I save every time predict result and finally combine them into one ? – QH. Zhu Sep 22 '16 at 11:27
  • In general, SVM does not support _incremental_ training. You can however look for some incremental SVM implementation [here](http://stats.stackexchange.com/questions/30834/is-it-possible-to-append-training-data-to-existing-svm-models) and [here](http://stackoverflow.com/questions/3967460/can-an-svm-learn-incrementally) – Miki Sep 22 '16 at 11:30

0 Answers0