Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV 3.0 Assertion fail while train boost model

Hi all, I am trying to train my own boosting model, but I encountered Assertion failed on trainning stage. My program is trying to read a CSV file into cv::Mat and use cv::Mat to be the input of trainning process. Following is my code:

int main(int argc, char *argv[]){

int rows = 10;
int cols = 4;
string pixel;

Mat img(Size(cols,rows),CV_32F);
Mat response(Size(1,rows),CV_32F);

ifstream file("D:/testFile2/test.csv", ifstream::in);
for(int i=0; i<rows; i++){
    float* data = (float*)img.ptr<ushort>(i);
    float* data2 = (float*)response.ptr<ushort>(i);

        for(int j=0; j<cols+1; j++){

            if(j==0){
                getline(file, pixel, ',');
                data2[j] = (float)atof(pixel.c_str());

            }
            else if(j == cols){
                getline(file, pixel, '\n');
                data[j-1] = (float)atof(pixel.c_str());

            }
            else{
                getline(file, pixel, ',');
                data[j-1] = (float)atof(pixel.c_str());

            }

        }

}

 printf("Data Read\n");

 Ptr<ml::TrainData> dataset = ml::TrainData::create(img,ml::SampleTypes::ROW_SAMPLE,response);
 Ptr<ml::Boost> boost = ml::Boost::create();
 boost->setBoostType(ml::Boost::REAL);
 boost->setWeakCount(10);
 boost->setMaxDepth(2);
 boost->setWeightTrimRate(0.95);

 cout<<"Training data: "<<endl
     <<"getSamples\n"<<dataset->getSamples()<<endl
     <<"getResponse\n"<<dataset->getResponses()<<endl
     <<endl;

 cout<<"Boostiing Model Trainning..."<<endl;
 boost = ml::Boost::train<ml::Boost>(dataset,0);
 cout<<"Finished Boosting Trainning!!!"<<endl;

 printf("Finished\n");

return 0;

}

And I got this

image description

As you can see, the input data is pretty simple, just a four dimensions data. Alternatively, if I used:

Ptr<ml::TrainData> dataset = ml::TrainData::loadFromCSV("D:/testFile2/test.csv",0,0);

Then, everything is fine, no assertion failed occured.

Does anyone have idea about this? Thanks