C++ num++书写位置影响其值的输出问题

来源:互联网 发布:李婧磁 知乎 编辑:程序博客网 时间:2024/05/20 22:03

问题描述:

Ubuntu下,C++代码初始化变量:
int num = 0;

在while循环中,首先改变num的值:
num++;

在接下的一系列操作后,进行输出:
if(num%100 == 0){std::cout<<"the "<<num<<"th image output !"<<endl;}

但在编译之后并未输出,甚至之前的cout遇到100的倍数时也未输出。

尝试将num++与上述输出语句放在一起:
num++;if(num%100 == 0){std::cout<<"the "<<num<<"th image output !"<<endl;}
就正常了。。。

关键是同一份代码,在我自己的电脑上跑得ok,放到服务器上去跑就遇到了上面问题,在num++与if语句之间,没有对num有任何操作。。。

本机系统版本:Ubuntu 15.10   64bit
服务器系统版本:CentOS release 6.6 (Final)   64bit


全部代码如下:
int main(int argc, char** argv) {plog::init(plog::info, "plog.txt"); const int num_required_args = 2;if( argc < num_required_args ){    cout<<    "This program extracts features of an image and predict its label and score.\n"    "Usage: Demo_mainboby loadImgList keyfile\n";    return 1;  }/***********************************Init**********************************/string loadImgList = argv[1];string keyfile = argv[2];int num = 0;int nRet = 0;API_IMAGE_ADS ads_predict;GeekeyeDLL geekeyedll;std::ifstream infile_img(loadImgList);std::string imageString;double feature_prediction_time = 0;double load_image_time = 0;double resize_image_time = 0;double feature_extract_time = 0;string deploy_file= string("/home/in66/programs/test/keyfile/caffenet/Predicted_val.prototxt");string mean_file= string("/home/in66/programs/test/keyfile/caffenet/imagenet_mean.binaryproto");string model_file= string("/home/in66/programs/test/keyfile/caffenet/finetune_ads_caffenet_iter_6000.caffemodel");/***********************************Init*********************************/nRet = geekeyedll.init(deploy_file, mean_file, model_file);if( nRet != 0 ){   LOOGE<<"Fail to initialize the GeekeyeDLL Features Extract object !";   return -1;}nRet = ads_predict.init(keyfile);if( nRet != 0 ){   LOOGE<<"Fail to initialize the imagePredict object !";   return -1;}std::cout<<"init end!"<<std::endl;/*******************************imagePredict Deal*************************/while( infile_img>>imageString ){<span style="white-space:pre"></span>//num++;  放在此处则循环尾部的cout不能输出clock_t feature_prediction_time_start = clock();IplImage* image = cvLoadImage(imageString.c_str(), 1);if(!image || (image->width<32) || (image->height<32) || image->nChannels != 3 || image->depth != IPL_DEPTH_8U) {LOOGE<<"[image read error:]";cvReleaseImage(&image);image = 0;continue;}clock_t load_image_time_end = clock();load_image_time += static_cast<double>(load_image_time_end - feature_prediction_time_start)/CLOCKS_PER_SEC * 1000;//extract image featurestd::vector<IplImage*> img_dl;std::vector<std::vector<float>> feat_all;nRet = geekeyedll.gen_one_blob(image, img_dl);if( nRet != 0 || img_dl.size() != 1 ){LOOGE<< "[Resize Image Error!!]";cvReleaseImage(&image);for(int i = 0; i<img_dl.size(); i++){    if(img_dl[i] != NULL) cvReleaseImage(&(img_dl[i]));}std::vector < IplImage* >().swap( img_dl);return -1;}clock_t resize_image_time_end = clock();resize_image_time += static_cast<double>(resize_image_time_end - load_image_time_end)/CLOCKS_PER_SEC * 1000;feat_all.clear();nRet = geekeyedll.get_layer_features( img_dl, feat_all );if( nRet != 0 || feat_all.size() < 1 ){LOOGE<<"[Extract Caffe Features Error!!]";cvReleaseImage(&image);std::vector<std::vector<float>>().swap(feat_all);for(int i = 0; i<img_dl.size(); i++){    if(img_dl[i] != NULL) cvReleaseImage(&(img_dl[i]));}std::vector < IplImage* >().swap( img_dl);return -1;}clock_t feature_extract_time_end = clock();feature_extract_time += static_cast<double>(feature_extract_time_end - resize_image_time_end)/CLOCKS_PER_SEC * 1000;//std::pair<string,float> labelString;nRet = ads_predict.predict( img_dl, feat_all, labelString);if( nRet != 0  ){LOOGE<<"[Predict Err!! loadImgPath:]"<<imageString ;cvReleaseImage(&image);std::vector<std::vector<float>>().swap(feat_all);for(int i = 0; i<img_dl.size(); i++){    if(img_dl[i] != NULL) cvReleaseImage(&(img_dl[i]));}std::vector < IplImage* >().swap( img_dl);continue;}std::cout<<"the "<<num<<"th image output label:"<<labelString.first<<"_"<<labelString.second<<endl;   //num++放在循环开始时,此处num遇到100的倍数时也无法正常输出cvReleaseImage(&image);std::vector<std::vector<float>>().swap(feat_all);for(int i = 0; i<img_dl.size(); i++){    if(img_dl[i] != NULL) cvReleaseImage(&(img_dl[i]));}std::vector < IplImage* >().swap( img_dl);clock_t feature_prediction_time_end = clock();feature_prediction_time += static_cast<double>(feature_prediction_time_end - feature_extract_time_end)/CLOCKS_PER_SEC * 1000;num++; //将num++放在此处,问题解决if(num%100 == 0){std::cout<<"the "<<num<<"th image output !"<<endl;std::cout<< "per image load time: "<<load_image_time/(num*1.0)<<"ms"<<endl;std::cout<< "per image resize time: "<<resize_image_time/(num*1.0)<<"ms"<<endl;std::cout<< "per image feature extraction time: "<<feature_extract_time/(num*1.0)<<"ms"<<endl; std::cout<< "per image feature prediction time: "<<feature_prediction_time/(num*1.0)<<"ms"<<endl; }}std::cout<< "per image load time: "<<load_image_time/(num*1.0)<<"ms"<<endl;std::cout<< "per image load time: "<<resize_image_time/(num*1.0)<<"ms"<<endl;std::cout<< "per image feature extraction time: "<<feature_extract_time/(num*1.0)<<"ms"<<endl; std::cout<< "per image feature prediction time: "<<feature_prediction_time/(num*1.0)<<"ms"<<endl;std::cout<<"predict output end!"<<std::endl;infile_img.close();geekeyedll.release();ads_predict.release();return 0;}



0 0
原创粉丝点击