Skip to content

Commit

Permalink
Merge pull request #248 from disktnk/fix/backport-chainer-v7rc
Browse files Browse the repository at this point in the history
Backport from Chainer v7rc
  • Loading branch information
disktnk authored Nov 6, 2019
2 parents 9ecbd31 + 5985650 commit ae36f8a
Show file tree
Hide file tree
Showing 22 changed files with 416 additions and 243 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ matrix:
- ONNX_VER="1.4.1"
- name: "Py36, Chainer v6"
python: "3.6"
- name: "Py37, Chainer v6, ONNX 1.5.0"
python: "3.7"
env:
- ONNX_VER="1.5.0"
- name: "Py37, Chainer v6"
python: "3.7"
env:
Expand Down
136 changes: 2 additions & 134 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ This is an add-on package for ONNX support by Chainer.

## Tested environment

- Python 3.5.5, 3.6.7, 3.7.2
- ONNX 1.4.1, 1.5.0
- opset version 7, 8, 9, 10
- Chainer stable, preview
- ONNX-Runtime 0.4.0

**(You can still specify all opset versions <= 9, but please noted that opset versions <= 6 are not tested)**
see [Tested environments](https://onnx-chainer.readthedocs.io/en/latest/introduction/index.html#tested-environments)

## Installation

Expand Down Expand Up @@ -73,137 +67,11 @@ onnx_chainer.export(model, x, filename='vgg16.onnx')

## Supported Functions

Currently 84 Chainer Functions are supported to export in ONNX format.

### Activation

- ClippedReLU
- ELU
- HardSigmoid
- LeakyReLU
- LogSoftmax
- PReLUFunction
- ReLU
- Selu
- Sigmoid
- Softmax
- Softplus
- Tanh

### Array

- Cast
- Concat
- Copy
- Depth2Space
- Dstack
- ExpandDims
- GetItem
- Hstack
- Pad <sup>[1](#pad1)</sup><sup>[2](#pad2)</sup>
- Repeat
- Reshape
- ResizeImages
- Separate
- Shape <sup>[5](#shape1)</sup>
- Space2Depth
- SplitAxis
- Squeeze
- Stack
- Swapaxes
- Tile
- Transpose
- Vstack
- Where

### Connection

- Convolution2DFunction
- ConvolutionND
- Deconvolution2DFunction
- DeconvolutionND
- EmbedIDFunction <sup>[3](#embed1)</sup>
- LinearFunction

### Loss

- SoftmaxCrossEntropy

### Math

- Absolute
- Add
- AddConstant
- ArcCos
- ArcSin
- ArcTan
- ArgMax
- ArgMin
- BroadcastTo
- Cos
- Cosh
- Clip
- Div
- DivFromConstant
- Exp
- Identity
- LinearInterpolate
- Log
- LogSumExp
- MatMul
- Max
- Maximum
- Mean
- Min
- Minimum
- Mul
- MulConstant
- Neg
- PowConstVar
- PowVarConst
- PowVarVar
- Prod
- RsqrtGPU
- Sin
- Sinh
- Sqrt
- Square
- Sub
- SubFromConstant
- Sum
- Tan

### Noise

- Dropout <sup>[4](#dropout1)</sup>

### Normalization

- BatchNormalization
- FixedBatchNormalization
- LocalResponseNormalization
- NormalizeL2

### Pooling

- AveragePooling2D
- AveragePoolingND
- MaxPooling2D
- MaxPoolingND
- ROIPooling2D
- Unpooling2D
see [Supported Functions](https://onnx-chainer.readthedocs.io/en/latest/introduction/index.html#supported-functions)


## Contribution

Any contribution to ONNX-Chainer is welcome!

- Python codes follow [Chainer Coding Guidelines](https://docs.chainer.org/en/stable/contribution.html#coding-guidelines)

---

<a name="pad1">1</a>: mode should be either 'constant', 'reflect', or 'edge'<br />
<a name="pad2">2</a>: ONNX doesn't support multiple constant values for Pad operation<br />
<a name="embed1">3</a>: Current ONNX doesn't support ignore_label for EmbedID<br />
<a name="dropout1">4</a>: In test mode, all dropout layers aren't included in the exported file<br />
<a name="shape1">5</a>: Chainer doesn't support Shape function<br />
15 changes: 9 additions & 6 deletions docs/source/introduction/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ First, install `ChainerCV <https://github.com/chainer/chainercv>`__ to get the p
``vgg16.onnx`` file will be exported.

Other export examples are put on `examples <https://github.com/chainer/onnx-chainer/tree/master/examples>`__. Please check them.

Supported Functions
-------------------

Expand Down Expand Up @@ -71,6 +73,7 @@ Currently 82 Chainer Functions are supported to export in ONNX format.
* GetItem
* Hstack
* Pad [#pad1]_ [#pad2]_
* Permutate
* Repeat
* Reshape
* ResizeImages
Expand Down Expand Up @@ -172,12 +175,12 @@ Tested Environments
* Windows 10

* Python 3.5.5, 3.6.7, 3.7.2
* ONNX 1.4.1, 1.5.0
* ONNX 1.4.1, 1.5.0, 1.6.0

* opset version 7, 8, 9, 10
* opset version 7, 8, 9, 10, 11

* Chainer stable, preview
* ONNX-Runtime 0.4.0
* Chainer 6.5.0
* ONNX-Runtime 1.0.0


Run Test
Expand Down Expand Up @@ -211,6 +214,6 @@ on GPU environment::
Contribution
------------

Any contribution to ONNX-Chainer is welcom!
Any contribution to ONNX-Chainer is welcome!

* Python codes follow `Chainer Coding Guidelines <ttps://docs.chainer.org/en/stable/contribution.html#coding-guidelines>`__
* Python codes follow `Chainer Coding Guidelines <https://docs.chainer.org/en/stable/contribution.html#coding-guidelines>`__
23 changes: 12 additions & 11 deletions onnx_chainer/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
import warnings

import chainer
import onnx
from onnx.mapping import NP_TYPE_TO_TENSOR_TYPE
from onnx import shape_inference

from onnx_chainer.context import Context
from onnx_chainer.graph import Graph
from onnx_chainer import mapping
from onnx_chainer.onnx_helper import is_support_non_standard_domain

try:
import onnx
from onnx import checker
from onnx import helper
from onnx.mapping import NP_TYPE_TO_TENSOR_TYPE
from onnx import numpy_helper
from onnx import shape_inference

from onnx_chainer.context import Context
from onnx_chainer.graph import Graph
from onnx_chainer import mapping
from onnx_chainer.onnx_helper import is_support_non_standard_domain

_available = True
except ImportError:
_available = False

MINIMUM_OPSET_VERSION = 7
MAXIMUM_OPSET_VERSION = 10
MAXIMUM_OPSET_VERSION = 11


def _check_available():
if not _available:
raise ImportError(
'ONNX is not installed on your environment. Exporting your model '
'in ONNX format needs the onnx package.\n\n'
'\t$ pip install onnx\n\n')
'\t$ pip install \'onnx<1.7.0\'\n\n')


def convert_parameter(parameter, context):
Expand Down Expand Up @@ -462,7 +462,8 @@ def _export(model, args, filename, export_params, graph_name, save_text,

if input_shapes is not None:
for output in model.graph.output:
output.type.Clear()
for d in output.type.tensor_type.shape.dim:
d.Clear()
model = shape_inference.infer_shapes(model)
check_onnx_model(model, external_converters, external_opset_imports)

Expand Down
10 changes: 7 additions & 3 deletions onnx_chainer/export_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

import chainer

from onnx_chainer.export import export
from onnx_chainer.onnx_helper import cleanse_param_name
from onnx_chainer.onnx_helper import write_tensor_pb
from onnx_chainer.export import _available

if _available:
from onnx_chainer.export import export

from onnx_chainer.onnx_helper import cleanse_param_name
from onnx_chainer.onnx_helper import write_tensor_pb


def export_testcase(model, args, out_dir, output_grad=False, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions onnx_chainer/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from onnx_chainer.functions.array import convert_Hstack # NOQA
from onnx_chainer.functions.array import convert_Moveaxis # NOQA
from onnx_chainer.functions.array import convert_Pad # NOQA
from onnx_chainer.functions.array import convert_Permutate # NOQA
from onnx_chainer.functions.array import convert_Repeat # NOQA
from onnx_chainer.functions.array import convert_Reshape # NOQA
from onnx_chainer.functions.array import convert_ResizeImages # NOQA
Expand Down
10 changes: 9 additions & 1 deletion onnx_chainer/functions/activation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

from onnx_chainer.functions.opset_version import support
from onnx_chainer import onnx_helper

Expand Down Expand Up @@ -25,7 +27,7 @@ def _convert_softmax_impl(op_type, func, input_names, output_names):
return gb.nodes(output_names=output_names)


@support((1, 6))
@support((1, 6, 11))
def convert_ClippedReLU(
func, opset_version, input_names, output_names, context):
if opset_version == 1:
Expand All @@ -39,6 +41,12 @@ def convert_ClippedReLU(
'Clip', input_names, output_names,
min=0.0, max=func.cap,
),
elif opset_version == 11:
min_name = context.add_const(np.array(0, dtype=np.float32), 'zero')
max_name = context.add_const(
np.array(func.cap, dtype=np.float32), 'clip_z')
input_names.extend([min_name, max_name])
return onnx_helper.make_node('Clip', input_names, output_names),


@support((1, 6))
Expand Down
Loading

0 comments on commit ae36f8a

Please sign in to comment.