Ask Your Question
3

SVM save/load problems

asked 2015-06-29 06:56:22 -0600

Jens gravatar image

Hi all,

I'm currently working with am SVM in OpenCV 3.0.0. When I train a C_SVC-SVM with RBF kernel I'll get good classification results of about 90-95%. These results are really good, in the problem case I'm working on.

Now I save the SVM to a file (in my case I use YML, but I think it doesn't matter):

classifier->save(filename);

... where classifier is of type cv::Ptr<cv::ml::svm>. The next time I start the program, I'll load the SVM again:

classifier = cv::Algorithm::load<cv::ml::svm &gt;(filename);<="" p="">

... but now I get recognition rates of 0-36% with exactly the same dataset.

I dumped all parameters, the decision function, and all other values to console and compared them manually. Except for the support vector indices of the decision function they do not differ before save, after load, and in comparison to the saved files.

What's going on here? Any ideas?

Cheers, Jens

edit retag flag offensive close merge delete

Comments

I run into the same problem even when the number of classes is 6. Is there a fix for it?

Vishnu Makkapati gravatar imageVishnu Makkapati ( 2015-11-06 04:18:46 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-07-01 07:22:11 -0600

Jens gravatar image

Hi all,

I'll give the answer myself:

The problem is the already mentioned difference between the support vector indices. Within the implementation of the SVMs write and read methods (modules/ml/src/svm.cpp, lines 2022 ff and 2139 ff), the author makes the assumption, that support vector indices are not required in 2-class problems. That is probably correct if a linear kernel is used (i.e., when you have only one support vector). But with other kernels (in my case with an RBF kernel), the support vector indices are definitly required.

I've added a git diff where I commented this questionable conditions out. Now everything works fine! IMHO the lines should be removed.

I posted a bug report on this issue right now.

Btw.: That new users must wait 2 days to answer their own questions sucks!!!

Jens

diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp
index 9318085..b9f1c51 100644
--- a/modules/ml/src/svm.cpp
+++ b/modules/ml/src/svm.cpp
@@ -2066,14 +2066,14 @@ public:
                << "alpha" << "[:";
              fs.writeRaw("d", (const uchar*)&df_alpha[df.ofs], sv_count*sizeof(df_alpha[0]));
              fs << "]";
-            if( class_count > 2 )
-            {
+//            if( class_count > 2 )
+//            {
                  fs << "index" << "[:";
                  fs.writeRaw("i", (const uchar*)&df_index[df.ofs], sv_count*sizeof(df_index[0]));
                  fs << "]";
-            }
-            else
-                CV_Assert( sv_count == sv_total );
+//            }
+//            else
+//                CV_Assert( sv_count == sv_total );
              fs << "}";
          }
          fs << "]";
@@ -2191,12 +2191,12 @@ public:
              df_index.resize(ofs + sv_count);
              df_alpha.resize(ofs + sv_count);
              dfi["alpha"].readRaw("d", (uchar*)&df_alpha[ofs], sv_count*sizeof(df_alpha[0]));
-            if( class_count > 2 )
+//            if( class_count > 2 )
                  dfi["index"].readRaw("i", (uchar*)&df_index[ofs], sv_count*sizeof(df_index[0]));
              decision_func.push_back(df);
          }
-        if( class_count <= 2 )
-            setRangeVector(df_index, sv_total);
+//        if( class_count <= 2 )
+//            setRangeVector(df_index, sv_total);
          if( (int)fn["optimize_linear"] != 0 )
              optimize_linear_svm();
      }
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-06-29 06:56:22 -0600

Seen: 2,632 times

Last updated: Nov 06 '15