C++调用PyTorch一个简单实现 附(从下载安装到输出例子)

文章目录

      • 关于PyTorch
      • 关于LibTorch
      • 安装
      • 卸载
      • 例子

关于PyTorch

PyTorch是一个Python包,提供了两个高级特性:
-强大的GPU加速张量计算(如NumPy)
-深度神经网络建立在一个基于磁带的autograd系统

您可以在需要时重用您喜欢的Python包(如NumPy、SciPy和Cython)来扩展PyTorch。

关于LibTorch

从PyTorch版本迭代到1.0以后,Facebook就更新了Pytorch支持C++导入模型的torch.jit API, 用于生成支持C++ API的模型文件。

安装

  1. 下载安装包
# Mac (不支持CUDA)
wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-1.4.0.zip
unzip libtorch-macos-1.4.0.zip

# Linux (CUDA10.1)
wget https://download.pytorch.org/libtorch/cu101/libtorch-shared-with-deps-1.4.0.zip
unzip libtorch-shared-with-deps-1.4.0.zip

# Linux (CPU)
wget https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.4.0+cpu.zip
unzip libtorch-shared-with-deps-1.4.0+cpu.zip

2.执行脚本文件
保存为install.sh至解压目录下

# copy torch header file
sudo cp -r include/* /usr/local/include

# copy torch library file
sudo cp -r lib/* /usr/local/lib

# copy torch share file
sudo cp -r share/* /usr/local/share

卸载

1.执行脚本文件
保存为uninstall.sh至解压目录下

# delete torch header file
sudo rm -r /usr/local/include/ATen
sudo rm -r /usr/local/include/c10
sudo rm -r /usr/local/include/caffe2
sudo rm -r /usr/local/include/pybind11
sudo rm -r /usr/local/include/TH
sudo rm -r /usr/local/include/THCUNN
sudo rm -r /usr/local/include/THNN
sudo rm -r /usr/local/include/torch

# delete torch library file
sudo rm /usr/local/lib/libCaffe2_perfkernels_avx.a
sudo rm /usr/local/lib/libgmock_main.a
sudo rm /usr/local/lib/libCaffe2_perfkernels_avx2.a        
sudo rm /usr/local/lib/libgtest.a
sudo rm /usr/local/lib/libCaffe2_perfkernels_avx512.a      
sudo rm /usr/local/lib/libgtest_main.a
sudo rm /usr/local/lib/libasmjit.a                         
sudo rm /usr/local/lib/libiomp5.dylib
sudo rm /usr/local/lib/libbenchmark.a                      
sudo rm /usr/local/lib/libmkldnn.a
sudo rm /usr/local/lib/libbenchmark_main.a                 
sudo rm /usr/local/lib/libnnpack.a
sudo rm /usr/local/lib/libc10.dylib                        
sudo rm /usr/local/lib/libnnpack_reference_layers.a
sudo rm /usr/local/lib/libc10d.a                           
sudo rm /usr/local/lib/libonnx.a
sudo rm /usr/local/lib/libcaffe2_detectron_ops.dylib       
sudo rm /usr/local/lib/libonnx_proto.a
sudo rm /usr/local/lib/libcaffe2_module_test_dynamic.dylib 
sudo rm /usr/local/lib/libprotobuf-lite.a
sudo rm /usr/local/lib/libcaffe2_observers.dylib           
sudo rm /usr/local/lib/libprotobuf.a
sudo rm /usr/local/lib/libcaffe2_protos.a                  
sudo rm /usr/local/lib/libprotoc.a
sudo rm /usr/local/lib/libclog.a                           
sudo rm /usr/local/lib/libpthreadpool.a
sudo rm /usr/local/lib/libcpuinfo.a                        
sudo rm /usr/local/lib/libpytorch_jni.dylib
sudo rm /usr/local/lib/libcpuinfo_internals.a              
sudo rm /usr/local/lib/libpytorch_qnnpack.a
sudo rm /usr/local/lib/libfbgemm.a                         
sudo rm /usr/local/lib/libqnnpack.a
sudo rm /usr/local/lib/libfbjni.dylib                      
sudo rm /usr/local/lib/libshm.dylib
sudo rm /usr/local/lib/libfoxi_loader.a                    
sudo rm /usr/local/lib/libtorch.dylib
sudo rm /usr/local/lib/libgloo.a                           
sudo rm /usr/local/lib/libtorch_python.dylib
sudo rm /usr/local/lib/libgmock.a

# delete torch share file
sudo rm -r /usr/local/share/cmake/ATen
sudo rm -r /usr/local/share/cmake/Caffe2
sudo rm -r /usr/local/share/cmake/Gloo
sudo rm -r /usr/local/share/cmake/Torch

例子

1.编写c++文件,保存为example-app.cpp

#include 
#include 

int main() {
     
    torch::Tensor tensor = torch::rand({
     2, 3});
    std::cout << tensor << std::endl;
}

2.编写cmake文件,保存为CMakeLists.txtexample-app.cpp同级目录下

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

3.执行命令

mkdir build
cd build
cmake ..
make

4.打印结果

./example-app

[out]:
 0.1091  0.0758  0.0716
 0.9589  0.4803  0.0593
[ CPUFloatType{2,3} ]

如果文章对您有用请您为我点赞。

有任何问题请咨询 [email protected]或至https://github.com/lornatang提交问题。

你可能感兴趣的:(PyTorch,Python,python,cmake,深度学习,linux,c++)