• Skip to main content
  • Skip to primary sidebar

Anastasios Chondrogiannis

Software developer with a focus on iOS

Convert an MXnet model to ONNX format

October 25, 2019 by Anastasios Chondrogiannis Leave a Comment

1. Setup the Anaconda environment

  1. Download, install and run Anaconda.
  2. Select the Environments tab on the left.
  3. Click the Update index… button and wait for the update to finish.
  4. Click the Create button.
  5. Create a new environment with the following settings:
    • Name: mxnet2onnx
    • Leave Python checked and select the 3.7 version.
    • Leave R unchecked.

2. Install the dependencies

  1. Open a new Terminal.
  2. Type the following command to activate the new environment:
conda activate mxnet2onnx
  1. Type the following command and press y when prompted:
conda install -c conda-forge protobuf numpy
  1. Type the following commands to install the version 1.2.1 of ONNX:
export ONNX_ML=1
pip install onnx==1.2.1
  1. Finally, type the following command to install the latest stable version of MXnet:
pip install mxnet

3. Convert the model

  1. Return to Anaconda and select the Home tab from the left.
  2. Make sure the mxnet2onnx environment is selected.
  3. Locate the jupyter notebook application and click the Install button.
  4. Wait for the installation to finish and the click the Launch button.
  5. Navigate to your Documents folder.
  6. Select New -> Python3 to create a new notebook.
  7. Type the following code in the first cell and the click Shift+Enter to execute it (and create a new cell at the same time). This will download a pre-trained MXnet model.
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')]
  1. Type the following code in a new cell and then press Shift+Enter:
import mxnet as mx
import numpy as np
from mxnet.contrib import onnx as onnx_mxnet
import logging
logging.basicConfig(level=logging.INFO)

# 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_resnet50.onnx'

# Invoke export model API. It returns path of the converted onnx model
converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)

4. Check the new ONNX model

Type the following code in a new cell and then press Shift+Enter:

from onnx import checker
import onnx

# Load onnx model
model_proto = onnx.load_model(converted_model_path)

# Check if converted ONNX protobuf is valid
checker.check_graph(model_proto.graph)

If you see no errors then you are good to go!

5. Common Errors

No conversion function registered for op type SoftmaxActivation yet.

SoftmaxActivation is deprecated:

https://mxnet.incubator.apache.org/api/python/docs/api/symbol/op/index.html?highlight=softmaxactivation#mxnet.symbol.op.SoftmaxActivation

Use softmax instead:

https://mxnet.incubator.apache.org/api/python/docs/api/symbol/op/index.html#mxnet.symbol.op.softmax

You must also use axis=1:

https://github.com/onnx/onnx/issues/2000

References

  • https://github.com/onnx/onnx
  • https://github.com/onnx/tutorials
  • https://mxnet.apache.org/api/python/docs/tutorials/deploy/export/onnx.html
  • https://medium.com/@himanshuagarwal1395/setting-up-environment-variables-in-macos-sierra-f5978369b255

Filed Under: Machine Learning

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Let’s connect!

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Posts

  • Android OpenGL ES – Part 10: Specular lighting
  • Android OpenGL ES – Part 9: Diffuse lighting
  • Android OpenGL ES – Part 8: Ambient Lighting
  • Android OpenGL ES – Part 7: Textures
  • Android OpenGL ES – Part 6: Camera View

Archives

  • March 2020
  • February 2020
  • November 2019
  • October 2019
  • July 2019

Categories

  • Android Development
  • Machine Learning

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in