Example of "export to ONNX" does not work [solved]

Hi,

I’d like to export my own trained model (resnet-50) to ONNX. To learn about how to export, I ran the example from this page:

import mxnet as mx
import numpy as np
from mxnet.contrib import onnx as onnx_mxnet
import logging
logging.basicConfig(level=logging.INFO)

# Download pre-trained resnet model - json and params by running following code.
path='http://data.mxnet.io/models/imagenet/'
[mx.test_utils.download(path+'resnet/18-layers/resnet-18-0000.params'),
 mx.test_utils.download(path+'resnet/18-layers/resnet-18-symbol.json'),
 mx.test_utils.download(path+'synset.txt')]

# Downloaded input symbol and params files
sym = './resnet-18-symbol.json'
params = './resnet-18-0000.params'

# Standard Imagenet input - 3 channels, 224*224
input_shape = (1,3,224,224)

# Path of the output file
onnx_file = './mxnet_exported_resnet18.onnx'

converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)

Unfortunately, it gives the following error:

INFO:root:Converting json and weight file to sym and params
[14:08:24] src/nnvm/legacy_json_util.cc:209: Loading symbol saved by previous version v0.8.0. Attempting to upgrade...
[14:08:24] src/nnvm/legacy_json_util.cc:217: Symbol successfully upgraded!
Traceback (most recent call last):
  File "<input>", line 9, in <module>
  File "/Users/blake/.local/share/virtualenvs/convert-mxnet2onnx-x6c6rHhr/lib/python3.6/site-packages/mxnet/contrib/onnx/mx2onnx/export_model.py", line 83, in export_model
    verbose=verbose)
  File "/Users/blake/.local/share/virtualenvs/convert-mxnet2onnx-x6c6rHhr/lib/python3.6/site-packages/mxnet/contrib/onnx/mx2onnx/export_onnx.py", line 309, in create_onnx_graph_proto
    checker.check_graph(graph)
  File "/Users/blake/.local/share/virtualenvs/convert-mxnet2onnx-x6c6rHhr/lib/python3.6/site-packages/onnx/checker.py", line 52, in checker
    proto.SerializeToString(), ctx)
onnx.onnx_cpp2py_export.checker.ValidationError: Unrecognized attribute: spatial for operator BatchNormalization
==> Context: Bad node spec: input: "data" input: "bn_data_gamma" input: "bn_data_beta" input: "bn_data_moving_mean" input: "bn_data_moving_var" output: "bn_data" name: "bn_data" op_type: "BatchNormalization" attribute { name: "epsilon" f: 2e-05 type: FLOAT } attribute { name: "momentum" f: 0.9 type: FLOAT } attribute { name: "spatial" i: 0 type: INT }

I’m running with:

  • mxnet 1.4.1
  • onnx 1.5.0
  • protobuf 3.8.0

What can I do to fix it? Many thanks in advance,
Blake

I found the solution. This page tells that only onnx version 1.2.1 is supported. After downgrading onnx from 1.5.0 to 1.2.1, all worked fine.

1 Like

Usefull it is! But here another problem i met. Thx in advance.

I wanna export gluoncv.model_zoo.yolo3_darknet53_custom into ONNX.
I had export the network as .json and .params before.

When convert to ONNX,get error this:
ValueError: invalid literal for int() with base 10: ‘None’
and logs:
INFO:root:Converting idx: 494, op: slice_axis, name: yolov31_yolooutputv30_slice_axis3

with debug,i find out why: “end”'s value is “None” in mxnet_graph’s slice layer,that ONNX is not support

how can i convert the network to ONNX?