Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

MXNet to ONNX export tutorial #12297

Merged
merged 8 commits into from
Aug 23, 2018

Conversation

Roshrini
Copy link
Member

@Roshrini Roshrini commented Aug 22, 2018

Description

Example showing how to use MXNet to ONNX exporter
@aaronmarkham

Checklist

Essentials

  • Changes are complete (i.e. I finished coding on this PR)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

@Roshrini Roshrini requested a review from szha as a code owner August 22, 2018 21:15
@@ -124,6 +124,9 @@ def test_nlp_cnn():
def test_onnx_super_resolution():
assert _test_tutorial_nb('onnx/super_resolution')

def test_onnx_export_mxnet_to_onnx():
assert _test_tutorial_nb('onnx/export_mxnet_to_onnx')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are currently disabled from running on the nightly test run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails CI build if new test not added in test_tutorials. If this is currently disabled, do we need it as a part of CI lint check? @marcoabreu

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

checker.check_graph(model_proto.graph)
```

This method confirms exported model protobuf is valid. Now, the model is ready to be imported in other frameworks for inference!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it verifies that the proto file is as per the ONNX specification, but do you think we should perform inference on the serialized model on any of the frameworks to ensure that the model parameters and model structure was serialized properly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely good to have! We can extend this tutorial later to show inference with other frameworks or add another tutorial to show end to end usecase.

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

This API returns path of the converted model which you can use further to import in other frameworks.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. which you can later use to import the model into other frameworks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


[Open Neural Network Exchange (ONNX)](https://github.com/onnx/onnx) provides an open source format for AI models. It defines an extensible computation graph model, as well as definitions of built-in operators and standard data types.

In this tutorial, we will show how you can save MXNet models to ONNX format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ONNX format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


In this tutorial, we will show how you can save MXNet models to ONNX format.

Current MXNet-ONNX import and export operator support and coverage can be found [here](https://cwiki.apache.org/confluence/display/MXNET/ONNX+Operator+Coverage).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operators (plural)

Also, try linking to more context than (here). Say like:
MXNet-ONNX operators coverage and features are updated regularly. Visit the ONNX operator coverage page for the latest information.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


In this tutorial we will:

- learn how to use MXNet to ONNX exporter on pretrained models
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about one sentence? Unless you're thinking of adding more bullets....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


- learn how to use MXNet to ONNX exporter on pretrained models

## Pre-requisite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prerequisites

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

To run the tutorial you will need to have installed the following python modules:
- [MXNet == 1.3.0](http://mxnet.incubator.apache.org/install/index.html)
- [onnx](https://github.com/onnx/onnx) v1.2.1 (follow the install guide)
MXNet ONNX importer and exporter follows version 7 of ONNX operator set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this means. I see you want ONNX v1.2.1, but there's yet another version to know about within ONNX? Would I get v1.2.1 then have to download an operator set that is version 7? Maybe this is TMI?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1.2.1 comes with opset 7 which we currently support. Made it more clear


This is useful if we are training a model. At the end of training, we just need to invoke the `export_model` function and provide sym and params objects as inputs with other attributes to save the model in ONNX format.

#### MXNet's exported json and params files:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd number this 2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


This is useful if we have pre-trained models and we want to convert them to ONNX format.

In this tutorial, we will show second usecase to convert pretrained model to ONNX format:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


```python
# Downloaded input symbol and params files
sym = 'resnet-18-symbol.json'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I'm thinking I was supposed to manually download the files from the zoo. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we downloaded files before. we will use those now


## Check validity of ONNX model

Now we can check validity of the converted ONNX model by using ONNX checker tool as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...by using ONNX checker tool. The tool will validate the model by checking if the content contains valid protobuf:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

checker.check_graph(model_proto.graph)
```

This method confirms exported model protobuf is valid. Now, the model is ready to be imported in other frameworks for inference!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any comment on what to do if the check fails or common reasons why it might fail?

Also, is there a call to action or next steps?

@Roshrini
Copy link
Member Author

@aaronmarkham Addressed the comments.

@sandeep-krishnamurthy sandeep-krishnamurthy added Doc ONNX pr-awaiting-review PR is waiting for code review labels Aug 23, 2018
Copy link
Contributor

@aaronmarkham aaronmarkham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few more updates needed.


## Pre-requisite
## Prerequisite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make plural

From the above API description, we can see that export_model API accepts 2 kinds of inputs:

#### MXNet sym, params objects:
From the above API description, we can see that export_model API accepts two kinds of inputs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that the export_model

@@ -95,6 +95,8 @@ model_proto = onnx.load(converted_model_path)
checker.check_graph(model_proto.graph)
```

If the converted protobuf format doesn't qualify to ONNX proto specifications, the checker will throw errors but in this case it successfully passes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors, but

@Roshrini
Copy link
Member Author

@aaronmarkham Thanks for reviewing. Addressed new comments :)

Copy link
Contributor

@sandeep-krishnamurthy sandeep-krishnamurthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this tutorial @Roshrini !


MXNet-ONNX operators coverage and features are updated regularly. Visit the [ONNX operator coverage](https://cwiki.apache.org/confluence/display/MXNET/ONNX+Operator+Coverage) page for the latest information.

In this tutorial we will learn how to use MXNet to ONNX exporter on pre-trained models.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In this tutorial, we will..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making hyper link to API spec for mxnet-to-onnx exporter will be useful.

## Prerequisite

To run the tutorial you will need to have installed the following python modules:
- [MXNet == 1.3.0](http://mxnet.incubator.apache.org/install/index.html)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>= to make this tutorial relevant going forward ?


To run the tutorial you will need to have installed the following python modules:
- [MXNet == 1.3.0](http://mxnet.incubator.apache.org/install/index.html)
- [onnx](https://github.com/onnx/onnx) v1.2.1 (follow the install guide)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to install guide. May be this? https://github.com/onnx/onnx#installation


## Downloading a model from the MXNet model zoo

We download a pre-trained model, in this case ResNet-18 model, trained on [ImageNet](http://www.image-net.org/) from the [MXNet Model Zoo](http://data.mxnet.io/models/imagenet/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We download the pre-trained ResNet-18 ImageNet model from MXNet Model Zoo


## MXNet to ONNX exporter API

We can check MXNet's ONNX `export_model` API usage as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us describe the MXNet's export_model to ONNX API.

```python
help(onnx_mxnet.export_model)
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to show the output here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

help(onnx_mxnet.export_model)
```

From the above API description, we can see that export_model API accepts two kinds of inputs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export_model API can accept the MXNet model in one of the following two ways.

2. MXNet's exported json and params files:
* This is useful if we have pre-trained models and we want to convert them to ONNX format.

In this tutorial, we will show second use case to convert pre-trained model to ONNX format:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have downloaded a pre-trained model files, we will use the export_model API by passing the path for symbol and params files.


## How to use MXNet to ONNX exporter API

We will use downloaded files and define input variables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will use the downloaded pre-trained model files (sym, params)...


```python
# Downloaded input symbol and params files
sym = 'resnet-18-symbol.json'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be more clear that this is file path if we use - './resnet-18-symbol.json' etc. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be more explicit. Changed it.

@Roshrini
Copy link
Member Author

@sandeep-krishnamurthy Thanks for reviewing. Addressed comments.

Copy link
Contributor

@sandeep-krishnamurthy sandeep-krishnamurthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
LGTM

@sandeep-krishnamurthy sandeep-krishnamurthy added pr-awaiting-merge Review and CI is complete. Ready to Merge and removed pr-awaiting-review PR is waiting for code review labels Aug 23, 2018
@sandeep-krishnamurthy sandeep-krishnamurthy merged commit cc30fab into apache:master Aug 23, 2018
Roshrini added a commit to Roshrini/mxnet that referenced this pull request Aug 23, 2018
* mxnet to onnx export tutorial added

* test added

* addressing review comment

* comments addressed

* few more fixes

* addressing comments

* addressing comments

* retrigger build
szha pushed a commit that referenced this pull request Aug 23, 2018
* mxnet to onnx export tutorial added

* test added

* addressing review comment

* comments addressed

* few more fixes

* addressing comments

* addressing comments

* retrigger build
XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
* mxnet to onnx export tutorial added

* test added

* addressing review comment

* comments addressed

* few more fixes

* addressing comments

* addressing comments

* retrigger build
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request Sep 19, 2018
* mxnet to onnx export tutorial added

* test added

* addressing review comment

* comments addressed

* few more fixes

* addressing comments

* addressing comments

* retrigger build
@Roshrini Roshrini deleted the onnx_export_tutorial branch November 15, 2018 18:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Doc ONNX pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants