getting error in multiclass SVM classification??

asked 2019-01-10 00:28:21 -0500

arti gravatar image

updated 2019-01-10 00:43:10 -0500

berak gravatar image

Hello all, I'm getting error in my code. here i'm doing multiclass classification using svm with 20 classes. here the training data of each class is feature vector .yml file named as Dictionay1 , Dictionay2, ......Dictionay20 such as

vocabulary: !!opencv-matrix
   rows: 100
   cols: 1
   dt: f
   data: [ 4.48574181e+01, 3.61391605e-04, 1.06644783e+02,
       1.75997910e+02, 8.53040619e+01, 2.40742096e+02, 1.35315109e+02,
              9.74207458e+01, 7.31249542e+01, 5.95427322e+01, 3.08762417e+01............
%YAML:1.0
---
vocabulary: !!opencv-matrix
   rows: 100
   cols: 1
   dt: f
   data: [ 6.63631945e-04, 1.00589867e+02, 1.60471497e+02,
       4.06750679e+01, 1.32695053e+02, 2.54314590e+02, 8.82780228e+01... etc

My code is

char filename[80];

int main(int argc, char* argv[])
{
    Mat trainData, trainLabels;

        for (int i = 0; i < 22; i++ )
        {
        sprintf_s(filename, "VocabularyHOG/Dictionary%d.yml", i);
        Mat feature;
        FileStorage fs(filename, FileStorage::READ);
        fs["vocabulary"] >> feature;
        feature.convertTo(feature, CV_32F); // make sure we got float data
        trainData.push_back(feature.reshape(1, 1)); // as a flat column
        trainLabels.push_back(i); // the classlabel
        }

    // Train the SVM
    Ptr<SVM> svm = SVM::create();
    svm->setType(SVM::C_SVC);
    svm->setKernel(SVM::LINEAR);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
    svm->train(trainData, ROW_SAMPLE, trainLabels);

    Mat testData;
    FileStorage fs("DictionaryrunningHOG.yml", FileStorage::READ);
    fs["vocabulary"] >> testData;
    int response = svm->predict(testData);
    if (response == 1)
        cout << "boxing";
    else
        cout << "negative result";

    waitKey(27);
    return 0;
}

I'm getting errorimage description

please help where i'm doing wrong. Thanks

edit retag flag offensive close merge delete

Comments

you'll have to reshape() your testData to a single row, too.

apart from that, clearly a case, where you have to learn how to use the debugger ...

the screenshot is useless, but a backtrace would be useful.

berak gravatar imageberak ( 2019-01-10 01:03:16 -0500 )edit

then, you need more and better data. you cannot do successful machinelearning with a single example per class only.

berak gravatar imageberak ( 2019-01-10 01:47:07 -0500 )edit
1

Thank you sir or your response. i have taken 25 example videeos per class, then i created vocabulary from them using BOW. so Dictionary1 is vocabulary using these 25 videos......i'm doing any thong wrong here? i have to convert my data into single row??

arti gravatar imagearti ( 2019-01-10 02:32:15 -0500 )edit

oh, ok. your code above is just reading one yml file, containing a single feature per class only, right ??

svm->predict(testData.reshape(1,1))
berak gravatar imageberak ( 2019-01-10 02:50:46 -0500 )edit

yes sir this code is for single feature per class.

arti gravatar imagearti ( 2019-01-10 02:56:28 -0500 )edit

i have taken 25 example videeos per class, then i created vocabulary from them using BOW

that sounds weird. can you take a look here again, and then try to describe in more detail, what you did exactly with your videos ?

berak gravatar imageberak ( 2019-01-10 02:58:29 -0500 )edit

converted testData in single row but again the same error??

arti gravatar imagearti ( 2019-01-10 03:01:36 -0500 )edit

again, you have to debug it ;(

berak gravatar imageberak ( 2019-01-10 03:04:00 -0500 )edit

Thank you sir for your response....after debug It is crash in line trainData.push_back(feature.reshape (1,1));...I'm not getting what I'm doing wrong.

arti gravatar imagearti ( 2019-01-10 22:30:46 -0500 )edit