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


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

# Download pre-trained resnet model - json and params by running following code.

# 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
  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
  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,

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?

1 Like

Its been sometime, any update on this?

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.

Python2.x and Python3.x

Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .

With Python2.x , int(str(3/2)) gives you “1”. With Python3.x , the same gives you (“1.5”): ValueError: invalid literal for int() with base 10: “1.5”.