This is an add-on package for ONNX support by Chainer.
- onnx==0.2.1
- chainer>=3.2.0
See INSTALL.md
import numpy as np
import chainer
import chainer.links as L
import onnx_chainer
model = L.VGG16Layers()
# Pseudo input
x = np.zeros((1, 3, 224, 224), dtype=np.float32)
# Don't forget to set train flag off!
chainer.config.train = False
onnx_chainer.export(model, x, filename='VGG16.onnx')
Using onnx-caffe2 is a simple way to do it.
import chainer
import chainer.links as L
import numpy as np
from onnx_caffe2.backend import Caffe2Backend
from onnx_caffe2.backend import run_model
from onnx_caffe2.helper import save_caffe2_net
import onnx_chainer
# Instantiate a Chainer model (Chain object)
model = L.VGG16Layers()
# Prepare a dummy input
x = np.random.randn(1, 3, 224, 224).astype(np.float32)
# Do not forget setting train flag off!
chainer.config.train = False
# Export to ONNX model
onnx_model = onnx_chainer.export(model, x)
# Convert ONNX model to Caffe2 model
init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(
onnx_model.graph, device='CPU')
# Save the Caffe2 model to disk
init_file = "./vgg16_init.pb"
predict_file = "./vgg16_predict.pb"
save_caffe2_net(init_net, init_file, output_txt=False)
save_caffe2_net(predict_net, predict_file, output_txt=True)
# Run the model with Caffe2
caffe2_out = run_model(onnx_model, [x])[0]
Currently 50 Chainer Functions are supported to export in ONNX format.
- ELU
- HardSigmoid
- LeakyReLU
- LogSoftmax
- PReLUFunction
- ReLU
- Sigmoid
- Softmax
- Softplus
- Tanh
- Convolution2DFunction
- ConvolutionND
- Deconvolution2DFunction
- DeconvolutionND
- EmbedIDFunction 3
- LinearFunction
- Add
- Absolute
- Div
- Mul
- Neg
- PowVarConst
- Sub
- Clip
- Exp
- Identity
- MatMul 4
- Maximum
- Minimum
- Sqrt
- SquaredDifference
- Sum
- Dropout 5
- AveragePooling2D
- AveragePoolingND
- MaxPooling2D
- MaxPoolingND
- BatchNormalization
- FixedBatchNormalization
- LocalResponseNormalization
1: mode should be either 'constant', 'reflect', or 'edge'
2: ONNX doesn't support multiple constant values for Pad operation
3: Current ONNX doesn't support ignore_label for EmbedID
4: Current ONNX doesn't support transpose options for matmul ops
5: In test mode, all dropout layers aren't included in the exported file