YOLOv5

  • YOLOv5下载与测试运行
  • 导出ONNX格式文件
  • ONNX转为为IR中间格式

环境:

  • Windows 10
  • Anaconda 2.0.4
  • OpenVINO 工具包 2021.2
  • Python 3.6.13
  • torch 1.9.0
  • onnx 1.10.1
  • YOLOv5

YOLOv5下载与测试运行

YOLOv5是第二个非官方的YOLO对象检测版本,也是第一个Pytorch实现的YOLO对象检测版本。Github地址:https://github.com/ultralytics/yolov5

克隆到本地

git clone https://github.com/ultralytics/yolov5.git

安装YOLOv5所有依赖

pip install -r requirements.txt

导出ONNX格式文件

  • OpenVINO 工具包 2021.2 可以直接读取ONNX格式文件,所以我们既可以通过脚本直接导出onnx格式文件,直接给OpenVINO调用,也可以对得到ONNX文件通过OpenVINO的模型转换生成IR中间格式(.bin文件与.xml文件)。

Pytorch的YOLOv5项目本身已经提供了转换脚本,命令行运行方式如下:

(python37) C:\Program Files (x86)\Intel\openvino_2021.2.185\bin> setupvars.bat
Python 3.7.10

[setupvars.bat] OpenVINO environment initialized

cd 到YOLOv5项目所在目录下

使用 YOLOv5 提供的 export.py 将 yolov5s.pt 转换为 ONNX。

python models/export.py --weights yolov5s.pt --img 640 --batch 1

(python37) M:\python\OpenCV\yolov5\yolov5-master>python models/export.py --weights yolov5s.pt --img 640 --batch 1
Namespace(batch_size=1, device=‘cpu’, dynamic=False, half=False, img_size=[640, 640], include=[‘torchscript’, ‘onnx’, ‘coreml’], inplace=False, opset_version=12, optimize=False, simplify=False, train=False, weights=‘yolov5s.pt’)
YOLOv5 2021-5-18 torch 1.8.1+cpu CPU
Fusing layers…
Model Summary: 224 layers, 7266973 parameters, 0 gradients
PyTorch: starting from yolov5s.pt (14.8 MB)
TorchScript: starting export with torch 1.8.1+cpu…
M:\python\OpenCV\yolov5\yolov5-master\models\yolo.py:51: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can’t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic:
TorchScript: export success, saved as yolov5s.torchscript.pt (29.4 MB)
ONNX: starting export with onnx 1.10.1…
ONNX: export success, saved as yolov5s.onnx (29.2 MB)
CoreML: starting export with coremltools 4.1…
Tuple detected at graph output. This will be flattened in the converted model.
Converting graph.
Adding op ‘1’ of type const
Adding op ‘2’ of type const
Adding op ‘3’ of type const



Adding op ‘729’ of type add
Converting op 730 : select
Converting Frontend ==> MIL Ops: 87%|██████████████████████████████████████▏ | 604/695 [00:01<00:00, 557.66 ops/s]
CoreML: export failure:
Export complete (10.18s). Visualize with https://github.com/lutzroeder/netron.


生成的yolov5s.onnx文件在YOLOv5目录下。

ONNX转为为IR中间格式

Windows 10 下 torch模型转换为 OpenVINO需要的IR文件:https://blog.csdn.net/qq_44989881/article/details/119488209

使用 OpenVINO 工具包提供的 mo_onnx.py文件,对模型进行转换。

管理员模式打开Anaconda,启动虚拟环境的终端,cd进openVINO转换工具目录,执行转换代码:

cd C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer
python mo_onnx.py --input_model M:\python\OpenCV\yolov5\yolov5-master\yolov5s.onnx

(pytorch) C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer>python mo_onnx.py --input_model M:\python\OpenCV\yolov5\yolov5-master\yolov5s.onnx
Model Optimizer arguments:
Common parameters:
- Path to the Input Model: M:\python\OpenCV\yolov5\yolov5-master\yolov5s.onnx
- Path for generated IR: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer.
- IR output name: yolov5s
- Log level: ERROR
- Batch: Not specified, inherited from the model
- Input layers: Not specified, inherited from the model
- Output layers: Not specified, inherited from the model
- Input shapes: Not specified, inherited from the model
- Mean values: Not specified
- Scale values: Not specified
- Scale factor: Not specified
- Precision of IR: FP32
- Enable fusing: True
- Enable grouped convolutions fusing: True
- Move mean values to preprocess section: None
- Reverse input channels: False
ONNX specific parameters:
Model Optimizer version: 2021.2.0-1877-176bdf51370-releases/2021/2
[ SUCCESS ] Generated IR version 10 model.
[ SUCCESS ] XML file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer.\yolov5s.xml
[ SUCCESS ] BIN file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer.\yolov5s.bin
[ SUCCESS ] Total execution time: 26.05 seconds.
It’s been a while, check for a new version of Intel® Distribution of OpenVINO™ toolkit here https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/choose-download.html?cid=other&source=Prod&campid=ww_2021_bu_IOTG&content=upg_pro&medium=organic_uid_agjj or on the GitHub*

转换成功后,在转换工具 model_optimizer 目录下生成了bin和xml文件,然后就可以用 OpenVINO部署了。

.xml - 描述网络拓扑
.bin - 包含权重和偏差二进制数据。

遇到的问题:
缺少 onnx 库 和 coremltools库

(Python37) M:\python\OpenCV\yolov5\yolov5-master>python models/export.py --weights yolov5s.pt --img 640 --batch 1
Namespace(batch_size=1, device=‘cpu’, dynamic=False, half=False, img_size=[640, 640], include=[‘torchscript’, ‘onnx’, ‘coreml’], inplace=False, opset_version=12, optimize=False, simplify=False, train=False, weights=‘yolov5s.pt’)
YOLOv5 2021-5-18 torch 1.8.1+cpu CPU
Fusing layers…
Model Summary: 224 layers, 7266973 parameters, 0 gradients
PyTorch: starting from yolov5s.pt (14.8 MB)
TorchScript: starting export with torch 1.8.1+cpu…
M:\python\OpenCV\yolov5\yolov5-master\models\yolo.py:51: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can’t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic:
TorchScript: export success, saved as yolov5s.torchscript.pt (29.4 MB)
ONNX: export failure: No module named ‘onnx’
CoreML: export failure: No module named ‘coremltools’
Export complete (5.22s). Visualize with https://github.com/lutzroeder/netron.

安装:onnx

pip install onnx

(python37) C:\Program Files (x86)\Intel\openvino_2021.2.185\bin>pip install onnx
Collecting onnx
Downloading onnx-1.10.1-cp37-cp37m-win_amd64.whl (11.4 MB)
|████████████████████████████████| 11.4 MB 1.1 MB/s
Requirement already satisfied: typing-extensions>=3.6.2.1 in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from onnx) (3.7.4.3)
Requirement already satisfied: six in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from onnx) (1.16.0)
Requirement already satisfied: numpy>=1.16.6 in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from onnx) (1.20.2)
Requirement already satisfied: protobuf in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from onnx) (3.17.0)
Installing collected packages: onnx
Successfully installed onnx-1.10.1

安装:coremltools

pip install coremltools

(python37) C:\Program Files (x86)\Intel\openvino_2021.2.185\bin>pip install coremltools
Collecting coremltools
Downloading coremltools-4.1.tar.gz (783 kB)
|████████████████████████████████| 783 kB 726 kB/s
Collecting numpy<1.20,>=1.14.5
Downloading numpy-1.19.5-cp37-cp37m-win_amd64.whl (13.2 MB)
|████████████████████████████████| 13.2 MB 1.3 MB/s
Requirement already satisfied: protobuf>=3.1.0 in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from coremltools) (3.17.0)
Requirement already satisfied: six>=1.10.0 in



installed. This behaviour is the source of the following dependency conflicts.
imgaug 0.4.0 requires opencv-python-headless, which is not installed.
labelme 4.5.7 requires matplotlib<3.3, but you have matplotlib 3.4.2 which is incompatible.
Successfully installed attr-0.3.1 coremltools-4.1 mpmath-1.2.1 numpy-1.19.5 sympy-1.8

YOLOv5的pytorch模型文件转换为ONNX文件相关推荐

  1. 使用onnx包将pth文件转换为onnx文件

    本文对比一下两种pth文件转为onnx的区别以及onnx文件在NETRON中的图 只有参数的pth文件:cat_dog.pth 既有参数又有模型结构的pth文件:cat_dog_model_args. ...

  2. 【yolov5】pytorch模型导出为onnx模型

    博主想拿官网的yolov5训练好pt模型,然后转换成rknn模型,然后在瑞芯微开发板上调用模型检测.但是官网的版本对npu不友好,所以采用改进结构的版本: 将Focus层改成Conv层 将Swish激 ...

  3. pytorch模型(.pt)转onnx模型(.onnx)的方法详解(1)

    1. pytorch模型转换到onnx模型 2.运行onnx模型 3.比对onnx模型和pytorch模型的输出结果 我这里重点是第一点和第二点,第三部分  比较容易 首先你要安装 依赖库:onnx ...

  4. Python Qt GUI设计:将UI文件转换为Python文件的三种妙招(基础篇—2)

    目录 1.创建项目 2.将.ui文件生成.py文件 2.1.Eric 6编译 2.2.Python命令行编译 2.3.脚本编译 3.界面.逻辑分离思想 在开始本文之前提醒各位朋友,Python记得安装 ...

  5. php生成图片文件流,php如何将base64数据流文件转换为图片文件?

    2017-03-07 在开发中,自己遇到一个前端在上传图片的时候,使用的base64数据流文件显示的图片. 也就是说 ***image/后面的jpg是我们的图片文件格式,(base64,)后面的很大一 ...

  6. python中gettext文件格式_如何将gettext .mo文件转换为.po文件 - python

    Improve this question 当.po文件不再可用时,有什么方法可以将.mo文件转换为.po文件源?我需要编辑.mo文件的内容,但没有.po文件.可能吗? 参考方案 msgunfmt [ ...

  7. python批量pdf转word,python批量实现Word文件转换为PDF文件

    本文为大家分享了python批量转换Word文件为PDF文件的具体方法,供大家参考,具体内容如下 1.目的 通过万能的Python把一个目录下的所有Word文件转换为PDF文件. 2.遍历目录 作者总 ...

  8. python npy文件_python实现npy格式文件转换为txt文件操作

    如下代码会将npy的格式数据读出,并且输出来到控制台: import numpy as np ##设置全部数据,不输出省略号 import sys np.set_printoptions(thresh ...

  9. GDCM:将PAPYRUS 3.0文件转换为dcm文件的的测试程序

    GDCM:将PAPYRUS 3.0文件转换为dcm文件的测试 GDCM:将PAPYRUS 3.0文件转换为dcm文件的测试 GDCM:将PAPYRUS 3.0文件转换为dcm文件的测试 #includ ...

最新文章

  1. golang range 遍历 索引和值
  2. Dubbo之——将Dubbo服务打包成Jar包
  3. 这35个Java代码优化细节,你用了吗?
  4. ES6中object对象属性
  5. 《2018区块链整体架构及应用》(PPT全文)
  6. Eevnt Loop (事件循环)
  7. php mkdir 无效,PHP mkdir()无写权限的问题解决方法
  8. Python快速教程 尾声
  9. [转]麻省理工学院(MIT)研究生学习指导[上]
  10. Unity3D的Android移动之路之平台依赖编译
  11. 在Gutsy安装Freewins插件手记
  12. python alphago_资源 | 如何通过 Python 打造一款简易版 AlphaGo?
  13. linux 与 windows操作系统的区别
  14. 服务器背板fw信息,无纸化会议应用系统服务器 HG-FW02Z
  15. 【Android 教程系列第 31 篇】通过 adb install 命令安装 apk 时提示 signatures do not match previously installed version
  16. Debian 支持蓝牙音响
  17. 百度大脑EasyDL多人标注重磅上线啦
  18. 加班两年只赚2千块:低姿态的人,挣不了大钱
  19. PTA作业记录2(计算油费)
  20. Google浏览器限制网速调试加载速度

热门文章

  1. 当页面有多个js文件时,应如何引入?
  2. Execute Process Task
  3. IE6中PNG图片背景无法透明显示的最佳解决方案
  4. SQL server的with的用法(一)
  5. 美军称五角大楼遭网络袭击 2.4万份敏感文件被盗
  6. mysql的如何输入dateadd_mysql中date_add()函数的使用?
  7. 认证连接_长江连接器哪些产品通过认证?
  8. Python 列表(List)操作方法详解
  9. 引导界面(三)仿微信引导界面以及动画效果
  10. 后台开发必读书籍--计算机操作系统