Raspberry上的Libtorch无法加载pt文件,但在ubuntu上工作 [英] Libtorch on Raspberry can't load pt file but working on ubuntu

查看:280
本文介绍了Raspberry上的Libtorch无法加载pt文件,但在ubuntu上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Raspberry PI上使用libtorch构建C ++程序.该程序可在Ubuntu上运行,但在Raspberry上构建时出现以下错误:

I'm trying to build a C++ program with libtorch on a Raspberry PI. The program is working on Ubuntu, but I've got the following error at build on 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中的pyTorch在版本'1.0.0a0 + 8322165'中为Raspberry(ARM)构建libtorch

I build libtorch for Raspberry (ARM) using pyTorch from github in version '1.0.0a0+8322165'

推荐答案

TLDR :在1.6.0中编译libtorch,效果很好.

TLDR : Compile libtorch in 1.6.0 and that works fine.

首先,如果您的Raspberry PI 3或更低版本,则需要增加SWAP,因为该版本是RAM消耗者.

First of all, if you have a Raspberry PI 3 or lower, you need to increase the SWAP, since the build is a RAM eater.

如果您的RBPi 4或更高版本的RAM超过3GB,请跳过此步骤.

If you have a RBPi 4 or higher with more than 3GB of RAM, skip this step.

修改文件/etc/dphys-swapfile:

CONF_SWAPFILE=2048M

然后调用以下命令以更新更改.

Then call the following command to update changes.

sudo dphys-swapfile setup

安装基本软件包

安装以下软件包:

Install base packages

Install the following packages:

sudo apt install build-essential make cmake git python3-pip libatlas-base-dev

Libtorch需要CMake> = 3.15才能正确构建,请使用 cmake --version``

Libtorch needs CMake>=3.15 to be built properly, check cmake version with cmake --version``

如果该版本低于3.15,请按照以下命令构建较新的版本并删除先前的版本:

If it's lower than 3.15, follow the following commands to build a newer version and remove the previous one:

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后端

如果没有3GB或RAM,请不要忘记将SWAP增加到2048M.

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来源:

Getting PyTorch sources:

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来源:

Getting PyTorch sources:

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文件设置环境变量

Log in as root, and use the .bashrc file to setup the environment variables

sudo su
source /home/<user>/.bashrc

安装python依赖项

Install python dependencies

pip3 install setuptools pyyaml numpy

构建并安装PyTorch,花点时间抢购:coffee :,这需要一些时间.

Build and install PyTorch, time to grab a :coffee:, it make take a while.

别忘了强制使用环境变量的-E.

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上的Libtorch无法加载pt文件,但在ubuntu上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆