From 61d0033e14e8c59638978edfb6c2334358f2e749 Mon Sep 17 00:00:00 2001 From: Gemfield Date: Fri, 5 Apr 2019 09:24:31 +0800 Subject: [PATCH 1/3] Enhance upsample operator to adapt onnx opset version 9 for nnvm compiler --- nnvm/python/nnvm/frontend/onnx.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nnvm/python/nnvm/frontend/onnx.py b/nnvm/python/nnvm/frontend/onnx.py index 18eb213bab7b..917bdfde01de 100644 --- a/nnvm/python/nnvm/frontend/onnx.py +++ b/nnvm/python/nnvm/frontend/onnx.py @@ -388,8 +388,14 @@ class Upsample(OnnxOpConverter): """ @classmethod - def _impl_v7(cls, inputs, attr, params): + def _impl_v9(cls, inputs, attr, params): scales = attr.get('scales') + if not scales: + #Here we are going to higher OPSET version. + assert len(inputs) == 2, "Upsample op take 2 inputs, {} given".format(len(inputs)) + input_name = inputs[1].list_input_names()[0] + scales = params[input_name].asnumpy() + inputs = inputs[:1] assert len(scales) == 4 and scales[0] == 1.0 and scales[1] == 1.0 and scales[2] == scales[3] mode = attr.get('mode') if mode == b'nearest': From 9083be5219fa501522d49c9d75c16f13d86a02cf Mon Sep 17 00:00:00 2001 From: Gemfield Date: Mon, 22 Apr 2019 13:19:20 +0800 Subject: [PATCH 2/3] Add upsample test case for newer opset in nnvm --- .../python/frontend/onnx/test_forward.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/nnvm/tests/python/frontend/onnx/test_forward.py b/nnvm/tests/python/frontend/onnx/test_forward.py index a98ef297f1a9..9b32ac0445a0 100644 --- a/nnvm/tests/python/frontend/onnx/test_forward.py +++ b/nnvm/tests/python/frontend/onnx/test_forward.py @@ -422,9 +422,41 @@ def _test_upsample_bilinear(): tvm_out = get_tvm_output(model, in_array, target, ctx, out_shape, 'float32') tvm.testing.assert_allclose(out_array, tvm_out, rtol=1e-5, atol=1e-5) +def _test_upsample_bilinear_opset9(): + scale = 2 + in_shape = (1, 1, 3, 3) + out_shape = (1, 1, 3*scale, 3*scale) + y = helper.make_node("Upsample", ['in','scales'], ['out'], mode='linear') + scales=[1.0, 1.0, 2.0, 2.0] + in_array = np.random.uniform(size=in_shape).astype(np.float32) + out_array = topi.testing.bilinear_resize_python(in_array, (3*scale, 3*scale), "NCHW") + + ref_array = np.array(scales) + ref_node = helper.make_node('Constant', + inputs=[], + outputs=['scales'], + value=onnx.helper.make_tensor(name = 'const_tensor', + data_type = TensorProto.FLOAT, + dims = ref_array.shape, + vals = ref_array.flatten().astype(float))) + + graph = helper.make_graph([ref_node, y], + 'upsample_bilinear_opset9_test', + inputs = [helper.make_tensor_value_info("in", TensorProto.FLOAT, list(in_shape))], + outputs = [helper.make_tensor_value_info("out", TensorProto.FLOAT, list(out_shape))]) + + model = helper.make_model(graph, producer_name='upsample_bilinear_opset9_test') + inputs = [] + inputs.append(in_array) + + for target, ctx in ctx_list(): + tvm_out = get_tvm_output(model, inputs, target, ctx, out_shape, 'float32') + tvm.testing.assert_allclose(out_array, tvm_out, rtol=1e-5, atol=1e-5) + def test_upsample(): _test_upsample_nearest() _test_upsample_bilinear() + _test_upsample_bilinear_opset9() def _test_softmax(inshape, axis): opname = 'Softmax' From 22b1926f2f8625ea669094e8d0dd1cb6b299d086 Mon Sep 17 00:00:00 2001 From: Gemfield Date: Mon, 22 Apr 2019 14:54:34 +0800 Subject: [PATCH 3/3] re-trigger the CI