Python onnx.numpy_helper.to_array() Examples

The following are 30 code examples of onnx.numpy_helper.to_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: converter.py    From onnx2keras with MIT License 6 votes vote down vote up
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} 
Example #2
Source File: _graph.py    From onnx-coreml with MIT License 6 votes vote down vote up
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 
Example #3
Source File: optimizer.py    From onnxconverter-common with MIT License 6 votes vote down vote up
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 #4
Source File: optimizer.py    From onnxconverter-common with MIT License 6 votes vote down vote up
def find(node):
        if node.origin.op_type == 'Reshape' and len(node.successor) == 1 and node.get_precedence_by_idx(1) is not None:
            n_tensors = node.get_precedence_by_idx(1).tensors
            if len(n_tensors) > 0:
                reshape_value_0 = numpy_helper.to_array(n_tensors[0]).tolist()
                next = node.successor[0]
                if next.origin is not None:
                    if next.origin.op_type == 'Reshape' and next.get_precedence_by_idx(1) is not None:
                        next_tensors = next.get_precedence_by_idx(1).tensors
                        if len(next_tensors) > 0:
                            reshape_value_1 = numpy_helper.to_array(next_tensors[0]).tolist()
                            if _is_good_for_match_shape(reshape_value_0, reshape_value_1):
                                solution = Solution(node.get_precedence_by_idx(0), node, next, next)
                                return solution
                    elif next.origin.op_type == 'Transpose' and len(next.successor) == 1:
                        cur_perm = Solution.get_perm(next.origin)
                        reshape_ones = np.count_nonzero(np.array(reshape_value_0) == 1)
                        if reshape_value_0[0] == 0 and cur_perm[0] == 0 and reshape_ones + 2 == len(reshape_value_0):
                            solution = MergeReshapeTransposeSolution(node.get_precedence_by_idx(0), node, next,
                                                                     next.successor[0])
                            return solution

        return None 
Example #5
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
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() 
Example #6
Source File: _opt_const_folding.py    From onnxconverter-common with MIT License 6 votes vote down vote up
def calculate(self, node):
        func_name = '_On' + node.op_type
        func = type(self).__dict__.get(func_name, None)
        if func is None:
            return None

        inputs = []
        for ts_ in node.input:
            if ts_ in self.initializers:
                inputs.append(numpy_helper.to_array(self.initializers[ts_]))
            elif ts_ in self.variables:
                inputs.append(self.variables[ts_])
            else:
                return None

        output_values = func(self, node, inputs)
        for idx_, ots_ in enumerate(node.output):
            self.add_value_of_node(ots_, output_values[idx_])

        return output_values 
Example #7
Source File: sas_onnx_parse.py    From python-dlpy with Apache License 2.0 6 votes vote down vote up
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] 
Example #8
Source File: constant_of_shape.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
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)
    ] 
Example #9
Source File: check_model.py    From OLive with MIT License 6 votes vote down vote up
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 
Example #10
Source File: onnx_converter.py    From CrypTen with MIT License 6 votes vote down vote up
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) 
Example #11
Source File: onnx.py    From utensor_cgen with Apache License 2.0 6 votes vote down vote up
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] 
Example #12
Source File: _graph.py    From onnx2caffe with MIT License 6 votes vote down vote up
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)) 
Example #13
Source File: onnx_test_parser.py    From deep500 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)) 
Example #14
Source File: test_basic_onnx_exec.py    From finn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #15
Source File: modelwrapper.py    From finn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #16
Source File: test_brevitas_fc.py    From finn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #17
Source File: test_streamline_fc.py    From finn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #18
Source File: test_xnorpopcountmatmul.py    From finn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #19
Source File: optimizer.py    From onnxconverter-common with MIT License 5 votes vote down vote up
def _get_pad_from_Pad(node):
    if len(node.origin.input) == 1:
        pads = node.get_attribute('pads')
    else:
        pad_tensor = node.get_precedence_by_idx(1)
        if pad_tensor is None:
            pads = numpy_helper.to_array(node.initializers[0]).tolist()
        else:
            pads = numpy_helper.to_array(node.get_precedence_by_idx(1).tensors[0]).tolist()
    return pads 
Example #20
Source File: test_end2end_tfc_w2a2.py    From finn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_end2end_tfc_w2a2_run_on_pynq():
    # use the streamlined model as the "golden" model for right answers
    golden = ModelWrapper(build_dir + "/end2end_tfc_w2a2_streamlined.onnx")
    iname = golden.graph.input[0].name
    oname = golden.graph.output[0].name
    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)
    x = nph.to_array(input_tensor)
    # x = np.zeros(ishape, dtype=np.float32)
    # run using FINN-based execution
    ret_golden = execute_onnx(golden, {iname: x}, True)
    y_golden = ret_golden[oname]
    # set up parent+child graph to test
    # we'll use models from the previous step as the child model
    parent_model = ModelWrapper(build_dir + "/end2end_tfc_w2a2_dataflow_parent.onnx")
    iname = parent_model.graph.input[0].name
    oname = parent_model.graph.output[0].name
    try:
        ip = os.environ["PYNQ_IP"]  # NOQA
        if ip == "":
            pytest.skip("PYNQ board IP address not specified")
        # produce results with cppsim
        sdp_node = parent_model.get_nodes_by_op_type("StreamingDataflowPartition")[0]
        sdp_node = getCustomOp(sdp_node)
        sdp_node.set_nodeattr("model", build_dir + "/end2end_tfc_w2a2_pynq_deploy.onnx")
        ret = execute_onnx(parent_model, {iname: x}, True)
        y = ret[oname]
        assert np.isclose(y, y_golden).all()

    except KeyError:
        pytest.skip("PYNQ board IP address not specified") 
Example #21
Source File: onnx_transforms.py    From python-dlpy with Apache License 2.0 5 votes vote down vote up
def run_transform(self, graph, node):
        tensor = numpy_helper.to_array(node.attrs['value'])
        graph.tensor_dict[node.output[0]] = tensor

        # remove the constant op
        graph.remove_node(node.name)
        
        return graph 
Example #22
Source File: onnx_graph.py    From python-dlpy with Apache License 2.0 5 votes vote down vote up
def __init__(self, graph_def):
        self.name = graph_def.name
        self.node = [OnnxNode(n) for n in graph_def.node]
        self.value_info = list(graph_def.value_info)
        self.input = list(graph_def.input)
        self.output = list(graph_def.output)
        self.initializer = list(graph_def.initializer)
        self.tensor_dict = dict([(init.name, numpy_helper.to_array(init))
                                  for init in graph_def.initializer])
        self.uninitialized = [i for i in graph_def.input
                              if i.name not in self.tensor_dict] 
Example #23
Source File: test_fold_constants.py    From finn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_const_folding():
    raw_m = get_data("finn", "data/onnx/mnist-conv/model.onnx")
    model = ModelWrapper(raw_m)
    model = model.transform(InferShapes())
    model = model.transform(FoldConstants())
    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)
    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() 
Example #24
Source File: _opt_const_folding.py    From onnxconverter-common with MIT License 5 votes vote down vote up
def _OnConstant(self, node, inputs):
        return [numpy_helper.to_array(OnnxGraphContext.get_attribute(node, 'value'))] 
Example #25
Source File: _opt_const_folding.py    From onnxconverter-common with MIT License 5 votes vote down vote up
def _is_initializer_existed(intlzer, initializers):
    val = numpy_helper.to_array(intlzer)
    for i_ in initializers:
        if intlzer.name == i_.name:
            if np.allclose(val, numpy_helper.to_array(i_)):
                return True

    return False 
Example #26
Source File: import_onnx.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def _parse_array(self, tensor_proto):
        """Grab data in TensorProto and convert to numpy array."""
        try:
            from onnx.numpy_helper import to_array
        except ImportError:
            raise ImportError("Onnx and protobuf need to be installed. "
                              + "Instructions to install - https://github.com/onnx/onnx")
        np_array = to_array(tensor_proto).reshape(tuple(tensor_proto.dims))
        return nd.array(np_array) 
Example #27
Source File: test_batchnorm_to_affine.py    From finn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_batchnorm_to_affine_lfc_w1a1():
    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())
    new_model = model.transform(BatchNormToAffine())
    # 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)
    input_dict = {"0": nph.to_array(input_tensor)}
    assert oxe.compare_execution(model, new_model, input_dict)
    os.remove(export_onnx_path) 
Example #28
Source File: test_renaming.py    From finn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_renaming():
    # load the onnx model
    raw_m = get_data("finn", "data/onnx/mnist-conv/model.onnx")
    model = ModelWrapper(raw_m)
    model = model.transform(InferShapes())
    model = model.transform(GiveUniqueNodeNames())
    model = model.transform(GiveReadableTensorNames())
    # do some basic checks
    assert model.graph.input[0].name == "global_in"
    assert model.graph.output[0].name == "global_out"
    assert model.graph.node[1].op_type == "Conv"
    assert model.graph.node[1].name == "Conv_0"
    assert model.graph.node[1].input[1] == "Conv_0_param0"
    assert model.graph.node[6].op_type == "Add"
    assert model.graph.node[6].name == "Add_1"
    assert model.graph.node[6].input[1] == "Add_1_param0"
    # ensure running renaming twice still yields the same names
    model = model.transform(GiveUniqueNodeNames())
    model = model.transform(GiveReadableTensorNames())
    assert model.graph.node[1].op_type == "Conv"
    assert model.graph.node[1].name == "Conv_0"
    assert model.graph.node[1].input[1] == "Conv_0_param0"
    assert model.graph.node[6].op_type == "Add"
    assert model.graph.node[6].name == "Add_1"
    assert model.graph.node[6].input[1] == "Add_1_param0"
    # run renamed model to make sure we did not mess up the topology
    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)
    input_dict = {"global_in": 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["global_out"], atol=1e-3
    ).all() 
Example #29
Source File: onnx.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def get_numpy(tensor_proto):
    """Grab data in TensorProto and convert to numpy array."""
    try:
        from onnx.numpy_helper import to_array
    except ImportError as e:
        raise ImportError(
            "Unable to import onnx which is required {}".format(e))
    return to_array(tensor_proto) 
Example #30
Source File: onnx_test.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def get_test_files(name):
    """Extract tar file and returns model path and input, output data"""
    tar_name = download(URLS.get(name), dirname=CURR_PATH.__str__())
    # extract tar file
    tar_path = os.path.join(CURR_PATH, tar_name)
    tar = tarfile.open(tar_path.__str__(), "r:*")
    tar.extractall(path=CURR_PATH.__str__())
    tar.close()
    data_dir = os.path.join(CURR_PATH, name)
    model_path = os.path.join(data_dir, 'model.onnx')

    inputs = []
    outputs = []
    # get test files
    for test_file in os.listdir(data_dir):
        case_dir = os.path.join(data_dir, test_file)
        # skip the non-dir files
        if not os.path.isdir(case_dir):
            continue
        input_file = os.path.join(case_dir, 'input_0.pb')
        input_tensor = TensorProto()
        with open(input_file, 'rb') as proto_file:
            input_tensor.ParseFromString(proto_file.read())
        inputs.append(numpy_helper.to_array(input_tensor))

        output_tensor = TensorProto()
        output_file = os.path.join(case_dir, 'output_0.pb')
        with open(output_file, 'rb') as proto_file:
            output_tensor.ParseFromString(proto_file.read())
        outputs.append(numpy_helper.to_array(output_tensor))

    return model_path, inputs, outputs