PyTorch on Ubuntu 18.04
Last updated on May 7, 2023 pm
[TOC]
Install PyTorch locally
- https://pytorch.org/get-started/locally/
Conda (Python)
1 |
|
test
1 |
|
Pip (Python)
1 |
|
LibTorch (C++)
Dowload the prebuilt libs
1
set(CMAKE_PREFIX_PATH "<path>/libtorch/share/cmake/Torch")
Build from Source
1
2
3
4
5
6
7
8
9
10
11# example
git clone --recursive -b v1.0.1 https://github.com/pytorch/pytorch
cd pytorch
mkdir build
cd build
# options
export NO_CUDA=1
python ../tools/build_libtorch.pyThe built libtorch library is located at
pytorch/torch/lib/tmp_install/
in default.1
set(Torch_DIR "<path>/pytorch/torch/lib/tmp_install/share/cmake/Torch/")
use with cmake
1
2
3find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
target_link_libraries(<target> ${TORCH_LIBRARIES})include dir
1
2libtorch/include
libtorch/include/torch/csrc/api/include
PyTorch 模型文件
- PyTorch的模型文件一般会保存为 .pth 文件,C++接口一般读取的是 .pt 文件
- .pth 文件通过
torch.jit.trace
转换后得到 .pt 文件
只保存模型参数,不保存模型结构
1 |
|
保存整个模型,包括模型结构+模型参数
1 |
|
FAQ
UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero
1 |
|
PyTorch on Ubuntu 18.04
https://cgabc.xyz/posts/70f98416/