参考链接:
简化onnx模型
先在环境里安装
pip install onnx-simplifier
然后简化的代码是
import onnx
from onnxsim import simplify
ONNX_MODEL_PATH = './model.onnx'#需要被简化的onnx
ONNX_SIM_MODEL_PATH = './model_simple.onnx'#简化后的onnx保存
if __name__ == "__main__":
onnx_model = onnx.load(ONNX_MODEL_PATH)
onnx_sim_model, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.save(onnx_sim_model, ONNX_SIM_MODEL_PATH)
print('ONNX file simplified!')
就生成了