Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Training SVM Error - Assertion failed

Hello everyone, basically I have this code:

cout << "extracting histograms in the form of BOW for each Training image " << endl;
Mat labels(0, 1, CV_32S);
int dictSize = 1500;

Mat trainingData(0, dictSize, CV_32S);
int k = 0;
vector<KeyPoint> keypoint;
Mat bowDescriptor;
Mat img;

vector<String> fn;
glob("C:/Users/albma/Desktop/Università/Computer Vision/Final_Project/training_alberi/*.jpg", fn, true);

//extracting histogram in the form of bow for each image
for (size_t i = 0; i < fn.size(); i++) { //each class having 60 images

    printf("Training Image = %s\n", fn[i]);
    img = imread(fn[i], 0);
    detector->detect(img, keypoint); //detect keypoints
    bowDE.compute(img, keypoint, bowDescriptor); //compute descriptors
    trainingData.push_back(bowDescriptor);
    labels.push_back((int)1); //push labels in a matrix.. 1,2,3,4
}


//SVM Part

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));



printf("Training SVM\n");
Ptr<TrainData> td = TrainData::create(trainingData, ROW_SAMPLE, labels); //start training SVM
svm->train(td);

And the last 2 lines gives me the error: OpenCV(4.3.0-pre) Error: Assertion failed ((layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples)) in cv::ml::TrainDataImpl::setData

What could be the problem?

Training SVM Error - Assertion failed

Hello everyone, basically I have this code:

cout << "extracting histograms in the form of BOW for each Training image " << endl;
Mat labels(0, 1, CV_32S);
int dictSize = 1500;

Mat trainingData(0, dictSize, CV_32S);
trainingData;
int k = 0;
vector<KeyPoint> keypoint;
Mat bowDescriptor;
Mat img;

vector<String> fn;
glob("C:/Users/albma/Desktop/Università/Computer Vision/Final_Project/training_alberi/*.jpg", fn, true);

//extracting histogram in the form of bow for each image
for (size_t i = 0; i < fn.size(); i++) { //each class having 60 images

    printf("Training Image = %s\n", fn[i]);
    img = imread(fn[i], 0);
    detector->detect(img, keypoint); //detect keypoints
    bowDE.compute(img, keypoint, bowDescriptor); //compute descriptors
    trainingData.push_back(bowDescriptor);
    labels.push_back((int)1); labels.push_back((float)1); //push labels in a matrix.. 1,2,3,4
}


//SVM Part

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
 

printf("Training SVM\n");
Ptr<TrainData> td = TrainData::create(trainingData, ROW_SAMPLE, labels); //start training SVM
svm->train(td);

And the last 2 lines gives me the error: OpenCV(4.3.0-pre) Error: Assertion failed ((layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples)) in cv::ml::TrainDataImpl::setData

What could be the problem?

Training SVM Error - Assertion failed

Hello everyone, basically I have this code:

Mat labels(0, 1, CV_32S);
#include <opencv2/imgcodecs.hpp>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d.hpp>


using namespace cv;
using namespace std;
using namespace xfeatures2d;
using namespace ml;

int main() {
//load dictionary first
Mat dictionary;
FileStorage fs("myDictionary.yml", FileStorage::READ);
fs["vocabulary"] >> dictionary;
fs.release();

Ptr<SIFT> detector = SIFT::create(); //detector to detect SIFT features 


Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);

BOWImgDescriptorExtractor bowDE(detector, matcher);
bowDE.setVocabulary(dictionary);


cout << "extracting histograms in the form of BOW for each Training image " << endl;
Mat labels;
int dictSize = 1500;
400;

Mat trainingData;
int k = 0;
vector<KeyPoint> keypoint;
Mat bowDescriptor;
Mat img;

vector<String> fn;
glob("C:/Users/albma/Desktop/Università/Computer Vision/Final_Project/training_alberi/*.jpg", fn, true);

//extracting histogram in the form of bow for each image
for (size_t i = 0; i < fn.size(); i++) { //each class having 60 images

    printf("Training Image = %s\n", fn[i]);
    img = imread(fn[i], 0);
    detector->detect(img, keypoint); //detect keypoints
    bowDE.compute(img, keypoint, bowDescriptor); //compute descriptors
    trainingData.push_back(bowDescriptor);
    labels.push_back((float)1); labels.push_back((int)1); //push labels in a matrix.. 1,2,3,4
1
}

cout << trainingData.size() << endl;
cout << labels.size() << endl;

//SVM Part

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));


printf("Training SVM\n");
Ptr<TrainData> td = TrainData::create(trainingData, ROW_SAMPLE, labels); //start training SVM
svm->train(td);

And the last 2 lines gives me the error: OpenCV(4.3.0-pre) Error: Assertion failed ((layout == ROW_SAMPLE && responses.rows == nsamples) || (layout == COL_SAMPLE && responses.cols == nsamples)) in cv::ml::TrainDataImpl::setData

What could be the problem?