参考
https://github.com/LuongTanDat/AlphaPose-Cpp
转换
python convert_torchscript.py --cfg "model-zoo/fast_pose_res50/256x192_res50_lr1e-3_1x.yaml" --pth "model-zoo/fast_pose_res50/fast_res50_256x192.pth"
libtorch
下载:https://pytorch.org/
下载后上传至服务器
unzip libtorch-shared-with-deps-latest.zip
export LIB_TORCH=${libtorch path} # 设置libtorch路径
编译
mkdir build && cd build
export LIB_TORCH="/usr/local/libtorch"
cmake -DCMAKE_PREFIX_PATH=$LIB_TORCH ${Project Path}
cmake --build . --config Release
./torch-app
更改CMakeLists.txt
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(torch-app)
set(Torch_DIR /AlphaPose-Cpp-main/libtorch/share/cmake/Torch)
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)
if (Torch_FOUND)
message(STATUS "Torch library found!")
message(STATUS " include path: ${TORCH_INCLUDE_DIRS}" \n)
else ()
message(FATAL_ERROR "Could not locate Torch" \n)
endif()
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS})
find_package(OpenCV)
add_executable(torch-app main.cpp AlphaPose.cpp)
target_link_libraries(torch-app ${TORCH_LIBRARIES} ${OpenCV_LIBRARIES})
set_property(TARGET torch-app PROPERTY CXX_STANDARD 14)
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET torch-app
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:torch-app>)
endif (MSVC)
用这种方式到第四步是没报错的,但在执行第五步时出现如下问题
解决:
conda activate base
python convert_torchscript.py --cfg "model-zoo/fast_pose_res50/256x192_res50_lr1e-3_1x.yaml" --pth "model-zoo/fast_pose_res50/fast_res50_256x192.pth"
转换好模型后,在main.cpp更改模型路径和图片路径及图片box的值(xmin,ymin,width,height)
成功!
问题汇总
一、 找不到 -lCUDA_cublas_LIBRARY-NOTFOUND
解决方法:把cmake升级成最新版本,查看cmake版本方式cmake --version
-
下载并上传到服务器,解压
下载网站:https://cmake.org/download/ -
安装gcc等相关的程序包(已经安装过则忽略该步骤)
-
进入到解压后的目录,在最顶层目录,执行
./bootstrap
出现问题:CMake Error at Utilities/cmcurl/CMakeLists.txt:562 (message):
Could not find OpenSSL. Install an OpenSSL development package or
configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.
解决方法:sudo apt-get install libssl-dev
成功 -
在顶层目录下,执行
make
出现问题:Source/CMakeFiles/CMakeLib.dir/build.make:2147: recipe for target ‘Source/CMakeFiles/CMakeLib.dir/cmQtAutoMocUic.cxx.o’ failed
make[2]: *** [Source/CMakeFiles/CMakeLib.dir/cmQtAutoMocUic.cxx.o] Error 1
CMakeFiles/Makefile2:2096: recipe for target ‘Source/CMakeFiles/CMakeLib.dir/all’ failed
make[1]: *** [Source/CMakeFiles/CMakeLib.dir/all] Error 2
Makefile:165: recipe for target ‘all’ failed
make: *** [all] Error 2
解决方法:执行cmake .. -DBUILD_TESTS=OFF
成功 -
在顶层目录下,执行
make install
,如果编译错误,使用sudo make install
进行安装
出现问题:Source/CMakeFiles/CMakeLib.dir/build.make:2147: recipe for target ‘Source/CMakeFiles/CMakeLib.dir/cmQtAutoMocUic.cxx.o’ failed
make[2]: *** [Source/CMakeFiles/CMakeLib.dir/cmQtAutoMocUic.cxx.o] Error 1
CMakeFiles/Makefile2:2096: recipe for target ‘Source/CMakeFiles/CMakeLib.dir/all’ failed
make[1]: *** [Source/CMakeFiles/CMakeLib.dir/all] Error 2
Makefile:165: recipe for target ‘all’ failed
make: *** [all] Error 2
-
在~/.bashrc最后添加
export CMAKE_ROOT=${CMAKE PATH} export PATH=$PATH:$CMAKE_ROOT/bin: source ~/.bashrc
直接运行
执行g++ main.cpp -o main
出现问题:AlphaPose.h:2:25: 致命错误:torch/torch.h:没有那个文件或目录
解决方法:替换include的路径(这种方式太笨,不建议使用,建议更改CMakeLists.txt)