Python onnx.helper() Examples
The following are 30
code examples of onnx.helper().
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
, or try the search function
.
Example #1
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def test_hardsigmoid(): def hardsigmoid(data, alpha=float(0.2), beta=float(0.5)): return np.clip(alpha * data + beta, 0, 1) np.random.seed(133391) alpha = np.random.rand() beta = np.random.rand() data = np.random.rand(3, 4, 5).astype(np.float32) expected = hardsigmoid(data, alpha, beta) node = onnx.helper.make_node('HardSigmoid', inputs=['x'], outputs=['y'], alpha=alpha, beta=beta) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) expected = hardsigmoid(data) node = onnx.helper.make_node('HardSigmoid', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
Example #2
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 #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: 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 #5
Source File: test_ops_convpool.py From ngraph-python 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 = convert_and_calculate(node, [x], [y]) 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 = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #6
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 #7
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_softsign(): def softsign(x): return x / (1 + np.abs(x)) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node('Softsign', inputs=['x'], outputs=['y']) expected = softsign(data) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
Example #8
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_constant_err(): values = np.random.randn(5, 5).astype(np.float16) 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(np.float16)], dims=values.shape, vals=values.flatten())) ng_results = run_node(node, []) assert np.allclose(ng_results, [values])
Example #9
Source File: test_ngraph_backend.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_run_node(): input_data = _get_input_data([2, 3, 4, 5]) node = onnx.helper.make_node('Abs', inputs=['x'], outputs=['y']) ng_results = NgraphBackend.run_node(node, input_data) expected = np.abs(input_data) assert np.array_equal(ng_results, expected)
Example #10
Source File: test_ops_convpool.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_pool_average_3d(ndarray_1x1x4x4): x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4)) node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2, 2), strides=(2, 2, 2)) y = np.array([[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]], dtype=np.float32).reshape(1, 1, 2, 2, 2) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
Example #11
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])
Example #12
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 #13
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_softmax(): def softmax_2d(x): max_x = np.max(x, axis=1).reshape((-1, 1)) exp_x = np.exp(x - max_x) return exp_x / np.sum(exp_x, axis=1).reshape((-1, 1)) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y'], axis=0) expected = softmax_2d(data.reshape(1, 60)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y'], axis=1) expected = softmax_2d(data.reshape(3, 20)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) # default axis is 1 node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y'], axis=2) expected = softmax_2d(data.reshape(12, 5)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y'], axis=-1) expected = softmax_2d(data.reshape(12, 5)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) with pytest.raises(RuntimeError): node = onnx.helper.make_node('Softmax', inputs=['x'], outputs=['y'], axis=3) ng_results = run_node(node, [data])
Example #14
Source File: test_ops_convpool.py From ngraph-python with Apache License 2.0 | 5 votes |
def test_padding(): node = onnx.helper.make_node('Pad', inputs=['x'], outputs=['y'], pads=[1, 1, 1, 1]) x = np.ones((2, 2), dtype=np.float32) y = np.pad(x, pad_width=1, mode='constant') ng_results = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y]) node = onnx.helper.make_node('Pad', inputs=['x'], outputs=['y'], mode='constant', pads=[0, 0, 1, 3, 0, 0, 2, 4]) x = np.random.randn(1, 3, 4, 5).astype(np.float32) y = np.pad(x, pad_width=((0, 0), (0, 0), (1, 2), (3, 4)), mode='constant') ng_results = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #15
Source File: test_ops_convpool.py From ngraph-python with Apache License 2.0 | 5 votes |
def test_pool_average_3d(ndarray_1x1x4x4): x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4)) node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2, 2), strides=(2, 2, 2)) y = np.array([[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]], dtype=np.float32).reshape(1, 1, 2, 2, 2) ng_results = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #16
Source File: test_ops_convpool.py From ngraph-python 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 = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #17
Source File: test_ops_convpool.py From ngraph-python 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 = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #18
Source File: test_ops_convpool.py From ngraph-python 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 = convert_and_calculate(node, [x], [y]) assert np.array_equal(ng_results, [y])
Example #19
Source File: test_xnorpopcountmatmul.py From finn with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_xnorpopcountmatmul(): M = 1 K = 3 N = 3 x = helper.make_tensor_value_info("x", TensorProto.FLOAT, [M, K]) W = helper.make_tensor_value_info("W", TensorProto.FLOAT, [K, N]) out = helper.make_tensor_value_info("out", TensorProto.FLOAT, ["x", "y"]) node_def = helper.make_node( "XnorPopcountMatMul", ["x", "W"], ["out"], domain="finn" ) modelproto = helper.make_model( helper.make_graph([node_def], "test_model", [x], [out], value_info=[W]) ) model = ModelWrapper(modelproto) model.set_tensor_datatype("x", DataType.BINARY) model.set_tensor_datatype("W", DataType.BINARY) W_data = np.asarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32) model.set_initializer("W", W_data) # test shape inference model = model.transform(InferShapes()) assert model.get_tensor_shape("out") == [M, N] # test datatype inference assert model.get_tensor_datatype("out") is DataType.FLOAT32 model = model.transform(InferDataTypes()) assert model.get_tensor_datatype("out") is DataType.UINT32 # test execution x_data = np.asarray([[1, 0, 0]], dtype=np.float32) inp_dict = {"x": x_data} out_dict = oxe.execute_onnx(model, inp_dict) Wb = 2 * W_data - 1 xb = 2 * x_data - 1 rb = np.matmul(xb, Wb) assert (2 * out_dict["out"] - K == rb).all()
Example #20
Source File: test_operators.py From onnx-fb-universe with MIT License | 5 votes |
def assertONNXExpected(self, binary_pb, subname=None): model_def = onnx.ModelProto.FromString(binary_pb) onnx.checker.check_model(model_def) # doc_string contains stack trace in it, strip it onnx.helper.strip_doc_string(model_def) self.assertExpected(google.protobuf.text_format.MessageToString(model_def, float_format='.15g'), subname) return model_def
Example #21
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_exp(input_data): input_data = input_data.astype(np.float32) expected_output = np.exp(input_data) node = onnx.helper.make_node('Exp', inputs=['x'], outputs=['y']) ng_results = run_node(node, [input_data]) assert np.allclose(ng_results, [expected_output])
Example #22
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_reshape_opset5(): original_shape = [2, 3, 4] test_cases = { 'reordered_dims': np.array([4, 2, 3], dtype=np.int64), 'reduced_dims': np.array([3, 8], dtype=np.int64), 'extended_dims': np.array([3, 2, 2, 2], dtype=np.int64), 'one_dim': np.array([24], dtype=np.int64), 'negative_dim': np.array([6, -1, 2], dtype=np.int64), } input_data = np.random.random_sample(original_shape).astype(np.float32) for test_name, shape in test_cases.items(): const_node = make_node('Constant', inputs=[], outputs=['const_shape'], value=onnx.helper.make_tensor( name='const_tensor', data_type=onnx.TensorProto.INT64, dims=shape.shape, vals=shape.flatten())) reshape_node = onnx.helper.make_node('Reshape', inputs=['data', 'const_shape'], outputs=['reshaped']) graph = make_graph([const_node, reshape_node], 'test_graph', [make_tensor_value_info('data', onnx.TensorProto.FLOAT, input_data.shape)], [make_tensor_value_info('reshaped', onnx.TensorProto.FLOAT, ())]) model = make_model(graph, producer_name='ngraph ONNX Importer') model.opset_import[0].version = 5 ng_model_function = import_onnx_model(model) runtime = get_runtime() computation = runtime.computation(ng_model_function) ng_results = computation(input_data) expected_output = np.reshape(input_data, shape) assert np.array_equal(ng_results[0], expected_output)
Example #23
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_reshape_opset5_param_err(): original_shape = [2, 3, 4] output_shape = np.array([4, 2, 3], dtype=np.int64) input_data = np.random.random_sample(original_shape).astype(np.float32) reshape_node = onnx.helper.make_node('Reshape', inputs=['x', 'y'], outputs=['z']) ng_result = run_node(reshape_node, [input_data, output_shape], opset_version=5) assert ng_result[0].shape == output_shape
Example #24
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_flatten(axis, expected_output): data = np.arange(120).reshape(2, 3, 4, 5) node = onnx.helper.make_node('Flatten', inputs=['x'], outputs=['y'], axis=axis) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
Example #25
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_flatten_exception(): data = np.arange(120).reshape(2, 3, 4, 5) node = onnx.helper.make_node('Flatten', inputs=['x'], outputs=['y'], axis=5) with pytest.raises(RuntimeError): run_node(node, [data])
Example #26
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_squeeze(): data = np.arange(6).reshape(1, 2, 3, 1) expected_output = data.reshape(2, 3) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[0, 3]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) data = np.random.randn(1, 3, 4, 5).astype(np.float32) expected_output = np.squeeze(data, axis=0) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
Example #27
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_split_1d(): # 1D data = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['z', 'w'], axis=0) expected_outputs = [np.array([1., 2., 3.]).astype(np.float32), np.array([4., 5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z', 'w'], axis=0, split=[2, 3, 1]) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5.]).astype(np.float32), np.array([6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) # Default values data = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z', 'w']) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z'], split=[2, 4]) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs)
Example #28
Source File: test_reshape.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_depth_to_space(): b, c, h, w = shape = (2, 8, 3, 3) blocksize = 2 data = np.random.random_sample(shape).astype(np.float32) tmp = np.reshape(data, [b, blocksize, blocksize, c // (blocksize ** 2), h, w]) tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2]) expected_output = np.reshape(tmp, [b, c // (blocksize ** 2), h * blocksize, w * blocksize]) node = onnx.helper.make_node('DepthToSpace', inputs=['x'], outputs=['y'], blocksize=blocksize) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) # (1, 4, 2, 3) input tensor data = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23]]]]).astype(np.float32) # (1, 1, 4, 6) output tensor expected_output = np.array([[[[0, 6, 1, 7, 2, 8], [12, 18, 13, 19, 14, 20], [3, 9, 4, 10, 5, 11], [15, 21, 16, 22, 17, 23]]]]).astype(np.float32) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
Example #29
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_sqrt(input_data): input_data = input_data.astype(np.float32) expected_output = np.sqrt(input_data) node = onnx.helper.make_node('Sqrt', inputs=['x'], outputs=['y']) ng_results = run_node(node, [input_data]) assert np.allclose(ng_results, [expected_output])
Example #30
Source File: test_ops_unary.py From ngraph-onnx with Apache License 2.0 | 5 votes |
def test_softplus(): def softplus(x): return np.log(np.exp(x) + 1) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node('Softplus', inputs=['x'], outputs=['y']) expected = softplus(data) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])