本文整理汇总了Python中onnx.numpy_helper.from_array方法的典型用法代码示例。如果您正苦于以下问题:Python numpy_helper.from_array方法的具体用法?Python numpy_helper.from_array怎么用?Python numpy_helper.from_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onnx.numpy_helper
的用法示例。
在下文中一共展示了numpy_helper.from_array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_tensor_impl
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def new_tensor_impl(self, ndarray_, name):
'''
generate a tensor which contains np data
it is for constant input
'''
if not config.float_restrict:
if ndarray_.dtype == np.float64:
ndarray_ = ndarray_.astype(np.float32)
tensor = numpy_helper.from_array(ndarray_, name=name)
dt = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(ndarray_.dtype)]
tensor_value = oh.make_tensor_value_info(name, dt, ndarray_.shape)
self.generator.onnx_tensors[name] = tensor_value
return tensor, tensor_value
示例2: test_conv
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_conv(self): # type: () -> None
kernel_shape = (3, 2)
strides = (2, 3)
pads = (4, 2, 4, 2)
dilations = (1, 2)
group = 1
weight = from_array(_random_array((16, 3, 3, 2)), name="weight")
input_shape = (1, 3, 224, 224)
output_size = _conv_pool_output_size(input_shape, dilations,
kernel_shape, pads, strides)
output_shape = (1, int(weight.dims[0]), output_size[0], output_size[1])
_test_single_node(
"Conv",
[input_shape],
[output_shape],
initializer=[weight],
dilations=dilations,
group=group,
kernel_shape=kernel_shape,
pads=pads,
strides=strides
)
示例3: test_conv_without_pads
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_conv_without_pads(self): # type: () -> None
kernel_shape = (3, 2)
strides = (2, 3)
dilations = (1, 2)
group = 1
weight = from_array(_random_array((16, 3, 3, 2)), name="weight")
input_shape = (1, 3, 224, 224)
output_size = _conv_pool_output_size(input_shape, dilations,
kernel_shape, [0, 0, 0, 0],
strides)
output_shape = (1, int(weight.dims[0]), output_size[0], output_size[1])
_test_single_node(
"Conv",
[input_shape],
[output_shape],
initializer=[weight],
dilations=dilations,
group=group,
kernel_shape=kernel_shape,
strides=strides
)
示例4: test_gemm
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_gemm(self, minimum_ios_deployment_target='12'): # type: () -> None
input_shape = (1, 2048)
output_shape = (1, 5)
W = from_array(
_random_array((output_shape[1], input_shape[1])), name="weight"
)
b = from_array(
_random_array((output_shape[1],)), name="bias"
)
_test_single_node(
"Gemm",
[input_shape],
[output_shape],
initializer=[W, b],
decimal=3,
transB=1,
minimum_ios_deployment_target=minimum_ios_deployment_target
)
示例5: test_conv_without_pads
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_conv_without_pads(self): # type: () -> None
kernel_shape = (3, 2)
strides = (2, 3)
dilations = (1, 2)
group = 1
weight = from_array(_random_array((16, 3, 3, 2)), name="weight")
input_shape = (1, 3, 224, 224)
output_size = _conv_pool_output_size(
input_shape, dilations, kernel_shape, [0, 0, 0, 0], strides
)
output_shape = (1, int(weight.dims[0]), output_size[0], output_size[1])
_test_single_node(
"Conv",
[input_shape],
[output_shape],
initializer=[weight],
dilations=dilations,
group=group,
kernel_shape=kernel_shape,
strides=strides,
)
示例6: test_dropout_remover
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_dropout_remover(self): # type: () -> None
inputs = [("input", (1, 3, 50, 50))]
outputs = [("out", (1, 5, 50, 50), TensorProto.FLOAT)]
weight = numpy_helper.from_array(_random_array((5, 3, 1, 1)), name="weight")
conv = helper.make_node(
"Conv",
inputs=["input", "weight"],
outputs=["conv_output"],
kernel_shape=(1, 1),
strides=(1, 1),
)
drop = helper.make_node(
"Dropout", inputs=["conv_output"], outputs=["drop_output"],
)
exp = helper.make_node("Exp", inputs=["drop_output"], outputs=["out"])
onnx_model = _onnx_create_model([conv, drop, exp], inputs, outputs)
graph = Graph.from_onnx(onnx_model.graph, onnx_ir_version=5)
new_graph = graph.transformed([DropoutRemover()])
self.assertEqual(len(graph.nodes), 3)
self.assertEqual(len(new_graph.nodes), 2)
self.assertEqual(new_graph.nodes[0].inputs[0], "input")
self.assertEqual(new_graph.nodes[1].inputs[0], new_graph.nodes[0].outputs[0])
self.assertEqual(new_graph.nodes[1].outputs[0], "out")
示例7: set_initializer
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def set_initializer(self, tensor_name, tensor_value):
"""Sets the initializer value for tensor with given name."""
graph = self._model_proto.graph
# convert tensor_value (numpy array) into TensorProto w/ correct name
tensor_init_proto = np_helper.from_array(tensor_value)
tensor_init_proto.name = tensor_name
# first, remove if an initializer already exists
init_names = [x.name for x in graph.initializer]
try:
init_ind = init_names.index(tensor_name)
init_old = graph.initializer[init_ind]
graph.initializer.remove(init_old)
except ValueError:
pass
# create and insert new initializer
graph.initializer.append(tensor_init_proto)
# set shape
dtype = tensor_init_proto.data_type
self.set_tensor_shape(tensor_name, list(tensor_value.shape), dtype)
示例8: test_replace_initializer
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def test_replace_initializer(self):
try:
import onnx
from dlpy.model_conversion.onnx_graph import OnnxGraph, OnnxNode
from onnx import numpy_helper
except:
unittest.TestCase.skipTest(self, 'onnx package not found')
graph_ = self._generate_graph1()
graph = OnnxGraph.from_onnx(graph_)
conv1 = np.random.rand(64, 3, 7, 7).astype('float32')
init1 = numpy_helper.from_array(conv1,
name='conv1')
graph.replace_initializer('conv0', init1)
self.assertEqual(len(graph.initializer), 1)
self.assertEqual(graph.initializer[0], init1)
示例9: apply
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def apply(self, node_list):
if self.end_p.is_reserved:
return None, False
n_tensors = self.begin_n.get_precedence_by_idx(1).tensors
reshape_value_0 = numpy_helper.to_array(n_tensors[0]).tolist()
cur_perm = Solution.get_perm(self.end_p.origin)
adjust_reshape = np.array([reshape_value_0[i_] for i_ in cur_perm], dtype=np.int64)
reshape_initilizer = numpy_helper.from_array(adjust_reshape,
name=self.begin_n.origin.name + '_initializer_' + str(
MergeReshapeTransposeSolution.init_number))
MergeReshapeTransposeSolution.init_number += 1
self.begin_n.initializers = [reshape_initilizer]
prev = self.begin_n.get_precedence_by_idx(1)
prev.successor.remove(self.begin_n)
self.begin_n.precedence.remove(prev)
self.begin_n.in_redirect(self.begin_n.get_input_by_idx(1), reshape_initilizer.name)
node_list = Solution.delete_node_nto1(node_list, self.begin_n, self.end_p, self.end)
return node_list, True
示例10: totensor
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def totensor(x, env, dtype=None):
if istensor(x):
assert dtype is None
return x
# We use a scalar false as a None.
# TODO(hamaji): Revisit to check if this decision is OK.
if x is None:
x = False
if type(x) == tuple or type(x) == list:
def f(v):
tv = v.to_tensor(env)
tw = env.calc(
'Unsqueeze',
inputs=[tv.name],
axes=[0]
)
return tw.name
vs = list(map(f, x))
# print(vs)
res = env.calc(
'Concat',
inputs=vs,
axis=0
)
else:
if dtype is None and type(x) == float:
dtype = np.float32
x = np.array(x, dtype=dtype)
res = env.calc(
'Constant',
inputs=[],
value=numpy_helper.from_array(x, name="hoge")
)
return res
示例11: rewrite_onnx_tensor
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def rewrite_onnx_tensor(xtensor, new_type):
value = numpy_helper.to_array(xtensor)
if new_type.shape is not None and value.shape != new_type.shape:
sys.stderr.write('The shape of tensor `%s` was changed from '
'%s to %s and values were randomized\n' %
(xtensor.name, value.shape, new_type.shape))
value = np.random.rand(*new_type.shape)
if new_type.dtype is not None:
value = value.astype(new_type.dtype)
xtensor.CopyFrom(numpy_helper.from_array(value, xtensor.name))
示例12: dump_test_inputs_outputs
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def dump_test_inputs_outputs(inputs, outputs, gradients, test_data_dir):
if not os.path.exists(test_data_dir):
os.makedirs(test_data_dir)
for typ, values in [('input', inputs),
('output', outputs),
('gradient', gradients)]:
for i, (value_info, value) in enumerate(values):
if typ == 'gradient':
name = value_info.name
else:
name = onnx_name(value_info)
if isinstance(value, list):
assert value
digits = len(str(len(value)))
for j, v in enumerate(value):
filename = os.path.join(
test_data_dir,
'%s_%d_%s.pb' % (typ, i, str(j).zfill(digits)))
tensor = numpy_helper.from_array(v, name)
with open(filename, 'wb') as f:
f.write(tensor.SerializeToString())
#value_info.type.CopyFrom(onnx.TypeProto())
#sequence_type = value_info.type.sequence_type
#tensor_type = sequence_type.elem_type.tensor_type
#tensor_type.elem_type = tensor.data_type
else:
filename = os.path.join(test_data_dir,
'%s_%d.pb' % (typ, i))
if value is None:
if get_test_args().allow_unused_params:
continue
raise RuntimeError('Unused parameter: %s' % name)
tensor = numpy_helper.from_array(value, name)
with open(filename, 'wb') as f:
f.write(tensor.SerializeToString())
vi = onnx.helper.make_tensor_value_info(
name, tensor.data_type, tensor.dims)
#value_info.CopyFrom(vi)
示例13: make_constant_node
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def make_constant_node(name, typ, value):
value = np.array(value)
tensor = numpy_helper.from_array(value, name=name + '_val')
node = onnx.helper.make_node('Constant', inputs=[], outputs=[name],
value=tensor)
return node
示例14: gen_test
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def gen_test(graph, inputs, outputs, name):
model = onnx.helper.make_model(graph, producer_name='backend-test')
test_dir = os.path.join('out', name)
test_data_set_dir = os.path.join(test_dir, 'test_data_set_0')
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
os.makedirs(test_data_set_dir)
with open(os.path.join(test_dir, 'model.onnx'), 'wb') as f:
f.write(model.SerializeToString())
for typ, values in [('input', inputs), ('output', outputs)]:
for i, (name, value) in enumerate(values):
if isinstance(value, list):
assert value
digits = len(str(len(value)))
for j, v in enumerate(value):
filename = os.path.join(
test_data_set_dir,
'%s_%d_%s.pb' % (typ, i, str(j).zfill(digits)))
tensor = numpy_helper.from_array(v, name)
with open(filename, 'wb') as f:
f.write(tensor.SerializeToString())
else:
filename = os.path.join(test_data_set_dir,
'%s_%d.pb' % (typ, i))
tensor = numpy_helper.from_array(value, name)
with open(filename, 'wb') as f:
f.write(tensor.SerializeToString())
示例15: make_graph
# 需要导入模块: from onnx import numpy_helper [as 别名]
# 或者: from onnx.numpy_helper import from_array [as 别名]
def make_graph(self):
inputs_vi = [_extract_value_info(a, n)
for n, a in self.inputs + self.params]
outputs_vi = [_extract_value_info(a, n) for n, a in self.outputs]
initializer = []
for name, value in self.params:
typ = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[value.dtype]
tensor = numpy_helper.from_array(value, name=name)
initializer.append(tensor)
graph = onnx.helper.make_graph(self.nodes, self.graph_name,
inputs=inputs_vi, outputs=outputs_vi,
initializer=initializer)
return graph