Raspberry 上的 Libtorch 无法加载 pt 文件,但正在处理 ubuntu
Libtorch on Raspberry can't load pt file but working on ubuntu
我正在尝试在 Raspberry PI 上使用 libtorch 构建一个 C++ 程序。该程序正在 Ubuntu 上运行,但在 Raspberry 上构建时出现以下错误:
error: use of deleted function ‘void torch::jit::script::Module::operator=(const torch::jit::script::Module&)’
In file included from /usr/include/torch/csrc/jit/ir.h:18,
from /usr/include/torch/csrc/jit/tracer.h:9,
from /usr/include/torch/csrc/autograd/generated/variable_factories.h:8,
from /usr/include/torch/csrc/api/include/torch/types.h:7,
from /usr/include/torch/script.h:3,
from /tmp/tmp.k6618dczxt/src/../include/suvoNet.h:26,
from /tmp/tmp.k6618dczxt/src/../include/classifier.h:17,
from /tmp/tmp.k6618dczxt/src/classifier.cpp:11:
/usr/include/torch/csrc/jit/script/module.h:319:3: note: declared here
TH_DISALLOW_COPY_AND_ASSIGN(Module);
这是崩溃的代码:
MyClass::MyClass() {
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
network = torch::jit::load(MODEL_FILE);
}
catch (const c10::Error& e) {
std::cerr << "Error loading the model\n";
exit(-1);
}
}
network
声明为私有 torch::jit::script::Module network
我使用来自 github 版本 '1.0.0a0+8322165'
的 pyTorch 为 Raspberry (ARM) 构建 libtorch
TLDR :在 1.6.0 中编译 libtorch 并且工作正常。
如何为 Raspberry 编译 Libtorch 并在我的 C++ 项目中使用?
准备建造
增加 RBPi SWAP
首先,如果您有 Raspberry PI 3 或更低版本,您需要增加 SWAP,因为构建会消耗 RAM。
If you have a RBPi 4 or higher with more than 3GB of RAM, skip this step.
修改文件/etc/dphys-swapfile
:
CONF_SWAPFILE=2048M
然后调用以下命令来更新更改。
sudo dphys-swapfile setup
安装基础包
安装以下软件包:
sudo apt install build-essential make cmake git python3-pip libatlas-base-dev
Libtorch 需要 CMake>=3.15
才能正确构建,请使用
cmake --version``
检查 cmake 版本
如果低于 3.15,请按照以下命令构建更新版本并删除以前的版本:
wget https://github.com/Kitware/CMake/releases/download/v3.18.0-rc1/cmake-3.18.0-rc1.tar.gz
tar -xzf cmake-3.18.0-rc1.tar.gz
cd cmake<version>
mkdir build
cd build
cmake ..
make
sudo make install
sudo apt remove cmake
sudo ln -s /usr/local/bin/cmake /usr/bin/cmake
sudo ldconfig
从源代码构建 PyTorch 以获得 ARM 的 libtorch 后端
Don't forget to increase the SWAP to 2048M if you don't have 3GB or RAM.
获取所有需要的库:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
获取 PyTorch 源代码:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
初始化所有子模块:
git submodule update --init --recursive
git submodule update --remote third_party/protobuf # To prevent a bug I had
获取所有需要的库:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
获取 PyTorch 源代码:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
正在为构建设置环境变量。
将以下行添加到 ~/.bashrc
文件。
export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
以 root 身份登录,并使用 .bashrc 文件设置环境变量
sudo su
source /home/<user>/.bashrc
安装python依赖项
pip3 install setuptools pyyaml numpy
构建并安装 PyTorch,是时候喝杯 :coffee: 了,这需要一段时间。
Don't forget the -E
that forces the environment variables to be used.
sudo -E python3 setup.py install
检查安装是否有效:
cd
python3
import torch
torch.__version__
使用 Torch 构建程序
在你的 CMakeLists.txt
中:
cmake_minimum_required(VERSION 2.6)
project(projectName)
set(CMAKE_PREFIX_PATH "/home/pi/pytorch/torch") # Adding the directory where torch as been installed
set(CMAKE_CXX_STANDARD 14) # C14 required to compile Torch
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) # Torch is compiled with CXX11_ABI, so your program needs to be also, or you may have conflicts in some libraries (such as GTest for example)
# Specifying we are using pthread for UNIX systems.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status :")
message(STATUS " libraries: ${TORCH_LIBRARIES}")
message(STATUS " Torch Flags: ${TORCH_CXX_FLAGS}")
# Program executable
add_executable(projectName <sources>)
target_link_libraries(projectName PRIVATE pthread dl util ${TORCH_LIBRARIES})
我正在尝试在 Raspberry PI 上使用 libtorch 构建一个 C++ 程序。该程序正在 Ubuntu 上运行,但在 Raspberry 上构建时出现以下错误:
error: use of deleted function ‘void torch::jit::script::Module::operator=(const torch::jit::script::Module&)’
In file included from /usr/include/torch/csrc/jit/ir.h:18,
from /usr/include/torch/csrc/jit/tracer.h:9,
from /usr/include/torch/csrc/autograd/generated/variable_factories.h:8,
from /usr/include/torch/csrc/api/include/torch/types.h:7,
from /usr/include/torch/script.h:3,
from /tmp/tmp.k6618dczxt/src/../include/suvoNet.h:26,
from /tmp/tmp.k6618dczxt/src/../include/classifier.h:17,
from /tmp/tmp.k6618dczxt/src/classifier.cpp:11:
/usr/include/torch/csrc/jit/script/module.h:319:3: note: declared here
TH_DISALLOW_COPY_AND_ASSIGN(Module);
这是崩溃的代码:
MyClass::MyClass() {
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
network = torch::jit::load(MODEL_FILE);
}
catch (const c10::Error& e) {
std::cerr << "Error loading the model\n";
exit(-1);
}
}
network
声明为私有 torch::jit::script::Module network
我使用来自 github 版本 '1.0.0a0+8322165'
的 pyTorch 为 Raspberry (ARM) 构建 libtorchTLDR :在 1.6.0 中编译 libtorch 并且工作正常。
如何为 Raspberry 编译 Libtorch 并在我的 C++ 项目中使用?
准备建造
增加 RBPi SWAP
首先,如果您有 Raspberry PI 3 或更低版本,您需要增加 SWAP,因为构建会消耗 RAM。
If you have a RBPi 4 or higher with more than 3GB of RAM, skip this step.
修改文件/etc/dphys-swapfile
:
CONF_SWAPFILE=2048M
然后调用以下命令来更新更改。
sudo dphys-swapfile setup
安装基础包
安装以下软件包:
sudo apt install build-essential make cmake git python3-pip libatlas-base-dev
Libtorch 需要 CMake>=3.15
才能正确构建,请使用
cmake --version``
如果低于 3.15,请按照以下命令构建更新版本并删除以前的版本:
wget https://github.com/Kitware/CMake/releases/download/v3.18.0-rc1/cmake-3.18.0-rc1.tar.gz
tar -xzf cmake-3.18.0-rc1.tar.gz
cd cmake<version>
mkdir build
cd build
cmake ..
make
sudo make install
sudo apt remove cmake
sudo ln -s /usr/local/bin/cmake /usr/bin/cmake
sudo ldconfig
从源代码构建 PyTorch 以获得 ARM 的 libtorch 后端
Don't forget to increase the SWAP to 2048M if you don't have 3GB or RAM.
获取所有需要的库:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
获取 PyTorch 源代码:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
初始化所有子模块:
git submodule update --init --recursive
git submodule update --remote third_party/protobuf # To prevent a bug I had
获取所有需要的库:
sudo apt-get update
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
获取 PyTorch 源代码:
git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
cd pytorch
正在为构建设置环境变量。
将以下行添加到 ~/.bashrc
文件。
export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
以 root 身份登录,并使用 .bashrc 文件设置环境变量
sudo su
source /home/<user>/.bashrc
安装python依赖项
pip3 install setuptools pyyaml numpy
构建并安装 PyTorch,是时候喝杯 :coffee: 了,这需要一段时间。
Don't forget the
-E
that forces the environment variables to be used.
sudo -E python3 setup.py install
检查安装是否有效:
cd
python3
import torch
torch.__version__
使用 Torch 构建程序
在你的 CMakeLists.txt
中:
cmake_minimum_required(VERSION 2.6)
project(projectName)
set(CMAKE_PREFIX_PATH "/home/pi/pytorch/torch") # Adding the directory where torch as been installed
set(CMAKE_CXX_STANDARD 14) # C14 required to compile Torch
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) # Torch is compiled with CXX11_ABI, so your program needs to be also, or you may have conflicts in some libraries (such as GTest for example)
# Specifying we are using pthread for UNIX systems.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")
find_package(Torch REQUIRED)
if(NOT Torch_FOUND)
message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
message(STATUS "Pytorch status :")
message(STATUS " libraries: ${TORCH_LIBRARIES}")
message(STATUS " Torch Flags: ${TORCH_CXX_FLAGS}")
# Program executable
add_executable(projectName <sources>)
target_link_libraries(projectName PRIVATE pthread dl util ${TORCH_LIBRARIES})