You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@mxnet.apache.org by GitBox <gi...@apache.org> on 2022/04/04 03:30:45 UTC
[GitHub] [incubator-mxnet] dai-ichiro commented on issue #20985: Is there onnx import support in 1.9.0
dai-ichiro commented on issue #20985:
URL: https://github.com/apache/incubator-mxnet/issues/20985#issuecomment-1087073867
https://github.com/apache/incubator-mxnet/issues/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")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@mxnet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@mxnet.apache.org
For additional commands, e-mail: issues-help@mxnet.apache.org