Chainer to ONNX to CNTK Tutorial
Note
We no longer include the CNTK, Caffe, Caffe2 and Theano Conda environments in the AWS Deep Learning AMI starting with the v28 release. Previous releases of the AWS Deep Learning AMI that contain these environments will continue to be available. However, we will only provide updates to these environments if there are security fixes published by the open source community for these frameworks.
ONNX Overview
The Open Neural Network Exchange (ONNX
This tutorial shows you how to use the Deep Learning AMI with Conda with ONNX. By following these steps, you can train a model or load a pre-trained model from one framework, export this model to ONNX, and then import the model in another framework.
ONNX Prerequisites
To use this ONNX tutorial, you must have access to a Deep Learning AMI with Conda version 12 or later. For more information about how to get started with a Deep Learning AMI with Conda, see Deep Learning AMI with Conda.
Important
These examples use functions that might require up to 8 GB of memory (or more). Be sure to choose an instance type with enough memory.
Launch a terminal session with your Deep Learning AMI with Conda to begin the following tutorial.
Convert a Chainer Model to ONNX, then Load the Model into CNTK
First, activate the Chainer environment:
$
source activate chainer_p36
Create a new file with your text editor, and use the following program in a script to fetch a model from Chainer's model zoo, then export it to the ONNX format.
import numpy as np import chainer import chainercv.links as L import onnx_chainer # Fetch a vgg16 model model = L.VGG16(pretrained_model='imagenet') # Prepare an input tensor x = np.random.rand(1, 3, 224, 224).astype(np.float32) * 255 # Run the model on the data with chainer.using_config('train', False): chainer_out = model(x).array # Export the model to a .onnx file out = onnx_chainer.export(model, x, filename='vgg16.onnx') # Check that the newly created model is valid and meets ONNX specification. import onnx model_proto = onnx.load("vgg16.onnx") onnx.checker.check_model(model_proto)
After you run this script, you will see the newly created .onnx file in the same directory.
Now that you have an ONNX file you can try running inference with it with the following example: