文章目录
- 环境准备
- 获取模型和测试图像
- 准备CMakeList.txt
- 准备C++推理代码
- 编译可执行程序
- 运行可执行程序
- 可能遇到的问题
- 参考
环境准备
- CUDA >= 11.2
- cuDNN >= 8.0
- python >= 3.6
- Linux(x64)
下载FastDeploy C++ SDK
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-gpu-1.0.6.tgz
tar -xzvf fastdeploy-linux-x64-gpu-1.0.6.tgz
获取模型和测试图像
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://bj.bcebos.com/fastdeploy/tests/test_det.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz
准备CMakeList.txt
cmake_minimum_required(VERSION 3.0.0)
project(ppyoloe VERSION 0.1.0)
include(CTest)
enable_testing()
include(/mnt/c/yp/lib/fastdeploy_cpp_sdk/FastDeploy.cmake)
# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})
# opencv
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(ppyoloe main.cpp)
target_link_libraries(ppyoloe ${FASTDEPLOY_LIBS} ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
准备C++推理代码
main.cpp
#include <stdio.h>
#include <iostream>
#include <string>
#include "fastdeploy/vision.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main() {
std::string model_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/model.pdmodel";
std::string params_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/model.pdiparams";
std::string infer_cfg_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/infer_cfg.yml";
// 模型推理的配置信息
fastdeploy::RuntimeOption option;
auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file, infer_cfg_file, option);
assert(model.Initialized()); // 判断模型是否初始化成功
cv::Mat im = cv::imread("/mnt/c/yp/fastDeploy/ppyoloe/test_det.jpg");
fastdeploy::vision::DetectionResult result;
assert(model.Predict(&im, &result)); // 判断是否预测成功
std::cout << result.Str() << std::endl;
cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
// 可视化结果保存到本地
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result save in vis_result.jpg" << std::endl;
return 0;
}
编译可执行程序
mkdir build && cd build && cmake .. && make -j8
运行可执行程序
./ppyoloe
PS:执行时如提示error while loading shared libraries: libxxx.so: cannot open shared object file: No such file…,说明程序执行时没有找到FastDeploy的库路径,可通过执行如下命令,将FastDeploy的库路径添加到环境变量之后,重新执行二进制程序。
source /Path/to/fastdeploy_cpp_sdk/fastdeploy_init.sh
可能遇到的问题
/usr/bin/ld: warning: libcudnn.so.8, needed by
/mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so,
not found (try using -rpath or -rpath-link)
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference tocudnnGetErrorString@libcudnn.so.8' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to
cudnnCreateTensorDescriptor@libcudnn.so.8’
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference tocudnnDestroy@libcudnn.so.8' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to
cudnnCreate@libcudnn.so.8’
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference tocudnnSetStream@libcudnn.so.8' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to
cudnnBatchNormalizationForwardTraining@libcudnn.so.8’
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference tocudnnDestroyTensorDescriptor@libcudnn.so.8' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to
cudnnSetTensorNdDescriptor@libcudnn.so.8’
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference tocudnnSetTensor4dDescriptor@libcudnn.so.8' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to
cudnnDeriveBNTensorDescriptor@libcudnn.so.8’
解决方法
查找libcudnn.so.8 find / -name libcudnn.so.8
拷贝到/mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib中去
cp /xxx/ libcudnn.so.8 /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib
参考
https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/quick_start/models/cpp.md