svm train method segmantation fault problem

asked 2014-01-16 04:05:14 -0500

jossyy gravatar image

updated 2014-01-16 04:09:03 -0500

Hello,

I am using svm train method.But segmentation fault is occurred train method's line.I am using linux machine. My code is :

    Mat responseMat(resSize, 1, CV_32FC1, res);
Mat trainingDataMat(trainSize, featureSize, CV_32FC1,trainingData);
CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
cout << "svm settings" << endl;

    // Train the SVM
    CvSVM SVM;
    SVM.train(trainingDataMat, responseMat, Mat(), Mat(), params);
cout << "svm train" << endl;
edit retag flag offensive close merge delete

Comments

1

since you're using a construct like : Mat responseMat(resSize, 1, CV_32FC1, res);

if the res pointer leaves the scope, before you're done with processing responseMat, you're doomed.

try: Mat responseMat = Mat(resSize, 1, CV_32FC1, res).clone(); // for a deep copy of the contents.

same for trainingDataMat

berak gravatar imageberak ( 2014-01-16 04:26:11 -0500 )edit
1

@hsrt What is your trainingData? Looks like the problem is there.

Daniil Osokin gravatar imageDaniil Osokin ( 2014-01-16 08:44:33 -0500 )edit

My train data :

float *trainingData; trainingData = new float[trainSize]; for(int i = 0; i < trainSize; i++) {

    trainingData[i] = new float[featureSize];
    /*for(int j = 0; j &lt; featureSize; j++) {

        trainingData[i][j] = trainVector[i][j];
    }
}

t

trainVector is filled in my code...

jossyy gravatar imagejossyy ( 2014-01-17 17:10:11 -0500 )edit

Dear Berak, "res" is still alive on this line...

jossyy gravatar imagejossyy ( 2014-01-17 17:13:09 -0500 )edit
1

so, trainingData is a float** ? that won't be continuous memory

berak gravatar imageberak ( 2014-01-18 01:52:14 -0500 )edit

yes beark , you are right...

How can I create continuous array with dynamic size? it looks like :

Mat sampleMat = (Mat_<float>(1,featureSize) << (trainData[i][0], trainData[i][1], trainData[i][2]....));

jossyy gravatar imagejossyy ( 2014-01-18 02:32:37 -0500 )edit

ah, that explains your other question ;)

what if you simply push_back() one value after the other, and reshape(1,cols) afterwards to get a NxM matrix ?

berak gravatar imageberak ( 2014-01-18 02:46:51 -0500 )edit

I am jumping between questions :)

I will try this way...

jossyy gravatar imagejossyy ( 2014-01-18 03:11:45 -0500 )edit