当前位置: 首页>>代码示例>>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;未经允许,请勿转载。