一、环境介绍
芯片型号:NVIDIA Jetson AGX Orin(32GB ram)
AI算力:200TOPS
架构:aarch64
系统版本:Ubuntu20.04 focal
内核版本:5.10.104-1-2-0-Tegra
Python版本:3.8.10
二、任务介绍
完成新环境搭建,包括CUDA版OpenCV,pytorch,torchvision;cuda,cudnn,tensorrt,jtop等
三、环境搭建
1.jtop安装
jtop是Jetson系列的系统监控工具,使用以下指令安装:
sudo apt install python3-pip
sudo -H pip3 install -U pip
sudo -H pip install jetson-stats
使用以下指令开启工具:
jtop
显示界面如下:
环境搭建时主要看的是数字7INFO中的内容,其中显示了大部分的版本号信息。
2.cuda、cudnn、tensorrt安装
Jetpack是Jetson板上的开发工具包,apt安装Jetpack时会默认安装适配版本的cuda、cudnn、tensorrt。
1)检查L4T(Linux for Tegra)版本
cat /etc/nv_tegra_release
# R35 (release), REVISION: 1.0, GCID: 31346300, BOARD: t186ref, EABI: aarch64, DATE: Thu Aug 25 18:41:45 UTC 2022
R35 (release), REVISION: 1.0代表L4T是35.1.0版本
添加源:(不添加源后面安装依赖会报错Unable to locate package nvidia-jetpack)
sudo bash -c 'echo "deb https://repo.download.nvidia.com/jetson/common r35.1 main" >> /etc/apt/sources.list.d/nvidia-l4t-apt-source.list'
sudo bash -c 'echo "deb https://repo.download.nvidia.com/jetson/t234 r35.1 main" >> /etc/apt/sources.list.d/nvidia-l4t-apt-source.list'
2)开始安装
sudo apt upgrade
sudo apt update
sudo apt dist-upgrade
sudo reboot
sudo apt install nvidia-jetpack
3)安装验证
jtop中查看7INFO中是否正确显示版本号,如下图则安装成功:
3.OpenCV安装
1)版本检查
jtop中查看OpenCV版本,如果显示带有‘-with CUDA:NO’则表示自带的OpenCV不支持CUDA加速,需要卸载重装。
2)卸载原版OpenCV
卸载默认版本OpenCV:
sudo apt purge libopencv*
sudo apt autoremove
sudo apt update
如果有自己后安装的OpenCV,则使用以下指令卸载(假设源码编译安装的OpenCV4.6.0):
# 进入编译安装目录
cd .../opencv-4.6.0/build
sudo make uninstall
cd .../opencv-4.6.0
sudo rm -r build
3)安装依赖以及解决报错
# 依次输入以下命令
sudo apt install -y build-essential checkinstall cmake pkg-config yasm git gfortran
sudo apt update
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpngdev libtiff-dev libjasper-dev libdc1394-22-dev
# 以防上面保存为pdf以后显示不全
sudo apt install -y build-essential checkinstall cmake pkg-config yasm git gfortran
sudo apt update
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpngdev libtiff-dev libjasper-dev libdc1394-22-dev
·如果第一条命令就报错则需要先换源:
修改文件/etc/apt/source.list,改为下面的源:
# 默认注释了源码仓库,如有需要可自行取消注释
deb [arch=arm64] http://mirrors.ustc.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal main main restricted universe multiverse
deb [arch=arm64] http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb [arch=arm64] http://mirrors.ustc.edu.cn/ubuntu-ports/ focalbackports main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb [arch=arm64] http://mirrors.ustc.edu.cn/ubuntu-ports/ focalsecurity main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
改完以后sudo apt-get update即可。
·倒数第三条命令如果报错‘无法定位libjasper-dev’:
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev
·最后一条命令python-dev报错:
python-dev改成python3-dev,重新运行命令 。
4)下载OpenCV以及OpenCV-contrib源码
OpenCV:Releases · opencv/opencv (github.com)
OpenCV-contrib:Tags · opencv/opencv_contrib · GitHub
因为我的Python版本是3.8,所以下载OpenCV和contrib都下的4.6.0版本
5)编译安装OpenCV
·把OpenCV和OpenCV-contrib解压,把OpenCV-contrib解压后的文件夹改名成opencv_contrib后放到OpenCV的文件夹。
# 进入opencv文件夹
cd opencv-4.6.0
# 建立编译文件夹
mkdir build
cd build
·执行cmake构建
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-D CUDA_ARCH_BIN='8.7' \
-D CUDA_ARCH_PTX='8.7' \
-D WITH_CUDA=1 \
-D BUILD_opencv_python3=1 \
-D BUILD_opencv_python2=1 \
-D WITH_FFMPEG=1 \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D OPENCV_GENERATE_PKGCONFIG=1 \
-D WITH_GTK_2_X=ON \
-D WITH_GSTREAMER=ON \
..
其中:
CMAKE_BUILD_TYPE=RELEASE $~~~~~~$ #表示编译发布版本
CMAKE_INSTALL_PREFIX$~~~~~~$ #指定安装路径
OPENCV_EXTRA_MODULED_PATH $~~~~~~$ # opencv_contrib路径
CUDA_ARCH_BIN $~~~~~~$ #指定GPU算力,在jtop的INFO中查看(很重要)
WITH_CUDA $~~~~~~$ #使用CUDA加速
WITH_GSTREAMER $~~~~~~$ #使用OpenCV的gstreamer包
执行完之后再build目录下查找download_with_wget.sh文件,如果文件不存在说明全部下载成功。
·编译OpenCV
注意:如果有安装anaconda,建议关闭conda环境,进入主机环境后再编译:
conda deactivate
因为conda屏蔽了某些主机环境的库,导致编译时链接到了conda的库而冲突报错。
# 开始编译,按设备配置设置编译时使用的线程数,越多越快
make -j8
编译完成后:
# 开始安装
sudo make install
6)安装验证
在jtop中查看OpenCV的版本以及后面是否为‘-with CUDA:YES’
python验证:
python
import cv2
print(cv2.__version__)
输出OpenCV版本则表示安装成功
如果报错'No module named 'cv2''找不到则参考手动编译CUDA版OpenCV4.6.0,在python中报错‘No module named ‘cv2‘’的解决方法-CSDN博客
4.pytorch,torchvision安装
1)安装依赖
# 配置设备环境
sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev gfortran
# 安装其他库
pip install -U numpy==1.19.4 future==0.18.2 mock==3.0.5 h5py==2.10.0 keras_preprocessing==1.1.1 keras_applications==1.0.8 gast==0.2.2 futures protobuf pybind11
2)安装pytorch
python,pytorch,torchvision对应关系表:
torch | torchvision | Python |
---|---|---|
main / nightly | main / nightly | >=3.8 , <=3.11 |
2.1 | 0.16 | >=3.8 , <=3.11 |
2.0 | 0.15 | >=3.8 , <=3.11 |
1.13 | 0.14 | >=3.7.2 , <=3.10 |
1.12 | 0.13 | >=3.7 , <=3.10 |
1.11 | 0.12 | >=3.7 , <=3.10 |
1.10 | 0.11 | >=3.6 , <=3.9 |
1.9 | 0.10 | >=3.6 , <=3.9 |
1.8 | 0.9 | >=3.6 , <=3.9 |
1.7 | 0.8 | >=3.6 , <=3.9 |
1.6 | 0.7 | >=3.6 , <=3.8 |
1.5 | 0.6 | >=3.5 , <=3.8 |
1.4 | 0.5 | ==2.7 , >=3.5 , <=3.8 |
1.3 | 0.4.2 / 0.4.3 | ==2.7 , >=3.5 , <=3.7 |
1.2 | 0.4.1 | ==2.7 , >=3.5 , <=3.7 |
1.1 | 0.3 | ==2.7 , >=3.5 , <=3.7 |
<=1.0 | 0.2 | ==2.7 , >=3.5 , <=3.7 |
我是python3.8所以安装pytorch1.12.0和torchvision0.13.0
下载完以后:
# pip安装pytorch
pip install torch-1.12.0a0+2c916ef.nv22.3-cp38-cp38-linux-aarch64.whl
安装完以后:
# 验证安装
python
import torch
print(torch.__version__)
3)安装torchvision
因为torchvision没有支持CUDA版本的whl包,所以需要下载源码编译安装:
下载:
git clone --branch v0.13.0 https://github.com/pytorch/vision torchvision
配置环境:
sudo apt-get install libopenblas-base libopenmpi-dev
pip install Cython
进入文件夹编译安装:
cd torchvision
export BUILD_VERSION=0.13.0
python setup.py install --user
安装完以后验证:
python
import torchvision
print(torchvision.__version__)
至此,所需环境基本安装完毕。