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

Is there onnx import support in 1.9.0 #20985

Open
timprepscius opened this issue Apr 2, 2022 · 3 comments
Open

Is there onnx import support in 1.9.0 #20985

timprepscius opened this issue Apr 2, 2022 · 3 comments

Comments

@timprepscius
Copy link

This page claims there is:
https://mxnet.apache.org/versions/1.9.0/api/python/docs/tutorials/packages/onnx/super_resolution.html#

But it seems in 1.9.0 there isn't.

Is there some way of importing onnx files?

Thanks

@dai-ichiro
Copy link

#20769

from PIL import Image
import numpy as np
import mxnet as mx
from mxnet.gluon.utils import download

import onnx
from mxnet.contrib.onnx.onnx2mx.import_onnx import GraphProto

img_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_res_input.jpg'
fname = download(img_url)

model_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_resolution.onnx'
onnx_model_file = download(model_url)

graph = GraphProto()
model_proto = onnx.load_model(onnx_model_file)

sym, arg, aux = graph.from_onnx(model_proto.graph, opset_version=10)
# 10 is dummy number.

img = Image.open(fname).resize((224, 224))
img_ycbcr = img.convert("YCbCr")
img_y, img_cb, img_cr = img_ycbcr.split()
test_image = np.array(img_y)[np.newaxis, np.newaxis, :, :]

data_names = [graph_input for graph_input in sym.list_inputs()
                      if graph_input not in arg and graph_input not in aux]

mod = mx.mod.Module(symbol=sym, data_names=data_names, context=mx.cpu(), label_names=None)
mod.bind(for_training=False, data_shapes=[(data_names[0],test_image.shape)], label_shapes=None)
mod.set_params(arg_params=arg, aux_params=aux, allow_missing=True, allow_extra=True)

from collections import namedtuple
Batch = namedtuple('Batch', ['data'])

mod.forward(Batch([mx.nd.array(test_image)]))

output = mod.get_outputs()[0][0][0]
img_out_y = Image.fromarray(np.uint8((output.asnumpy().clip(0, 255))))

result_img = Image.merge(
"YCbCr", [
                img_out_y,
                img_cb.resize(img_out_y.size, Image.BICUBIC),
                img_cr.resize(img_out_y.size, Image.BICUBIC)
]).convert("RGB")
result_img.save("super_res_output.jpg")

@waytrue17
Copy link
Contributor

The onnx import module has not been maintained for a long time and it will be deprecated in the next mxnet release (1.10). Here is our RFC for the new onnx-mxnet module where we decided to deprecate import #20063

@timprepscius
Copy link
Author

Ok. Thanks for this response.

Although, I understand my vote to be of basically no consequence - I would vote for MXNet to have an "approved" onnx importer.

This is because you can not only verify MxNet's functioning, but also those of other platforms. And you can have relative performance measurements. (everyone runs the same model, who is faster, is anybody incorrect)

For instance, MxNet used to have a bug in the CPU version, which affected some work I had with noise removal (I can find the mxnet issue if you'd like). It was really hard to cope with because I assumed MxNet was correct. I only found out MxNet wasn't correct because of the onnx importer. As of now, I'm actually not sure if MxNet has fixed the bug, because I can no longer test. And so, instead I'm using the ONNX runtime, which I could verify.

Although I'm not sure of the overhead of the ONNX runtime, which I'm not so happy about.

Anyhow, end babble, beep borp.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants