Python onnx.numpy_helper.from_array() Examples
The following are 30
code examples of onnx.numpy_helper.from_array().
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.numpy_helper
, or try the search function
.
Example #1
Source File: test_operators.py From coremltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
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, )
Example #2
Source File: test_transformers.py From coremltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
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")
Example #3
Source File: onnx_converters.py From chainer-compiler with MIT License | 6 votes |
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
Example #4
Source File: optimizer.py From onnxconverter-common with MIT License | 6 votes |
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
Example #5
Source File: test_onnx_graph.py From python-dlpy with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: operators_test.py From onnx-coreml with MIT License | 6 votes |
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 )
Example #7
Source File: modelwrapper.py From finn with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #8
Source File: operators_test.py From onnx-coreml with MIT License | 6 votes |
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 )
Example #9
Source File: operators_test.py From onnx-coreml with MIT License | 6 votes |
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 )
Example #10
Source File: graph.py From kaldi-onnx with Apache License 2.0 | 5 votes |
def make_const(self, name, np_val): onnx_tensor = numpy_helper.from_array(np_val, name) self.add_initializer(onnx_tensor)
Example #11
Source File: onnx_graph.py From python-dlpy with Apache License 2.0 | 5 votes |
def make_onnx(self): ''' Generate ONNX model from current graph ''' self.clean_init() nodes = [] for node in self.node: n = NodeProto() n.input.extend(node.input) n.output.extend(node.output) n.name = node.name n.op_type = node.op_type n.attribute.extend( helper.make_attribute(key, value) for key, value in sorted(node.attrs.items()) ) nodes.append(n) inputs = [] initializer = [] for k,v in self.tensor_dict.items(): init = numpy_helper.from_array(v, name=k) initializer.append(init) value_info = helper.make_tensor_value_info( name=k, elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[v.dtype], shape=list(v.shape) ) inputs.append(value_info) graph_ = helper.make_graph( nodes=nodes, name='dlpy_graph', inputs=inputs+self.uninitialized, outputs=self.output, initializer=initializer ) model = helper.make_model(graph_) return model
Example #12
Source File: graph.py From kaldi-onnx with Apache License 2.0 | 5 votes |
def kaldi_to_onnx_tensor(tensor, name=""): onnx_tensor = numpy_helper.from_array(tensor, name=name) return onnx_tensor
Example #13
Source File: _builtin.py From keras-onnx with MIT License | 5 votes |
def convert_tf_scatter_nd(scope, operator, container): if operator.target_opset < 11: raise ValueError("ScatterNd op is not supported for opset = " + str(operator.target_opset)) else: oopb = OnnxOperatorBuilder(container, scope) node = operator.raw_operator const_shape_dtype = _to_onnx_type(node.inputs[2].dtype) if const_shape_dtype != oopb.int64: const_of_shape_input = oopb.apply_cast(operator.inputs[2].full_name, to=oopb.int64, name=operator.full_name + '_const_of_shape_input') else: const_of_shape_input = [operator.inputs[2].full_name] np_val = np.array([0], dtype=np.int64) onnx_tensor = numpy_helper.from_array(np_val, operator.inputs[2].full_name + '_value') const_of_shape = oopb.add_node('ConstantOfShape', const_of_shape_input, operator.inputs[2].full_name + '_const_of_shape', value=onnx_tensor) node_input_0_dtype = _to_onnx_type(node.inputs[0].dtype) if node_input_0_dtype != oopb.int64: node_input_0_cast = oopb.apply_cast(operator.inputs[0].full_name, to=oopb.int64, name=operator.full_name + '_input_0') else: node_input_0_cast = [operator.inputs[0].full_name] oopb.add_node_with_output('ScatterND', [const_of_shape] + node_input_0_cast + [operator.inputs[1].full_name], operator.outputs[0].full_name, name=operator.full_name + '_scatter_nd')
Example #14
Source File: _builtin.py From keras-onnx with MIT License | 5 votes |
def convert_tf_const(scope, operator, container): node = operator.raw_operator np_arr = _cal_tensor_value(node.outputs[0]) onnx_tensor = numpy_helper.from_array(np_arr, operator.outputs[0].onnx_name) container.add_initializer_from_tensor(onnx_tensor)
Example #15
Source File: export.py From onnx-chainer with MIT License | 5 votes |
def convert_parameter(parameter, context): if isinstance(parameter, chainer.Parameter): array = parameter.array elif isinstance(parameter, chainer.Variable): array = parameter.array elif isinstance(parameter, chainer.get_array_types()): array = parameter else: raise ValueError( 'The type of parameter is unknown. It should be either Parameter ' 'or Variable or ndarray, but the type was {}.'.format( type(parameter))) array = chainer.cuda.to_cpu(array) tensor = numpy_helper.from_array(array, context.get_name(parameter)) return tensor
Example #16
Source File: context.py From onnx-chainer with MIT License | 5 votes |
def _tensor_from_array_for_constant(array, name): tensor = numpy_helper.from_array(array, name=name) # Avoid `raw_data` for better debuggability. This would be OK # since constants are usually small. field_name = onnx.mapping.STORAGE_TENSOR_TYPE_TO_FIELD.get( tensor.data_type, None) if field_name is not None: tensor.ClearField('raw_data') getattr(tensor, field_name)[:] = array.flatten().tolist() return tensor
Example #17
Source File: test_operators.py From coremltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
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, )
Example #18
Source File: create_input.py From OLive with MIT License | 5 votes |
def create_tensor(name, dims, tensor_name, path, data_type=np.float32, vals=None): if vals is None: vals = np.random.random_sample(dims).astype(data_type) tensor = numpy_helper.from_array(vals) tensor.name = tensor_name with open(os.path.join(path, name), 'wb') as f: f.write(tensor.SerializeToString())
Example #19
Source File: test_operators.py From onnx-fb-universe with MIT License | 5 votes |
def assertONNX(self, f, args, params=tuple(), **kwargs): if isinstance(f, nn.Module): m = f else: m = FuncModule(f, params) onnx_model_pb = export_to_string(m, args, **kwargs) model_def = self.assertONNXExpected(onnx_model_pb) if _onnx_test: test_function = inspect.stack()[1][0].f_code.co_name test_name = test_function[0:4] + "_operator" + test_function[4:] output_dir = os.path.join(test_onnx_common.pytorch_operator_dir, test_name) # Assume: # 1) the old test should be delete before the test. # 2) only one assertONNX in each test, otherwise will override the data. assert not os.path.exists(output_dir), "{} should not exist!".format(output_dir) os.makedirs(output_dir) with open(os.path.join(output_dir, "model.onnx"), 'wb') as file: file.write(model_def.SerializeToString()) data_dir = os.path.join(output_dir, "test_data_set_0") os.makedirs(data_dir) if isinstance(args, Variable): args = (args,) for index, var in enumerate(flatten(args)): tensor = numpy_helper.from_array(var.data.numpy()) with open(os.path.join(data_dir, "input_{}.pb".format(index)), 'wb') as file: file.write(tensor.SerializeToString()) outputs = m(*args) if isinstance(outputs, Variable): outputs = (outputs,) for index, var in enumerate(flatten(outputs)): tensor = numpy_helper.from_array(var.data.numpy()) with open(os.path.join(data_dir, "output_{}.pb".format(index)), 'wb') as file: file.write(tensor.SerializeToString())
Example #20
Source File: helper.py From dragon with BSD 2-Clause "Simplified" License | 5 votes |
def from_tensor(tensor, ws): """Return a tensor proto from the existing tensor.""" return from_array(fetch_tensor(tensor, ws), tensor)
Example #21
Source File: utils.py From chainer-compiler with MIT License | 5 votes |
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
Example #22
Source File: test_onnx_transforms.py From python-dlpy with Apache License 2.0 | 5 votes |
def test_init_unsqueeze(self): try: import onnx from dlpy.model_conversion.onnx_transforms import (Transformer, OpTypePattern, ConstToInitializer, InitReshape, InitUnsqueeze, FuseMulAddBN) from dlpy.model_conversion.onnx_graph import OnnxGraph from onnx import helper, numpy_helper except: unittest.TestCase.skipTest(self, 'onnx package not found') unsqueeze_1 = helper.make_node( 'Unsqueeze', inputs=['unsqueeze_in'], outputs=['unsqueeze_out'], name='Unsqueeze_1', axes=[1, 2] ) unsqueeze_in = np.random.rand(64).astype('float32') init = numpy_helper.from_array(unsqueeze_in, name='unsqueeze_in') graph_ = helper.make_graph( nodes=[unsqueeze_1], name='', inputs=[], outputs=[], initializer=[init] ) graph = OnnxGraph.from_onnx(graph_) graph = InitUnsqueeze()(graph) self.assertEqual(len(graph.node), 0) self.assertTrue('unsqueeze_out' in graph.tensor_dict) t = graph.tensor_dict['unsqueeze_out'] t = np.squeeze(t) self.assertTrue(np.array_equal(unsqueeze_in, t))
Example #23
Source File: test_onnx_transforms.py From python-dlpy with Apache License 2.0 | 5 votes |
def test_init_reshape(self): try: import onnx from dlpy.model_conversion.onnx_transforms import (Transformer, OpTypePattern, ConstToInitializer, InitReshape, InitUnsqueeze, FuseMulAddBN) from dlpy.model_conversion.onnx_graph import OnnxGraph from onnx import helper, numpy_helper except: unittest.TestCase.skipTest(self, 'onnx package not found') reshape_1 = helper.make_node( 'Reshape', inputs=['reshape_in', 'shape'], outputs=['reshape_out'], name='Reshape_1' ) reshape_in = np.random.rand(64).astype('float32') shape = np.array([8, 8]) init = numpy_helper.from_array(reshape_in, name='reshape_in') shape_init = numpy_helper.from_array(shape, name='shape') graph_ = helper.make_graph( nodes=[reshape_1], name='', inputs=[], outputs=[], initializer=[init, shape_init] ) graph = OnnxGraph.from_onnx(graph_) graph = InitReshape()(graph) self.assertEqual(len(graph.node), 0) self.assertTrue('reshape_out' in graph.tensor_dict) t = graph.tensor_dict['reshape_out'] self.assertEqual(t.shape, (8, 8))
Example #24
Source File: test_onnx_graph.py From python-dlpy with Apache License 2.0 | 5 votes |
def _generate_graph1(self): try: from onnx import helper, numpy_helper, TensorProto from dlpy.model_conversion.onnx_graph import OnnxGraph, OnnxNode except: unittest.TestCase.skipTest(self, 'onnx package not found') input0 = helper.make_tensor_value_info('data0', TensorProto.FLOAT, [1, 3, 224, 224]) input1 = helper.make_tensor_value_info('conv0', TensorProto.FLOAT, [64, 3, 7, 7]) output0 = helper.make_tensor_value_info('output0', TensorProto.FLOAT, [1, 64, 122, 122]) conv_op = helper.make_node('Conv', inputs=['data0', 'conv0'], outputs=['output0'], kernel_shape=[7, 7], pads=[3, 3, 3, 3], strides=[2, 2]) conv0 = np.random.rand(64, 3, 7, 7).astype('float32') init0 = numpy_helper.from_array(conv0, name='conv0') graph = helper.make_graph( nodes=[conv_op], name='', inputs=[input0, input1], outputs=[output0], initializer=[init0] ) return graph
Example #25
Source File: optimizer.py From onnxconverter-common with MIT License | 5 votes |
def _update_broadcast_from_initializers(node, init_pred_value, cur_perm, init_idx): for axis_ in range(len(cur_perm) - len(init_pred_value.shape)): init_pred_value = np.expand_dims(init_pred_value, axis=axis_) init_pred_value = np.transpose(init_pred_value, tuple(_get_reverse_perm(cur_perm))) add_initilizer = numpy_helper.from_array(init_pred_value, name=node.origin.name + '_initializer_' + str( PushTransposeSolution.transpose_number)) PushTransposeSolution.transpose_number += 1 node.initializers = [add_initilizer] prev = node.get_precedence_by_idx(init_idx) prev.successor.remove(node) node.precedence.remove(prev) node.in_redirect(node.get_input_by_idx(init_idx), add_initilizer.name) return node
Example #26
Source File: optimizer.py From onnxconverter-common with MIT License | 5 votes |
def _process_transpose_pad(node, node_list, node_transpose_pass_name, cur_perm_map): if len(node.origin.input) == 1: pads_value = node.get_attribute('pads') else: pad_tensor = node.get_precedence_tensor_by_idx(1) pads_value = numpy_helper.to_array(pad_tensor).tolist() cur_perm = cur_perm_map[node.get_precedence_by_idx(0).unique_name] target_perm = _get_reverse_perm(cur_perm) target_perm_shift = [perm_ + len(target_perm) for perm_ in target_perm] reshape_perm = target_perm + target_perm_shift pads_value = np.asarray([pads_value[reshape_perm[idx_]] for idx_ in range(len(reshape_perm))], dtype=np.int64) add_initilizer = numpy_helper.from_array(pads_value, name=node.origin.name + '_initializer_' + str( PushTransposeSolution.transpose_number)) if len(node.origin.input) == 1: node.attributes['pads'] = pads_value.tolist() else: PushTransposeSolution.transpose_number += 1 node.initializers = [add_initilizer] pred_1 = node.get_precedence_by_idx(1) if pred_1 is not None: node.precedence.remove(pred_1) node.in_redirect(node.get_input_by_idx(1), add_initilizer.name) cur_perm_map[node.unique_name] = cur_perm return cur_perm_map
Example #27
Source File: optimizer.py From onnxconverter-common with MIT License | 5 votes |
def _process_transpose_slice(node, node_list, node_transpose_pass_name, cur_perm_map): cur_perm = cur_perm_map[node.get_precedence_by_idx(0).unique_name] add_initilizer = numpy_helper.from_array(np.asarray(cur_perm).astype(np.int64), name=node.origin.name + '_initializer_' + str( PushTransposeSolution.transpose_number)) PushTransposeSolution.transpose_number += 1 node.initializers = [add_initilizer] node.precedence.remove(node.get_precedence_by_idx(3)) node.in_redirect(node.get_input_by_idx(3), add_initilizer.name) cur_perm_map[node.unique_name] = cur_perm return cur_perm_map
Example #28
Source File: test_operators.py From coremltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bn(self): # type: () -> None scale = from_array(_random_array((3,)), name="scale") bias = from_array(_random_array((3,)), name="bias") mean = from_array(_random_array((3,)), name="mean") var = from_array(_random_array((3,)), name="var") epsilon = 1e-5 momentum = 0.001 op_types = ["BatchNormalization", "SpatialBN"] for op_type in op_types: _test_single_node( "BatchNormalization", [(1, 3, 224, 224)], [(1, 3, 224, 224)], initializer=[scale, bias, mean, var], epsilon=epsilon, momentum=momentum, ) # epsilon by default _test_single_node( "BatchNormalization", [(1, 3, 224, 224)], [(1, 3, 224, 224)], initializer=[scale, bias, mean, var], # epsilon=epsilon, momentum=momentum, )
Example #29
Source File: operators_test.py From onnx-coreml with MIT License | 5 votes |
def test_bn(self): # type: () -> None scale = from_array(_random_array((3,)), name="scale") bias = from_array(_random_array((3,)), name="bias") mean = from_array(_random_array((3,)), name="mean") var = from_array(_random_array((3,)), name="var") epsilon = 1e-5 momentum = 0.001 op_types = ["BatchNormalization", "SpatialBN"] for op_type in op_types: _test_single_node( "BatchNormalization", [(1, 3, 224, 224)], [(1, 3, 224, 224)], initializer=[scale, bias, mean, var], epsilon=epsilon, momentum=momentum ) # epsilon by default _test_single_node( "BatchNormalization", [(1, 3, 224, 224)], [(1, 3, 224, 224)], initializer=[scale, bias, mean, var], # epsilon=epsilon, momentum=momentum )
Example #30
Source File: operators_test.py From onnx-coreml with MIT License | 5 votes |
def test_conv_transpose(self): # type: () -> None kernel_shape = (3, 3) pads = (0, 0, 0, 0) C_in = 3 C_out = 12 H_in, W_in = 30, 30 strides = (2, 2) input_shape = (1, C_in, H_in, W_in) weight = from_array(_random_array((C_in, C_out, kernel_shape[0], kernel_shape[1])), name="weight") H_out = (H_in-1) * strides[0] + kernel_shape[0] - pads[0] - pads[2] W_out = (W_in-1) * strides[1] + kernel_shape[1] - pads[1] - pads[3] output_shape = (1, C_out, H_out, W_out) _test_single_node( "ConvTranspose", [input_shape], [output_shape], initializer=[weight], # Default values for other attributes: dilations=[1, 1], group=1 strides = strides, kernel_shape=kernel_shape, pads=pads, output_padding=(0, 0) )