<bdo id='qvmzY'></bdo><ul id='qvmzY'></ul>
    1. <legend id='qvmzY'><style id='qvmzY'><dir id='qvmzY'><q id='qvmzY'></q></dir></style></legend><tfoot id='qvmzY'></tfoot>
    2. <i id='qvmzY'><tr id='qvmzY'><dt id='qvmzY'><q id='qvmzY'><span id='qvmzY'><b id='qvmzY'><form id='qvmzY'><ins id='qvmzY'></ins><ul id='qvmzY'></ul><sub id='qvmzY'></sub></form><legend id='qvmzY'></legend><bdo id='qvmzY'><pre id='qvmzY'><center id='qvmzY'></center></pre></bdo></b><th id='qvmzY'></th></span></q></dt></tr></i><div id='qvmzY'><tfoot id='qvmzY'></tfoot><dl id='qvmzY'><fieldset id='qvmzY'></fieldset></dl></div>

        <small id='qvmzY'></small><noframes id='qvmzY'>

      1. 树莓派上的 Libtorch 无法加载 pt 文件,但可以在 ubuntu 上工作

        时间:2023-12-03
        <i id='3PtCJ'><tr id='3PtCJ'><dt id='3PtCJ'><q id='3PtCJ'><span id='3PtCJ'><b id='3PtCJ'><form id='3PtCJ'><ins id='3PtCJ'></ins><ul id='3PtCJ'></ul><sub id='3PtCJ'></sub></form><legend id='3PtCJ'></legend><bdo id='3PtCJ'><pre id='3PtCJ'><center id='3PtCJ'></center></pre></bdo></b><th id='3PtCJ'></th></span></q></dt></tr></i><div id='3PtCJ'><tfoot id='3PtCJ'></tfoot><dl id='3PtCJ'><fieldset id='3PtCJ'></fieldset></dl></div>
          <bdo id='3PtCJ'></bdo><ul id='3PtCJ'></ul>
        • <tfoot id='3PtCJ'></tfoot>

                  <small id='3PtCJ'></small><noframes id='3PtCJ'>

                  <legend id='3PtCJ'><style id='3PtCJ'><dir id='3PtCJ'><q id='3PtCJ'></q></dir></style></legend>

                    <tbody id='3PtCJ'></tbody>

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

                  问题描述

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

                  error: 使用已删除的函数void torch::jit::script::Module::operator=(const torch::jit::script::Module&)"在/usr/include/torch/csrc/jit/ir.h:18 包含的文件中,来自/usr/include/torch/csrc/jit/tracer.h:9,来自/usr/include/torch/csrc/autograd/generated/variable_factories.h:8,来自/usr/include/torch/csrc/api/include/torch/types.h:7,来自/usr/include/torch/script.h:3,来自/tmp/tmp.k6618dczxt/src/../include/suvoNet.h:26,来自/tmp/tmp.k6618dczxt/src/../include/classifier.h:17,来自/tmp/tmp.k6618dczxt/src/classifier.cpp:11:/usr/include/torch/csrc/jit/script/module.h:319:3: 注意:这里声明TH_DISALLOW_COPY_AND_ASSIGN(模块);

                  这是崩溃的代码:

                  MyClass::MyClass() {尝试 {//使用 torch::jit::load() 从文件中反序列化 ScriptModule.network = torch::jit::load(MODEL_FILE);}catch (const c10::Error&e) {std::cerr <<加载模型时出错
                  ";退出(-1);}}

                  使用 network 声明私有 torch::jit::script::Module network

                  我在 '1.0.0a0+8322165' 版本中使用来自 github 的 pyTorch 为 Raspberry (ARM) 构建 libtorch

                  解决方案

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

                  如何为 Raspberry 编译 Libtorch 并在我的 C++ 项目中使用?

                  准备构建

                  增加 RBPi SWAP

                  首先,如果您有 Raspberry PI 3 或更低版本,则需要增加 SWAP,因为构建是 RAM 吞噬者.

                  <块引用>

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

                  修改文件/etc/dphys-swapfile:

                  CONF_SWAPFILE=2048M

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

                  sudo dphys-swapfile 设置

                  安装基础包

                  安装以下软件包:

                  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.gztar -xzf cmake-3.18.0-rc1.tar.gzcd cmake<版本>mkdir 构建光盘构建cmake ..制作须藤制作安装sudo apt 删除 cmake须藤 ln -s/usr/local/bin/cmake/usr/bin/cmake须藤ldconfig

                  从源代码构建 PyTorch 以获得 ARM 的 libtorch 后端

                  <块引用>

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

                  获取所有需要的库:

                  sudo apt-get updatesudo 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.6cd pytorch

                  初始化所有子模块:

                  git submodule update --init --recursivegit submodule update --remote third_party/protobuf # 为了防止我遇到的错误

                  获取所有需要的库:

                  sudo apt-get updatesudo 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.6cd pytorch

                  为构建设置环境变量.

                  将以下行添加到 ~/.bashrc 文件中.

                  导出 NO_CUDA=1出口 NO_DISTRIBUTED=1出口 NO_MKLDNN=1出口 NO_NNPACK=1出口 NO_QNNPACK=1

                  以 root 身份登录,并使用 .bashrc 文件设置环境变量

                  sudo su源/home/<用户>/.bashrc

                  安装python依赖

                  pip3 install setuptools pyyaml numpy

                  构建并安装 PyTorch,是时候拿一杯 :coffee: 了,这需要一些时间.

                  <块引用>

                  不要忘记强制使用环境变量的 -E.

                  sudo -E python3 setup.py install

                  检查安装是否成功:

                  cd蟒蛇3进口火炬火炬.__版本__

                  使用 Torch 构建您的程序

                  在您的 CMakeLists.txt 中:

                  cmake_minimum_required(VERSION 2.6)项目(项目名称)set(CMAKE_PREFIX_PATH "/home/pi/pytorch/torch") # 添加安装torch的目录set(CMAKE_CXX_STANDARD 14) # 编译 Torch 需要 C14设置(CMAKE_CXX_STANDARD_REQUIRED TRUE)add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) # Torch 是用CXX11_ABI 编译的,所以你的程序也需要,不然你可能在某些库(比如GTest)中存在冲突# 指定我们在 UNIX 系统上使用 pthread.设置(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall")find_package(需要火炬)如果(不是 Torch_FOUND)消息(致命错误找不到 Pytorch!")endif(不是 Torch_FOUND)消息(状态Pytorch 状态:")消息(状态库:${TORCH_LIBRARIES}")消息(状态火炬标志:${TORCH_CXX_FLAGS}")# 程序可执行add_executable(项目名称<源>)target_link_libraries(projectName PRIVATE pthread dl util ${TORCH_LIBRARIES})

                  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);
                  

                  Here is the code that crashes :

                  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
                  ";
                          exit(-1);
                      }
                  }
                  
                  

                  With network declared private torch::jit::script::Module network

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

                  解决方案

                  TLDR : Compile libtorch in 1.6.0 and that works fine.

                  How to compile Libtorch for Raspberry and use in my C++ Project ?

                  Prepare to build

                  Increase RBPi SWAP

                  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.

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

                  Modify the file /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 needs CMake>=3.15 to be built properly, check cmake version with cmake --version``

                  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
                  

                  Building PyTorch from source to get libtorch backend for ARM

                  Don't forget to increase the SWAP to 2048M if you don't have 3GB or RAM.

                  Getting all needed libraries:

                  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
                  

                  Getting PyTorch sources:

                  git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
                  cd pytorch
                  

                  Init all the submodules :

                  git submodule update --init --recursive
                  git submodule update --remote third_party/protobuf # To prevent a bug I had
                  

                  Getting all needed libraries:

                  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
                  

                  Getting PyTorch sources:

                  git clone --recursive https://github.com/pytorch/pytorch --branch=release/1.6
                  cd pytorch
                  

                  Setting up environment variables for the build.

                  Add the following lines to the ~/.bashrc file.

                  export NO_CUDA=1
                  export NO_DISTRIBUTED=1
                  export NO_MKLDNN=1 
                  export NO_NNPACK=1
                  export NO_QNNPACK=1
                  

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

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

                  Install python dependencies

                  pip3 install setuptools pyyaml numpy
                  

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

                  Don't forget the -E that forces the environment variables to be used.

                  sudo -E python3 setup.py install
                  

                  Check the installation worked:

                  cd 
                  python3
                  import torch
                  torch.__version__
                  

                  Building your program with Torch

                  In your 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})                        
                  

                  这篇关于树莓派上的 Libtorch 无法加载 pt 文件,但可以在 ubuntu 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CMakeLists.txt:3 处的 CMake 错误(find_package) 下一篇:在 raspberry 中交叉编译的 qt 中构建应用程序时出错

                  相关文章

                      <bdo id='qLRHk'></bdo><ul id='qLRHk'></ul>
                    <i id='qLRHk'><tr id='qLRHk'><dt id='qLRHk'><q id='qLRHk'><span id='qLRHk'><b id='qLRHk'><form id='qLRHk'><ins id='qLRHk'></ins><ul id='qLRHk'></ul><sub id='qLRHk'></sub></form><legend id='qLRHk'></legend><bdo id='qLRHk'><pre id='qLRHk'><center id='qLRHk'></center></pre></bdo></b><th id='qLRHk'></th></span></q></dt></tr></i><div id='qLRHk'><tfoot id='qLRHk'></tfoot><dl id='qLRHk'><fieldset id='qLRHk'></fieldset></dl></div>

                    <small id='qLRHk'></small><noframes id='qLRHk'>

                  1. <legend id='qLRHk'><style id='qLRHk'><dir id='qLRHk'><q id='qLRHk'></q></dir></style></legend>

                      <tfoot id='qLRHk'></tfoot>