Hello !
I would like to switch from knn to svm in order to see how performance goes:
for knn This is the "script" I use it like that to generate a file call "raw-features.yml".
int main(int argc, char **argv)
{
// data to return
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_8UC1);
Mat labels(nbrOfCells, 1, CV_32S);
labels(1, nbrOfCells, CV_8UC1);
// Ptr<ml::KNearest> knn(ml::KNearest::create());
std::map<int, std::map<int, int>> knownCellValues(cellValues());
int value;
string fileName;
Mat raw, sudoku;
ExtractionInformation extractInfo;
string raw_features_path("./../../assets/raw-features.yml");
raw_features_path("./../assets/raw-features.yml");
cv::FileStorage raw_features(raw_features_path, cv::FileStorage::WRITE); // open the classifications file
Mat roi;
int incrCell = 0; // --> 1184
Mat roi, normalized;
for (int i = 0; i <= lastTrainingPuzzle; i++)
{
// cout << i << endl;
stringstream ss;
ss << "./../assets/puzzles/s";
ss << i;
ss << ".jpg";
string fileName(ss.str());
raw = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
vector<Point> biggestApprox = findBiggestBlob(raw);
extractInfo = extractPuzzle(raw, biggestApprox);
Mat sudoku = recursiveExtraction(extractInfo.image);
for (int k = 0; k < 81; k++)
{
roi = extractRoiFromCell(sudoku, k);
if (!roi.empty())
{
features.push_back(roi.reshape(1, 1));
labels.push_back(knownCellValues[i][k]);
}
}
}
value = knownCellValues[i][k];
Mat feat = roi.reshape(1, 1);
feat.copyTo(features.row(incrCell));
labels.at<unsigned char>(0, incrCell) = value;
incrCell++;
}
}
}
features.convertTo(features, CV_32F);
labels.convertTo(labels, CV_32F);
raw_features << "features" << features;
raw_features << "labels" << labels;
raw_features.release();
return 0;
}
Once "raw-features.yml" is ready, I can use it like that !
I use this the "getKnn" function from my project to train the knn algo.
Ptr<ml::KNearest> getKnn(cv::FileStorage raw_features)
{
int trainingNbr = nbrOfCells * 0.9;
int testingNbr = nbrOfCells - trainingNbr;
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_8UC1);
Mat labels(1, nbrOfCells, CV_8UC1);
Ptr<ml::KNearest> knn(ml::KNearest::create());
knn->train(features,
if (raw_features.isOpened() == false)
{
throw std::logic_error("error, unable to open training classifications file, exiting program\n\n");
}
raw_features["features"] >> features;
raw_features["labels"] >> labels;
raw_features.release();
Mat sub_features = features(cv::Range(0, trainingNbr), cv::Range::all());
Mat sub_labels = labels(cv::Range::all(), cv::Range(0, trainingNbr));
knn->train(sub_features, ml::ROW_SAMPLE, labels);
sub_labels);
return knn;
}
for svm I try to use I'm really happy of it, it works perfectly but I have a problem of performance so I would like to test other algo.
I created the same features & labels but it does not work like that function for SVM:
Ptr<ml::SVM> getSvm(FileStorage raw_features)
{
Mat features(nbrOfCells, normalizedSizeForCell * normalizedSizeForCell, CV_32F);
Mat svm_labels(nbrOfCells, 1, CV_32S);
raw_features["features"] >> features;
raw_features["svm_labels"] >> svm_labels;
raw_features.release();
Ptr<ml::SVM> svm = ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setDegree(3); // I had to put it if not it fails
svm->setGamma(3);
Ptr<ml::TrainData> tData = ml::TrainData::create(features, ml::SampleTypes::ROW_SAMPLE, labels);
svm_labels);
svm->train(tData);
return svm;
}
I tried some transfomation en labels (one column instead of one row) but use it fails I finish with this error message like that:
string grabNumbers(Mat extractedPuzzle, Ptr<ml::svm> svm)
{
Mat roi, response, dist;
stringstream ss;
for (int k = 0; k < 81; k++)
{
cout << k << endl;
roi = extractRoiFromCell(extractedPuzzle, k);
if (!roi.empty())
{
roi.convertTo(roi, CV_32F);
svm->predict(roi);
}
else
{
ss << "0";
}
}
return ss.str();
}
But I have this weird error I say weird because I already done the conversion !
Gtk-Message: Failed to load module "pantheon-filechooser-module"
OpenCV Error: Assertion failed ((unsigned)i0 < (unsigned)(size.p[0] * size.p[1])) (samples.cols == var_count && samples.type() == CV_32F) in at, predict, file /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp, /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/ml/src/svm.cpp, line 961
1930
terminate called after throwing an instance of 'cv::Exception'
what(): /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp:961: /home/benng/bin/opencv_working_dir/opencv-3.1.0/modules/ml/src/svm.cpp:1930: error: (-215) (unsigned)i0 < (unsigned)(size.p[0] * size.p[1]) samples.cols == var_count && samples.type() == CV_32F in function at
predict
Any ideas ?
Thank you !