2017-02-02 94 views
1

我使用的是OpenCV 3.0.0。在运行OpenCV的SVM示例1时,我注意到当自动训练而不是训练时,预测值介于0和1之间。当我直接使用libsvm运行相同示例时,我看不到相同的行为。Opencv 3支持SVM训练自动缩放标签吗?

这是一个错误还是有意缩放标签以及其他功能?行为似乎也没有记录。

这里是我运行代码:

// Set up training data 
size_t numberOfSamples = 4; 
cv::Mat1i labelsMat(numberOfSamples, 1); 
labelsMat(0, 0) = 1; 
labelsMat(1, 0) = -1; 
labelsMat(2, 0) = -1; 
labelsMat(3, 0) = -1; 

cv::Mat1f trainingDataMat(numberOfSamples, 2); 
// Sample 0 
trainingDataMat(0, 0) = 501; 
trainingDataMat(0, 1) = 10; 

// Sample 1 
trainingDataMat(1, 0) = 255; 
trainingDataMat(1, 1) = 10; 

// Sample 2 
trainingDataMat(2, 0) = 501; 
trainingDataMat(2, 1) = 255; 

// Sample 3 
trainingDataMat(3, 0) = 10; 
trainingDataMat(3, 1) = 501; 

// Set up SVM's parameters 
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create(); 
svm->setType(cv::ml::SVM::C_SVC); 
svm->setKernel(cv::ml::SVM::LINEAR); 
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6)); 

// Train the SVM with given parameters 
cv::Ptr<cv::ml::TrainData> td = 
    cv::ml::TrainData::create(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat); 

// train the SVM 
// svm->train(td); 

// or auto train 
svm->trainAuto(td); 

// predict 
// first point used for training 
cv::Mat point1 = (cv::Mat_<float>(1, 2) << 501, 10); 
float response1 = svm->predict(point1); 
// second point used for training 
cv::Mat point2 = (cv::Mat_<float>(1, 2) << 255, 10); 
float response2 = svm->predict(point2); 

std::cout << "first point: " << response1 << "\n" << 
      << "second point: " << response2 << std::endl; 

如果有trainAuto RAN将输出0和1,而不是-1和1

回答