11
$\begingroup$

ONNX is a open format to represent deep learning models.

So I want to import neural networks from other frameworks via ONNX.

import workflow

And the Mathematica 11.3 supports python now.

I think I can use ONNX-MXNet to export the mxnet.json and mxnet.params then just Import these.

But I have no idea about how to install packages on Python-ExternalSessions.


Example

Here is an onnx-format file of Super Resolution Model.

How can I import this using Mathematica and Python-ExternalSessions?

$\endgroup$
  • 1
    $\begingroup$ According to this live CEOing video, it looks like it's in the works. youtube.com/watch?v=l_KzSb9GGbE $\endgroup$ – Chip Hurst Apr 30 '18 at 14:19
  • $\begingroup$ According to this article, it will be possible in Mathematica 12. $\endgroup$ – C. E. Aug 17 '18 at 1:48
6
$\begingroup$

This is incomplete answer because Mathematica doesn't support the latest version of mxnet.

Step 1

Install onnx-mxnet. Open Command Prompt and enter:

pip install onnx-mxnet

enter image description here

Step 2

Download model and image. Run this code in Mathematica:

enter image description here

import mxnet as mx
import onnx_mxnet
import numpy as np

sym, params = onnx_mxnet.import_model('C:/Users/Alexey/Documents/super_resolution.onnx')

from PIL import Image
img = Image.open('C:/Users/Alexey/Documents/super_res_input.jpg').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, :, :]

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

mod.save_checkpoint("super_resolution",1)


net = Import["super_resolution-symbol.json", "MXNet"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.