Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
1f3e482b
Unverified
提交
1f3e482b
authored
4月 16, 2021
作者:
Glenn Jocher
提交者:
GitHub
4月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ONNX Simplifier (#2815)
* ONNX Simplifier Add ONNX Simplifier to ONNX export pipeline in export.py. Will auto-install onnx-simplifier if onnx is installed but onnx-simplifier is not. * Update general.py
上级
e5d71223
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
31 行增加
和
16 行删除
+31
-16
export.py
models/export.py
+30
-15
general.py
utils/general.py
+1
-1
没有找到文件。
models/export.py
浏览文件 @
1f3e482b
"""Exports a YOLOv5 *.pt model to ONNX and TorchScript formats
Usage:
$ export PYTHONPATH="$PWD" && python models/export.py --weights
./weights/
yolov5s.pt --img 640 --batch 1
$ export PYTHONPATH="$PWD" && python models/export.py --weights yolov5s.pt --img 640 --batch 1
"""
import
argparse
...
...
@@ -16,7 +16,7 @@ import torch.nn as nn
import
models
from
models.experimental
import
attempt_load
from
utils.activations
import
Hardswish
,
SiLU
from
utils.general
import
set_logging
,
check_img_size
from
utils.general
import
colorstr
,
check_img_size
,
check_requirements
,
set_logging
from
utils.torch_utils
import
select_device
if
__name__
==
'__main__'
:
...
...
@@ -59,20 +59,22 @@ if __name__ == '__main__':
y
=
model
(
img
)
# dry run
# TorchScript export
prefix
=
colorstr
(
'TorchScript:'
)
try
:
print
(
'
\n
Starting TorchScript export with torch
%
s...'
%
torch
.
__version__
)
print
(
f
'
\n
{prefix} starting export with torch {torch.__version__}...'
)
f
=
opt
.
weights
.
replace
(
'.pt'
,
'.torchscript.pt'
)
# filename
ts
=
torch
.
jit
.
trace
(
model
,
img
,
strict
=
False
)
ts
.
save
(
f
)
print
(
'TorchScript export success, saved as
%
s'
%
f
)
print
(
f
'{prefix} export success, saved as {f}'
)
except
Exception
as
e
:
print
(
'TorchScript export failure:
%
s'
%
e
)
print
(
f
'{prefix} export failure: {e}'
)
# ONNX export
prefix
=
colorstr
(
'ONNX:'
)
try
:
import
onnx
print
(
'
\n
Starting ONNX export with onnx
%
s...'
%
onnx
.
__version__
)
print
(
f
'{prefix} starting export with onnx {onnx.__version__}...'
)
f
=
opt
.
weights
.
replace
(
'.pt'
,
'.onnx'
)
# filename
torch
.
onnx
.
export
(
model
,
img
,
f
,
verbose
=
False
,
opset_version
=
12
,
input_names
=
[
'images'
],
output_names
=
[
'classes'
,
'boxes'
]
if
y
is
None
else
[
'output'
],
...
...
@@ -80,25 +82,38 @@ if __name__ == '__main__':
'output'
:
{
0
:
'batch'
,
2
:
'y'
,
3
:
'x'
}}
if
opt
.
dynamic
else
None
)
# Checks
onnx_model
=
onnx
.
load
(
f
)
# load onnx model
onnx
.
checker
.
check_model
(
onnx_model
)
# check onnx model
# print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
print
(
'ONNX export success, saved as
%
s'
%
f
)
model_onnx
=
onnx
.
load
(
f
)
# load onnx model
onnx
.
checker
.
check_model
(
model_onnx
)
# check onnx model
# print(onnx.helper.printable_graph(model_onnx.graph)) # print
# Simplify
try
:
check_requirements
([
'onnx-simplifier'
])
import
onnxsim
print
(
f
'{prefix} simplifying with onnx-simplifier {onnxsim.__version__}...'
)
model_onnx
,
check
=
onnxsim
.
simplify
(
model_onnx
)
assert
check
,
'assert check failed'
onnx
.
save
(
model_onnx
,
f
)
except
Exception
as
e
:
print
(
f
'{prefix} simplifier failure: {e}'
)
print
(
f
'{prefix} export success, saved as {f}'
)
except
Exception
as
e
:
print
(
'ONNX export failure:
%
s'
%
e
)
print
(
f
'{prefix} export failure: {e}'
)
# CoreML export
prefix
=
colorstr
(
'CoreML:'
)
try
:
import
coremltools
as
ct
print
(
'
\n
Starting CoreML export with coremltools
%
s...'
%
ct
.
__version__
)
print
(
f
'{prefix} starting export with coremltools {onnx.__version__}...'
)
# convert model from torchscript and apply pixel scaling as per detect.py
model
=
ct
.
convert
(
ts
,
inputs
=
[
ct
.
ImageType
(
name
=
'image'
,
shape
=
img
.
shape
,
scale
=
1
/
255.0
,
bias
=
[
0
,
0
,
0
])])
f
=
opt
.
weights
.
replace
(
'.pt'
,
'.mlmodel'
)
# filename
model
.
save
(
f
)
print
(
'CoreML export success, saved as
%
s'
%
f
)
print
(
f
'{prefix} export success, saved as {f}'
)
except
Exception
as
e
:
print
(
'CoreML export failure:
%
s'
%
e
)
print
(
f
'{prefix} export failure: {e}'
)
# Finish
print
(
'
\n
Export complete (
%.2
fs). Visualize with https://github.com/lutzroeder/netron.'
%
(
time
.
time
()
-
t
)
)
print
(
f
'
\n
Export complete ({time.time() - t:.2f}s). Visualize with https://github.com/lutzroeder/netron.'
)
utils/general.py
浏览文件 @
1f3e482b
...
...
@@ -111,7 +111,7 @@ def check_requirements(requirements='requirements.txt', exclude=()):
except
Exception
as
e
:
# DistributionNotFound or VersionConflict if requirements not met
n
+=
1
print
(
f
"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-update..."
)
print
(
subprocess
.
check_output
(
f
"pip install
'{e.req}'
"
,
shell
=
True
)
.
decode
())
print
(
subprocess
.
check_output
(
f
"pip install
{e.req}
"
,
shell
=
True
)
.
decode
())
if
n
:
# if packages updated
source
=
file
.
resolve
()
if
'file'
in
locals
()
else
requirements
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论