OpenCV 3.0 Assertion fail while train boost model

asked 2015-11-04 02:15:48 -0500

JesseCh gravatar image

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

edit retag flag offensive close merge delete

Comments

1

float* data2 = (float*)response.ptr<ushort>(i); - both your response and img are float , so you have to use .ptr<float> (or, whatever the Mat's type is)

then, you probably want int responses for classification, and float responses for regression.

berak gravatar imageberak ( 2015-11-04 02:30:19 -0500 )edit

I had tried both .ptr<float> and .ptr<int> and still got exact same result.

JesseCh gravatar imageJesseCh ( 2015-11-04 18:48:07 -0500 )edit