Python onnx.helper.make_node() Examples
The following are 30
code examples of onnx.helper.make_node().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
onnx.helper
, or try the search function
.
Example #1
Source File: onnx_import_test.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 7 votes |
def test_broadcast(): """Test for broadcasting in onnx operators.""" input1 = np.random.rand(1, 3, 4, 5).astype("float32") input2 = np.random.rand(1, 5).astype("float32") inputs = [helper.make_tensor_value_info("input1", TensorProto.FLOAT, shape=(1, 3, 4, 5)), helper.make_tensor_value_info("input2", TensorProto.FLOAT, shape=(1, 5))] outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=(1, 3, 4, 5))] nodes = [helper.make_node("Add", ["input1", "input2"], ["output"])] graph = helper.make_graph(nodes, "bcast_test", inputs, outputs) bcast_model = helper.make_model(graph) bkd_rep = mxnet_backend.prepare(bcast_model) numpy_op = input1 + input2 output = bkd_rep.run([input1, input2]) npt.assert_almost_equal(output[0], numpy_op)
Example #2
Source File: test_ops_matmul.py From ngraph-onnx with Apache License 2.0 | 7 votes |
def make_onnx_model_for_matmul_op(input_left, input_right): output_shape = np.matmul(input_left, input_right).shape node = make_node('MatMul', ['X', 'Y'], ['Z'], name='test_node') graph = make_graph([node], 'test_graph', [make_tensor_value_info('X', onnx.TensorProto.FLOAT, input_left.shape), make_tensor_value_info('Y', onnx.TensorProto.FLOAT, input_right.shape)], [make_tensor_value_info('Z', onnx.TensorProto.FLOAT, output_shape)]) model = make_model(graph, producer_name='ngraph ONNXImporter') return model
Example #3
Source File: test_ops_convpool.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def test_pool_average(ndarray_1x1x4x4): x = ndarray_1x1x4x4 node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2)) y = np.array([[13.5, 15.5], [21.5, 23.5]], dtype=np.float32).reshape(1, 1, 2, 2) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y]) node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2), pads=(1, 1, 1, 1)) y = np.array([[11, 12.5, 14], [17, 18.5, 20], [23, 24.5, 26]], dtype=np.float32).reshape(1, 1, 3, 3) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
Example #4
Source File: custom_layers_test.py From onnx-coreml with MIT License | 6 votes |
def _make_model_acos_exp_topk(): # type: (...) -> ModelProto ''' make a very simple model for testing: input->clip->exp->topk->2 outputs ''' inputs = [('input0', (10,), TensorProto.FLOAT), ('K', (1,), TensorProto.INT64)] outputs = [('output_values', (3,), TensorProto.FLOAT), ('output_indices', (3,), TensorProto.INT64)] acos = helper.make_node("Acos", inputs=[inputs[0][0]], outputs=['acos_out']) exp = helper.make_node("Exp", inputs=[acos.output[0]], outputs=['exp_out']) topk = helper.make_node("TopK", inputs=[exp.output[0], inputs[1][0]], outputs=[outputs[0][0], outputs[1][0]], axis=0) return _onnx_create_model([acos, exp, topk], inputs, outputs)
Example #5
Source File: utils.py From chainer-compiler with MIT License | 6 votes |
def make_graph(nodes, graph_name, inputs, outputs): input_dict = {} for input in inputs: input_dict[input.name] = (input, None) outputs_fixed = [] for output in outputs: if output.name in input_dict: input, new_output = input_dict[output.name] if new_output is None: new_output = new_tensor(name=graph_name + '_out') nodes.append(helper.make_node('Identity', inputs=[input.name], outputs=[new_output.name])) input_dict[output.name] = (input, new_output) else: new_output = output outputs_fixed.append(new_output) graph_name = gen_graph_name(graph_name) return helper.make_graph(nodes, graph_name, inputs, outputs_fixed)
Example #6
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 6 votes |
def test_add(self): node_def = helper.make_node("Add", ["X", "Y"], ["Z"]) x = self._get_rnd_float32(shape=[5, 10, 5, 5]) y = self._get_rnd_float32(shape=[10, 1, 1]) output = run_node(node_def, [x, y]) np.testing.assert_almost_equal(output["Z"], np.add(x, y.reshape([1, 10, 1, 1]))) # node_def = helper.make_node("Add", ["A", "B"], ["C"], broadcast=1) # a = self._get_rnd([10, 10]) # b = self._get_rnd([10, 10]) # output = run_node(node_def, [a, b]) # np.testing.assert_almost_equal(output["C"], np.add(a, b)) # node_def = helper.make_node("Add", ["A", "B"], ["C"], broadcast=1) # a = self._get_rnd([10, 10]) # b = self._get_rnd([10,]) # output = run_node(node_def, [a, b]) # np.testing.assert_almost_equal(output["C"], np.add(a, b))
Example #7
Source File: _test_utils.py From onnx-coreml with MIT License | 6 votes |
def _onnx_create_single_node_model(op_type, # type: Text input_shapes, # type: Sequence[Tuple[int, ...]] output_shapes, # type: Sequence[Tuple[int, ...]] initializer=[], # type: Sequence[TensorProto] **kwargs # type: Any ): # type: (...) -> ModelProto inputs = [ ("input{}".format(i,), input_shapes[i]) for i in range(len(input_shapes)) ] outputs = [ ("output{}".format(i,), output_shapes[i], TensorProto.FLOAT) for i in range(len(output_shapes)) ] node = helper.make_node( op_type, inputs=[i[0] for i in inputs] + [t.name for t in initializer], outputs=[o[0] for o in outputs], **kwargs ) return _onnx_create_model([node], inputs, outputs, initializer)
Example #8
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def test_unsqueeze(): data = np.random.randn(3, 4, 5).astype(np.float32) expected_output = np.expand_dims(data, axis=0) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 4, 5, 1]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 4]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 1, 4, 5]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 2]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
Example #9
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 6 votes |
def test_batch_normalization(self): if legacy_opset_pre_ver(6): raise unittest.SkipTest("Backend doesn't support consumed flag") node_def = helper.make_node("BatchNormalization", ["X", "scale", "bias", "mean", "var"], ["Y"], epsilon=0.001) x_shape = [3, 5, 4, 2] param_shape = [5] _param_shape = [1, 5, 1, 1] x = self._get_rnd_float32(0, 1, shape=x_shape) m = self._get_rnd_float32(0, 1, shape=param_shape) _m = m.reshape(_param_shape) v = self._get_rnd_float32(0, 1, shape=param_shape) _v = v.reshape(_param_shape) scale = self._get_rnd_float32(0, 1, shape=param_shape) _scale = scale.reshape(_param_shape) bias = self._get_rnd_float32(0, 1, shape=param_shape) _bias = bias.reshape(_param_shape) golden = self._batch_normalization(x, _m, _v, _bias, _scale, 0.001) output = run_node(node_def, [x, scale, bias, m, v]) np.testing.assert_almost_equal(output["Y"], golden, decimal=5)
Example #10
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def test_identity(): np.random.seed(133391) shape = [2, 4] input_data = np.random.randn(*shape).astype(np.float32) identity_node = make_node('Identity', inputs=['x'], outputs=['y']) ng_results = run_node(identity_node, [input_data]) assert np.array_equal(ng_results, [input_data]) node1 = make_node('Add', inputs=['A', 'B'], outputs=['add1'], name='add_node1') node2 = make_node('Identity', inputs=['add1'], outputs=['identity1'], name='identity_node1') node3 = make_node('Abs', inputs=['identity1'], outputs=['Y'], name='abs_node1') graph = make_graph([node1, node2, node3], 'test_graph', [make_tensor_value_info('A', onnx.TensorProto.FLOAT, shape), make_tensor_value_info('B', onnx.TensorProto.FLOAT, shape)], [make_tensor_value_info('Y', onnx.TensorProto.FLOAT, shape)]) model = make_model(graph, producer_name='ngraph ONNX Importer') ng_model_function = import_onnx_model(model) runtime = get_runtime() computation = runtime.computation(ng_model_function) ng_results = computation(input_data, input_data) expected_result = np.abs(input_data + input_data) assert np.array_equal(ng_results[0], expected_result)
Example #11
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def test_constant(value_type): values = np.random.randn(5, 5).astype(value_type) node = onnx.helper.make_node( 'Constant', inputs=[], outputs=['values'], value=onnx.helper.make_tensor( name='const_tensor', data_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(value_type)], dims=values.shape, vals=values.flatten())) ng_results = run_node(node, []) assert np.allclose(ng_results, [values]) # See https://github.com/onnx/onnx/issues/1190
Example #12
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 6 votes |
def test_constant_fill(self): if not legacy_opset_pre_ver(9): raise unittest.SkipTest( "ONNX version {} doesn't support ConstantFill.".format( defs.onnx_opset_version())) shape = [1, 2, 3, 4] extra_shape = [5, 6] value = 3. node_def = helper.make_node( "ConstantFill", ["X"], ["Y"], value=value, extra_shape=extra_shape, dtype=1, ) x = self._get_rnd_float32(shape=shape) y = np.zeros(shape + extra_shape) y.fill(value) output = run_node(node_def, [x]) np.testing.assert_equal(output["Y"].dtype, tf.float32) np.testing.assert_equal(output["Y"], y)
Example #13
Source File: test_ops_matmul.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def make_onnx_model_for_gemm_op(input_a, input_b, input_c, **kwargs): input_a_for_output = input_a input_b_for_output = input_b if kwargs.get('transA'): input_a_for_output = input_a.T if kwargs.get('transB'): input_b_for_output = input_b.T output_shape = np.dot(input_a_for_output, input_b_for_output).shape node = make_node('Gemm', ['A', 'B', 'C'], ['Y'], name='test_node', **kwargs) graph = make_graph([node], 'test_graph', [make_tensor_value_info('A', onnx.TensorProto.FLOAT, input_a.shape), make_tensor_value_info('B', onnx.TensorProto.FLOAT, input_b.shape), make_tensor_value_info('C', onnx.TensorProto.FLOAT, input_c.shape)], [make_tensor_value_info('Y', onnx.TensorProto.FLOAT, output_shape)]) model = make_model(graph, producer_name='ngraph ONNXImporter') return model
Example #14
Source File: yolov3_to_onnx.py From iAI with MIT License | 6 votes |
def _make_upsample_node(self, layer_name, layer_dict): """Create an ONNX Upsample node with the properties from the DarkNet-based graph. Keyword arguments: layer_name -- the layer's name (also the corresponding key in layer_configs) layer_dict -- a layer parameter dictionary (one element of layer_configs) """ upsample_factor = float(layer_dict['stride']) previous_node_specs = self._get_previous_node_specs() inputs = [previous_node_specs.name] channels = previous_node_specs.channels assert channels > 0 upsample_node = helper.make_node( 'Upsample', mode='nearest', # For ONNX versions <0.7.0, Upsample nodes accept different parameters than 'scales': scales=[1.0, 1.0, upsample_factor, upsample_factor], inputs=inputs, outputs=[layer_name], name=layer_name, ) self._nodes.append(upsample_node) return layer_name, channels
Example #15
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 6 votes |
def test_dequantize_linear(self): node_def = helper.make_node("DequantizeLinear", ["x", "x_scale", "x_zero_point"], ["y"]) for x, x_zero_point in [[ self._get_rnd_int(-128, 127, [2, 6], np.int8), self._get_rnd_int(-128, 127, dtype=np.int8) ], [ self._get_rnd_int(0, 255, [2, 6], np.uint8), self._get_rnd_int(0, 255, dtype=np.uint8) ], [self._get_rnd_int(-512, 512, [2, 6]), np.int32(0)]]: x_scale = self._get_rnd_float32(-10., 10) y = np.subtract(np.float32(x), np.float32(x_zero_point)) y = np.multiply(y, x_scale) output = run_node(node_def, [x, x_scale, x_zero_point]) np.testing.assert_almost_equal(output["y"], y)
Example #16
Source File: onnx_import_test.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_equal(): """Test for logical greater in onnx operators.""" input1 = np.random.rand(1, 3, 4, 5).astype("float32") input2 = np.random.rand(1, 5).astype("float32") inputs = [helper.make_tensor_value_info("input1", TensorProto.FLOAT, shape=(1, 3, 4, 5)), helper.make_tensor_value_info("input2", TensorProto.FLOAT, shape=(1, 5))] outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=(1, 3, 4, 5))] nodes = [helper.make_node("Equal", ["input1", "input2"], ["output"])] graph = helper.make_graph(nodes, "equal_test", inputs, outputs) greater_model = helper.make_model(graph) bkd_rep = mxnet_backend.prepare(greater_model) numpy_op = np.equal(input1, input2).astype(np.float32) output = bkd_rep.run([input1, input2]) npt.assert_almost_equal(output[0], numpy_op)
Example #17
Source File: onnx_import_test.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_lesser(): """Test for logical greater in onnx operators.""" input1 = np.random.rand(1, 3, 4, 5).astype("float32") input2 = np.random.rand(1, 5).astype("float32") inputs = [helper.make_tensor_value_info("input1", TensorProto.FLOAT, shape=(1, 3, 4, 5)), helper.make_tensor_value_info("input2", TensorProto.FLOAT, shape=(1, 5))] outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=(1, 3, 4, 5))] nodes = [helper.make_node("Less", ["input1", "input2"], ["output"])] graph = helper.make_graph(nodes, "lesser_test", inputs, outputs) greater_model = helper.make_model(graph) bkd_rep = mxnet_backend.prepare(greater_model) numpy_op = np.less(input1, input2).astype(np.float32) output = bkd_rep.run([input1, input2]) npt.assert_almost_equal(output[0], numpy_op)
Example #18
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 5 votes |
def test_conv(self): device = "CUDA" if supports_device("CUDA") else "CPU" N, C, H, W = 4, 3, 5, 5 x_shape = [N, C, H, W] K, kH, kW = 6, 3, 3 weight_shape = [K, C, kH, kW] node_def = helper.make_node("Conv", ["X", "weights"], ["Y"], pads=[1, 1, 1, 1], kernel_shape=[kH, kW]) x = self._get_rnd_float32(shape=x_shape) weights = self._get_rnd_float32(shape=weight_shape) output = run_node(node_def, [x, weights], device=device) out_shape = [N, K, H, W] test_output = np.zeros(out_shape) for n in range(N): for c in range(C): for h in range(H): for w in range(W): for k in range(K): for kh in range(kH): for kw in range(kW): h_in_range = (h - kH // 2 + kh) < H and (h - kH // 2 + kh) >= 0 w_in_range = (w - kW // 2 + kw) < W and (w - kW // 2 + kw) >= 0 if h_in_range and w_in_range: test_output[n][k][h][w] += ( x[n][c][h - kH // 2 + kh][w - kW // 2 + kw] * weights[k][c][kh][kw]) np.testing.assert_almost_equal(output["Y"], test_output, decimal=5)
Example #19
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 5 votes |
def test_constant_of_shape(self): if defs.onnx_opset_version() < 9: raise unittest.SkipTest( "ONNX version {} doesn't support ConstantOfShape.".format( defs.onnx_opset_version())) v = helper.make_tensor("value", TensorProto.FLOAT, [1], [1]) node_def = helper.make_node("ConstantOfShape", ["X"], ["Y"], value=v) x = np.array([4, 3, 2]) output = run_node(node_def, inputs=[x]) np.testing.assert_almost_equal(output["Y"], np.ones(x, dtype=np.float32)) v = helper.make_tensor("value", TensorProto.INT32, [1], [0]) node_def = helper.make_node("ConstantOfShape", ["X"], ["Y"], value=v) x = np.array([10, 6]) output = run_node(node_def, inputs=[x]) np.testing.assert_almost_equal(output["Y"], np.zeros(x, dtype=np.int32))
Example #20
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 5 votes |
def test_compress(self): if legacy_opset_pre_ver(9): raise unittest.SkipTest( "ONNX version {} doesn't support Compress.".format( defs.onnx_opset_version())) axis = 1 node_def = helper.make_node("Compress", inputs=['X', 'condition'], outputs=['Y'], axis=axis) x = self._get_rnd_float32(shape=[5, 5, 5]) cond = np.array([1, 0, 1]) output = run_node(node_def, inputs=[x, cond]) np.testing.assert_almost_equal(output['Y'], np.compress(cond, x, axis=axis))
Example #21
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 5 votes |
def test_abs(self): node_def = helper.make_node("Abs", ["X"], ["Y"]) x = self._get_rnd_float32(shape=[1000]) output = run_node(node_def, [x]) np.testing.assert_almost_equal(output["Y"], np.abs(x))
Example #22
Source File: onnx_converters.py From chainer-compiler with MIT License | 5 votes |
def add_node(self, optype, inputs, outputs, name, **kwargs): inputs_ = [] outputs_ = [] for input in inputs: if isinstance(input, str): inputs_.append(input) elif isinstance(input, ONNXValue): inputs_.append(input.name) elif isinstance(input, values.Value): inputs_.append(value2onnx_parameter[input].onnx_name) else: assert(False) output_values = [] for output in outputs: if isinstance(output, str): outputs_.append(output) elif isinstance(output, ONNXValue): outputs_.append(output.name) elif isinstance(output, values.Value): outputs_.append(value2onnx_parameter[output].onnx_name) elif output is None: o = ONNXValue(self, np.float32, [ name, '/', optype, '/Output'], is_constant=False) output_values.append(o) outputs_.append(o.name) else: assert(False) node = oh.make_node(optype, inputs_, outputs_, name, **kwargs) self.nodes.append(node) return tuple(output_values)
Example #23
Source File: test_node.py From onnx-tensorflow with Apache License 2.0 | 5 votes |
def test_ceil(self): node_def = helper.make_node("Ceil", ["X"], ["Y"]) x = self._get_rnd_float32(shape=[1000]) output = run_node(node_def, [x]) np.testing.assert_almost_equal(output["Y"], np.ceil(x))
Example #24
Source File: onnx_converters.py From chainer-compiler with MIT License | 5 votes |
def convert_node_unary_op(onnx_graph, node: 'nodes.NodeUnaryOp'): if node.unaryop == nodes.UnaryOpType.UAdd: zero_ = ONNXValue(onnx_graph, np.array(0, dtype=np.float), [ node, '/Zero']) onnx_node = oh.make_node( 'Add', [zero_.name, value2onnx_parameter[node.operand].onnx_name], [value2onnx_parameter[node.outputs[0]].onnx_name]) onnx_graph.nodes.append(onnx_node) elif node.unaryop == nodes.UnaryOpType.USub: zero_ = ONNXValue(onnx_graph, np.array(0, dtype=np.float), [ node, '/Zero']) onnx_node = oh.make_node( 'Sub', [zero_.name, value2onnx_parameter[node.operand].onnx_name], [value2onnx_parameter[node.outputs[0]].onnx_name]) onnx_graph.nodes.append(onnx_node) elif node.unaryop == nodes.UnaryOpType.Not: onnx_node = oh.make_node( 'Not', [value2onnx_parameter[node.operand].onnx_name], [value2onnx_parameter[node.outputs[0]].onnx_name]) onnx_graph.nodes.append(onnx_node) else: raise utils.UnimplementedError('{} is not implemented'.format(type(node.unaryop)), node.lineprop)
Example #25
Source File: env.py From chainer-compiler with MIT License | 5 votes |
def addnode(self, *args, **kwargs): node = helper.make_node(*args, **kwargs) node.doc_string = _get_trace_str() self.nodes.append(node)
Example #26
Source File: links.py From chainer-compiler with MIT License | 5 votes |
def call_impl(self, env, x, **kwargs): assert not kwargs # TODO(hamaji): finetune not supported yet. res = new_tensor(['unknown', 'unknown', 'unknown']) env.nodes.append( helper.make_node( "BatchNormalization", inputs=[x.to_tensor(env).name, self.scale.name, self.B.name, self.mean.name, self.var.name], outputs=[res.name], epsilon=self.eps, momentum=self.momentum, # とりあえずspatialは1で(0でも値が変わらなかったのでよくわからん) ) ) return res
Example #27
Source File: links.py From chainer-compiler with MIT License | 5 votes |
def call_impl(self, env, x): res = new_tensor(['unknown', 'unknown', 'unknown']) env.nodes.append( helper.make_node( "Conv", inputs=[x.to_tensor(env).name, self.W.name] + ([] if self.b is None else [self.b.name]), outputs=[res.name], kernel_shape=self.ksize, pads=self.pads, strides=self.stride ) ) return res
Example #28
Source File: test_ops_convpool.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_pool_global_average_3d(ndarray_1x1x4x4): x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4)) node = onnx.helper.make_node('GlobalAveragePool', inputs=['x'], outputs=['y']) y = np.array([18.5], dtype=np.float32).reshape(1, 1, 1, 1, 1) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
Example #29
Source File: test_ops_convpool.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_pool_global_max(ndarray_1x1x4x4): node = onnx.helper.make_node('GlobalMaxPool', inputs=['x'], outputs=['y']) x = ndarray_1x1x4x4 y = np.array([26], dtype=np.float32).reshape(1, 1, 1, 1) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
Example #30
Source File: test_ops_convpool.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_pool_max(ndarray_1x1x4x4): node = onnx.helper.make_node('MaxPool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2)) x = ndarray_1x1x4x4 y = np.array([[16, 18], [24, 26]], dtype=np.float32).reshape(1, 1, 2, 2) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])