Reputation: 253
I have feature vector of size 100 .Total training samples are 500 in which there are 10 samples of each class.I want to design a separate svm classifier for each class.That is classifier of each class will be fed with 10 positive(for that class) and 490 negative instances. My opencv code is as follows
For training:
Mat trainingDataMat(500, 100, CV_32FC1, trainingData);//trainingData is 2D MATRIX
Mat labelsMat(500, 1, CV_32FC1, labels);//10 positive and 490 -ve labels
CvSVMParams params;
params.svm_type = SVM::C_SVC;
params.kernel_type = SVM::RBF;
CvSVM SVM;
SVM.train_auto(trainingDataMat, labelsMat, Mat(), Mat(), params,5);
SVM.save(name);
For testing
Mat sampleMat(1, size, CV_32FC1, testing_vector);// testing_vector is 1D vector
CvSVM SVM;
SVM.load(name);
float response = SVM.predict(sampleMat);
The problem is that the classifier for class outputs -1 even when I give positive testing sample from the training set and same is the case for other testing samples.
I also tried ONE_CLASS svm but it gives 0 for every testing sample.
Where am I going wrong or what svm type should I use?Please explain with code if possible. Thank you in advance.
Upvotes: 2
Views: 4022
Reputation: 5250
It seems you've missed the normalization step. SVM classifier in OpenCV uses the same as libsvm, and if you read the documentation of libsvm it says you should normalize your train data in the interval [-1,1] and get scale parameters. Then use those scale parameters to scale your test data. This might be the one problem. Or it can be because of non-equivalent number of positive and negative samples. Did tried to classify your train data as a cross validation, after you have trained the SVM?
Upvotes: 4
Reputation: 10852
Try use linear kernel and approximately equal positives and negatives, for each class. You can ajust precision/recall by setting values of gamma and cost parameters. Take a look at: The gamma and cost parameter of SVM
Upvotes: 0