Exporting model to ONNX (or alternative way to run on Android)

I fine-tuned an SSD model on a custom dataset (everything working properly), and I’m trying to export it to ONNX in order to run it on Android.
This is what I’m doing:

from os import path as osp
import numpy as np
import mxnet as mx
import gluoncv as gcv
from mxnet.contrib import onnx as onnx_mxnet
from mxnet import gluon
from gluoncv import model_zoo, data, utils

ctx = mx.cpu(0)

OUTPUT = 'oxnn/'
DATA = "./friends.png"
MODEL = "CML_exported"
INPUT_SHAPE = ((1,3,512,683))

dummy_img, _ = data.transforms.presets.ssd.load_test(DATA, short=512)

CML_classes = ["CML_mug"]
net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=CML_classes, pretrained_base=False, ctx=ctx)
net.load_parameters("saved_weights/CML_mobilenet_mug_00/ep_035.params", ctx=ctx)
net.hybridize()
_ = net(dummy_img)

net.export(osp.join(OUTPUT, MODEL))
sym = osp.join(OUTPUT, MODEL + "-symbol.json")
params = osp.join(OUTPUT, MODEL + "-0000.params")
onnx_file = osp.join(OUTPUT, MODEL + ".onnx")

converted_model_path = onnx_mxnet.export_model(sym, params, [INPUT_SHAPE], np.float32, onnx_file, verbose=True)

I’m getting the error:

  File "/home/lews/anaconda3/envs/gluon/lib/python3.7/site-packages/mxnet/contrib/onnx/mx2onnx/_op_translations.py", line 1502, in convert_slice_axis
    ends = int(attrs.get("end", None))
ValueError: invalid literal for int() with base 10: 'None'

This is a known problem, it is also discussed here …it seems there are some operatrions (in this case, convert_slice_axis, that are not corrcetly supported)

I found a few discussions here on the forum suggesting to uninstall onnx and reinstalling older version. I tried that, but if I run

pip install onnx==1.3.0

I get anothere error:

ERROR: Command errored out with exit status 1:
     command: /home/lews/anaconda3/envs/gluon/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rn77ezl3/onnx/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rn77ezl3/onnx/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-8a5a5p6o
         cwd: /tmp/pip-install-rn77ezl3/onnx/
    Complete output (6 lines):
    fatal: not a git repository (or any of the parent directories): .git
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-rn77ezl3/onnx/setup.py", line 71, in <module>
        assert CMAKE, 'Could not find "cmake" executable!'
    AssertionError: Could not find "cmake" executable!
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output

The issue reported in the git discussion I linked is kinda old. Have there been any updates since last year? Was this somehow fixed?

Anyway, the reason I need this is to try and run the fine-tuned model on Android, and it seems to be the most straightforward way.
Could I try anything else?

Hi LewsTherin511,
There’s been a lot updates on the MX-ONNX exporting support recently. The changes are all in branch v1.x for now. In this case, the error at ends = int(attrs.get("end", None)) should be fixed in incubator-mxnet/_op_translations.py at 8a83a21fc387b8b90a9ec2c7bdb624e2f9792307 · apache/incubator-mxnet · GitHub. Can you try to pull the v1.x branch and do the export again? To just update ONNX export related files, you can also try out: [v1.x] Tool to help update/restore onnx support by Zha0q1 · Pull Request #19876 · apache/incubator-mxnet · GitHub

1 Like

Hi, thank yoyu very much for your answer!

I know it’s a terribly lame question, but I generally always installed mxnet/gluoncv with pip. In order to try your suggestion, I’m assuming I should follow the instructions for an installation from source and switch to the branch you indicated?
So something like:

* pip install --upgrade mxnet -f https://dist.mxnet.io/python/all
* git clone https://github.com/dmlc/gluon-cv
* git checkout v1.x
* cd gluon-cv && python setup.py install --user

or am I getting this completely wrong?

As for using the specific tool you indicated, to update only the ONNX export files…how could I do that? :sweat:

I’m sorry, I’m not completely familiar with these things and I want to be sure to be following the right procedure!

Thanks!

Hi, I think the update tool is the easier way for you. It should work fine with your current mxnet installed by pip. To do so:

  1. Install mxnet from pip (you already have).
  2. Copy file update_onnx.py in https://github.com/apache/incubator-mxnet/pull/19876 to your working directory.
  3. Call python update_onnx.py. It should find your installed mxnet and do the work automatically.
  4. Try to export your model.
1 Like