在训练机器学习算法时出现未处理的异常

我正在尝试使用OpenCV实现词袋模型。在创建词典后,我使用NormalBayesClassifier来训练和预测系统。

我按照文档的要求准备了trainme矩阵,每个样本位于每一行。但问题是,在这一行classifier.train(trainme, labels);出现了未处理的异常。

我使用的完整代码如下:

int _tmain(int argc, _TCHAR* argv[]){initModule_nonfree();Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");//定义bowkmeans训练器的参数TermCriteria tc(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 10, 0.001);int dictionarySize = 100;int retries = 1;int flags = KMEANS_PP_CENTERS;BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);BOWImgDescriptorExtractor bowDE(descriptor, matcher);//**创建词典**//Mat features1, features2;Mat img = imread("c:\\1.jpg", 0);Mat img2 = imread("c:\\2.jpg", 0);vector<KeyPoint> keypoints, keypoints2;features->detect(img, keypoints);features->detect(img2,keypoints2);descriptor->compute(img, keypoints, features1);descriptor->compute(img2, keypoints2, features2);bowTrainer.add(features1);bowTrainer.add(features2);Mat dictionary = bowTrainer.cluster();bowDE.setVocabulary(dictionary);//**词典已创建**////**现在训练分类器**//Mat trainme(0, dictionarySize, CV_32FC1); Mat labels(0, 1, CV_32FC1); //1d矩阵,32fc1是normalbayesclassifier类的要求Mat bowDescriptor, bowDescriptor2;bowDE.compute(img, keypoints, bowDescriptor);trainme.push_back(bowDescriptor);float label = 1.0;labels.push_back(label);bowDE.compute(img2, keypoints2, bowDescriptor2);trainme.push_back(bowDescriptor2);labels.push_back(label);NormalBayesClassifier classifier;classifier.train(trainme, labels);//**分类器已训练**////**现在尝试使用相同的训练过的分类器进行预测,应该返回1.0**//Mat tryme(0, dictionarySize, CV_32FC1);Mat tryDescriptor;Mat img3 = imread("2.jpg", 0);vector<KeyPoint> keypoints3;features->detect(img3, keypoints3);bowDE.compute(img3, keypoints3, tryDescriptor);tryme.push_back(tryDescriptor);cout<<classifier.predict(tryme);waitKey(0);return 0;}

回答:

我已经解决了这个问题,问题出在这里:float label = 1.0;因为所有训练的图像不能有相同的标签。系统必须能够区分给定的图像,因此最好将图像按组排列,并为这些组分配浮点值。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注