1 使用onnx2ncnn转换onnx模型到ncnn格式出错

使用NCNN中的onnx2ncnn工具转换onnx模型到ncnn格式出现错误,使用的转换命令如下

./onnx2ncnn.exe ./mobile.onnx ./mobile.param ./mobile.bin

出现了以下错误:

Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0
Shape not supported yet!
Unknown data type 0
Unknown data type 0
Unknown data type 0

这是因为onnx模型中存在不支持的胶水op,需要在导出onnx模型之后,使用onnx-simplifier库对onnx进行简化

首先安装onnx-simplifier

pip install onnx-simplifier

然后使用以下代码对onnx模型进行简化,消除一些不必要的胶水op

import onnx
from onnxsim import simplify
onnx_model = onnx.load(output_path)  # load onnx model
model_simp, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.save(model_simp, output_path)

然后用简化后的onnx模型重新使用onnx2ncnn工具进行转换即可成功。