0

This is the result after applying trained svm..i don't know why i got support vectors as 0's and why i got uncompressed support vectors. Can someone help me?

<?xml version="1.0"?>

-<opencv_storage>

-<opencv_ml_svm>

<format>3</format>

<svmType>C_SVC</svmType>

-<kernel>

<type>LINEAR</type>

</kernel>

<C>1.</C>

-<term_criteria>

<epsilon>9.9999999999999995e-07</epsilon>

<iterations>1000</iterations>

</term_criteria>

<var_count>8</var_count>

<class_count>2</class_count>

-<class_labels type_id="opencv-matrix">

<rows>2</rows>

<cols>1</cols>

<dt>i</dt>

<data> -1 1</data>

</class_labels>

<sv_total>1</sv_total>

-<support_vectors>

<_> 0. 0. 0. 0. 0. 0. 0. 0.</_>

</support_vectors>

<uncompressed_sv_total>2</uncompressed_sv_total>

-<uncompressed_support_vectors>

<_> 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00</_>

<_> 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00 1.00024951e+00</_>

</uncompressed_support_vectors>

-<decision_functions>

-<_>

<sv_count>1</sv_count>

<rho>1.</rho>

<alpha> 1.</alpha>

</_>

</decision_functions>

</opencv_ml_svm>

</opencv_storage>

Here is my code:

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <iostream>
#include <fstream>
#include<string.h>

#define POS 8
#define NEG 28
#define TOTAL 36 
#define DESCRIPT 2937060   

using namespace std;
using namespace cv;
using namespace cv::ml;

float trainingData[TOTAL][DESCRIPT];
int labels[TOTAL];

void set_labels() {

    int i;

    for (i = 0; i < TOTAL; i++) {

        if (i < POS) {
            labels[i] = 1; //positive 
        }

        else {
            labels[i] = -1; //negative
        }
    }
    return;
}

int main(int, char**)
{

    FileStorage fpd("Positive_image.xml", FileStorage::READ);
    FileStorage fnd("Negative_image.xml", FileStorage::READ);

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

//POSITIVE images
    for (int i = 1; i < POS + 1; i++) {
        stringstream a;
        a << i;
        fpd["Descriptors" + a.str()] >> pos_D;
        for (int j = 0; j < pos_D.size(); j++) {
            train_D.push_back(pos_D[j]);
        }
    }

//negative images
    for (int i = 1; i < NEG + 1; i++) {
        stringstream a;
        a << i;
        fpd["Descriptors" + a.str()] >> neg_D;
        for (int j = 0; j < neg_D.size(); j++) {
            train_D.push_back(neg_D[j]);
        }
    }
//total number of samples

    for (int i = 0; i < TOTAL; i++) {
        for (int j = 0; j < DESCRIPT; j++) {
            trainingData[i][j] = train_D[k];
            k++;
        }

    }

    Mat trainingDataMat(TOTAL, DESCRIPT, CV_32FC1, trainingData);//training data matrix

    Mat labelsMat(TOTAL, 1, CV_32SC1, labels);//label matrix

    // Train the SVM
    Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();

//set up SVM parameters
    svm->setType(SVM::C_SVC);
    svm->setKernel(SVM::LINEAR);
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

    cout << "Training SVM" << endl;

    svm->train(trainingDataMat, ROW_SAMPLE, labelsMat);
    cout << "Saving Trained SVM xml ..." << endl;

    svm->save("SVM.xml");//saving trained svm file 

    //Classification data
    vector< float> descriptorsValues;
    //vector to Mat
    Mat fm = Mat(descriptorsValues);
    Ptr<SVM> svm2 = Algorithm::load<SVM>("SVM.xml");//loading SVM file 
    std::cout << "Model Loaded" << std::endl;

    float result = svm->predict(fm);
    cout << "Predict value: " << result << endl;
    return(0);
}

Edited by tom_22

2
Contributors
1
Reply
26
Views
8 Months
Discussion Span
Last Post by rproffitt
0

I can't offer much here. So I began with a SVM primer at http://www.cs.columbia.edu/~kathy/cs4701/documents/jason_svm_tutorial.pdf
At the end of the tutorial I see the link and a google like https://www.google.com/search?q=www.kernel-machines.org&ie=utf-8&oe=utf-8 finds more scholarly pages and tutorials.

While you have a problem, your post didn't state the problem clearly. Either hit your prof for more direction, take more tutorials but one thing is clear. You have a lot of work to do.

Edited by rproffitt: Spelling

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.