Skip to content

Latest commit

 

History

History
59 lines (52 loc) · 2.39 KB

yolov5_model_cvt.md

File metadata and controls

59 lines (52 loc) · 2.39 KB

YOLOv5模型转换

Yolov5原作者一直在更新版本,每个版本的模型可能会发生变化,目前本仓库支持两个版本。

如果使用v6.0需要配置json: "yolov5_version": "v6"

如果使用v5.0需要配置json: "yolov5_version": "v5"

YOLOv5 v6.0

  1. 根据这个仓库的指导下载配置YOLOv5环境,建议使用virtual env,具体命令
git clone https://github.com/ultralytics/yolov5
cd yolov5
git checkout v6.0
pip install -r requirements.txt
pip install -U coremltools onnx scikit-learn==0.19.2
  1. 生成onnx, 以yolov5s为例,最终得到yolov5s.onnx
python export.py --include onnx --weights yolov5s.pt --img 640 --batch 1 --opset=11

如果报错 AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'(issue 50),可以降级一下 torch 和 torchvision 的版本:

pip install torch==1.9.0
pip install torchvision==0.10.0
  1. 使用atc转换模型 将model/yuv420sp_aipp.cfg拷贝到yolov5文件夹下,使用atc转换模型。
atc --mode=0 --model yolov5s.onnx --framework=5 --output=yolov5s_v6 --soc_version=Ascend310 --insert_op_conf=yuv420sp_aipp.cfg
  1. 将得到的yolov5s_v6.om放到model文件夹备用

YOLOv5 v5.0后的某个版本

  1. 根据这个仓库的指导下载配置YOLOv5环境,建议使用virtual env,具体命令
git clone https://github.com/ultralytics/yolov5
cd yolov5
git checkout e96c74b5a1c4a27934c5d8ad52cde778af248ed8
pip install -r requirements.txt
pip install -U coremltools onnx scikit-learn==0.19.2
  1. 生成onnx, 以yolov5s为例,最终得到yolov5s.onnx
python export.py --include onnx --weights yolov5s.pt --img 640 --batch 1 --opset=11
  1. 修改onnx
    • model/yolov5_remove_nodes.py拷贝到前一步得到的onnx目录下
    • 执行以下命令,将yolov5的focus层的slice和concate算子删除
python yolov5_remove_nodes.py yolov5s.onnx -o yolov5s_truncate.onnx
  1. 使用atc转换模型
atc --mode=0 --model yolov5s_truncate.onnx --framework=5 --output=yolov5s_truncate --soc_version=Ascend310
  1. 将得到的yolov5s_truncate.om放到model文件夹备用