當前位置: 首頁>>代碼示例>>Python>>正文


Python onnx.helper方法代碼示例

本文整理匯總了Python中onnx.helper方法的典型用法代碼示例。如果您正苦於以下問題:Python onnx.helper方法的具體用法?Python onnx.helper怎麽用?Python onnx.helper使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在onnx的用法示例。


在下文中一共展示了onnx.helper方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_unsqueeze

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:18,代碼來源:test_reshape.py

示例2: test_hardsigmoid

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:21,代碼來源:test_ops_unary.py

示例3: test_constant

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:19,代碼來源:test_ops_unary.py

示例4: test_pool_average

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:18,代碼來源:test_ops_convpool.py

示例5: test_pool_average

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-python,代碼行數:19,代碼來源:test_ops_convpool.py

示例6: test_reshape

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
def test_reshape():
    input_data = np.arange(2560).reshape(16, 4, 4, 10)
    reshape_node = onnx.helper.make_node('Reshape', inputs=['x'], outputs=['y'], shape=(256, 10))
    expected_output = input_data.reshape(256, 10)

    ng_results = run_node(reshape_node, [input_data], opset_version=4)
    assert np.array_equal(ng_results, [expected_output]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:9,代碼來源:test_reshape.py

示例7: test_reshape_opset5

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:35,代碼來源:test_reshape.py

示例8: test_reshape_opset5_param_err

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:9,代碼來源:test_reshape.py

示例9: test_flatten

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:7,代碼來源:test_reshape.py

示例10: test_flatten_exception

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:8,代碼來源:test_reshape.py

示例11: test_squeeze

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:15,代碼來源:test_reshape.py

示例12: test_split_1d

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:35,代碼來源:test_reshape.py

示例13: test_depth_to_space

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:31,代碼來源:test_reshape.py

示例14: test_sqrt

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:8,代碼來源:test_ops_unary.py

示例15: test_exp

# 需要導入模塊: import onnx [as 別名]
# 或者: from onnx import helper [as 別名]
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]) 
開發者ID:NervanaSystems,項目名稱:ngraph-onnx,代碼行數:8,代碼來源:test_ops_unary.py


注:本文中的onnx.helper方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。