Python onnx.NodeProto() Examples

The following are 27 code examples of onnx.NodeProto(). 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 , or try the search function .
Example #1
Source File: onnx_graph.py    From python-dlpy with Apache License 2.0 6 votes vote down vote up
def __init__(self, node):
        '''
        Create OnnxNode from NodeProto

        Parameters
        ----------
        node : NodeProto

        Returns
        -------
        :class:`OnnxNode` object

        '''
        self.name = str(node.name)
        self.op_type = str(node.op_type)
        self.domain = str(node.domain)
        self.attrs = dict([(attr.name,
                            _convert_onnx_attribute_proto(attr))
                           for attr in node.attribute])
        self.input = list(node.input)
        self.output = list(node.output)
        self.node_proto = node
        self.parents = []
        self.children = []
        self.tensors = {} 
Example #2
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
               node=None,
               name=None,
               inputs=None,
               outputs=None,
               attr=None,
               domain=None,
               op_type=None):
    # storing a reference to the original protobuf object
    if node is None:
      self.node = None
      self.name = name or ""
      self.inputs = inputs or []
      self.attr = attr or {}
      self.domain = domain or ""
      self.op_type = op_type or ""
      self.outputs = outputs or self.get_outputs_names()
    elif isinstance(node, (OnnxNode, NodeProto)):
      self._load_onnx_node(node)
    elif isinstance(node, NodeDef):
      self._load_tf_node(node) 
Example #3
Source File: net_drawer.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def GetOpNodeProducer(embed_docstring=False, **kwargs):  # type: (bool, **Any) -> _NodeProducer
    def ReallyGetOpNode(op, op_id):  # type: (NodeProto, int) -> pydot.Node
        if op.name:
            node_name = '%s/%s (op#%d)' % (op.name, op.op_type, op_id)
        else:
            node_name = '%s (op#%d)' % (op.op_type, op_id)
        for i, input in enumerate(op.input):
            node_name += '\n input' + str(i) + ' ' + input
        for i, output in enumerate(op.output):
            node_name += '\n output' + str(i) + ' ' + output
        node = pydot.Node(node_name, **kwargs)
        if embed_docstring:
            url = _form_and_sanitize_docstring(op.doc_string)
            node.set_URL(url)
        return node
    return ReallyGetOpNode 
Example #4
Source File: optimizer_test.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def _make_fake_if_op(self,
                         true_nodes,  # type: Sequence[NodeProto]
                         false_nodes,  # type: Sequence[NodeProto]
                         output_types  # type: Sequence[Tuple[TensorProto.DataType, Sequence[int], Text]]
                         ):  # type: (...) -> List[NodeProto]
        true = helper.make_tensor("condition", TensorProto.BOOL, (), [True])
        true_graph = helper.make_graph(true_nodes, "true_graph", [], [])
        false_graph = helper.make_graph(false_nodes, "false_graph", [], [])
        if_inputs = ["condition"]
        if_outputs = [name for _, _, name in output_types]
        retval_nodes = [
            helper.make_node("Constant", [], ["condition"], value=true),
            helper.make_node("If", if_inputs, if_outputs, then_branch=true_graph,
                             else_branch=false_graph)
        ]
        return retval_nodes

    # fn is a function that takes a single node as argument 
Example #5
Source File: optimizer_test.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def test_nop_transpose(self):  # type: () -> None
        nodes = [helper.make_node("Transpose", ["X"], ["Y"], perm=[0, 1])]
        nodes.extend(self._make_fake_loop_op(
            [helper.make_node("Transpose", ["_Y"], ["_Y2"], perm=[0, 1])],
            [(TensorProto.FLOAT, (2, 3), "Y")],
            [(TensorProto.FLOAT, (2, 3), "Y2")]))
        graph = helper.make_graph(
            nodes,
            "test",
            [helper.make_tensor_value_info("X", TensorProto.FLOAT, (2, 3))],
            [helper.make_tensor_value_info("Y", TensorProto.FLOAT, (2, 3)),
             helper.make_tensor_value_info("Y2", TensorProto.FLOAT, (2, 3))])
        optimized_model = self._optimized(graph, ["eliminate_nop_transpose"])

        def check_transpose(node):  # type: (NodeProto) -> None
            assert node.op_type != "Transpose"
        self._visit_all_nodes_recursive(optimized_model.graph, check_transpose)
        # Use of the output from the Transpose node in the main graph should
        # have been replaced with the input to the identity node
        assert len(optimized_model.graph.output) == 2
        assert optimized_model.graph.output[0].name == "X"
        # Use of the output from the Transpose node in the loop graph should
        # have been replaced with the input to that identity node
        assert len(optimized_model.graph.node[2].attribute[0].g.output) == 2
        assert optimized_model.graph.node[2].attribute[0].g.output[1].name == "_Y" 
Example #6
Source File: _opt_const_folding.py    From onnxconverter-common with MIT License 5 votes vote down vote up
def _dfs_calc(graph, node, reserved_names, node_status):
    # type: (OnnxGraphContext, onnx.NodeProto, frozenset, dict) -> int
    if node.name in node_status:
        return node_status[node.name]

    if len(node.input) == 0:
        assert node.op_type in ['Constant', 'RandomNormal', 'RandomUniform'], \
            "Assume only the generator operation node hasn't any inputs"
        status = -1
        if node.op_type == 'Constant':
            graph.calculate(node)
            node_status[node.name] = 0
        return status
    else:
        calc_status = [0] * len(node.input)
        for idx_, ts_ in enumerate(node.input):
            if ts_ in graph.initializers or ts_ in graph.variables:
                calc_status[idx_] = -1 if ts_ in reserved_names else 0
            elif ts_ not in graph.tensor_to_node:  # input of graph
                calc_status[idx_] = -1
            else:
                calc_status[idx_] = _dfs_calc(graph, graph.tensor_to_node[ts_], reserved_names, node_status)

        status_up = max(calc_status)
        status_low = min(calc_status)
        if status_low < 0:
            status = - max(-status_low, abs(status_up)) - 1
        else:
            status = status_up + 1

        node_status[node.name] = status
        if status > 0:
            if any(o_ in reserved_names for o_ in node.output):
                status = -status
            else:
                graph.calculate(node)
        return status 
Example #7
Source File: backend.py    From ngraph-onnx with Apache License 2.0 5 votes vote down vote up
def run_node(cls,
                 node,  # type: onnx.NodeProto
                 inputs,  # type: List[np.ndarray]
                 device='CPU',  # type: Text
                 outputs_info=None,  # type: Optional[Sequence[Tuple[np.dtype, Tuple[int, ...]]]]
                 **kwargs  # type: Any
                 ):  # type: (...) -> List[Any]
        """Prepare and run a computation on an ONNX node."""
        # default values for input/output tensors
        input_tensor_types = [np_dtype_to_tensor_type(node_input.dtype) for node_input in inputs]
        output_tensor_types = [onnx.TensorProto.FLOAT for idx in range(len(node.output))]
        output_tensor_shapes = [()]  # type: List[Tuple[int, ...]]

        if outputs_info is not None:
            output_tensor_types = [np_dtype_to_tensor_type(dtype) for (dtype, shape) in
                                   outputs_info]
            output_tensor_shapes = [shape for (dtype, shape) in outputs_info]

        input_tensors = [make_tensor_value_info(name, tensor_type, value.shape)
                         for name, value, tensor_type in zip(node.input, inputs,
                                                             input_tensor_types)]
        output_tensors = [make_tensor_value_info(name, tensor_type, shape)
                          for name, shape, tensor_type in zip(node.output, output_tensor_shapes,
                                                              output_tensor_types)]

        graph = make_graph([node], 'compute_graph', input_tensors, output_tensors)
        model = make_model(graph, producer_name='NgraphBackend')
        if 'opset_version' in kwargs:
            model.opset_import[0].version = kwargs['opset_version']
        return cls.prepare(model, device).run(inputs) 
Example #8
Source File: onnx_graph.py    From python-dlpy with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: backend.py    From dragon with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def run_node(cls, node, inputs, device='CUDA:0', **kwargs):
        """Build and run a TensorRT engine from the onnx node.

        Parameters
        ----------
        node : onnx.NodeProto
            The onnx node.
        inputs : Union[Sequence, Dict]
            The input arrays.
        device : str, optional
            The executing device.

        Returns
        -------
        namedtuple
            The model outputs.

        """
        super(ONNXBackend, cls).run_node(node, inputs, device)
        model = onnx_helper.make_model_from_node(node, inputs, use_weights=True)
        try:
            results = cls.prepare(model, device).run(inputs[:1])
        except RuntimeError:
            model = onnx_helper.make_model_from_node(node, inputs, use_weights=False)
            results = cls.prepare(model, device).run(inputs)
        return results 
Example #10
Source File: onnx_helper.py    From onnx-chainer with MIT License 5 votes vote down vote up
def nodes(self, output_names=None):
        """Returns all nodes created so far.

        Args:
            output_names (list of str): The names of output values to be set at
                the last node.

        Returns:
            A list of `onnx.NodeProto` objects, suitable as the return
            value of converter functions.
        """
        if output_names is not None:
            assert len(self._nodes[-1].output) == len(output_names)
            self._nodes[-1].output[:] = output_names
        return tuple(self._nodes) 
Example #11
Source File: onnx_helper.py    From onnx-chainer with MIT License 5 votes vote down vote up
def make_node(*args, **kwargs):
    """A thin wrapper of `onnx.helper.make_node`.

    Node name will be assigned automatically.

    Args:
        *args (tuple): ONNX node parameters of the node
        **kwargs (dict): ONNX attributes of the node.
    Returns:
        An `onnx.NodeProto` object.
    """
    return onnx.helper.make_node(*args, name=get_func_name(), **kwargs) 
Example #12
Source File: _graph.py    From onnx2caffe with MIT License 5 votes vote down vote up
def from_onnx(node):  # type: (NodeProto) -> Node
        attrs = Attributes.from_onnx(node.attribute)
        name = Text(node.name)
        if len(name) == 0:
            name = "_".join(node.output)
        return Node(
            name, node.op_type, attrs, list(node.input), list(node.output)
        ) 
Example #13
Source File: _test_utils.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _onnx_create_model(
    nodes,  # type: Sequence[NodeProto]
    inputs,  # type: Sequence[Tuple[Text,Tuple[int, ...]]]
    outputs,  # type: Sequence[Tuple[Text,Tuple[int, ...], int]]
    initializer=[],  # type: Sequence[TensorProto]
):
    # type: (...) -> ModelProto
    initializer_inputs = [
        helper.make_tensor_value_info(t.name, TensorProto.FLOAT, t.dims)
        for t in initializer
    ]

    graph = helper.make_graph(
        nodes=nodes,
        name="test",
        inputs=initializer_inputs
        + [
            helper.make_tensor_value_info(input_[0], TensorProto.FLOAT, input_[1])
            for input_ in inputs
        ],
        outputs=[
            helper.make_tensor_value_info(output_[0], output_[2], output_[1])
            for output_ in outputs
        ],
        initializer=initializer,
    )
    onnx_model = helper.make_model(graph)
    return onnx_model 
Example #14
Source File: onnx_helper.py    From chainer with MIT License 5 votes vote down vote up
def nodes(self, output_names=None):
        """Returns all nodes created so far.

        Args:
            output_names (list of str): The names of output values to be set at
                the last node.

        Returns:
            A list of `onnx.NodeProto` objects, suitable as the return
            value of converter functions.
        """
        if output_names is not None:
            assert len(self._nodes[-1].output) == len(output_names)
            self._nodes[-1].output[:] = output_names
        return tuple(self._nodes) 
Example #15
Source File: onnx_helper.py    From chainer with MIT License 5 votes vote down vote up
def make_node(*args, **kwargs):
    """A thin wrapper of `onnx.helper.make_node`.

    Node name will be assigned automatically.

    Args:
        *args (tuple): ONNX node parameters of the node
        **kwargs (dict): ONNX attributes of the node.
    Returns:
        An `onnx.NodeProto` object.
    """
    return onnx.helper.make_node(*args, name=get_func_name(), **kwargs) 
Example #16
Source File: shape_inference_test.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def _make_graph(self,
                    seed_values,  # type: Sequence[Union[Text, Tuple[Text, TensorProto.DataType, Any]]]
                    nodes,  # type: List[NodeProto]
                    value_info,  # type: List[ValueInfoProto]
                    initializer=None  # type: Optional[Sequence[TensorProto]]
                    ):  # type: (...) -> GraphProto
        if initializer is None:
            initializer = []
        names_in_initializer = set(x.name for x in initializer)
        input_value_infos = []
        # If the starting values are not also initializers,
        # introduce the starting values as the output of reshape,
        # so that the sizes are guaranteed to be unknown
        for seed_value in seed_values:
            if isinstance(seed_value, tuple):
                seed_name = seed_value[0]
                seed_value_info = make_tensor_value_info(*seed_value)
            else:
                seed_name = seed_value
                seed_value_info = make_empty_tensor_value_info(seed_value)

            if seed_name in names_in_initializer:
                input_value_infos.append(seed_value_info)
            else:
                value_info.append(seed_value_info)
                input_value_infos.append(make_tensor_value_info('SEED_' + seed_name, TensorProto.UNDEFINED, ()))
                input_value_infos.append(make_tensor_value_info('UNKNOWN_SHAPE_' + seed_name, TensorProto.UNDEFINED, ()))
                nodes[:0] = [make_node("Reshape", ['SEED_' + seed_name, 'UNKNOWN_SHAPE_' + seed_name], [seed_name])]
        return helper.make_graph(nodes, "test", input_value_infos, [], initializer=initializer, value_info=value_info) 
Example #17
Source File: optimizer_test.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def _visit_all_nodes_recursive(self, graph, fn):  # type: (GraphProto, Callable[[NodeProto], None]) -> None
        for node in graph.node:
            fn(node)
            for attr in node.attribute:
                if attr.g is not None:
                    self._visit_all_nodes_recursive(attr.g, fn)
                if len(attr.graphs):
                    for gr in attr.graphs:
                        self._visit_all_nodes_recursive(gr, fn) 
Example #18
Source File: optimizer_test.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def _make_fake_loop_op(self,
                           body_nodes,  # type: Sequence[NodeProto]
                           input_types,  # type: Sequence[Tuple[TensorProto.DataType, Sequence[int], Text]]
                           output_types  # type: Sequence[Tuple[TensorProto.DataType, Sequence[int], Text]]
                           ):  # type: (...) -> List[NodeProto]
        zero = helper.make_tensor("trip_count_value", TensorProto.INT32, (), [10])
        true = helper.make_tensor("condition", TensorProto.BOOL, (), [True])
        # lcd is a dummy loop-carried dependency that only exists because
        # right now the schema checker is broken and assumes a variadic
        # input needs at least one value.
        graph_inputs = [helper.make_tensor_value_info("i", TensorProto.INT32, ()),
                        helper.make_tensor_value_info("cond", TensorProto.BOOL, ())]
        for type, shape, name in input_types:
            graph_inputs.append(helper.make_tensor_value_info("_" + name, type, shape))
        graph_outputs = [helper.make_tensor_value_info("cond", TensorProto.BOOL, ())]
        for type, shape, name in output_types:
            graph_outputs.append(helper.make_tensor_value_info("_" + name, type, shape))
        body_graph = helper.make_graph(body_nodes, "body_graph", graph_inputs,
                                       graph_outputs)
        loop_inputs = ["trip_count", "condition"]
        loop_inputs.extend([name for _, _, name in input_types])
        # TODO: fix checker to accept 0-input variadic inputs
        if len(loop_inputs) == 2:
            loop_inputs.append("")
        loop_outputs = [name for _, _, name in output_types]
        retval_nodes = [
            helper.make_node("Constant", [], ["trip_count"], value=zero),
            helper.make_node("Constant", [], ["condition"], value=true),
            helper.make_node("Loop", loop_inputs, loop_outputs, body=body_graph)
        ]
        return retval_nodes 
Example #19
Source File: checker.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def check_node():  # type: () -> None
    parser = argparse.ArgumentParser('check-node')
    parser.add_argument('node_pb', type=argparse.FileType('rb'))
    args = parser.parse_args()

    node = NodeProto()
    node.ParseFromString(args.node_pb.read())
    checker.check_node(node) 
Example #20
Source File: backend.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def run_node(cls, onnx_node, inputs, device='CPU'):
        # type: (onnx.NodeProto, List[numpy.ndarray], str) -> List[numpy.ndarray]
        input_tensors = [make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape)
                         for name, value in zip(onnx_node.input, inputs)]
        output_tensors = [make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape)
                          for name, value in zip(onnx_node.output, ())]

        graph = make_graph([onnx_node], 'compute_graph', input_tensors, output_tensors)
        model = make_model(graph, producer_name='NgraphBackend')
        return cls.prepare(model).run(inputs) 
Example #21
Source File: model_wrappers.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, onnx_proto_instance, graph):  # type: (onnx.NodeProto, GraphWrapper) -> None
        super(NodeWrapper, self).__init__(onnx_proto_instance, graph)
        self.input = [self._graph.get_input(input_name) for input_name in self._proto.input]
        self.output = [self._graph.get_input(output_name) for output_name in self._proto.output]
        self.attribute = [AttributeWrapper(attr, self._graph) for attr in self._proto.attribute] 
Example #22
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 5 votes vote down vote up
def outputs_proto(self):
    return self._outputs_proto

  # This list holds the protobuf objects of type NodeProto
  # representing the ops in the converted ONNX graph. 
Example #23
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 5 votes vote down vote up
def _load_onnx_node(self, node):
    if isinstance(node, NodeProto):
      node = OnnxNode(node)
    self.name = node.name
    self.inputs = node.inputs
    self.outputs = node.outputs
    self.attr = node.attrs
    self.domain = node.domain
    self.op_type = node.op_type 
Example #24
Source File: _test_utils.py    From onnx-coreml with MIT License 5 votes vote down vote up
def _onnx_create_model(nodes,  # type: Sequence[NodeProto]
                       inputs,  # type: Sequence[Tuple[Text,Tuple[int, ...]]]
                       outputs,  # type: Sequence[Tuple[Text,Tuple[int, ...], int]]
                       initializer=[],  # type: Sequence[TensorProto]
                       ):
    # type: (...) -> ModelProto
    initializer_inputs = [
        helper.make_tensor_value_info(
            t.name,
            TensorProto.FLOAT,
            t.dims
        ) for t in initializer
    ]

    graph = helper.make_graph(
        nodes=nodes,
        name="test",
        inputs=initializer_inputs + [
            helper.make_tensor_value_info(
                input_[0],
                TensorProto.FLOAT,
                input_[1]
            ) for input_ in inputs
        ],
        outputs=[
            helper.make_tensor_value_info(
                output_[0],
                output_[2],
                output_[1]
            ) for output_ in outputs
        ],
        initializer=initializer
    )
    onnx_model = helper.make_model(graph)
    return onnx_model 
Example #25
Source File: _graph.py    From onnx-coreml with MIT License 5 votes vote down vote up
def from_onnx(node):  # type: (NodeProto) -> Node
        attrs = Attributes.from_onnx(node.attribute)
        name = Text(node.name)
        if len(name) == 0:
            name = "_".join(node.output)
        return Node(
            name, node.op_type, attrs, list(node.input), list(node.output)
        ) 
Example #26
Source File: __init__.py    From ngraph-onnx with Apache License 2.0 5 votes vote down vote up
def run_node(onnx_node, data_inputs, **kwargs):
    # type: (onnx.NodeProto, List[np.ndarray], Dict[Text, Any]) -> List[np.ndarray]
    """
    Convert ONNX node to ngraph node and perform computation on input data.

    :param onnx_node: ONNX NodeProto describing a computation node
    :param data_inputs: list of numpy ndarrays with input data
    :return: list of numpy ndarrays with computed output
    """
    NgraphBackend.backend_name = BACKEND_NAME
    if NgraphBackend.supports_ngraph_device(NgraphBackend.backend_name):
        return NgraphBackend.run_node(onnx_node, data_inputs, **kwargs)
    else:
        raise RuntimeError('The requested nGraph backend <'
                           + NgraphBackend.backend_name + '> is not supported!') 
Example #27
Source File: onnx_backend_node_test.py    From onnx-coreml with MIT License 4 votes vote down vote up
def run_node(cls,
                 node,  # type: onnx.NodeProto
                 inputs,  # type: Sequence[numpy.ndarray[Any]]
                 device='CPU',  # type: Text
                 outputs_info=None,  # type: Optional[Sequence[Tuple[numpy.dtype, Tuple[int, ...]]]]
                 ):
        # type: (...) -> onnx.namedtupledict
        '''
        CoreML requires full model for prediction, not just single layer.
        Also input/output shapes are required to build CoreML spec for model.
        As a temporary decision we use caffe2 backend for shape inference
        task to build the appropriate ONNX model and convert it to
        CoreML model.
        '''
        super(CoreMLTestingBackend, cls).run_node(node, inputs, device)

        assert outputs_info is not None, "CoreML needs output shapes"

        graph_inputs = []
        for i in range(len(inputs)):
            input_ = inputs[i]
            value_info = onnx.helper.make_tensor_value_info(
                name=node.input[i],
                elem_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[input_.dtype],
                shape=input_.shape
            )
            graph_inputs.append(value_info)

        graph_outputs = []
        for i in range(len(outputs_info)):
            output_info = outputs_info[i]
            value_info = onnx.helper.make_tensor_value_info(
                name=node.output[i],
                elem_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[output_info[0]],
                shape=output_info[1]
            )
            graph_outputs.append(value_info)

        graph = onnx.helper.make_graph(
            nodes=[node],
            name='dummy',
            inputs=graph_inputs,
            outputs=graph_outputs,
        )

        model = onnx.helper.make_model(graph)
        return cls.prepare(model).run(inputs)