Foreword:
My environment:
Ubuntu 16.04 / GCC 5.4.0 ;
PyTorch 1.1 Linux CPU ;
Anaconda3 5.3.1;
Qt5 5.12.6;
Installation steps (although installation is actually just downloading the LibTorch package):
1. Download LibTorch
It mentioned:
Intercepted blog post from OLDPAN
For a quick try, I decided to directly download the compiled files from the official website:
The PyTorch web page link is given in the blog post of the download address OLDPAN, as follows:
https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
# 浏览器输入上述网址即可下载LibTorch
Original English webpage address: Installing C++ Distributions of PyTorch
Corresponding Chinese webpage address: use PyTorch C++ front end
Original English page
Corresponding Chinese page
After downloading it on the local host, share it to the virtual machine through the samba file service, and move the compressed package to /home/book, which is the'home directory'.
2. Unzip
Enter at the command line:
unzip libtorch-shared-with-deps-latest.zip
Libtorch folder after decompression
The function of each directory after decompression :
PS: If you use windows system, you also need to pay attention to the following tips:
For windows users
Source code compilation PyTorch can refer to: Use Pytorch's C++ front end (libtorch) to read pre-training weights and make predictions-Oldpan's personal blog
3. Test the libtorch package
Main reference: https://pytorch.apachecn.org/docs/1.0/cpp_frontend.html?h=LibTorch (refer to " Writing Basic Applications ")
3.1 Create a new pure C++ project in Qt
Unlike the reference link, we use Qt as the IDE for developing pure C++ projects
New pure C++ project
Choose CMake
3.2 Modify main.cpp
You can directly modify the code in dcgan.cpp in the example
Dcgan.cpp in the example
My main.cpp
3.3 Modify CMakeLists.txt
Reference: Carrying the handle on the street: Try to use CMake to build a simple application of Qt+Pytorch
The most important thing in this step is to set your libtorch path in CMakeLists.txt, my modified CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(dcgan LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Torch_DIR /home/book/libtorch/share/cmake/Torch) # 我的 libtorch 的路径
find_package(Torch REQUIRED)
add_executable(dcgan main.cpp)
target_link_libraries(dcgan "${TORCH_LIBRARIES}")
set_property(TARGET dcgan PROPERTY CXX_STANDARD 11)
3.4 Test
Press the green arrow, compile and run, output:
Output
Regarding this output, the reference example explains it like this:
4. Good article summary
This part summarizes some high-quality and good articles found during the installation and testing of LibTorch:
(2) The use of Pytorch's C++ side (libtorch) in Windows-Oldpan's personal blog
(3) Carry the handle at the street: try to use CMake to build a simple Qt+Pytorch application
(5)Installing C++ Distributions of PyTorch
(7) Gloomy Ghost Blog --GloomyGhost Blog