linux运行C++ 版本AlphaPose

参考

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)
成功!
在这里插入图片描述

在这里插入图片描述

问题汇总

error
一、 找不到 -lCUDA_cublas_LIBRARY-NOTFOUND
解决方法:把cmake升级成最新版本,查看cmake版本方式cmake --version

  1. 下载并上传到服务器,解压
    下载网站:https://cmake.org/download/

  2. 安装gcc等相关的程序包(已经安装过则忽略该步骤)

  3. 进入到解压后的目录,在最顶层目录,执行./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.
    error
    解决方法:sudo apt-get install libssl-dev
    在这里插入图片描述
    成功

  4. 在顶层目录下,执行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
    在这里插入图片描述
    成功

  5. 在顶层目录下,执行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
    在这里插入图片描述

  6. 在~/.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)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值