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

39 篇文章 1 订阅

问题描述:

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 feature
		std::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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bicelove

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值