撰文|FengWen、BBuf
1
模型导出
这个教程用来解释如何导出一个训练好的OneFlow YOLOv5模型到 ONNX。欢迎大家到这里查看本篇文章的完整版本:https://start.oneflow.org/oneflow-yolo-doc/tutorials/06_chapter/export_onnx_tflite_tensorrt.html
2
开始之前
克隆工程并在 Python>3.7.0 的环境中安装 requiresments.txt , OneFlow 请选择 nightly 版本或者 >0.9 版本 。模型和数据可以从源码中自动下载。
git clone https://github.com/Oneflow-Inc/one-yolov5.git
cd one-yolov5
pip install -r requirements.txt # install
3
格式
YOLOv5支持多种模型格式的导出,并基于特定模型对应的框架获得推理加速。
4
导出训练好的 YOLOv5 模型
下面的命令把预训练的 YOLOV5s 模型导出为 ONNX 格式。yolov5s 是小模型,是可用的模型里面第二小的。其它选项是 yolov5n ,yolov5m,yolov5l,yolov5x ,以及他们的 P6 对应项比如 yolov5s6 ,或者你自定义的模型,即 runs/exp/weights/best 。有关可用模型的更多信息,可以参考我们的README
python export.py --weights ../yolov5s/ --include onnx
💡 提示: 添加 --half 以 FP16 半精度导出模型以实现更小的文件大小。
输出:
export: data=data/coco128.yaml, weights=['../yolov5s/'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx']
YOLOv5 🚀 270ac92 Python-3.8.11 oneflow-0.8.1+cu117.git.0c70a3f6be CPUFusing layers...
YOLOv5s summary: 157 layers, 7225885 parameters, 229245 gradientsOneFlow: starting from ../yolov5s with output shape (1, 25200, 85) (112.9 MB)ONNX: starting export with onnx 1.12.0...
Converting model to onnx....
Using opset <onnx, 12>
Optimizing ONNX model
After optimization: Const +17 (73->90), Identity -1 (1->0), Unsqueeze -60 (60->0), output -1 (1->0), variable -60 (127->67)
Succeed converting model, save model to ../yolov5s.onnx
<class 'tuple'>
Comparing result between oneflow and onnx....
Compare succeed!
ONNX: export success, saved as ../yolov5s.onnx (28.0 MB)Export complete (24.02s)
Results saved to /home/zhangxiaoyu
Detect: python detect.py --weights ../yolov5s.onnx
Validate: python val.py --weights ../yolov5s.onnx
OneFlow Hub: model = flow.hub.load('OneFlow-Inc/one-yolov5', 'custom', '../yolov5s.onnx')
Visualize: https://netron.app
导出的 onnx 模型使用 Netron Viewer 进行可视化的结果如下:
5
导出模型的示例用法
detect.py 可以对导出的模型进行推理:
python path/to/detect.py --weights yolov5s/ # OneFlowyolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnnyolov5s.xml # OpenVINOyolov5s.engine # TensorRTyolov5s.mlmodel # CoreML (macOS only)yolov5s_saved_model # TensorFlow SavedModelyolov5s.pb # TensorFlow GraphDefyolov5s.tflite # TensorFlow Liteyolov5s_edgetpu.tflite # TensorFlow Edge TPU
val.py 可以对导出的模型进行验证:
python path/to/val.py --weights yolov5s/ # OneFlowyolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnnyolov5s.xml # OpenVINOyolov5s.engine # TensorRTyolov5s.mlmodel # CoreML (macOS only)yolov5s_saved_model # TensorFlow SavedModelyolov5s.pb # TensorFlow GraphDefyolov5s.tflite # TensorFlow Liteyolov5s_edgetpu.tflite # TensorFlow Edge TPU
6
ONNX Runtime 推理
基于 onnx 模型使用 onnxruntime 进行推理:
python3 detect.py --weights ../yolov5s/yolov5s.onnx
输出:
detect: weights=['../yolov5s/yolov5s.onnx'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 270ac92 Python-3.8.11 oneflow-0.8.1+cu117.git.0c70a3f6be
Loading ../yolov5s/yolov5s.onnx for ONNX Runtime inference...
detect.py:159: DeprecationWarning: In future, it will be an error for 'np.bool_' scalars to be interpreted as an indexs += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
image 1/2 /home/zhangxiaoyu/one-yolov5/data/images/bus.jpg: 640x640 4 persons, 1 bus, Done. (0.009s)
image 2/2 /home/zhangxiaoyu/one-yolov5/data/images/zidane.jpg: 640x640 2 persons, 2 ties, Done. (0.011s)
0.5ms pre-process, 10.4ms inference, 4.8ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp14
参考文章
https://github.com/ultralytics/yolov5/issues/251
其他人都在看
-
Transformer模型的基础演算
-
关于大型语言模型的争论和局限
-
揭秘RLHF;可商用开源LLM列表
-
John Schulman:通往TruthGPT之路
-
为什么ChatGPT用强化学习而非监督学习
-
OneEmbedding:单卡训练TB级推荐模型不是梦
-
GLM训练加速:性能最高提升3倍,显存节省1/3
欢迎Star、试用OneFlow: github.com/Oneflow-Inc/oneflow/http://github.com/Oneflow-Inc/oneflow/