简体   繁体   English

SVM训练错误(样本类型的断言失败)

[英]Error with SVM training (assertion failed for sample type)

I am trying to use SVM to match a query image with its appropriate class. 我正在尝试使用SVM将查询图像与其相应的类进行匹配。 Right now the classes are just 1 or 0. I extract the class from a .txt file and store it into a Mat. 现在,类仅为1或0。我从.txt文件中提取该类并将其存储到Mat中。 I use BoW to compute a histogram for each image in the training set, and also store it into a Mat. 我使用BoW为训练集中的每个图像计算直方图,并将其存储到Mat中。

Mat response_hist;
Mat histograms;
Mat classes;
ifstream ifs("train.txt");
int total_samples_in_file = 0;
vector<string> classes_names;
vector<string> lines;


for (int i = 1; i <= trainingSetSize; i++){
    cout << "in for loop iteration"<< i << endl;
    _snprintf_s(filepath, 100, "C:/Users/Randal/Desktop/TestCase1Training/train/%d.bmp", i);
    Mat temp = imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
    Mat tempBW;
    adaptiveThreshold(temp, tempBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
    detector->detect(tempBW, keypoints1);
    BOW.compute(tempBW, keypoints1, response_hist);
    response_hist.convertTo(response_hist, CV_32F);
    histograms.push_back(response_hist);
}

    //read from the file - ifs and put into a vector
    std::string line;
    float class_num;
    string imgfilepath;
    for (int j = 1; getline(ifs, line); j++)
        {
            istringstream ss(line);             
            ss >> imgfilepath >> class_num;
            classes.push_back(class_num);

        }

The Mats class_num and histograms are used in training the SVM. Mats class_num和直方图用于训练SVM。 Each row in "histograms" represents a sample (a histogram of an image in the training set). “直方图”中的每一行代表一个样本(训练集中图像的直方图)。 "class_num" is one row with each column being the class (1 or 0) of a corresponding image in the training set. “ class_num”是一行,每一列是训练集中相应图像的类(1或0)。

Ptr<ml::SVM> svm = ml::SVM::create();

svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setGamma(3);

Mat trainingDataMat(histograms);
Mat trainingDataClass(classes);

trainingDataMat.convertTo(trainingDataMat, CV_32F);
trainingDataMat = trainingDataMat.reshape(trainingDataMat.cols, 1);
trainingDataClass.convertTo(classes, CV_32F);
svm->train(trainingDataMat, ml::ROW_SAMPLE, trainingDataClass); //incorrect types? I think it is a problem with ROW_SAMPLE
Mat res;   // output
svm->predict(output, res);

When I run this I get the error "Assertion failed (samples.type() == CV_32F || samples.type() == CV_32S) in cv::ml::TrainDataImpl::setData". 运行此命令时,出现错误“在cv :: ml :: TrainDataImpl :: setData中断言失败(samples.type()== CV_32F || samples.type()== CV_32S)”。 However, I have placed lines of code in to convert both my class Mat and my histogram Mat to type CV_32F. 但是,我放置了几行代码以将类Mat和直方图Mat都转换为CV_32F类型。 Is the issue with my inputs or does it have something to do with ROW_SAMPLE in svm->train? 是我输入的问题,还是和svm-> train中的ROW_SAMPLE有关? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks 谢谢

The error was caused by my input being incorrect. 该错误是由于我的输入不正确引起的。 I changed the type of Mat classesMat to CV_32S in its declaration. 我在其声明中将Mat classesMat的类型更改为CV_32S。 I also changed 我也变了

trainingDataMat_32.reshape(trainingDataMat_32.cols, 1); trainingDataMat_32.reshape(trainingDataMat_32.cols,1);

to have the correct number of channels and rows. 具有正确数量的通道和行。

trainingDataMat_32.reshape(1, trainingDataMat_32.rows); trainingDataMat_32.reshape(1,trainingDataMat_32.rows);

TrainingDataMat.cols was not the correct value at all. TrainingDataMat.cols根本不是正确的值。 It needed 1 channel (first parameter) and the same number of rows as my histogram input. 它需要1个通道(第一个参数),并且行数与我的直方图输入相同。

This caused a new error regarding the kernel I am using (SVM parameter "POLY"). 这导致有关我正在使用的内核的新错误(SVM参数“ POLY”)。 I had to add another line right below the kernel parameter: 我必须在内核参数下添加另一行:

svm->setDegree(3); svm-> setDegree(3);

This fixed the error. 这解决了错误。 My output is not correct, but this solved the assertion failure. 我的输出不正确,但是这解决了断言失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM