Jeston AGX Xavier配置ROS+Miniforge+pyTorch-GPU+TorchVision+pcl+Opencv3.2+Ceres(2022.11.07)

前言

本篇博客由于学校有项目要求,必须用ARM架构的平台进行算法开发,要对其深度学习环境配置。但是由于Xavier平台是极为特殊的,在CSDN上研究(摸)了好久参考了巨多博客才最终完成安装。本博客仅适用于我使用的这个平台,因此具体安装流程仅供参考。
本机器基本信息:
Jetpack: 4.5 (L4T 32.5.0)
Ubuntu: 18.04
Board Type:AGX Xavier(16GB)

本次安装包括以下软件:

1、ROS melodic
2、Miniforge
3、pyTorch-GPU
4、TorchVision
5、python-pcl库
6、Opencv3.2
7、Ceres-solver库

安装ros melodic

在安装ros前最好对安装源进行更换,因为arm架构进行科学上网是十分费劲的。
1、首先备份下原来的sources.list文件:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

2、然后进入清华Ubuntu Ports镜像源官网:
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu-ports/
这里选18.04 LTS,将内容全部复制,然后输入以下代码打开list文件,将其中所有内容替换为官网镜像源内容:

cd /etc/apt/
sudo gedit sources.list

3、由于Ubuntu版本是18.05,因此自然要安装melodic版本的ros。好消息是AGX Xavier是有官方的ros安装脚本的,请见如下网址。
https://github.com/jetsonhacks/installROSXavier
将zip压缩文件下载后,解压缩,在解压缩后的文件夹内打开终端输入:

./installROS.sh -p ros-melodic-desktop-full

至此完成ros安装。

安装Miniforge

NVIDIA Jetson Xavier使用的aarch64架构是没法使用anaconda的,虽然可以自己编译,但是conda指令是没法编译到aarch64架构上运行的。不过有替代品,名字是Miniforge。

与anaconda一样,Miniforge是基于Conda的包管理工具。用Conda创建环境相当于创建一个虚拟的空间,将指定版本的python编译环境和软件包都装在这个位置,不需要了可以直接打包放入垃圾箱,对于工程开发十分好用(新手友好)!

1、首先到miniforge的.sh release下载地址下载.sh文件:
https://github.com/conda-forge/miniforge/releases
2、下载后,cd到下载到的文件夹,打开终端输入:

sh 下载的sh文件名.sh

3、完成Miniforge的安装。常用的指令如下:
(1)创建新环境

注意:pyTorch安装对python版本是有要求的,详见下文。

语法:conda create -n 环境名 python=版本。例如:

conda create -n new1 python=3.6

(2)激活环境
语法:conda acitvate 环境名。例如:

conda activate new1

(3)删除环境
语法:conda remove -n 环境名 --all。例如:

conda remove -n new1 --all

安装pyTorch-GPU

需要到Nvidia PyTorch for Jetson官网上下载wheel文件安装才可以,不可以用其他版本的pytorch,否则不是GPU版本,性能极低:
官网地址:https://forums.developer.nvidia.com/t/pytorch-for-jetson/72048
本文由于xavier的Jetpack版本是JetPack 4.5 (L4T R32.5.0) ,因此最高安装1.10.0版本。

1、下载对应版本的wheel文件。
2、打开下载文件所在文件夹,输入如下代码(以我安装的1.10.0版本为例):

注意:不要sudo。

sudo apt-get install libopenblas-base libopenmpi-dev python3-pip
pip3 install Cython
pip3 install numpy torch-1.10.0-cp36-cp36m-linux_aarch64.whl

至此完成pyTorch-GPU版本的安装。

安装TorchVision

TorchVision和PyTorch的版本是要对应上的,不能乱装,不然不能正常运行。在官网地址的Installation一栏里有描述对应关系,由此可以看出我安装的pyTorch v1.10.0应该对应安装torchvision v0.11.1 版本。

Select the version of torchvision to download depending on the version of PyTorch that you have installed:
PyTorch v1.0 - torchvision v0.2.2
PyTorch v1.1 - torchvision v0.3.0
PyTorch v1.2 - torchvision v0.4.0
PyTorch v1.3 - torchvision v0.4.2
PyTorch v1.4 - torchvision v0.5.0
PyTorch v1.5 - torchvision v0.6.0
PyTorch v1.6 - torchvision v0.7.0
PyTorch v1.7 - torchvision v0.8.1
PyTorch v1.8 - torchvision v0.9.0
PyTorch v1.9 - torchvision v0.10.0
PyTorch v1.10 - torchvision v0.11.1
PyTorch v1.11 - torchvision v0.12.0
PyTorch v1.12 - torchvision v0.13.0

到pipy官网寻找到对应版本号,0.11.1网址如下:
https://pypi.org/project/torchvision/0.11.1/
输入以下代码进行安装:

pip install torchvision==0.11.1

至此完成torch和torchvision的安装

进行验证:
首先输入python,然后依次输入以下代码
得到如下答案即为安装成功:

>>> import torch
>>> import torchvision
>>> print(torch.__version__)
1.10.0##torch版本
>>> print('CUDA available: ' + str(torch.cuda.is_available()))
CUDA available: True##GPU版本
>>> print('cuDNN version: ' + str(torch.backends.cudnn.version()))
cuDNN version: 8201
>>> a = torch.cuda.FloatTensor(2).zero_()
>>> print('Tensor a = ' + str(a))
Tensor a = tensor([0., 0.], device='cuda:0')
>>> b = torch.randn(2).cuda()
>>> print('Tensor b = ' + str(b))
Tensor b = tensor([-0.3352,  0.5866], device='cuda:0')
>>> c = a + b
>>> print('Tensor c = ' + str(c))
Tensor c = tensor([-0.3352,  0.5866], device='cuda:0')
>>> print(torchvision.__version__)
0.11.1##torchvision版本

安装python-pcl库

这里安装方法借鉴了这篇文章:http://t.csdn.cn/r5Xuf
1、首先安装C++版本的PCL库

sudo apt update  #不更新可能找不到最新的版本
sudo apt install libpcl-dev #安装pcl依赖库

2、下载pcl库源码

直接pip安装会报错,原因是pip会直接安装pcl1.8版本,需要去github上下载源码修改后安装

网址:
https://github.com/strawlab/python-pcl
下载后解压, 进入解压后的文件夹。

3、修改setup.py文件
把setup.py打开,修改里面的语句
1)在726行 VTK=7.0 改为6.3
2)752替换为如下代码

        vtklibreleases = ['vtkalglib-' + vtk_version, 'vtkChartsCore-' + vtk_version, 'vtkCommonColor-' + vtk_version, 'vtkCommonComputationalGeometry-' + vtk_version, 'vtkCommonCore-' + vtk_version, 'vtkCommonDataModel-' + vtk_version, 'vtkCommonExecutionModel-' + vtk_version, 'vtkCommonMath-' + vtk_version, 'vtkCommonMisc-' + vtk_version, 'vtkCommonSystem-' + vtk_version, 'vtkCommonTransforms-' + vtk_version, 'vtkDICOMParser-' + vtk_version, 'vtkDomainsChemistry-' + vtk_version, 'vtkexoIIc-' + vtk_version, 'vtkFiltersAMR-' + vtk_version, 'vtkFiltersCore-' + vtk_version, 'vtkFiltersExtraction-' + vtk_version, 'vtkFiltersFlowPaths-' + vtk_version, 'vtkFiltersGeneral-' + vtk_version, 'vtkFiltersGeneric-' + vtk_version, 'vtkFiltersGeometry-' + vtk_version, 'vtkFiltersHybrid-' + vtk_version, 'vtkFiltersHyperTree-' + vtk_version, 'vtkFiltersImaging-' + vtk_version, 'vtkFiltersModeling-' + vtk_version, 'vtkFiltersParallel-' + vtk_version, 'vtkFiltersParallelImaging-' + vtk_version, 'vtkFiltersProgrammable-' + vtk_version, 'vtkFiltersSelection-' + vtk_version, 'vtkFiltersSMP-' + vtk_version, 'vtkFiltersSources-' + vtk_version, 'vtkFiltersStatistics-' + vtk_version, 'vtkFiltersTexture-' + vtk_version, 'vtkFiltersVerdict-' + vtk_version,  'vtkGeovisCore-' + vtk_version, 'vtkImagingColor-' + vtk_version, 'vtkImagingCore-' + vtk_version, 'vtkImagingFourier-' + vtk_version, 'vtkImagingGeneral-' + vtk_version, 'vtkImagingHybrid-' + vtk_version, 'vtkImagingMath-' + vtk_version, 'vtkImagingMorphological-' + vtk_version, 'vtkImagingSources-' + vtk_version, 'vtkImagingStatistics-' + vtk_version, 'vtkImagingStencil-' + vtk_version, 'vtkInfovisCore-' + vtk_version, 'vtkInfovisLayout-' + vtk_version, 'vtkInteractionImage-' + vtk_version, 'vtkInteractionStyle-' + vtk_version, 'vtkInteractionWidgets-' + vtk_version, 'vtkIOAMR-' + vtk_version, 'vtkIOCore-' + vtk_version, 'vtkIOEnSight-' + vtk_version, 'vtkIOExodus-' + vtk_version, 'vtkIOExport-' + vtk_version, 'vtkIOGeometry-' + vtk_version, 'vtkIOImage-' + vtk_version, 'vtkIOImport-' + vtk_version, 'vtkIOInfovis-' + vtk_version, 'vtkIOLegacy-' + vtk_version, 'vtkIOLSDyna-' + vtk_version, 'vtkIOMINC-' + vtk_version, 'vtkIOMovie-' + vtk_version, 'vtkIONetCDF-' + vtk_version, 'vtkIOParallel-' + vtk_version, 'vtkIOParallelXML-' + vtk_version, 'vtkIOPLY-' + vtk_version, 'vtkIOSQL-' + vtk_version, 'vtkIOVideo-' + vtk_version, 'vtkIOXML-' + vtk_version, 'vtkIOXMLParser-' + vtk_version,'vtkmetaio-' + vtk_version, 'vtkParallelCore-' + vtk_version, 'vtkRenderingAnnotation-' + vtk_version, 'vtkRenderingContext2D-' + vtk_version, 'vtkRenderingContextOpenGL-' + vtk_version, 'vtkRenderingCore-' + vtk_version, 'vtkRenderingFreeType-' + vtk_version, 'vtkRenderingGL2PS-' + vtk_version, 'vtkRenderingImage-' + vtk_version, 'vtkRenderingLabel-' + vtk_version, 'vtkRenderingLIC-' + vtk_version, 'vtkRenderingLOD-' + vtk_version, 'vtkRenderingOpenGL-' + vtk_version, 'vtkRenderingVolume-' + vtk_version, 'vtkRenderingVolumeOpenGL-' + vtk_version, 'vtksys-' + vtk_version, 'vtkverdict-' + vtk_version, 'vtkViewsContext2D-' + vtk_version, 'vtkViewsCore-' + vtk_version, 'vtkViewsInfovis-' + vtk_version]

4、编译、安装pcl库
执行如下代码:

python setup.py build_ext -i
python setup.py install

到这里,等完成不报错,就是安装成功了

5、验证

(new1) aaeon@aaeon-desktop:~/Downloads/python-pcl-master$ python
Python 3.6.15 | packaged by conda-forge | (default, Dec  3 2021, 19:12:04) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pcl
>>> 

至此,成功安装python-pcl库。

安装Opencv 3.2

其实我这个机器是自带Opencv4.1.1的,但项目要求要使用3.2,所以还是要安装一下

1、安装依赖

sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python2.7-dev
sudo apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update

如果“E: Unable to locate package libjasper-dev”这类错误,一般是源设置导致的,可以参考下这篇文章和他下面的评论:
http://t.csdn.cn/2DIsE
我主要是执行了这个代码后成功的:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

虽然apt update的时候还会报错,但可以安装

2、下载源码

mkdir -p ~/tools/
cd ~/tools/
curl -L https://github.com/opencv/opencv/archive/3.2.0.zip -o opencv-3.2.0.zip
unzip opencv-3.2.0.zip 
cd opencv-3.2.0/

3、编译

mkdir release
cd release/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D ENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

这里由于【The following variables are used in this project, but they are set to NOTFOUND.解决方案】
cuda9不再支持2.0架构等等原因,会有
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
后面跟着一大长串报错,借鉴这篇文章进行修改后重新编译可以解决
http://t.csdn.cn/9nXRT

1)在/home/mario/Projects/opencv-3.1.0/cmake下找到FindCUDA.cmake文件,找到行:

find_cuda_helper_libs(nppi)

替换为:

find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)

2)找到行

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

替换为

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};
${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};
${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

3)找到行

unset(CUDA_nppi_LIBRARY CACHE)

替换为

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

4)在/home/mario/Projects/opencv-3.1.0/cmake下找到OpenCVDetectCUDA.cmake文件,找到以下行,删除:

    if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")

5)在/usr/local/cuda-9.0/include下找到cudafp16.h文件,将这个文件拷贝到opencv目录的cudev下opencv-3.2.0/modules/cudev/include/opencv2/cudev,然后在该目录下的common.hpp的文件添加:

#include 

6)重新编译

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..

编译成功

5、安装

sudo make install

6、配置环境
打开/etc/ld.so.conf在文件中加上一行

include /usr/loacal/lib

执行:

sudo ldconfig

打开/etc/bash.bashrc,在文件中添加

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

执行:

source /etc/bash.bashrc

配置完成

7、检查Opencv版本:

pkg-config opencv --modversion

至此完成Opencv3.2安装

安装Ceres-solver库

1、下载ceres包
下载地址:https://github.com/ceres-solver/ceres-solver/releases/tag/1.14.0
2、解压,进入解压后文件夹,新建build文件夹:

mkdir build
cd build
cmake ..

在这里很有可能由于ceres和eigen版本对应不好而导致cmake失败,我这里报错如下:

Can’t find Google Log (glog). Please set either: glog_DIR (newer CMake

猜测可能是某个依赖包没有装,索性全部安装,运行命令:

sudo apt-get install -y google-mock libboost-all-dev  libeigen3-dev libgflags-dev libgoogle-glog-dev liblua5.2-dev libprotobuf-dev  libsuitesparse-dev libwebp-dev ninja-build protobuf-compiler python-sphinx  ros-melodic-tf2-eigen libatlas-base-dev libsuitesparse-dev liblapack-dev

安装后,继续cmake,然后安装即可

 cmake ..
 sudo make install

至此,完成ceres-solver库安装

你可能感兴趣的:(ARM,pytorch,人工智能,python,opencv)