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))
文章来源地址https://www.toymoban.com/news/detail-815619.html
文章来源:https://www.toymoban.com/news/detail-815619.html
到了这里,关于Mxnet导出onnx模型的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!