mindyolo在ckpt模型转为ONNX模型时报错

1 系统环境

硬件环境(Ascend/GPU/CPU): CPU
MindSpore版本: mindspore=2.3.1 mindyolo=master
执行模式(PyNative/ Graph):PyNative/ Graph
Python版本: Python=3.9
操作系统平台: linux

2 报错信息

python ./deploy/export.py --config ./configs/yolov8/yolov8n.yaml --weight yolov8-n_500e_mAP372-cc07f5bd.ckpt --per_batch_size 1 --file_format ONNX --device_target CPU

yolo v8n 转换报错

2024-09-27 15:47:31,563 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:47:32,299 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:47:37,168 [INFO] number of network params, total: 3.167795M, trainable: 3.157184M  
[WARNING] ME(200291:139697284708160,MainProcess):2024-09-27-15:47:37.530.337 [mindspore/train/serialization.py:1658] For 'load_param_into_net', remove parameter prefix name: ema., continue to load.  
2024-09-27 15:47:37,541 [INFO] Load checkpoint from [yolov8-n_500e_mAP372-cc07f5bd.ckpt] success.  
Traceback (most recent call last):  
  File "/home/py/mindyolo/./deploy/export.py", line 84, in <module>  
    export_weight(args)  
  File "/home/py/mindyolo/./deploy/export.py", line 76, in export_weight  
    export(network, input_arr, file_name=file_name, file_format=args.file_format)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1895, in export  
    _export(net, file_name, file_format, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1951, in _export  
    _save_onnx(net, file_name, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 2008, in _save_onnx  
    onnx_stream = _executor._get_func_graph_proto(net, graph_id)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/common/api.py", line 1958, in _get_func_graph_proto  
    return self._graph_executor.get_func_graph_proto(exec_id, ir_type, incremental)  
RuntimeError: i: 5 out of range: 5, cnode: @2118_2117_1_mindyolo_models_yolov8_YOLOv8_construct_37:CNode_38{[0]: ValueNode<Primitive> PrimFunc_StridedSlice, [1]: CNode_39, [2]: ValueNode<ValueTuple> (0, 0, 0), [3]: ValueNode<ValueTuple> (0, 64, 0), [4]: ValueNode<ValueTuple> (1, 1, 1)}  
  
----------------------------------------------------  
- Framework Unexpected Exception Raised:  
----------------------------------------------------  
This exception is caused by framework's unexpected error. Please create an issue at https://gitee.com/mindspore/mindspore/issues to get help.  
  
----------------------------------------------------  
- C++ Call Stack: (For framework developers)  
----------------------------------------------------  
mindspore/core/ir/anf.cc:422 input

3 解决方案

新版本cpu支持有问题 2.3.1 硬件平台 支持上只写了Ascend,需要用mindspore 2.2.14版本,安装mindspore 2.2.14版本后执行会报RuntimeError: Can not find key SiLU in convert map. Exporting SiLU operator is not yet supported.

这个在文档中有说明,

2024-09-27 15:21:36,532 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:21:37,216 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:21:41,764 [INFO] number of network params, total: 3.167795M, trainable: 3.157184M  
[WARNING] ME(188841:140207829800768,MainProcess):2024-09-27-15:21:42.907.44 [mindspore/train/serialization.py:1469] For 'load_param_into_net', remove parameter prefix name: ema., continue to load.  
2024-09-27 15:21:42,099 [INFO] Load checkpoint from [yolov8-n_500e_mAP372-cc07f5bd.ckpt] success.  
Traceback (most recent call last):  
  File "/home/py/mindyolo/./deploy/export.py", line 84, in <module>  
    export_weight(args)  
  File "/home/py/mindyolo/./deploy/export.py", line 76, in export_weight  
    export(network, input_arr, file_name=file_name, file_format=args.file_format)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1664, in export  
    _export(net, file_name, file_format, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1718, in _export  
    _save_onnx(net, file_name, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1775, in _save_onnx  
    onnx_stream = _executor._get_func_graph_proto(net, graph_id)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/common/api.py", line 1667, in _get_func_graph_proto  
    return self._graph_executor.get_func_graph_proto(exec_id, ir_type, incremental)  
RuntimeError: Can not find key SiLU in convert map. Exporting SiLU operator is not yet supported.  
  
----------------------------------------------------  
- C++ Call Stack: (For framework developers)  
----------------------------------------------------  
mindspore/ccsrc/transform/express_ir/onnx_exporter.cc:4115 ExportPrimitive

修改方法也简单,修改mindyolo/models/layers/conv.py
把self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Cell) else Identity)
替换成self.act = Swish()
导入增加 from .activation import Swish
记得有两处self.act需要改动。

修改后继续导出

python ./deploy/export.py --config ./configs/yolov8/yolov8n.yaml --weight yolov8-n_500e_mAP372-cc07f5bd.ckpt --per_batch_size 1 --file_format ONNX --device_target CPU  
2024-09-27 15:22:37,156 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:22:37,976 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 15:22:43,617 [INFO] number of network params, total: 3.167795M, trainable: 3.157184M  
[WARNING] ME(189459:139720360478528,MainProcess):2024-09-27-15:22:43.968.741 [mindspore/train/serialization.py:1469] For 'load_param_into_net', remove parameter prefix name: ema., continue to load.  
2024-09-27 15:22:43,978 [INFO] Load checkpoint from [yolov8-n_500e_mAP372-cc07f5bd.ckpt] success.  
Traceback (most recent call last):  
  File "/home/py/mindyolo/./deploy/export.py", line 84, in <module>  
    export_weight(args)  
  File "/home/py/mindyolo/./deploy/export.py", line 76, in export_weight  
    export(network, input_arr, file_name=file_name, file_format=args.file_format)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1664, in export  
    _export(net, file_name, file_format, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1718, in _export  
    _save_onnx(net, file_name, *inputs, **kwargs)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/train/serialization.py", line 1775, in _save_onnx  
    onnx_stream = _executor._get_func_graph_proto(net, graph_id)  
  File "/root/miniconda3/envs/py3.9/lib/python3.9/site-packages/mindspore/common/api.py", line 1667, in _get_func_graph_proto  
    return self._graph_executor.get_func_graph_proto(exec_id, ir_type, incremental)  
RuntimeError: Can not find key FillV2 in convert map. Exporting FillV2 operator is not yet supported.  
  
----------------------------------------------------  
- C++ Call Stack: (For framework developers)  
----------------------------------------------------  
mindspore/ccsrc/transform/express_ir/onnx_exporter.cc:4115 ExportPrimitive  

上面报错就是算子支持了。看来yolov8n不支持导出onnx。

经测试yolo5n可以正常导出。导出命令和结果如下。

python3 ./deploy/export.py --config ./configs/yolov5/yolov5n.yaml --weight yolov5n_300e_mAP273-9b16bd7b.ckpt --per_batch_size 1 --file_format ONNX --device_target CPU  
2024-09-27 16:12:46,281 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 16:12:47,242 [WARNING] Parse Model, args: nearest, keep str type  
2024-09-27 16:12:50,199 [INFO] number of network params, total: 1.8817M, trainable: 1.872157M  
[WARNING] ME(210591:139772762715968,MainProcess):2024-09-27-16:12:50.520.379 [mindspore/train/serialization.py:1469] For 'load_param_into_net', remove parameter prefix name: ema., continue to load.  
[WARNING] ME(210591:139772762715968,MainProcess):2024-09-27-16:12:50.529.926 [mindspore/train/serialization.py:1378] For 'load_param_into_net', 1 parameters in the 'net' are not loaded, because they are not in the 'parameter_dict', please check whether the network structure is consistent when training and loading checkpoint.  
[WARNING] ME(210591:139772762715968,MainProcess):2024-09-27-16:12:50.530.095 [mindspore/train/serialization.py:1383] model.model.24.stride is not loaded.  
2024-09-27 16:12:50,530 [INFO] Load checkpoint from [yolov5n_300e_mAP273-9b16bd7b.ckpt] success.  
2024-09-27 16:12:53,600 [INFO] Export completed.