How to use my HOG matrix in the SVM classifier? [closed]

asked 2017-03-02 18:23:25 -0600

Allison gravatar image

I want to use the normalised HOG matrix which contains the features of my image. How to take the HoG matrix to use it in the SVM classifier? Does anyone has any idea how to do it?

Here is my HOG matrix:

FileStorage f("Normalised_HOG.xml", FileStorage::READ);

Here is my data labelling :

//assign label
        Mat labels(num_files, 1, CV_32FC1);

        float trainingData;
        cv::Mat trainDataMat(num_files, DESCRIPT, CV_32FC1, trainingData);

Here is my SVM Parameters

//set up SVM Parameters
    Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
    svm->setType(SVM::C_SVC);
    svm->setKernel(SVM::LINEAR);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
    // Train the SVM
    svm->train(trainDataMat, ROW_SAMPLE, labels);
    cout << "Saving Trained SVM xml ..." << endl;
    //svm->write(FileStorage("test.xml", FileStorage::WRITE));
    svm->save("SVM.xml");
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-18 16:09:07.074904

Comments

1

it seems your code is training then saving your svm classifier. once you have your .xml file you just have to load it and then use svm->predict to classify test images. is this comment revealing or do I misunderstood your question?

guidovitale gravatar imageguidovitale ( 2017-03-03 05:04:25 -0600 )edit

Yes my code is to train then save the svm classifier but the problem is i don't know how to use the normalised HOG xml file containing the features of the image in training SVM. @guidovitale

Allison gravatar imageAllison ( 2017-03-03 08:08:55 -0600 )edit

something like:

  • load your svm

Ptr<ml::svm> svm = ml::SVM::load<cv::ml::svm>(yourSVMfilename);

  • compute hog features (same parameters you used for training!)

HOGd.compute(yourImage, descriptorsValues);

  • classify with the svm:

previsionLabel = svm->predict(descriptorsValues, noArray(), 0);

is it helpful?

guidovitale gravatar imageguidovitale ( 2017-03-03 09:02:23 -0600 )edit

Don't we have to use the xml file in the training set as shown below?

  FileStorage f("Normalised_HOG.xml", FileStorage::READ);



 // Set up training data
        vector <float> train_D, pos_D;
        int k = 0;

        for (int i = 1; i < POS + 1; i++) {
            stringstream a;
            a << i;
            f ["Descriptors" + a.str()] >> pos_D;
            for (int j = 0; j < pos_D.size(); j++) {
                train_D.push_back(pos_D[j]);
            }
        }
Allison gravatar imageAllison ( 2017-03-03 09:45:53 -0600 )edit

the .xml file contains the trained SVM data you'll use for prediction (i.e. support vectors, margin, etc...); try to open it with a txt editor to see that. once you have it, just use it for prediction (svm->predict...); it is meaningless to re-train another svm using the .xml as you did.

guidovitale gravatar imageguidovitale ( 2017-03-03 10:40:35 -0600 )edit

I think you have misunderstood my question. I need to take the HOG matrix (saved as xml file) and use it in training the SVM since it contains the features. How to do it ?

Allison gravatar imageAllison ( 2017-03-03 11:03:16 -0600 )edit

@Allison, you probably need to explain better, what exactly is inside your HOG matrix. (exact shape, type, how you got there, etc.)

berak gravatar imageberak ( 2017-03-04 03:28:37 -0600 )edit

you've already trained your svm when typing:

// Train the SVM
    svm->train(trainDataMat, ROW_SAMPLE, labels);

you don't need to train it another time

guidovitale gravatar imageguidovitale ( 2017-03-06 02:55:02 -0600 )edit