省流
python export.py --data coco128.yaml --weights yolov5s.pt --batch 1 --img 640 640 --train --simplify --include onnx --opset 10
python -m onnxsim yolov5s.onnx yolov5s-sim.onnx
详细:
输入命令
python export.py --data coco128.yaml --weights yolov5s.pt --batch 1 --img 640 640 --train --simplify --include onnx --opset 10
其中,--img,--data和 --weights 参数是基于读者自己输入模型尺寸的大小,训练集的yaml文件地址以及生成的 .pt (.pth) 文件地址而定,剩余参数可依据情况而定(6.0版本的export.py文件进行的模型转化尽量都加上,要不容易产生错误,具体原因还没有查清)。
————————————————
版权声明:本文为CSDN博主「丶Endeavor」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44743827/article/details/125779320
报错
(yolov5) F:\PycharmProjects\yolov5-master>python export.py --data coco128.yaml --weights yolov5s.pt --batch 1 --img 640 640 --train --simplify --include onnx --opset 10
requirements: 'pip install onnx' skipped (offline)
解决
(yolov5) F:\PycharmProjects\yolov5-master>pip install onnx
输出
Successfully installed onnx-1.13.1 protobuf-3.20.3
再次运行export.py,仍然报错
(yolov5) F:\PycharmProjects\yolov5-master>python export.py --data coco128.yaml --weights yolov5s.pt --batch 1 --img 640 640 --train --simplify --include onnx --opset 10
F:\Anaconda3\envs\yolov5\lib\site-packages\torch\onnx\utils.py:105: UserWarning: You are exporting the model in training mode with onnx opset version 10. Opset versions lower than opset 12 will not be able to export nodes such as Dropout and BatchNorm correctly.
warnings.warn(
F:\Anaconda3\envs\yolov5\lib\site-packages\torch\onnx\symbolic_helper.py:829: UserWarning: You are trying to export the model with onnx:Resize for ONNX opset version 10. This operator might cause results to not match the expected results by PyTorch.
ONNX's Upsample/Resize operator did not match Pytorch's Interpolation until opset 11. Attributes to determine how to transform the input were added in onnx:Resize in opset 11 to support Pytorch's behavior (like coordinate_transformation_mode and nearest_mode).
We recommend using opset 11 and above for models using this operator.
warnings.warn(
============== Diagnostic Run torch.onnx.export version 2.0.0+cpu ==============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================
requirements: 'pip install onnx-simplifier' skipped (offline)
解决
(yolov5) F:\PycharmProjects\yolov5-master>pip install onnx-simplifier
输出
Successfully installed markdown-it-py-2.2.0 mdurl-0.1.2 onnx-simplifier-0.4.28 rich-13.3.5
第3次运行export.py,成功
过程中又另外下载了其他包
Successfully installed coloredlogs-15.0.1 flatbuffers-23.3.3 humanfriendly-10.0 onnxruntime-1.14.1 pyreadline3-3.4.1
得到后缀为onnx的权重文件
将生成的onnx文件进行进一步的简化
python -m onnxsim yolov5s.onnx yolov5s-sim.onnx
成功
参考:
(1条消息) 海思 YOLOv5 pytorch 转 onnx 转 Caffe 再转 wk 的转化详解_yolov5 caffe_丶Endeavor的博客-CSDN博客