Ask Your Question
0

SVM xml file support vectors all 0

asked 2016-11-04 15:45:34 -0600

aneksteind gravatar image

updated 2016-11-04 18:42:42 -0600

berak gravatar image

Hello. I am attempting to train a model using gradients from an HOG descriptor. In total, there are about 7000 images whose feature vectors I am writing to my trainingData matrix.

My svm finishes training within a minute and outputs a file whose support vectors all take on the form 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.

    // our labels are all initialized to being -1, Negative
    Mat gLabels = new Mat(posGCount + negCount, 1, CvType.CV_32FC1, new Scalar(-1.0));
    Mat aLabels = new Mat(posACount + negCount, 1, CvType.CV_32FC1, new Scalar(-1.0));

    // we overwrite the positive portion of the matrix with 1, Positive
    gLabels.rowRange(0,posGCount).setTo(new Scalar(1.0));
    aLabels.rowRange(0,posACount).setTo(new Scalar(1.0));

    Mat trainingMatG = new Mat(posGCount + negCount, 23328, gLabels.type());
//      Mat trainingMatA = new Mat(posACount + negCount, 23328, gLabels.type());

    //creating arrays for our feature vectors
    Mat[] featuresG = new Mat[posGCount];
    Mat[] featuresA = new Mat[posACount];
    Mat[] featuresN = new Mat[negCount];

    // TODO - add each feature vector to corresponding array
    // TODO - fix issue with insufficient memory


    for(File[] set : trainingSets){

        int count = 0;

        for(File image : set){

            // read in the image as a matrix
            Mat img = Highgui.imread(image.getAbsolutePath(), 0);

            // create a new descriptor
            HOGDescriptor descriptor = new HOGDescriptor(new Size(640,360),new Size(512,288), new Size(64,36), new Size(32,16), 9);

            // initialize a vector in which the features will be placed
            MatOfFloat descriptors = new MatOfFloat();

            // compute the feature vector and store it in 'descriptors'
            descriptor.compute(img, descriptors);

            if(set.equals(trainingSetG)) featuresG[count] = descriptors;
            if(set.equals(negative)) featuresN[count] = descriptors;

            count++;
            System.out.println(count);
        }
    }

    System.out.println("Adding features to training matrix...");

    for(int i=0;i<posGCount;i++){
        featuresG[i].copyTo(trainingMatG.rowRange(i,i+1));
    }
    for(int i=0;i<negCount;i++){
        featuresN[i].copyTo(trainingMatG.rowRange(i+posGCount,i+posGCount+1));
    }

    System.out.println("Added to Matrix");

    System.out.println("Training model...");

    CvSVM svm = new CvSVM();
    CvSVMParams params = new CvSVMParams();
    params.set_kernel_type(CvSVM.LINEAR);
    params.set_svm_type(CvSVM.C_SVC);
    params.set_C(1);

    svm.train(trainingMatG, gLabels, new Mat(), new Mat(), params);
    svm.save("C:/Users/danekstein/Desktop/svm.xml");
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2016-11-07 15:19:48 -0600

aneksteind gravatar image

updated 2016-11-07 15:21:33 -0600

The answer is that the featuresG[i] is 1x23328 in size. Being used to linear algebra where mxn signifies m rows and n columns, I believed that copying featuresG[i] into (trainingMatG.rowRange(i,i+1) was matching the correct column size of 23328, but no. In actuality, OpenCV's Mat.size() method returns columns x rows... So, after addressing the issue from this standpoint by setting features[count] = descriptors.t(), I was able to fix the problem.

edit flag offensive delete link more
-1

answered 2016-11-05 00:11:20 -0600

berak gravatar image

updated 2016-11-05 00:12:53 -0600

your labels must be integer (CV_32S) for classification

edit flag offensive delete link more

Comments

can you explain the reasoning behind this? all the examples I see use CV_32FC1, and when I switched to what you suggested I got a bad argument arrow saying it has to be a floating point matrix anyway

aneksteind gravatar imageaneksteind ( 2016-11-07 13:45:44 -0600 )edit

it's because my features in my array are not copying to my training matrix, but i'm not sure how to fix this problem

aneksteind gravatar imageaneksteind ( 2016-11-07 14:33:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-11-04 15:45:34 -0600

Seen: 529 times

Last updated: Nov 07 '16