PyTorch模型主要用Python语言开发,在工业界,往往需要将Python的模型转化为C++,以方便其部署和跨平台使用;
1.创建PyTorch模型
2.转换成torch_script
3.保存pt模型
4.C++Load模型
5.编译
编译cmake的版本问题,下载和更新cmake的版本将会是比较麻烦、甚至看运气的事情,根据当前的torch版本,和CPU/GPU环境,选择libtorch版本
编辑CMakeList.txt文件
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example_app)
find_package(Torch REQUIRED)
add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
$ cmake -DCMAKE_PREFIX_PATH=/Users/xxx/CLionProjects/libtorch ..
-- The C compiler identification is AppleClang 9.1.0.9020039
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Found torch: /Users/tiger/CLionProjects/libtorch/lib/libtorch.dylib
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/xxx/CLionProjects/example-app/build
$ cmake --build . --config Release
Scanning dependencies of target example-app
[ 50%] Building CXX object CMakeFiles/example-app.dir/example-app.cpp.o
[100%] Linking CXX executable example-app
[100%] Built target example-app
6.测试效果
./example-app /Users/xxx/PycharmProjects/ReadCamera/model1.pt
ok
-0.0913 -0.2469 0.3947 -0.0506 0.0468
7.模型在GPU里运行
model.to(at::kCUDA)
tensor.to(at::kCUDA)