Jetson MMYOLO(安装篇)

mmyolo/README_zh-CN.md at main · open-mmlab/mmyolo (github.com) | 欢迎来到 MMYOLO 中文文档! — MMYOLO 0.5.0 文档

MMYOLO_cheatsheet_SpringFestival.pdf

NVIDIA L4T ML | NVIDIA NGC

PyTorch for Jetson - Jetson & Embedded Systems / Jetson Nano - NVIDIA Developer Forums

open-mmlab/mmcv at v2.0.1 (github.com)

open-mmlab/mmdetection at v3.0.0 (github.com)

1 背景

在Nvidia agx xavier中使用Nvidia L4T ML容器安装使用MMYOLO,实现将模型转换。

转换路线:自己的yolo模型 → mmyolo模型 → ONNX → TRT Engine

主要环境:jetpack:5.1 l4t-ml:r35.2.1

容器内自带环境:

  • Python 3.8.10
  • TensorFlow 2.11.0
  • PyTorch v2.0.0
  • torchvision v0.14.1
  • torchaudio v0.13.1
  • onnx 1.13.0
  • CuPy 11.5.0
  • numpy 1.21.1
  • numba 0.56.4
  • PyCUDA 2022.2
  • OpenCV 4.5.0 (with CUDA)
  • pandas 1.5.3
  • scipy 1.10.0
  • scikit-learn 1.2.1
  • JupyterLab 3.6.1

经过测试容器预编译过的pytorch版本,最新只有v1.11.0支持torch.distributed,否则安装mmcv有问题,在后面的【安装mmcv】中会提供安装方法。

在NVIDIA论坛中也有关于这个问题的讨论 👇

Torch.distributed is not available on Jetson PyTorch - Jetson & Embedded Systems / Jetson Orin NX - NVIDIA Developer Forums

在这里插入图片描述

mmyolo需要安装的主要依赖关系如下图:

在这里插入图片描述

经过requirments的梳理,选择了以下版本:

mmcvmmdetection(mmdet)mmyolopytorchtorchvision
v2.0.1v3.0.0v0.5.01.11.00.12.0或0.13.0

下述文件可从网盘获取 👉 链接:https://pan.baidu.com/s/12jr3dJ7c8nunXA9ErtpRSw?pwd=56kv提取码:56kv

  • torch-1.11.0-cp38-cp38-linux_aarch64.whl

注:主机向docker容器复制文件的方法 👇

sudo docker cp 主机文件路径 容器名:存放路径

例如:

sudo docker cp ~/cly/torch-1.11.0-cp38-cp38-linux_aarch64.whl mm_v1:/root

2 构建容器基本环境

2.1 构建容器

拉取镜像

sudo docker pull nvcr.io/nvidia/l4t-ml:r35.2.1-py3

构建并运行容器

sudo docker run -dit --runtime nvidia --name mm_v1 -p 2221:22 nvcr.io/nvidia/l4t-ml:r35.2.1-py3 /bin/bash

参数说明:

  • -i: 交互式操作。
  • -t: 终端。
  • -d: 指定容器的运行模式,让docker容器后台运行。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
  • -p: ​这里将主机2221(可自定义)端口映射到容器22端口,方便后续ssh远程连接容器

进入容器

sudo docker exec -it mm_v1 /bin/bash

使用exit退出后也不会停止docker容器,所以推荐使用docker exec进入容器。

2.2 切换默认python版本

(10条消息) Jetson人工智能系列(1)- jetson安装python和anaconda那些事_jetson anaconda_Gaosiy的博客-CSDN博客

((20230731115218-hb2ayd8 ‘切换python版本’))

进行配置

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2

切换版本

sudo update-alternatives --config python

回复版本对应数字,这里是2,回车。

2.3 换源

2.3.1 apt换源

备份

cd /etc/apt
cp sources.list sources.list.bak

更换清华源

echo 'deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse' > sources.list

附阿里源:

deb http://mirrors.aliyun.com/ubuntu-ports/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security main restricted universe multiverse

更新

sudo apt update
2.3.2 pip换源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

附其他源:

阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
官方源 https://pypi.Python.org/simple/

2.4 安装ninja加快编译(强烈推荐)

sudo apt install ninja-build

3 安装MMCV

3.1 Pytorch和torchvision

mmcv依赖pytorch,需要先测试pytorch的安装

python -c "import torch; import torchvision; print('cuda', torch.cuda.is_available()); print('distributed', torch.distributed.is_available())"

如果distributed False的话就要重新安装Pytorch和torchvision了

安装pytorch 1.11.0

wget https://nvidia.box.com/shared/static/ssf2v7pf5i245fk4i0q926hy4imzs2ph.whl

注:上述文件网盘已提供

pip install torch-1.11.0-cp38-cp38-linux_aarch64.whl

安装torchvision

sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev libavcodec-dev libavformat-dev libswscale-dev libopenblas-dev -y
git clone https://gitee.com/cly0216/torchvision
cd torchvision
git checkout tags/v0.12.0 -b v0.12.0
export BUILD_VERSION=0.12.0
pip install -e .

再次检验

python -c "import torch; import torchvision; print('cuda', torch.cuda.is_available()); print('distributed', torch.distributed.is_available())"

在这里插入图片描述

3.2 mmcv

安装依赖

pip install opencv-python install "opencv-python-headless<4.3"
pip install onnxruntime

编译安装

git clone -b v2.0.1 https://gitee.com/cly0216/mmcv.git
cd mmcv
MMCV_WITH_OPS=1 pip install -e . -v

过程会安装mmengine,不用另外安装

在这里插入图片描述

设置PYTHONPATH(注意,这里把后面需要设置的也一起加上)

由于是编译安装的,如果不设置PATHONPATH,后面执行程序的时候from xx import xxx 的时候就会找不到xx。

防止误操作,先备份

sudo cp ~/.bashrc ~/.bashrc.bak

添加PYTHONPATH(注意,实际路径以自己的为准,这里我文件全放在/root路径下了)

sudo echo 'export PYTHONPATH=/root/mmcv:/root/mmdetection:/root/mmyolo:$PYTHONPATH' >> ~/.bashrc

更新

source ~/.bashrc

如果上述方法有权限问题,建议使用vim

sudo apt update
sudo install vim -y
sudo vim ~/.bashrc

按住shift + g 跳到最后一行

按字母o在下面一行插入export PYTHONPATH=/root/mmcv:/root/mmdetection:/root/mmyolo:$PYTHONPATH

:x回车退出

更新

source ~/.bashrc

安装验证

python .dev_scripts/check_installation.py

如果还是出错,检查PYTHONPATH是否正确,还是不行,尝试手动执行

输入

python

执行下述代码:mmcv/.dev_scripts/check_installation.py at main · open-mmlab/mmcv (github.com)

import numpy as np
import torch

from mmcv.ops import box_iou_rotated
from mmcv.utils import collect_env


def check_installation():
    """Check whether mmcv has been installed successfully."""
    np_boxes1 = np.asarray(
        [[1.0, 1.0, 3.0, 4.0, 0.5], [2.0, 2.0, 3.0, 4.0, 0.6],
         [7.0, 7.0, 8.0, 8.0, 0.4]],
        dtype=np.float32)
    np_boxes2 = np.asarray(
        [[0.0, 2.0, 2.0, 5.0, 0.3], [2.0, 1.0, 3.0, 3.0, 0.5],
         [5.0, 5.0, 6.0, 7.0, 0.4]],
        dtype=np.float32)
    boxes1 = torch.from_numpy(np_boxes1)
    boxes2 = torch.from_numpy(np_boxes2)

    # test mmcv with CPU ops
    box_iou_rotated(boxes1, boxes2)
    print('CPU ops were compiled successfully.')

    # test mmcv with both CPU and CUDA ops
    if torch.cuda.is_available():
        boxes1 = boxes1.cuda()
        boxes2 = boxes2.cuda()
        box_iou_rotated(boxes1, boxes2)
        print('CUDA ops were compiled successfully.')
    else:
        print('No CUDA runtime is found, skipping the checking of CUDA ops.')


if __name__ == '__main__':
    print('Start checking the installation of mmcv ...')
    check_installation()
    print('mmcv has been installed successfully.\n')

    env_info_dict = collect_env()
    env_info = '\n'.join([(f'{k}: {v}') for k, v in env_info_dict.items()])
    dash_line = '-' * 60 + '\n'
    print('Environment information:')
    print(dash_line + env_info + '\n' + dash_line)

看到下面表示安装成功

在这里插入图片描述

4 安装MMDetection

mmdetection/docs/zh_cn/get_started.md at master · open-mmlab/mmdetection (github.com)

mmdetection安装比较简单

git clone -b v3.0.0 https://gitee.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e .

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NH52V4pp-1691034752658)(assets/image-20230801145920-da3o9lx.png)]

如果没有设置PYTHONPATH,参照mmcv进行设置。

验证安装

下载模型文件

mkdir checkpoints
wget http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
cd ..
python
from mmdet.apis import init_detector, inference_detector

config_file = 'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py'
# 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
# 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)
# 推理演示图像
inference_detector(model, 'demo/demo.jpg')

在这里插入图片描述

5 安装MMYOLO

mmyolo安装也很简单

git clone -b v0.5.0 https://gitee.com/open-mmlab/mmyolo.git
cd mmyolo
# Install albumentations
pip install -r requirements/albu.txt
# Install MMYOLO
pip install -v -e .
# "-v" 指详细说明,或更多的输出
# "-e" 表示在可编辑模式下安装项目,因此对代码所做的任何本地修改都会生效,从而无需重新安装。

在这里插入图片描述

如果没有设置PYTHONPATH,参照mmcv进行设置。

安装验证

mmyolo/configs/yolov5 at main · open-mmlab/mmyolo (github.com)
安装和验证 — MMYOLO 0.5.0 文档

wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth
python3
from mmdet.apis import init_detector, inference_detector

config_file = 'configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'
checkpoint_file = 'yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')  # or device='cpu'
inference_detector(model, 'demo/demo.jpg')

# 可选参数
# --out-dir ./output *检测结果输出到指定目录下,默认为./output, 当--show参数存在时,不保存检测结果
# --device cuda:0    *使用的计算资源,包括cuda, cpu等,默认为cuda:0
# --show             *使用该参数表示在屏幕上显示检测结果,默认为False
# --score-thr 0.3    *置信度阈值,默认为0.3

在这里插入图片描述

环境搭建部分已经完成,使用MMYOLO进行模型转换放到下部分。

6 安装mmLab系列项目出现过的一些问题

Q1:

The conflict is caused by:
mmdeploy 1.0.0 depends on protobuf<=3.20.2
onnx 1.13.1 depends on protobuf<4 and >=3.20.2
mmdeploy 1.0.0 depends on protobuf<=3.20.2
onnx 1.13.0 depends on protobuf<4 and >=3.20.2

A1:

这个是因为使用git clone后没有选择特定的版本,默认安装了最新版本,最新版本之间的依赖产生冲突。

Q2:

mportError: No module named ‘seaborn’

A2:在使用mmyolo时,用pytorch加载模型报这个错时,缺少这个包,安装即可。

pip install seaborn

Q3:

DeprecationWarning: pkg_resources is deprecated as an API

mim cannot import name ‘six’ from ‘pkg_resources.extern’

A3:

这是一个警告,但是报这个问题,并且无法使用pip安装某个包的时候,很可能是因为apt换源不对的问题(我也不知道为什么apt源会影响到pip安装)。

原来jetpack4.6.1是ubuntu 18.04,使用的是这个源👇没有问题

#清华源
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main universe restricted
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main universe restricted

但是升级jetpack5.1后是ubuntu20.04,再使用这个源就会出错。

改为这个源👇之后就没问题了

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值