Ask Your Question
0

svm->train raise exception with out train

asked 2017-04-08 02:05:09 -0500

aligoglos gravatar image

i have vector<vector<float> > of features in size 1800*160 now i need train svm on it, i try use OPENCV SVM but in debug mode svm->train return false and in release mode this exeption raised :

Exception thrown at 0x00007FFF587AC387 (vcruntime140.dll):Access violation reading location 0x00000048B7FED000.

my code:

void Classifier::trainSVM(vector<vector<float> > data,cv::Mat Lable)
{
    // Train the SVM
    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, 50000, 1e-6));
    cv::Mat trainingData = cv::Mat(data.size(), 160, CV_32FC1, data.data());
    std::cout << "\nBegan Training Svm in vector faces.";
    bool trained = svm->train(trainingData, cv::ml::ROW_SAMPLE, Lable);
    if (trained)
        svm->save("svm_data.xml");
    std::cout << "\nEnd Training Svm in vector faces.";

}

my opencv version is: 3.2

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-08 02:16:37 -0500

berak gravatar image

your vector<vector<float>> is the culprit, opencv's machine learning expects a single Mat with continuous data.

you will need to copy it into a Mat, each vector goes on a single row, maybe like this:

vector<vector<float>> vf {{1,2,3},{4,5,6},{7,8,9}}; // demo data

Mat data;
for ( auto v : vf ) {
    Mat row = Mat(v, true).reshape(1,1); // deep copy, reshape to row
    data.push_back(row);
}

cerr << data << endl;

[1, 2, 3;
 4, 5, 6;
 7, 8, 9]
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-08 02:05:09 -0500

Seen: 168 times

Last updated: Apr 08 '17