Mxnet导出onnx模型

Mxnet导出onnx模型

requirements

  • mxnet==1.9.1
  • python3.8+
  • onnxsim

导出模型

import os
import mxnet as mx
import numpy as np
import onnx
from onnx import checker
from mxnet.onnx import export_model
from mxnet.gluon.model_zoo import vision
from onnxsim import simplify

os.environ['MXNET_GLUON_REPO'] = 'https://apache-mxnet.s3.cn-north-1.amazonaws.com.cn/'
model_name = 'resnet50_v1'
input_shape = (1, 3, 224, 224)
m = vision.get_model(model_name, pretrained=True)
m.hybridize()
m(mx.nd.ones(input_shape))  # 随便传入一个输入以使模型完成初始化
m.export(model_name)
sym = './{}-symbol.json'.format(model_name)
params = './{}-0000.params'.format(model_name)
onnx_file = './mxnet_{}.onnx'.format(model_name)
converted_model_path = export_model(sym, params, [input_shape], np.float32, onnx_file)
model_proto = onnx.load_model(converted_model_path)
checker.check_graph(model_proto.graph)
model = onnx.load(onnx_file)
model_sim, check = simplify(model)
assert check, 'simplify failed'
onnx.save(model_sim, './mxnet_{}_sim.onnx'.format(model_name))

你可能感兴趣的:(模型部署,mxnet,人工智能,深度学习)