Advertisement
Guest User

OpenCV SVM error in calc_non_rbf_base()

a guest
Aug 2nd, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/ml/ml.hpp>
  3.  
  4. using namespace cv;
  5. using namespace std;
  6.  
  7. void main(){
  8.     int number_samples = 10400;
  9.  
  10.     Mat labels(number_samples, 1, CV_32F, Scalar(0));
  11.  
  12.     int features_length = 206000;
  13.  
  14.     Mat descriptors(number_samples, features_length, CV_32F, Scalar(0));
  15.     randn(descriptors, 0, 1);   //fill with random data
  16.  
  17.     SVM my_svm;
  18.     SVMParams params;
  19.     params.kernel_type = SVM::LINEAR;
  20.  
  21.     //This test case will succeed - clear imbalance of positive and negative samples
  22.     labels.rowRange(0, number_samples / 40) = 1;
  23.    
  24.     try{
  25.         cout << "Training SVM with feature length of: " << features_length << "...";
  26.         my_svm.train(descriptors, labels, Mat(), Mat(), params);
  27.         cout << "Done!" << endl;
  28.     }
  29.     catch (...){
  30.         cout << "Exception ocurred while trying to train SVM" << endl;
  31.     }
  32.  
  33.     //This test case will fail - same proportion of positive and negative samples
  34.     labels.rowRange(0, number_samples / 2) = 1;
  35.  
  36.     try{
  37.         cout << "Training SVM with feature length of: " << features_length << "...";
  38.         my_svm.train(descriptors, labels, Mat(), Mat(), params);
  39.         cout << "Done!" << endl;
  40.     }
  41.     catch (...){
  42.         cout << "Exception ocurred while trying to train SVM" << endl;
  43.     }
  44.  
  45.     //However, this test case will succeed again
  46.     labels.rowRange(0, number_samples / 2) = 1;
  47.     features_length = 205000;
  48.  
  49.     try{
  50.         cout << "Training SVM with feature length of: " << features_length << "...";
  51.         my_svm.train(descriptors, labels, Mat(), Mat(), params);
  52.         cout << "Done!" << endl;
  53.     }
  54.     catch (...){
  55.         cout << "Exception ocurred while trying to train SVM" << endl;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement