onnx_chainer.export(model, args, filename=None, export_params=True, graph_name='Graph', save_text=False, opset_version=None, input_names=None, output_names=None, train=False, return_named_inout=False, external_converters=None, external_opset_imports=None, input_shapes=None, no_testcase=False)

Export function for chainer.Chain in ONNX format.

This function performs a forward computation of the given Chain, model, by passing the given arguments args directly. It means, the output Variable object y to make the computational graph will be created by:

y = model(*args)

external_converters and external_opset_imports are for external custom operator. When some ~chainer.FunctionNode are expected to convert to own customized operator, set converter function with ~chainer.FunctionNode name.

>>> import onnx
>>> def custom_converter(param):
...     return onnx.helper.make_node(
...         'CustomizedRelu', param.input_names, param.output_names,
...         domain='chainer'),
>>> external_converters = {'ReLU': custom_converter}
>>> external_imports = {'chainer': 0}
>>> model = chainer.Sequential(F.relu)  # set the target model
>>> args = chainer.Variable(np.random.rand(1,10))  # set dummy input
>>> onnx_graph = onnx_chainer.export(
...     model, args,
...     external_converters=external_converters,
...     external_opset_imports=external_imports)

Returned model has CustomizedRelu node.

  • model (Chain) – The model object you want to export in ONNX format. It should have __call__() method because the second argument args is directly given to the model by the [] accessor.

  • args (list or dict) – The arguments which are given to the model directly.

  • filename (str or file-like object) – The filename used for saving the resulting ONNX model. If None, nothing is saved to the disk.

  • export_params (bool) – If True, this function exports all the parameters included in the given model at the same time. If False, the exported ONNX model doesn’t include any parameter values.

  • graph_name (str) – A string to be used for the name field of the graph in the exported ONNX model.

  • save_text (bool) – If True, the text format of the output ONNX model is also saved with .txt extention.

  • opset_version (int) – The operator set version of ONNX. If not specified or None is given, the latest opset version of the onnx module is used. If an integer is given, it will be ensured that all the operator version in the exported ONNX file is less than this value.

  • input_names (str, list or dict) – Customize input names of the graph. Number of input_names must be same as number of args. When set dict type, keys must be same as args’s keys.

  • output_names (str, list or dict) – Customize output name of the graph. Number of output_names must be same as actual outputs from model. When set dict type, keys must be same as the key of model output.

  • train (bool) – If True, output computational graph with train mode.

  • return_named_inout (bool) – If set True, return ONNX model with named inputs, and named outputs.

  • external_converters (dict) – Add-on converter. Convert functions keyed by ~chainer.FunctionNode name.

  • external_opset_imports (dict) – Import external opset. opset version number keyed by domain name.

  • input_shapes (tuple, list, dict) – Input shape of output graph follows the customized shapes if set. When input are collection type, set list or dict. Tuple of tuple is not allowed.


When return_named_inout is False, return ModelProto as an ONNX model. Otherwise return the tuple of ModelProto, named inputs and outputs, both inputs and outputs are list of ~chainer.Variable.

Return type

ModelProto or tuple