Use Apache MXNet (Incubating) for Inference with an ONNX Model - Deep Learning AMI

Use Apache MXNet (Incubating) for Inference with an ONNX Model

How to Use an ONNX Model for Image Inference with Apache MXNet (Incubating)

    • (Option for Python 3) - Activate the Python 3 Apache MXNet (Incubating) environment:

      $ source activate mxnet_p36
    • (Option for Python 2) - Activate the Python 2 Apache MXNet (Incubating) environment:

      $ source activate mxnet_p27
  1. The remaining steps assume you are using the mxnet_p36 environment.

  2. Download a picture of a husky.

    $ curl -O
  3. Download a list of classes that work with this model.

    $ curl -O
  4. Download the pre-trained VGG 16 model in ONNX format.

    $ wget -O vgg16.onnx
  5. Use a your preferred text editor to create a script that has the following content. This script will use the image of the husky, get a prediction result from the pre-trained model, then look this up in the file of classes, returning an image classification result.

    import mxnet as mx import mxnet.contrib.onnx as onnx_mxnet import numpy as np from collections import namedtuple from PIL import Image import pickle # Preprocess the image img ="Siberian_Husky_bi-eyed_Flickr.jpg") img = img.resize((224,224)) rgb_img = np.asarray(img, dtype=np.float32) - 128 bgr_img = rgb_img[..., [2,1,0]] img_data = np.ascontiguousarray(np.rollaxis(bgr_img,2)) img_data = img_data[np.newaxis, :, :, :].astype(np.float32) # Define the model's input data_names = ['data'] Batch = namedtuple('Batch', data_names) # Set the context to cpu or gpu ctx = mx.cpu() # Load the model sym, arg, aux = onnx_mxnet.import_model("vgg16.onnx") mod = mx.mod.Module(symbol=sym, data_names=data_names, context=ctx, label_names=None) mod.bind(for_training=False, data_shapes=[(data_names[0],img_data.shape)], label_shapes=None) mod.set_params(arg_params=arg, aux_params=aux, allow_missing=True, allow_extra=True) # Run inference on the image mod.forward(Batch([mx.nd.array(img_data)])) predictions = mod.get_outputs()[0].asnumpy() top_class = np.argmax(predictions) print(top_class) labels_dict = pickle.load(open("imagenet1000_clsid_to_human.pkl", "rb")) print(labels_dict[top_class])
  6. Then run the script, and you should see a result as follows:

    248 Eskimo dog, husky