Reputation: 73
I need some help in training a SVM for an Android app. I have a set of images in different classes (12 classes) and got all descriptors from them. I managed to get the same amount of descriptors for each image. What I need is to train a SVM for my android application with those descriptors. I'm not sure if I should train it in the Android emulator or write a C++ program to train the SVM and then load it in my app (if I use the OpenCV's lib for windows to train the SVM and then save it, will the lib I'm using for Android recognize the saved SVM file?). I guess I shouldn't train the SVM with such big dataset in the emulator. I've already tested my descriptor's dataset on the SMO of Weka (http://www.cs.waikato.ac.nz/ml/weka/) and got good results, but I need to implement (or use the openCV's) SVM and save it trained for future classification.
Upvotes: 4
Views: 5496
Reputation: 27105
Here is an example for training your SVM in OpenCV4Android. trainData
is a MatOfFloat
, the form of which will depend on the method you're using to get feature vectors. To make trainData
, I used Core.hconcat()
to concatenate the feature vectors for each element of the dataset into a single Mat
.
Mat responses = new Mat(1, sizeOfDataset, CvType.CV_32F);
responses.put(0, 0, labelArray); // labelArray is a float[] of labels for the data
CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_svm_type(CvSVM.C_SVC);
params.set_kernel_type(CvSVM.LINEAR);
params.set_term_crit(new TermCriteria(TermCriteria.EPS, 100, 1e-6)); // use TermCriteria.COUNT for speed
svm.train_auto(trainData, responses, new Mat(), new Mat(), params);
I'm fairly sure OpenCV uses the same format to save SVMs in both the Android and C++ interfaces. Of course, you can always train the SVM in Android and save the XML file to your emulator's SD card using something like
File datasetFile = new File(Environment.getExternalStorageDirectory(), "dataset.xml");
svm.save(datasetFile.getAbsolutePath());
then pull it from the SD card and store it in your app's /res/raw
folder.
Upvotes: 6