SVM xml file support vectors all 0
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");