memory exception while training SVM
to train SVM classifier i use features extracted from different images ( each image have number of features =! of other images beacause i use SLIC classifier and from each region i extract 20 features) after preparing the TraingMatData and LabelsMat i got this bugwhith a notification : Microsoft C++ exception: cv::Exception at memory location 0x000000E5176E2740. I couldn't understand what's the wrong in my code here's my code and i'm waiting your helps.) whith a notification : Microsoft C++ exception: cv::Exception at memory location 0x000000E5176E2740. I couldn't understand what's the wrong in my code here's my code and i'm waiting your helps.
int main()
{
/// training
cv::Mat image;
//load the features from files
Mat trainingDataMat(47,27000, CV_32FC1);
float features[1350][20];
float labels[47] = {-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,... i delet the rest of them};
vector<int> size;
for (int i = 1; i < 48; i++) {
vector<float> numbers;
//Mat features(1200, 20, CV_32FC1);
string filename = "D:\\mémoire\\training\\trainingData\\featuers";
filename = filename + std::to_string(i);
filename = filename + ".txt";
int size_e = 0;
//read(numbers, filename);
ifstream in(filename);
float number;
string aa;
while (in.good()) {
getline(in, aa, ',');
number = strtod(aa.c_str(), NULL);
numbers.push_back(number);
size_e = size_e + 1;
//features.push_back(number);
}
in.close();
size.push_back(size_e);
int pp = numbers.size()/ 20;
int d = 0;
for (int p = 0; p < 1350; p++) {
for (int p1 = 0; p1 < 20; p1++) {
if (pp <= d) {
features[p][p1] = 0;
}
else {
//features.at<uchar>(p, p1) = numbers[d];
features[p][p1] = numbers[d];
d++;
}
}
}
cv::Mat A(1350, 20, CV_32FC1, features);
trainingDataMat.push_back(A.reshape(1, 1));
}
Mat labelsMat(47, 1, CV_32FC1, labels);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//seting the SVM
Ptr<ml::SVM> svm = ml::SVM::create();
// edit: the params struct got removed,
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::RBF);
svm->setGamma(100);
//Mat trainData; // one row per feature
svm->train(trainingDataMat, ml::ROW_SAMPLE, labelsMat);
// ...
imho, you should train / classify on SLIC regions, not on whole images.
also: if you preallocate a vector / Mat , AND use push_back(), you will end up with twice the expected size, and a ton of uninitialized elements in the front, just saying ...
then - you need integer, not float labels for a classification here.
i changed the Labels Mat to integer and nothing i still have the same error. i have question, the training Mat and the Labels Mat must have the same number of Row or the number of rows of the trainMat = cols of the labels Mat ?!
same number of rows.
and look at your push_back() operations , again.
what do you think of the objection, above ? (you cannot do this "per image")
(imho, you'll have to rewrite this part anyway)
'm using the push_back(reshape(1,1)) to put all my features ( from files) in the same Mat, i looked in many tuto and i followed them that it ;)
no, i mean:
if you push_back() further rows, you'll end up with 2 * 47 rows here
this is my first time with reshape and i know that reshape transform my Mat (1350,20) in my case to one row in other Mat , so the cols of the TrainMat must be (1350*20 = 27000). is that right or i'm missing something ?!
all i'm saying is: you have to leave
trainingDataMat
empty.