PaddleDetection部署C++

本文介绍了如何使用Cmake进行PaddlePaddle模型的GPU和CPU部署。首先,根据需要勾选WITH_GPU进行GPU或CPU配置,并修改相关路径。接着,在工程中设置Release/x64模式并生成main.exe。在代码修改部分,添加了模型目录、输入文件、输出目录等变量,并在主函数中设置了模型路径和设备类型。通过这些步骤,可以实现模型的编译与运行。
摘要由CSDN通过智能技术生成

一、Cmake编译
1、GPU部署
在这里插入图片描述
黄色部分的路径对应修改、WITH_GPU打勾
2、CPU部署
在这里插入图片描述
黄色部分的路径对应修改、取消WITH_GPU打勾
**注意:**paddle_inference的CPU版本与GPU版本的选择
二、工程
点击Cmake中的Open Project打开工程后,设置为Release/x64,再设置main为启动项目后,点击“生成”。
可以看到在Release文件夹下生成main.exe,则可在命令行中测试。
下面修改代码,以便直接在VS中运行。
1、在PrintBenchmarkLog函数前加入下列行。

std::string model_dir;
std::string image_file;
std::string video_file;
std::string image_dir;
int batch_size = 1;
bool use_gpu = true;	//若CPU部署,此处改为false
std::string device = "GPU";		//若CPU部署,此处改为"CPU"
int camera_id = -1;
double threshold = 0.1;
std::string output_dir = "output";
std::string run_mode = "paddle";
int gpu_id = 0;
bool run_benchmark = false;
bool use_mkldnn = false;
double cpu_threads = 0.9;
bool use_dynamic_shape = false;
int trt_min_shape = 1;
int trt_max_shape = 1280;
int trt_opt_shape = 640;
bool trt_calib_mode = false;

2、主函数对应修改

int main(int argc, char** argv) {
	std::string model_dir = "D:/Paddle/.../inference_model/solov2_r50_enhance_coco";
	std::string image_file = "D:/Paddle/..../test/208.bmp";
	std::string output_dir = "D:/Paddle/.../output";
	// Parsing command-line
	//google::ParseCommandLineFlags(&argc, &argv, true);
	if (model_dir.empty() ||
		(image_file.empty() && image_dir.empty() &&
			video_file.empty())) {
		std::cout << "Usage: ./main --model_dir=/PATH/TO/INFERENCE_MODEL/ "
			<< "--image_file=/PATH/TO/INPUT/IMAGE/" << std::endl;
		return -1;
	}
	if (!(run_mode == "paddle" || run_mode == "trt_fp32" ||
		run_mode == "trt_fp16" || run_mode == "trt_int8")) {
		std::cout
			<< "run_mode should be 'paddle', 'trt_fp32', 'trt_fp16' or 'trt_int8'.";
		return -1;
	}
	transform(device.begin(),
		device.end(),
		device.begin(),
		::toupper);
	if (!(device == "CPU" || device == "GPU" ||
		device == "XPU")) {
		std::cout << "device should be 'CPU', 'GPU' or 'XPU'.";
		return -1;
	}
	//if (use_gpu) {
	//	std::cout << "Deprecated, please use `--device` to set the device you want "
	//		"to run.";
	//	return -1;
	//}
	// Load model and create a object detector
	PaddleDetection::ObjectDetector det(model_dir,
		device,
		use_mkldnn,
		cpu_threads,
		run_mode,
		batch_size,
		gpu_id,
		trt_min_shape,
		trt_max_shape,
		trt_opt_shape,
		trt_calib_mode);
	// Do inference on input video or image
	if (!PathExists(output_dir)) {
		MkDirs(output_dir);
	}
	if (!video_file.empty() || camera_id != -1) {
		PredictVideo(video_file, &det, output_dir);
	}
	else if (!image_file.empty() || !image_dir.empty()) {
		std::vector<std::string> all_img_paths;
		std::vector<cv::String> cv_all_img_paths;
		if (!image_file.empty()) {
			all_img_paths.push_back(image_file);
			if (batch_size > 1) {
				std::cout << "batch_size should be 1, when set `image_file`."
					<< std::endl;
				return -1;
			}
		}
		else {
			cv::glob(image_dir, cv_all_img_paths);
			for (const auto& img_path : cv_all_img_paths) {
				all_img_paths.push_back(img_path);
			}
		}
		PredictImage(all_img_paths,
			batch_size,
			threshold,
			run_benchmark,
			&det,
			output_dir);
	}
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值