栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python

【YOLOX图像识别】初涉,环境配了几次,次次报错,写个笔记吧

Python 更新时间:发布时间: 百科书网 趣学号

目录
  • Get Started
    • 1.Installation
      • Step1. Install YOLOX.
        • 问题1:ping不通github
        • 问题2:ERROR: pip下载超时
        • 问题3:error: can't create or remove files in install directory(权限不够)
      • Step2. Install pycocotools.
    • 2.Demo
      • Step1. Download a pretrained model from the benchmark table.
      • Step2. Use either -n or -f to specify your detector’s config. For example:
    • 3.YOLOX-CPP-ncnn
      • Step1 Clone ncnn first, then please following build tutorial of ncnn to build on your own device.
        • 问题1 段错误 (核心已转储)
      • Step2 Use provided tools to generate onnx file. For example, if you want to generate onnx file of yolox-s, please run the following command
        • 问题2 An error has been caught in function '', process 'MainProcess' (19995), thread 'MainThread'
      • Step3 Generate ncnn param and bin file.
      • Step4 Open model.param, and modify it.
      • Step5 Use ncnn_optimize to generate new param and bin
      • Step6 Copy or Move yolox.cpp file into ncnn/examples, modify the CMakeList.txt, then build yolox
      • Step7 Inference image with executable file yolox, enjoy the detect result:
        • 问题3:段错误

YOLOX官方文档
YOLOX开源地址

Get Started 1.Installation Step1. Install YOLOX.
git clone git@github.com:Megvii-BaseDetection/YOLOX.git
问题1:ping不通github

查github的ip地址网址:
https://ipaddress.com/website/github.com#ipinfo
命令行输入:vim /etc/hosts
加入一行 140.82.114.3 github.com (我现在查的是这个)

cd YOLOX

pip3 install -U pip && pip3 install -r requirements.txt

问题2:ERROR: pip下载超时

换源(我用的清华源)

阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学 http://pypi.hustunique.com/
山东理工大学 http://pypi.sdutlinux.org/

pip3 install -U pip && pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

pip3 install -v -e . # or python3 setup.py develop

问题3:error: can’t create or remove files in install directory(权限不够)
sudo python3 setup.py develop
Step2. Install pycocotools.
pip3 install cython
pip3 install pycocotools 

官网的这一步的代码,我一直报错,用这个不报错

2.Demo Step1. Download a pretrained model from the benchmark table.


下载一个预训练模型

Step2. Use either -n or -f to specify your detector’s config. For example:
python tools/demo.py image -n yolox-s -c /path/to/your/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device [cpu/gpu]

把/path/to/your/yolox_s.pth更换成你下载模型时存放的地址
我的是python tools/demo.py image -n yolox-s -c weights/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device [cpu/gpu]
可以看见,最后一行的输出就是检测结果保存的位置,到达那个路径下就能看到检测的图片
也可以换的自己照片检测,比如
把这张图片放在assets文件夹里,然后执行
python tools/demo.py image -n yolox-s -c weights/yolox_s.pth --path assets/chicken.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device [cpu/gpu]
就能得到检测后图像

3.YOLOX-CPP-ncnn Step1 Clone ncnn first, then please following build tutorial of ncnn to build on your own device.

ncnn开源地址
ncnn官方教程
参考大佬的做法

git clone https://github.com/Tencent/ncnn.git
cd ncnn
sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libvulkan-dev vulkan-utils libopencv-dev
mkdir -p build
cd build
cmake ..
make
cd ../examples
../build/examples/squeezenet ../images/256-ncnn.png
问题1 段错误 (核心已转储)

可能是数组/堆栈越界之类的,不知道具体原因,也不知道怎么就好使了,我进行了以下操作:

git submodule update --init

继续测试验证

cd ../examples
../build/examples/squeezenet ../images/256-ncnn.png
cd ../benchmark
../build/benchmark/benchncnn 10 $(nproc) 0 0
Step2 Use provided tools to generate onnx file. For example, if you want to generate onnx file of yolox-s, please run the following command

cd < path of yolox>
python3 tools/export_onnx.py -n yolox-s

cd YOLOX

python3 tools/export_onnx.py -n yolox-s

问题2 An error has been caught in function ‘’, process ‘MainProcess’ (19995), thread ‘MainThread’
python3 tools/export_onnx.py -n yolox-s -c weights/yolox_s.pth
~~官方代码后面没有加训练权重路径,加上后就不报错了,正确生成了onnx文件~~ 
Step3 Generate ncnn param and bin file.

cd < path of ncnn>
cd build/tools/ncnn
./onnx2ncnn yolox.onnx model.param model.bin

注意:
1.把刚刚生成的YOLOX路径下的yolox.onnx复制到ncnn/build/tools/onnx路径下
2.看看自己的文件路径,教程是ncnn,我的是onnx

cd ncnn/build/tools/onnx
./onnx2ncnn yolox.onnx yolox.param yolox.bin
Step4 Open model.param, and modify it.

修正yolox.param文件。
在刚刚复制yolox.onnx的路径下,找到并打开yolox.param,进行以下修改:
1.把第二行的第一个数字减去9

(since we will remove 10 layers and add 1 layers, total layers number should minus 9).
因为我们将去除十层并添加一层,所以总层数减去9
235 -> 226

2.删除10层,替换为focus层

remove 10 lines of code from Split to Concat, but remember the last but 2nd number: 683.
删除从 Split 到 Concat 的十行代码,并且记住倒数第二个数字
Add YoloV5Focus layer After Input (using previous number 683)
添加focus层并且用到先前记住的数字
YoloV5Focus focus 1 1 images 502

Step5 Use ncnn_optimize to generate new param and bin
cd ncnn/build/tools/onnx
../ncnnoptimize yolox.param yolox.bin yolox-opt.param yolox-opt.bin 65536
Step6 Copy or Move yolox.cpp file into ncnn/examples, modify the CMakeList.txt, then build yolox

在 YOLOX/demo/ncnn/cpp/yolox.cpp 路径下找到yolox.cpp,复制到 ncnn/examples/
(最新的nccn已经自带yolox.cpp文件,不需要复制了> ^ <)

cd ncnn/examples/
vim yolox.cpp

第264和265行,修改模型路径
yolox-opt.param和yolox-opt.bin

重新编译nccn

# 删除之前的build目录,重建
# 注意保存在/ncnn/build/tools/onnx路径下的`yolox-opt.param`和`yolox-opt.bin`文件,随便存在哪儿,等会还要用到
rm -rf build
mkdir build
cd build
cmake ..
make
Step7 Inference image with executable file yolox, enjoy the detect result:

./yolox demo.jpg

cd ./examples
# 在这个路径下会有一个可执行yolox文件
# 放一个demo图片,记住路径
# 复制模型在正确路径(把刚刚保存的`yolox-opt.param`和`yolox-opt.bin`文件放在这个路径下)
./yolox ../assets/dog.jpg
问题3:段错误

如果路径找不到,会报错:

fopen yolox-opt.param failed
fopen yolox-opt.bin failed
find_blob_index_by_name images failed
Try
find_blob_index_by_name output failed
Try
段错误 (核心已转储)

把之前生成的opt两个文件复制到当前路径下,还是报错

find_blob_index_by_name input failed
段错误 (核心已转储)

准备阅读yolox.cpp的源码,今日工作告一段落 2022.06.22

2022.06.23找到错误原因了!!!
问题出在step4,因为我的param文件里concat层没有单纯的数字,因此focus层最后一个数字是错的,把应该出现数字的那一行写成input就可以了!多亏了ncnn群里的大佬!(致谢致谢)这个群
如图修改后,回到step5,就不会报段错误啦!成功跑通demo!
(没读源码…)
使用netron{
访问netron.app
netron开源地址:https://github.com/lutzroeder/netron#install

安装成功后,直接在命令行输入netron,然后稍等片刻,就会弹出这个软件啦
}
查看自己修改后的param文件,能看出有没有改对
从图上容易看出,output参数改为input的时候,才能连在一起,虽然不知道为啥,但是想必连在一起才是对的吧。至此,困扰我几个小时的难题终于解决啦!
其实可以用netron看看最初的param文件,得出concat的output参数就是input!以后碰到类似问题也可以这么查一下。

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/987156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号