opencv 3 (C++) auto trained SVM loading issue -
i'm using svm classification, , training in 1 project, testing in another, in order train once.
the training part follows:
classifier->trainauto(traindata); string svmdir = "/file/dir/"; string svmfile = "svmclassifier.xml"; classifier->save(svmdir+svmfile);
and testing part is:
string svmdir = "/file/dir/"; string svmfile = "svmclassifier.xml"; ptr<ml::svm> classifier = ml::svm::load<ml::svm>(svmdir+svmfile); ... float response = classifier->predict(tdescriptor);
the prediction gives 0s (all negative). when prediction right after svm training in training project, predictions correct (i used breakpoint before "predict", tdescriptor passed predict same in both projects.) think there might wrong saving , loading process..
can auto trained svm saved , loaded? or must in statmodel?
thanks help!
the flowing code taken , modified introduction support vector machines open cv. saved svm parameters object svmold "trainedsvm.xml". loaded xml file , used them create object svmnew.
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include "opencv2/imgcodecs.hpp" #include <opencv2/highgui.hpp> #include <opencv2/ml.hpp> using namespace cv; using namespace cv::ml; using namespace std; int main() { // data visual representation int width = 512, height = 512; mat image = mat::zeros(height, width, cv_8uc3); // set training data int lable[] = { 1, -1, -1, -1 }; mat labelsmat(4, 1, cv_32s, lable); float trainingdata[4][2] = { { 501, 10 },{ 255, 10 },{ 501, 255 },{ 10, 501 } }; mat trainingdatamat(4, 2, cv_32fc1, trainingdata); // set svm's parameters ptr<svm> svmold = svm::create(); svmold->settype(svm::c_svc); svmold->setkernel(svm::linear); svmold->settermcriteria(termcriteria(termcriteria::max_iter, 100, 1e-6)); // train svm given parameters ptr<traindata> td = traindata::create(trainingdatamat, row_sample, labelsmat); svmold->train(td); //same svm svmold->save("trainedsvm.xml"); //initialize svm object ptr<svm> svmnew = svm::create(); //load saved svm xml svmnew = svm::load<svm>("trainedsvm.xml"); vec3b green(0, 255, 0), blue(255, 0, 0); // show decision regions given svm (int = 0; < image.rows; ++i) (int j = 0; j < image.cols; ++j) { mat samplemat = (mat_<float>(1, 2) << j, i); float response = svmnew->predict(samplemat); if (response == 1) image.at<vec3b>(i, j) = green; else if (response == -1) image.at<vec3b>(i, j) = blue; } // show training data int thickness = -1; int linetype = 8; circle(image, point(501, 10), 5, scalar(0, 0, 0), thickness, linetype); circle(image, point(255, 10), 5, scalar(255, 255, 255), thickness, linetype); circle(image, point(501, 255), 5, scalar(255, 255, 255), thickness, linetype); circle(image, point(10, 501), 5, scalar(255, 255, 255), thickness, linetype); // show support vectors thickness = 2; linetype = 8; mat sv = svmnew->getsupportvectors(); (int = 0; < sv.rows; ++i) { const float* v = sv.ptr<float>(i); circle(image, point((int)v[0], (int)v[1]), 6, scalar(128, 128, 128), thickness, linetype); } imwrite("result.png", image); // save image imshow("svm simple example", image); // show user waitkey(0); return(0); }
Comments
Post a Comment