C++ openCV c++;:使用CvBoost(Adaboost Classifier)时出现问题
我正在创建一个应用程序,用于在城市环境的图像中对人类进行分类 我以以下方式培训一名分级员:C++ openCV c++;:使用CvBoost(Adaboost Classifier)时出现问题,c++,opencv,adaboost,C++,Opencv,Adaboost,我正在创建一个应用程序,用于在城市环境的图像中对人类进行分类 我以以下方式培训一名分级员: int main (int argc, char **argv) { /* STEP 2. Opening the file */ //1. Declare a structure to keep the data CvMLData cvml; //2. Read the file cvml.read_csv ("directory/train_rand.csv"); //3. Indicate
int main (int argc, char **argv)
{
/* STEP 2. Opening the file */
//1. Declare a structure to keep the data
CvMLData cvml;
//2. Read the file
cvml.read_csv ("directory/train_rand.csv");
//3. Indicate which column is the response
cvml.set_response_idx (0);
/* STEP 3. Splitting the samples */
//1. Select 4000 for the training
CvTrainTestSplit cvtts (4000, true);
//2. Assign the division to the data
cvml.set_train_test_split (&cvtts);
printf ("Training ... ");
/* STEP 4. The training */
//1. Declare the classifier
CvBoost boost;
//2. Train it with 100 features
boost.train (&cvml, CvBoostParams (CvBoost::REAL,100, 0, 1, false, 0),
false);
/* STEP 5. Calculating the testing and training error */
// 1. Declare a couple of vectors to save the predictions of each sample
std::vector<float> train_responses, test_responses;
// 2. Calculate the training error
float fl1 = boost.calc_error (&cvml, CV_TRAIN_ERROR, &train_responses);
// 3. Calculate the test error
float fl2 = boost.calc_error (&cvml, CV_TEST_ERROR, &test_responses);
cout<<"Error train: "<<fl1<<endl;
cout<<"Error test: "<<fl2<<endl;
/* STEP 6. Save your classifier */
// Save the trained classifier
boost.save ("./trained_boost_4000samples-100ftrs.xml", "boost");
return 0;
}
对于我的实际问题,我使用了100个特性和8000个示例。我用一半的数据训练分类器,用剩下的数据测试分类器
经过培训后,我得到了大约5%的测试错误(这对于100个特性来说非常好)
现在我想在新数据中使用分类器:
CvBoost boost
boost.load("directory/trained_boost_4000samples-100ftrs.xml");
float x = boost.predict(SampleData,Mat(),Range::all(),false,false);
cout<<x;
CvBoost
load(“directory/trained_boost_4000samples-100ftrs.xml”);
float x=boost.predict(SampleData,Mat(),Range::all(),false,false);
cout我通过调整。唯一的诀窍是确保有足够的样本数据(>=11)
发件人:
注意:由于一个非常奇怪的原因,OpenCV实现不能使用少于11个样本
//训练数据
浮动标签[11]={1.0,1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0};
Mat标签Mat(11,1,CV_32FC1,标签);
浮动训练数据[11][2]={
{501, 10}, {508, 15},
{255, 10}, {501, 255}, {10, 501}, {10, 501}, {11, 501}, {9, 501}, {10, 502}, {10, 511}, {10, 495} };
Mat培训数据Mat(11,2,CV_32FC1,培训数据);
//建立支持向量机的参数
CvSVMParams参数;
params.svm_type=CvSVM::C_SVC;
params.kernel_type=CvSVM::LINEAR;
参数term_crit=CVTerm标准(CVTerm crit_ITER,100,1e-6);
//训练支持向量机分类器
支持向量机;
训练(训练数据表,标签表,表(),表(),参数);
//训练增压分类器
CvBoost-boost;
增压列车(列车数据垫,
CV_行_样本,
标签纸);
//测试分类器
Mat testSample1=(Mat_u1;(1,2)我找不到任何关于训练数据格式的文档。您确定第一列是标签吗?我看到的示例也使用实数作为标签,您能将B
/R
映射到-1.0
/1.0
?
CvBoost boost
boost.load("directory/trained_boost_4000samples-100ftrs.xml");
float x = boost.predict(SampleData,Mat(),Range::all(),false,false);
cout<<x;
// Training data
float labels[11] = { 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
Mat labelsMat(11, 1, CV_32FC1, labels);
float trainingData[11][2] = {
{501, 10}, {508, 15},
{255, 10}, {501, 255}, {10, 501}, {10, 501}, {11, 501}, {9, 501}, {10, 502}, {10, 511}, {10, 495} };
Mat trainingDataMat(11, 2, CV_32FC1, trainingData);
// Set up SVM's parameters
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
// Train a SVM classifier
CvSVM SVM;
SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);
// Train a boost classifier
CvBoost boost;
boost.train(trainingDataMat,
CV_ROW_SAMPLE,
labelsMat);
// Test the classifiers
Mat testSample1 = (Mat_<float>(1,2) << 251, 5);
Mat testSample2 = (Mat_<float>(1,2) << 502, 11);
float svmResponse1 = SVM.predict(testSample1);
float svmResponse2 = SVM.predict(testSample2);
float boostResponse1 = boost.predict(testSample1);
float boostResponse2 = boost.predict(testSample2);
std::cout << "SVM: " << svmResponse1 << " " << svmResponse2 << std::endl;
std::cout << "BOOST: " << boostResponse1 << " " << boostResponse2 << std::endl;
// Output:
// > SVM: -1 1
// > BOOST: -1 1