Skip to content

Fix to accept chainer.Variable collection input #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions onnx_chainer/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ def export(model, args, filename=None, export_params=True,
for i, arg in enumerate(args):
if isinstance(arg, numpy.ndarray):
args[i] = chainer.Variable(arg)
network_inputs.append(args[i])
network_inputs.append(args[i])
outputs = model(*args)
elif isinstance(args, dict):
for key, arg in args.items():
if isinstance(arg, numpy.ndarray):
args[key] = chainer.Variable(arg)
network_inputs.append(args[key])
network_inputs.append(args[key])
outputs = model(**args)
elif isinstance(args, numpy.ndarray):
args = chainer.Variable(args)
Expand Down
19 changes: 12 additions & 7 deletions onnx_chainer/testing/test_onnxruntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ def check_output(model, x, fn, out_key='prob', opset_version=None):
for i in x:
assert isinstance(i, (np.ndarray, chainer.Variable))
chainer_out = model(*x)
x = tuple(
x_rt = tuple(
_x.array if isinstance(_x, chainer.Variable) else _x for _x in x)
elif isinstance(x, dict):
chainer_out = model(**x)
x_rt = tuple(_x.array if isinstance(_x, chainer.Variable) else _x
for _, _x in x.items())
elif isinstance(x, np.ndarray):
chainer_out = model(chainer.Variable(x))
x = (x,)
x_rt = x,
elif isinstance(x, chainer.Variable):
chainer_out = model(x)
x = (x.array,)
x_rt = x.array,
else:
raise ValueError(
'The \'x\' argument should be a list or tuple of numpy.ndarray or '
'chainer.Variable, or simply a numpy.ndarray or a chainer.Variable'
' itself. But a {} object was given.'.format(type(x)))
'The \'x\' argument should be a list, tuple or dict of '
'numpy.ndarray or chainer.Variable, or simply a numpy.ndarray or a'
' chainer.Variable itself. But a {} object was given.'.format(
type(x)))

if isinstance(chainer_out, (list, tuple)):
chainer_out = (y.array for y in chainer_out)
Expand Down Expand Up @@ -74,7 +79,7 @@ def check_output(model, x, fn, out_key='prob', opset_version=None):
assert input_names == list(sorted(graph_input_names))

rt_out = sess.run(
None, {name: array for name, array in zip(input_names, x)})
None, {name: array for name, array in zip(input_names, x_rt)})

for cy, my in zip(chainer_out, rt_out):
np.testing.assert_almost_equal(cy, my, decimal=5)
47 changes: 47 additions & 0 deletions tests/test_inout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import unittest

import chainer
import chainer.functions as F
import chainer.links as L
import numpy as np

from onnx_chainer.testing import test_onnxruntime


class TestMultipleInputs(unittest.TestCase):

def setUp(self):

class Model(chainer.Chain):

def __init__(self):
super(Model, self).__init__()
with self.init_scope():
self.prelu = L.PReLU()

def __call__(self, x, y, z):
return F.relu(x) + self.prelu(y) * z

self.model = Model()
self.ins = (np.zeros((1, 5), dtype=np.float32),
np.zeros((1, 5), dtype=np.float32),
np.zeros((1, 5), dtype=np.float32))
self.fn = 'MultiInputs.onnx'

def test_arrays(self):
test_onnxruntime.check_output(self.model, self.ins, self.fn)

def test_variables(self):
ins = [chainer.Variable(i) for i in self.ins]
test_onnxruntime.check_output(self.model, ins, self.fn)

def test_array_dicts(self):
arg_names = ['x', 'y', 'z'] # current exporter ignores these names
ins = {arg_names[i]: v for i, v in enumerate(self.ins)}
test_onnxruntime.check_output(self.model, ins, self.fn)

def test_variable_dicts(self):
arg_names = ['x', 'y', 'z'] # current exporter ignores these names
ins = {arg_names[i]: chainer.Variable(v)
for i, v in enumerate(self.ins)}
test_onnxruntime.check_output(self.model, ins, self.fn)