【问题标题】:"ImportError: No module named torch" in ROS packageROS包中的“ImportError:没有名为torch的模块”
【发布时间】:2020-05-11 22:42:16
【问题描述】:

我创建了一个 ROS 包,其中包含一些 python 脚本。 python脚本基于torch模块(它是pytorch模型的推理代码)。当我尝试运行我的脚本时,它给了我一个错误:
ImportError: No module named torch

为了安装 ROS,我使用了the instruction of ROS wiki。为了验证我的安装,我遵循了 ROS 的示例代码(一个简单的publisher and subscriber),它运行良好。我的系统信息是:

蟒蛇:3.6.9
火炬:1.1.0
火炬视觉:0.3.0
操作系统内核:Linux 4.15.0-74-generic
操作系统发行版:Ubuntu 18.04.3

我要导入以下库:

import torch
from cv_bridge import CvBridge
import cv2
import os
import numpy as np
from torch.autograd import Variable
from torchvision import transforms
import torch.nn.functional as F
import torch._utils
import time
from PIL import Image 

我的 CMake 文件如下:

cmake_minimum_required(VERSION 2.8.3)
project(inference_pytorch)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

set(Torch_DIR ".local/lib/python3.6/site-packages/torch/share/cmake/Torch")

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  cv_bridge
)
find_package(Torch REQUIRED)
find_package(OpenCV 3 REQUIRED)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)

###################################
## catkin specific configuration ##
###################################
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES inference_pytorch
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
  ${OPENCV_INCLUDE_DIRS}
  ${Torch_INSTALL_INCLUDE}
  ${Torch_DIR}
)

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

install(PROGRAMS
   scripts/inference_test.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

 catkin_install_python(PROGRAMS scripts/inference_test.py
                      DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

link_directories(
  ${Torch_INSTALL_LIB}
)

现在,我应该如何编辑 CMAKE 文件以将上述库添加到我的 ROS 包中?

【问题讨论】:

    标签: python-3.x cmake pytorch ros torchvision


    【解决方案1】:

    最后,我发现安装的 ROS 可以使用 python2,所以我用 pip2 安装了 torch。另外,在 cmake 文件中设置火炬路径。现在可以了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-11
      • 2021-03-30
      • 2022-08-15
      • 2013-03-11
      • 2018-10-16
      • 1970-01-01
      • 2012-05-23
      • 2019-07-29
      相关资源
      最近更新 更多