Raspberry Pi 4B 部署 YOLOX

Posted ʚVVcatɞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Raspberry Pi 4B 部署 YOLOX相关的知识,希望对你有一定的参考价值。

环境

  • Python:3.7.3
  • YOLOX:0.1.1rc0
  • torch:1.8.0
  • torchvision:0.9.0
  • OpenVINO:2021.4.582
  • g++ (Raspbian 8.3.0-6+rpi1) 8.3.0
  • raspberrypi:5.10.52-v7l+
  • 系统指令集:armv7l
  • numpy:1.20.3

组件

  • Raspberry Pi 4B 8G
  • NCS2

pi@raspberrypi:~/Desktop/YOLOX-main $ uname -a
Linux raspberrypi 5.10.52-v7l+ #1441 SMP Tue Aug 3 18:11:56 BST 2021 armv7l GNU/Linux

安装git后下载

git clone https://github.com/Megvii-BaseDetection/YOLOX.git

安装 torch 1.8.0

pip3 install torch-1.8.0a0+56b43f4-cp37-cp37m-linux_armv7l.whl 

安装 torchvision 0.9.0

pip3 install torchvision-0.9.0a0+8fb5838-cp37-cp37m-linux_armv7l.whl

安装 torch 依赖

sudo apt-get install libopenblas-dev

pi@raspberrypi:~/Desktop/YOLOX-main $ sudo apt-get install libopenblas-dev
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
下列软件包是自动安装的并且现在不需要了:
gconf-service gconf2-common libgconf-2-4 python-colorzero
使用’sudo apt autoremove’来卸载它(它们)。
将会同时安装下列软件:
libopenblas-base
下列【新】软件包将被安装:
libopenblas-base libopenblas-dev
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 3 个软件包未被升级。
需要下载 3,845 kB 的归档。
解压缩后会消耗 26.2 MB 的额外空间。
您希望继续执行吗? [Y/n]
获取:1 http://mirrors.aliyun.com/raspbian/raspbian buster/main armhf libopenblas-base armhf 0.3.5+ds-3+rpi1 [1,980 kB]
获取:2 http://mirrors.aliyun.com/raspbian/raspbian buster/main armhf libopenblas-dev armhf 0.3.5+ds-3+rpi1 [1,865 kB]
已下载 3,845 kB,耗时 3秒 (1,333 kB/s)
正在选中未选择的软件包 libopenblas-base:armhf。
(正在读取数据库 … 系统当前共安装有 175576 个文件和目录。)
准备解压 …/libopenblas-base_0.3.5+ds-3+rpi1_armhf.deb …
正在解压 libopenblas-base:armhf (0.3.5+ds-3+rpi1) …
正在选中未选择的软件包 libopenblas-dev:armhf。
准备解压 …/libopenblas-dev_0.3.5+ds-3+rpi1_armhf.deb …
正在解压 libopenblas-dev:armhf (0.3.5+ds-3+rpi1) …
正在设置 libopenblas-base:armhf (0.3.5+ds-3+rpi1) …
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/openblas/libblas.so.3 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/libblas.so.3 (libblas.so.3-arm-linux-gnueabihf)
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/openblas/liblapack.so.3 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/liblapack.so.3 (liblapack.so.3-arm-linux-gnueabihf)
正在设置 libopenblas-dev:armhf (0.3.5+ds-3+rpi1) …
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/openblas/libblas.so 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/libblas.so (libblas.so-arm-linux-gnueabihf)
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/openblas/liblapack.so 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/liblapack.so (liblapack.so-arm-linux-gnueabihf)
正在处理用于 libc-bin (2.28-10+rpi1) 的触发器 …

Raspberry Pi 4B 安装 OpenVINO,请参考以下链接,最新版本的OpenVINO,yolox也支持。
https://blog.csdn.net/qq_44989881/article/details/119792769?spm=1001.2014.3001.5501

安装 numpy
注:如果 numpy 低于 1.20.0 版本强烈建议升级 numpy版本,否则会报 numpy 版本过低的错误。

pip3 install numpy==1.20.3

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install numpy==1.20.3
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting numpy==1.20.3
Downloading https://www.piwheels.org/simple/numpy/numpy-1.20.3-cp37-cp37m-linux_armv7l.whl (11.6MB)
100% |████████████████████████████████| 11.6MB 38kB/s
Installing collected packages: numpy
Successfully installed numpy-1.20.3

安装 yolox 需要的环境

pip3 install -r requirements.txt

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install -r requirements.txt
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from -r requirements.txt (line 2)) (1.16.2)
Requirement already satisfied: torch>=1.7 in /home/pi/.local/lib/python3.7/site-packages (from -r requirements.txt (line 3)) (1.8.0a0+56b43f4)
Requirement already satisfied: opencv_python in /home/pi/.local/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (4.5.3.56)
Collecting loguru (from -r requirements.txt (line 5))
Downloading https://files.pythonhosted.org/packages/6d/48/0a7d5847e3de329f1d0134baf707b689700b53bd3066a5a8cfd94b3c9fc8/loguru-0.5.3-py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 744kB/s
Collecting scikit-image (from -r requirements.txt (line 6))
Downloading https://www.piwheels.org/simple/scikit-image/scikit_image-0.18.3-cp37-cp37m-linux_armv7l.whl (29.9MB)
100% |████████████████████████████████| 29.9MB 15kB/s
Collecting tqdm (from -r requirements.txt (line 7))
Downloading https://files.pythonhosted.org/packages/9c/05/cf212f57daa0eb6106fa668a04d74d932e9881fd4a22f322ea1dadb5aba0/tqdm-4.62.2-py2.py3-none-any.whl (76kB)
100% |████████████████████████████████| 81kB 1.7MB/s
Requirement already satisfied: torchvision in /home/pi/.local/lib/python3.7/site-packages (from -r requirements.txt (line 8)) (0.9.0a0+8fb5838)
Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (from -r requirements.txt (line 9)) (5.4.1)
Collecting thop (from -r requirements.txt (line 10))
Downloading https://files.pythonhosted.org/packages/6c/8b/22ce44e1c71558161a8bd54471123cc796589c7ebbfc15a7e8932e522f83/thop-0.0.31.post2005241907-py3-none-any.whl
Collecting ninja (from -r requirements.txt (line 11))
Downloading https://files.pythonhosted.org/packages/52/fd/6a82411fbc23b62bf5ddf511b09cb3d6059d1b84ec6833589aa064cea207/ninja-1.10.2.tar.gz
Installing build dependencies … done
Collecting tabulate (from -r requirements.txt (line 12))
Downloading https://files.pythonhosted.org/packages/ca/80/7c0cad11bd99985cfe7c09427ee0b4f9bd6b048bd13d4ffb32c6db237dfb/tabulate-0.8.9-py3-none-any.whl
Collecting tensorboard (from -r requirements.txt (line 13))
Downloading https://files.pythonhosted.org/packages/a0/20/a59a30c32330e4ff704faa4273b251db042d495e0c367bcdf045c6fe26e9/tensorboard-2.6.0-py3-none-any.whl (5.6MB)
100% |████████████████████████████████| 5.6MB 80kB/s
Collecting onnx==1.8.1 (from -r requirements.txt (line 16))
Downloading https://files.pythonhosted.org/packages/2b/8d/c924f54e19ec71543c18658d86e8337579d5a784bd524b7f18e2e2cd6f17/onnx-1.8.1.tar.gz (5.2MB)
100% |████████████████████████████████| 5.2MB 82kB/s
Installing build dependencies … done
Collecting onnxruntime==1.8.0 (from -r requirements.txt (line 17))
Could not find a version that satisfies the requirement onnxruntime==1.8.0 (from -r requirements.txt (line 17)) (from versions: )
No matching distribution found for onnxruntime==1.8.0 (from -r requirements.txt (line 17))

安装 YOLOX

python3 setup.py install

安装 loguru

pip3 install loguru

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install loguru
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting loguru
Using cached https://files.pythonhosted.org/packages/6d/48/0a7d5847e3de329f1d0134baf707b689700b53bd3066a5a8cfd94b3c9fc8/loguru-0.5.3-py3-none-any.whl
Installing collected packages: loguru
Successfully installed loguru-0.5.3

安装 thop

pip3 install thop

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install thop
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting thop
Using cached https://files.pythonhosted.org/packages/6c/8b/22ce44e1c71558161a8bd54471123cc796589c7ebbfc15a7e8932e522f83/thop-0.0.31.post2005241907-py3-none-any.whl
Requirement already satisfied: torch>=1.0.0 in /home/pi/.local/lib/python3.7/site-packages (from thop) (1.8.0a0+56b43f4)
Requirement already satisfied: numpy in /home/pi/.local/lib/python3.7/site-packages (from torch>=1.0.0->thop) (1.20.3)
Requirement already satisfied: typing-extensions in /home/pi/.local/lib/python3.7/site-packages (from torch>=1.0.0->thop) (3.10.0.2)
Installing collected packages: thop
Successfully installed thop-0.0.31.post2005241907

安装 pycocotools

pip3 install pycocotools

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install pycocotools
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pycocotools
Downloading https://www.piwheels.org/simple/pycocotools/pycocotools-2.0.2-cp37-cp37m-linux_armv7l.whl (304kB)
100% |████████████████████████████████| 307kB 91kB/s
Collecting cython>=0.27.3 (from pycocotools)
Downloading https://files.pythonhosted.org/packages/ec/30/8707699ea6e1c1cbe79c37e91f5b06a6266de24f699a5e19b8c0a63c4b65/Cython-0.29.24-py2.py3-none-any.whl (979kB)
100% |████████████████████████████████| 983kB 237kB/s
Requirement already satisfied: matplotlib>=2.1.0 in /usr/lib/python3/dist-packages (from pycocotools) (3.0.2)
Requirement already satisfied: setuptools>=18.0 in /usr/lib/python3/dist-packages (from pycocotools) (40.8.0)
Installing collected packages: cython, pycocotools
Successfully installed cython-0.29.24 pycocotools-2.0.2

安装 tqdm

pip3 install tqdm

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install tqdm
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting tqdm
Using cached https://files.pythonhosted.org/packages/9c/05/cf212f57daa0eb6106fa668a04d74d932e9881fd4a22f322ea1dadb5aba0/tqdm-4.62.2-py2.py3-none-any.whl
Installing collected packages: tqdm
Successfully installed tqdm-4.62.2

以下为我在 Raspberry Pi 4B 测试的结果:
IOU还存在一些问题,后续再微调以下。

查看树莓派有哪些设备支持 OpenVINO

pi@raspberrypi:~/Desktop $ source /opt/intel/openvino_2021/bin/setupvars.sh
[setupvars.sh] OpenVINO environment initialized
pi@raspberrypi:~/Desktop $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from openvino.inference_engine import IECore
>>> ie = IECore()
>>> print(ie.available_devices)
[‘MYRIAD’]

注:
MYRIAD为神经计算棒插入树莓派后显示的设备名称。
如果插入的对象为Windows 10电脑则显示:[‘CPU’, ‘GNA’, ‘GPU’, ‘MYRIAD’]

出现问题:

问题1:

pi@raspberrypi:~/Desktop/YOLOX-main $ pip3 install -r requirements.txt
Could not find a version that satisfies the requirement onnxruntime==1.8.0 (from -r requirements.txt (line 17)) (from versions: )
No matching distribution found for onnxruntime==1.8.0 (from -r requirements.txt (line 17))

因为在这里我只使用 OpenViNO 用不到 onnxruntime 这个包,所以忽略此问题。

问题2:

安装的 torch 找不到,无法使用:

pi@raspberrypi:~/Desktop/YOLOX-main $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import torch
Traceback (most recent call last):
File “”, line 1, in
File “/home/pi/.local/lib/python3.7/site-packages/torch/init.py”, line 196, in
from torch._C import *
ImportError: libopenblas.so.0: cannot open shared object file: No such file or directory

安装好pytorch之后import发现没法用,这可能是因为没有安装依赖:

sudo apt-get install libopenblas-dev

参考一个最近和我一样需要在 Raspberry Pi 4B 上部署YOLO dalao的文章,
最后得到了解决:
在树莓派4B上部署自己训练的yolov5模型(配合NCS2加速)
https://blog.csdn.net/qq_44166630/article/details/119994949?spm=1001.2014.3001.5501

问题3:

缺少 openvino 模块

Traceback (most recent call last):
File “./demo/OpenVINO/python/openvino_inference.py”, line 15, in
from openvino.inference_engine import IECore
ModuleNotFoundError: No module named ‘openvino’

Raspberry Pi 4B安装 OpenVINO™ 工具包:
https://blog.csdn.net/qq_44989881/article/details/119792769?spm=1001.2014.3001.5501

或者
设置 openvino 的环境变量

source /opt/intel/openvino_2021/bin/setupvars.sh

pi@raspberrypi:~/Desktop $ source /opt/intel/openvino_2021/bin/setupvars.sh
[setupvars.sh] OpenVINO environment initialized

问题4:

numpy 版本太低,导致无法使用 OpenVINO 运行,如果安装大于 1.16.6 小于 1.20.1版本,也会出现 OpenVINO 版本过低类似的错误。

(yolox) C:\\Users\\vvcat\\Desktop\\YOLOX-0.1.0>pip install numpy==1.15.1
Collecting numpy==1.15.1
Downloading numpy-1.15.1-cp37-none-win_amd64.whl (13.5 MB)
|████████████████████████████████| 13.5 MB 92 kB/s
Installing collected packages: numpy
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
scipy 1.7.1 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.15.1 which is incompatible.
scikit-image 0.18.3 requires numpy>=1.16.5, but you have numpy 1.15.1 which is incompatible.
onnxruntime 1.8.0 requires numpy>=1.16.6, but you have numpy 1.15.1 which is incompatible.
onnx 1.8.1 requires numpy>=1.16.6, but you have numpy 1.15.1 which is incompatible.
matplotlib 3.4.3 requires numpy>=1.16, but you have numpy 1.15.1 which is incompatible.

建议 numpy 安装 1.20.0 版本 以上
本文章使用 1.20.3 版本的 numpy

pip3 install numpy==1.20.3

问题5:

ModuleNotFoundError: No module named ‘loguru’
ModuleNotFoundError: No module named ‘thop’
ModuleNotFoundError: No module named ‘pycocotools’
ModuleNotFoundError: No module named ‘tqdm’

缺少以上模块,建议 一个一个的都安装上去。

pip3 install xxxx

问题6:

安装了 yolox 缺找不到 yolox 这个模块

raceback (most recent call last):
File “./demo/OpenVINO/python/openvino_inference.py”, line 17, in
from yolox.data.data_augment import preproc as preprocess
ModuleNotFoundError: No module named ‘yolox’

通过 pwd 命令查看 YOLOX 项目的根路径

pi@raspberrypi:~/Desktop/YOLOX-main $ pwd
/home/pi/Desktop/YOLOX-main

在报错的py文件下,添加该路径

import sys
sys.path.append(r'/home/pi/Desktop/YOLOX-main')

折腾没有结束,有时间继续更新…

树莓派4B 安装torch:torch1.8.0+torchvision0.9.0
https://blog.csdn.net/qq_44166630/article/details/119647498?spm=1001.2014.3001.5501

用于在 Raspberry Pi 4B 上安装 Pytorch 1.8.0 和 Pytorch Vision 0.9.0 版的文件:https://github.com/sungjuGit/PyTorch-and-Vision-for-Raspberry-Pi-4B

以上是关于Raspberry Pi 4B 部署 YOLOX的主要内容,如果未能解决你的问题,请参考以下文章

树莓派(Raspberry Pi)4B无界面安装Kali系统篇

树莓派(Raspberry Pi)4B无界面安装 Raspberry Pi 系统篇

树莓派(Raspberry Pi)4B无界面安装 Raspberry Pi 系统篇

Raspberry Pi 4B 循迹模块

Raspberry Pi 4B 颜色检测

Raspberry Pi 4B C和Python开发环境安装