How to form data for SVM training OpenCV3
I am trying to write utility for training svm classifier for image classification in OpenCV3. But I have Floating point exception (core dumped) error during training process.
My main problem is that I don't know, I'm not sure exactly how to form training data to feed svm.train method.
This is code which is forming training data.
TrainingDataType SVMTrainer::prepareDataForTraining() {
cv::Mat trainingData(m_numOfAllImages, 28*28, CV_32FC1);
cv::Mat trainingLabels(m_numOfAllImages, 1, CV_32FC1);
int rowNum = 0;
// Item is pair of classId (int) and vector of images.
for(auto item : m_data){
int classId = item.first;
for(auto item1 : item.second){
Mat temp = item1.reshape(1,1);
temp.copyTo(trainingData.row(rowNum));
trainingLabels.at<float>(rowNum) = item.first;
++rowNum;
}
}
return cv::ml::TrainData::create(trainingData,
cv::ml::SampleTypes::ROW_SAMPLE,
trainingLabels) ;
}
void SVMTrainer::train(std::string& configPath){
// Read and store images in memory.
formClassifierData(configPath);
m_classifier = cv::ml::SVM::create();
// Training parameters:
m_classifier->setType(cv::ml::SVM::C_SVC);
m_classifier->setKernel(cv::ml::SVM::POLY);
m_classifier->setGamma(3);
m_classifier->setDegree(3);
TrainingDataType trainData = prepareDataForTraining();
m_classifier->trainAuto(trainData);
}
All images are already prepared with dimensions 28*28, black&white.
And actual train call is in this method
Can somebody tell me what I am doing wrong.
Thanks,
where exactly do you get the error ? (seems nothing wrong with the code)
During training... Exactly after call of trainAuto. During that method it shows me floating point exception and terminate program.
might be a problem within the svm. are you able to run a debugger ?
what exactly is
TrainingDataType
? is it aPtr<ml::TrainData>
?how many images are there ? (if not many, maybe someone can reproduce it with the same trainset)
using TrainingDataType = cv::Ptr<cv::ml::traindata>; Training set is fairly simple, ~45 images, very small set just to see if I get it done. I can zip it all and send :)
Here is whole project in .tar.gz... https://goo.gl/gWAewI
I should be able to run debugger, I will give it a try when I get back home..
unfortunately, i cannot reproduce it
when did you last update your opencv repo ? there were changes wrt. SVM lately
I downloaded library not long after first stable release, at the end of June I think... Did everything go well? I mean did utility produced successfully .xml classifier file?
yes, i added a prediction step on the testingDIR, result is always 1 (not surprising)
there was e.g. a bug in trainAuto()
try to add that patch (or do a fresh git pull) .
(also, for 3.0 - good idea to keep an eye on github...)
Thank you very much :D I had serious headache about this one :D