本文整理汇总了Python中onnx.numpy_helper.to_array方法的典型用法代码示例。如果您正苦于以下问题:Python numpy_helper.to_array方法的具体用法?Python numpy_helper.to_array怎么用?Python numpy_helper.to_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onnx.numpy_helper
的用法示例。
在下文中一共展示了numpy_helper.to_array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _convertAttributeProto
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def _convertAttributeProto(onnx_arg): # type: (AttributeProto) -> AttributeValue
"""
Convert an ONNX AttributeProto into an appropriate Python object
for the type.
NB: Tensor attribute gets returned as numpy array
"""
if onnx_arg.HasField('f'):
return onnx_arg.f
elif onnx_arg.HasField('i'):
return onnx_arg.i
elif onnx_arg.HasField('s'):
return onnx_arg.s
elif onnx_arg.HasField('t'):
return numpy_helper.to_array(onnx_arg.t)
elif len(onnx_arg.floats):
return list(onnx_arg.floats)
elif len(onnx_arg.ints):
return list(onnx_arg.ints)
elif len(onnx_arg.strings):
return list(onnx_arg.strings)
else:
return None
示例2: __init__
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def __init__(self, name=None, graph_proto=None):
if graph_proto:
self._name = graph_proto.name
self._inputs_proto = list(graph_proto.input)
self._outputs_proto = list(graph_proto.output)
self._nodes_proto = list(graph_proto.node)
self._consts_proto = list(graph_proto.initializer)
self._value_info_proto = list(graph_proto.value_info)
self._consts = dict([(init.name, numpy_helper.to_array(init))
for init in graph_proto.initializer])
else:
self._name = name or ""
self._inputs_proto = []
self._outputs_proto = []
self._nodes_proto = []
self._consts = {}
self._consts_proto = []
self._value_info_proto = []
# Either way, data_type_cast_map is empty when initialized.
self._data_type_cast_map = {}
self._add_utility_constants()
示例3: version_9
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def version_9(cls, node, **kwargs):
attrs = copy.deepcopy(node.attrs)
shape = kwargs["tensor_dict"][node.inputs[0]]
# make sure the shape dtype is either int32 or int64
if shape.dtype not in [tf.int64, tf.int32]:
shape = tf.cast(shape, tf.int64)
# the default value is 0, float32
if "value" in node.attrs:
attr_value = node.attrs["value"]
value = numpy_helper.to_array(attr_value)
attrs["value"] = value[0]
else:
attrs["value"] = 0.
return [
cls.make_tensor_from_onnx_node(
node, inputs=[shape], attrs=attrs, **kwargs)
]
示例4: _get_attribute_value
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def _get_attribute_value(attr):
"""
Retrieves value from attribute in ONNX graph.
"""
if attr.HasField("f"): # floating-point attribute
return attr.f
elif attr.HasField("i"): # integer attribute
return attr.i
elif attr.HasField("s"): # string attribute
return attr.s # TODO: Sanitize string.
elif attr.HasField("t"): # tensor attribute
return torch.from_numpy(numpy_helper.to_array(attr.t))
elif len(attr.ints) > 0:
return list(attr.ints)
elif len(attr.floats) > 0:
return list(attr.floats)
raise ValueError("Unknown attribute type for attribute %s." % attr.name)
示例5: _convertAttributeProto
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def _convertAttributeProto(onnx_arg): # type: (AttributeProto) -> AttributeValue
"""
Convert an ONNX AttributeProto into an appropriate Python object
for the type.
NB: Tensor attribute gets returned as numpy array
"""
if onnx_arg.HasField('f'):
return onnx_arg.f
elif onnx_arg.HasField('i'):
return onnx_arg.i
elif onnx_arg.HasField('s'):
return onnx_arg.s
elif onnx_arg.HasField('t'):
return numpy_helper.to_array(onnx_arg.t)
elif len(onnx_arg.floats):
return list(onnx_arg.floats)
elif len(onnx_arg.ints):
return list(onnx_arg.ints)
elif len(onnx_arg.strings):
return list(onnx_arg.strings)
else:
raise ValueError("Unsupported ONNX attribute: {}".format(onnx_arg))
示例6: make_empty_exec_context
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def make_empty_exec_context(self):
"""Creates an empty execution context for this model.
The execution context is a dictionary of all tensors used for the
inference computation. Any initializer values will be taken into
account, all other tensors will be zero."""
execution_context = dict()
graph = self._model_proto.graph
# make empty tensors for all the graph inputs and outputs
for vi in graph.input:
new_tensor = onnxutil.valueinfo_to_tensor(vi)
execution_context[vi.name] = new_tensor
for vi in graph.output:
new_tensor = onnxutil.valueinfo_to_tensor(vi)
execution_context[vi.name] = new_tensor
# make empty tensors for all intermediate buffers
for vi in graph.value_info:
new_tensor = onnxutil.valueinfo_to_tensor(vi)
execution_context[vi.name] = new_tensor
# fill in the constants provided by the initializers (TensorProto to npy)
for t in graph.initializer:
execution_context[t.name] = np_helper.to_array(t)
return execution_context
示例7: test_brevitas_fc_onnx_export_and_exec
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def test_brevitas_fc_onnx_export_and_exec(size, wbits, abits):
if size == "LFC" and wbits == 2 and abits == 2:
pytest.skip("No LFC-w2a2 present at the moment")
if wbits > abits:
pytest.skip("No wbits > abits cases at the moment")
nname = "%s_%dW%dA" % (size, wbits, abits)
finn_onnx = export_onnx_path + "/%s.onnx" % nname
fc = get_test_model_trained(size, wbits, abits)
bo.export_finn_onnx(fc, (1, 1, 28, 28), finn_onnx)
model = ModelWrapper(finn_onnx)
model = model.transform(InferShapes())
model = model.transform(FoldConstants())
# load one of the test vectors
raw_i = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/input_0.pb")
input_tensor = onnx.load_tensor_from_string(raw_i)
# run using FINN-based execution
input_dict = {"0": nph.to_array(input_tensor)}
output_dict = oxe.execute_onnx(model, input_dict)
produced = output_dict[list(output_dict.keys())[0]]
# run using PyTorch/Brevitas
input_tensor = torch.from_numpy(nph.to_array(input_tensor)).float()
assert input_tensor.shape == (1, 1, 28, 28)
# do forward pass in PyTorch/Brevitas
expected = fc.forward(input_tensor).detach().numpy()
assert np.isclose(produced, expected, atol=1e-3).all()
示例8: test_convert_bipolar_matmul_to_xnorpopcountmatmul
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def test_convert_bipolar_matmul_to_xnorpopcountmatmul():
lfc = get_test_model_trained("LFC", 1, 1)
bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path)
model = ModelWrapper(export_onnx_path)
model = model.transform(InferShapes())
model = model.transform(FoldConstants())
model = model.transform(GiveUniqueNodeNames())
model = model.transform(GiveReadableTensorNames())
model = model.transform(ConvertSignToThres())
# load one of the test vectors
raw_i = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/input_0.pb")
input_tensor = onnx.load_tensor_from_string(raw_i)
# run using FINN-based execution
input_dict = {"global_in": nph.to_array(input_tensor)}
expected_ctx = oxe.execute_onnx(model, input_dict, True)
expected = expected_ctx[model.graph.output[0].name]
model = model.transform(ConvertBipolarMatMulToXnorPopcount())
produced_ctx = oxe.execute_onnx(model, input_dict, True)
produced = produced_ctx[model.graph.output[0].name]
assert np.isclose(expected, produced, atol=1e-3).all()
os.remove(export_onnx_path)
示例9: test_streamline_fc
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def test_streamline_fc(size, wbits, abits):
if size == "LFC" and wbits == 2 and abits == 2:
pytest.skip("No LFC-w2a2 present at the moment")
if wbits > abits:
pytest.skip("No wbits > abits cases at the moment")
nname = "%s_%dW%dA" % (size, wbits, abits)
finn_onnx = export_onnx_path + "/%s.onnx" % nname
fc = get_test_model_trained(size, wbits, abits)
bo.export_finn_onnx(fc, (1, 1, 28, 28), finn_onnx)
model = ModelWrapper(finn_onnx)
model = model.transform(InferShapes())
model = model.transform(FoldConstants())
model = model.transform(GiveUniqueNodeNames())
model = model.transform(GiveReadableTensorNames())
# load one of the test vectors
raw_i = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/input_0.pb")
input_tensor = onnx.load_tensor_from_string(raw_i)
# run using FINN-based execution
input_dict = {"global_in": nph.to_array(input_tensor)}
expected_ctx = oxe.execute_onnx(model, input_dict, True)
expected = expected_ctx[model.graph.output[0].name]
model = model.transform(Streamline())
produced_ctx = oxe.execute_onnx(model, input_dict, True)
produced = produced_ctx[model.graph.output[0].name]
assert np.isclose(expected, produced, atol=1e-3).all()
示例10: test_mnist_onnx_download_extract_run
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def test_mnist_onnx_download_extract_run():
# load the onnx model
raw_m = get_data("finn", "data/onnx/mnist-conv/model.onnx")
model = ModelWrapper(raw_m)
model = model.transform(InferShapes())
# load one of the test vectors
raw_i = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/input_0.pb")
raw_o = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/output_0.pb")
input_tensor = onnx.load_tensor_from_string(raw_i)
output_tensor = onnx.load_tensor_from_string(raw_o)
# run using FINN-based execution
input_dict = {"Input3": np_helper.to_array(input_tensor)}
output_dict = oxe.execute_onnx(model, input_dict)
assert np.isclose(
np_helper.to_array(output_tensor), output_dict["Plus214_Output_0"], atol=1e-3
).all()
示例11: _load_protobuf_data
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def _load_protobuf_data(self, model_dir,
data_sets: List[OnnxTestData]):
for test_data_dir in glob.glob(os.path.join(model_dir, "test_data_set*")):
inputs = {}
outputs = {}
inputs_num = len(glob.glob(os.path.join(test_data_dir, 'input_*.pb')))
for i in range(inputs_num):
input_file = os.path.join(test_data_dir, 'input_{}.pb'.format(i))
tensor = onnx.TensorProto()
with open(input_file, 'rb') as f:
tensor.ParseFromString(f.read())
inputs[tensor.name] = numpy_helper.to_array(tensor)
ref_outputs_num = len(glob.glob(os.path.join(test_data_dir, 'output_*.pb')))
for i in range(ref_outputs_num):
output_file = os.path.join(test_data_dir, 'output_{}.pb'.format(i))
tensor = onnx.TensorProto()
with open(output_file, 'rb') as f:
tensor.ParseFromString(f.read())
outputs[tensor.name] = numpy_helper.to_array(tensor)
data_sets.append(OnnxTestData(inputs, outputs))
示例12: _onnx_initializer_to_input_dict_items
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def _onnx_initializer_to_input_dict_items(initializer):
""" Convert ONNX graph initializer to input dict items.
:param initializer: ONNX graph initializer, list of TensorProto.
:return: List of input dict items.
"""
def tensor2list(onnx_tensor):
# Use the onnx.numpy_helper because the data may be raw
return numpy_helper.to_array(onnx_tensor).flatten().tolist()
return [(init.name,
tf.constant(
tensor2list(init),
shape=init.dims,
dtype=onnx2tf(init.data_type)))
for init in initializer]
示例13: gen_input_list
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def gen_input_list(input_path, isPytorch=False):
inputs = []
i = 0
print(input_path)
full_path = os.path.join(input_path, "input_%s.pb" % i)
while os.path.isfile(full_path):
if isPytorch:
inputs.append(torch.tensor(numpy_helper.to_array(readInputFromFile(full_path))))
else:
inputs.append(numpy_helper.to_array(readInputFromFile(full_path)))
i += 1
full_path = os.path.join(input_path, "input_%s.pb" % i)
if len(inputs) == 1:
return inputs[0]
else:
return inputs
示例14: onnx_initializer_to_tensors
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def onnx_initializer_to_tensors(initializer):
'''
Convert ONNX graph initializer to tensors
Parameters
----------
initializer : list of ONNX TensorProto
Specifies the initializer of the graph.
Returns
-------
list of numpy.ndarray
'''
return [(init.name,
numpy_helper.to_array(init))
for init in initializer]
示例15: onnx_node_attributes_to_dict
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import to_array [as 别名]
def onnx_node_attributes_to_dict(args):
"""
Parse ONNX attributes to Python dictionary
:param args: ONNX attributes object
:return: Python dictionary
"""
def onnx_attribute_to_dict(onnx_attr):
"""
Parse ONNX attribute
:param onnx_attr: ONNX attribute
:return: Python data type
"""
if onnx_attr.HasField('t'):
return numpy_helper.to_array(getattr(onnx_attr, 't'))
for attr_type in ['f', 'i', 's']:
if onnx_attr.HasField(attr_type):
return getattr(onnx_attr, attr_type)
for attr_type in ['floats', 'ints', 'strings']:
if getattr(onnx_attr, attr_type):
return list(getattr(onnx_attr, attr_type))
return {arg.name: onnx_attribute_to_dict(arg) for arg in args}