ONNX-MXNet API

Overview

ONNX is an open format to represent deep learning models. With ONNX as an intermediate representation, it is easier to move models between state-of-the-art tools and frameworks for training and inference.

The mxnet.contrib.onnx package refers to the APIs and interfaces that implement ONNX model format support for Apache MXNet.

With ONNX format support for MXNet, developers can build and train models with a variety of deep learning frameworks, and import these models into MXNet to run them for inference and training using MXNet’s highly optimized engine.

Warning

This package contains experimental APIs and may change in the near future.

Installation Instructions

  • To use this module developers need to install ONNX, which requires the protobuf compiler to be installed separately. Please follow the instructions to install ONNX and its dependencies. MXNet currently supports ONNX v1.2.1. Once installed, you can go through the tutorials on how to use this module.

This document describes all the ONNX-MXNet APIs.

mxnet.contrib.onnx.onnx2mx.import_model Functions for importing ONNX models to MXNet and for checking metadata
mxnet.contrib.onnx.onnx2mx.import_to_gluon Import ONNX model to gluon interface
mxnet.contrib.onnx.mx2onnx.export_model Exports an MXNet model to the ONNX model format

ONNX Examples

API Reference

Functions for importing ONNX models to MXNet and for checking metadata

mxnet.contrib.onnx.onnx2mx.import_model.import_model(model_file)[source]

Imports the ONNX model file, passed as a parameter, into MXNet symbol and parameters. Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration

Parameters:model_file (str) – ONNX model file name
Returns:
  • sym (Symbol) – MXNet symbol object
  • arg_params (dict of str to NDArray) – Dict of converted parameters stored in mxnet.ndarray.NDArray format
  • aux_params (dict of str to NDArray) – Dict of converted parameters stored in mxnet.ndarray.NDArray format

Notes

This method is available when you import mxnet.contrib.onnx

mxnet.contrib.onnx.onnx2mx.import_model.get_model_metadata(model_file)[source]

Returns the name and shape information of input and output tensors of the given ONNX model file.

Notes

This method is available when you import mxnet.contrib.onnx

Parameters:model_file (str) – ONNX model file name
Returns:model_metadata – A dictionary object mapping various metadata to its corresponding value. The dictionary will have the following template:
'input_tensor_data' : list of tuples representing the shape of the input paramters
'output_tensor_data' : list of tuples representing the shape of the output of the model
Return type:dict

Import ONNX model to gluon interface

mxnet.contrib.onnx.onnx2mx.import_to_gluon.import_to_gluon(model_file, ctx)[source]

Imports the ONNX model files, passed as a parameter, into Gluon SymbolBlock object.

Parameters:
  • model_file (str) – ONNX model file name
  • ctx (Context or list of Context) – Loads the model into one or many context(s).
Returns:

sym_block – A SymbolBlock object representing the given model file.

Return type:

SymbolBlock

Notes

This method is available when you import mxnet.contrib.onnx

Exports an MXNet model to the ONNX model format

mxnet.contrib.onnx.mx2onnx.export_model.export_model(sym, params, input_shape, input_type=, onnx_file_path=u'model.onnx', verbose=False)[source]

Exports the MXNet model file, passed as a parameter, into ONNX model. Accepts both symbol,parameter objects as well as json and params filepaths as input. Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration

Parameters:
  • sym (str or symbol object) – Path to the json file or Symbol object
  • params (str or symbol object) – Path to the params file or params dictionary. (Including both arg_params and aux_params)
  • input_shape (List of tuple) – Input shape of the model e.g [(1,3,224,224)]
  • input_type (data type) – Input data type e.g. np.float32
  • onnx_file_path (str) – Path where to save the generated onnx file
  • verbose (Boolean) – If true will print logs of the model conversion
Returns:

onnx_file_path – Onnx file path

Return type:

str

Notes

This method is available when you import mxnet.contrib.onnx