XGBoost model give different prediction to the same data after same irrelevant code is added


Ubuntu 16.04
g++ compile C++11

int main()
BoosterHandle h_booster;
const DMatrixHandle *g_trainHandle = nullptr;
int g_trainHandleLength = 0;
int res = XGBoosterCreate(g_trainHandle, g_trainHandleLength, &h_booster);
res = XGBoosterLoadModel(h_booster, “xgboost_norm_100.model”);

ifstream labellist(“label.txt”);
vector label;
string line;
int l;
while(getline(labellist, line))
stringstream ss;
ss << line;
ss >> l;
label.push_back(float(l)); //nothing wrong here, should be pushed twice

//labellist.close() //when this line or the following line is added, the prediction gets wrong
//when the following lines are added, the predictions also gets wrong
//ifstream imglist(“list.txt”);
//vector path;
//string str;
//while(getline(imglist, str))
// path.push_back(str);

FileStorage fs(“mat.xml”, FileStorage::READ);
Mat result;
fs[“feature”] >> result;

const int rows = result.rows;
const int cols = result.cols;

float **train = new float *[rows];

for (int i = 0; i < rows; i++)
train[i] = new float[cols];
cout<<“start copying”<<endl;
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
train[i][j] = result.at(i,j);
cout<<“mat to array finished”<<endl;
// convert to DMatrix
DMatrixHandle h_train[1], *h_test=nullptr;
res = XGDMatrixCreateFromMat(reinterpret_cast<float *>(train), rows, cols, -1.0, &h_train[0]);
bst_ulong out_len;
const float *f;
XGBoosterPredict(h_booster, h_train[0], 0,0,&out_len,&f);

int count = 0;
for (unsigned int i=0;i<out_len;i++)
if(f[i] == label[i]) count++;
cout<<"Accuracy: "<<float(count)/rows<<endl;
for(int i=0; i<rows; i++)
delete train[i];
delete train;
return 0;
This code is used to classify some images. The code works fine as it is. However, if I uncomment these lines commented now (or add a cout<<“hello world”), the xgboost model produces wrong predictions. I have no idea why this would happen, since the commented lines have no influences on the results.

What I have tried:

I check the vector label, there is nothing wrong with it.
I check the Mat result, it is also the same in those two situations(commented or uncommented)
The XGDMatrixFromMat, XGBoosterCreate and XGBoosterLoadModel works well.

I have worked on this problem for several days and right now I have no idea what’s wrong. Could anyone help me ?


Have anyone meet the same problem?