Пример #1
0
 def caffe2_op_to_onnx_node(cls, op_def, shapes):
     if C.support_onnx_export(op_def.type):
         shape_list = list(shapes.values())
         node_strs, tensor_strs = C.export_to_onnx(
             op_def.SerializeToString(), shapes)
         nodes = []
         for s in node_strs:
             node = NodeProto()
             node.ParseFromString(s)
             nodes.append(node)
         const_tensors = []
         for s in tensor_strs:
             tensor = TensorProto()
             tensor.ParseFromString(s)
             const_tensors.append(tensor)
         return nodes, const_tensors
     elif op_def.type in cls._special_operators:
         translator = getattr(cls, cls._special_operators[op_def.type])
     else:
         translator = cls._common_caffe2_op_to_onnx_node
     nodes = translator(op_def, shapes)
     const_tensors = []
     if isinstance(nodes, tuple):
         nodes, const_tensors = nodes
     if not isinstance(nodes, collections.Iterable):
         nodes = [nodes]
     return nodes, const_tensors
Пример #2
0
def make_node(
        op_type,  # type: Text
        inputs,  # type: Sequence[Text]
        outputs,  # type: Sequence[Text]
        name=None,  # type: Optional[Text]
        doc_string=None,  # type: Optional[Text]
        **kwargs  # type: Any
):  # type: (...) -> NodeProto
    """Construct a NodeProto.

    Arguments:
        op_type (string): The name of the operator to construct
        inputs (list of string): list of input names
        outputs (list of string): list of output names
        name (string, default None): optional unique identifier for NodeProto
        doc_string (string, default None): optional documentation string for NodeProto
        **kwargs (dict): the attributes of the node.  The acceptable values
            are documented in :func:`make_attribute`.
    """

    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if kwargs:
        node.attribute.extend(
            make_attribute(key, value)
            for key, value in sorted(kwargs.items()))
    return node
Пример #3
0
    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
Пример #4
0
def make_node(op_type, inputs, outputs, name=None, doc_string=None, **kwargs):
    """Construct a NodeProto.

    Arguments:
        op_type (string): The name of the operator to construct
        inputs (list of string): list of input names
        outputs (list of string): list of output names
        name (string, default None): optional unique identifier for NodeProto
        doc_string (string, default None): optional documentation string for NodeProto
        **kwargs (dict): the attributes of the node.  The acceptable values
            are documented in :func:`make_attribute`.
    """

    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if kwargs:
        node.attribute.extend(
            make_attribute(key, value) for key, value in kwargs.items())
    return node
Пример #5
0
def make_node(op_type: str, inputs: Sequence[str], outputs: Sequence[str], **kwargs) -> INodeProto:
    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    node.attribute.extend(make_attribute(key, value) for key, value in kwargs.items())
    return node
Пример #6
0
 def _create_reshape(cls, op, op_t):
     """
     get a onnx node from singa Concat operator
     Args:
         op: a given operator
     Args:
         op_t: the tensor of the operator
     Returns: 
         the onnx node
     """
     # make the shape node
     # because the reshape in singa does not provide its shape as input tensor
     shape_node_name = op.name + "#shape"
     shape_node = NodeProto()
     shape_node.name = shape_node_name
     shape_node.op_type = cls._rename_operators.get("Dummy", "Dummy")
     shape_node.output.extend([shape_node_name])
     shape_node.attribute.extend([
         helper.make_attribute(
             'value',
             helper.make_tensor(
                 name=shape_node_name,
                 data_type=TensorProto.FLOAT,
                 dims=[len(op_t.shape)],
                 vals=op_t.shape,
             ))
     ])
     # make the reshape node
     node = cls._common_singa_tensor_to_onnx_node(op, op_t)
     node.input.extend([shape_node_name])
     return [shape_node, node]
Пример #7
0
def check_node() -> 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)
Пример #8
0
def make_node(op_type, inputs, outputs, name=None, doc_string=None, **kwargs):
    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if kwargs:
        node.attribute.extend(
            make_attribute(key, value) for key, value in kwargs.items())
    return node
Пример #9
0
def _onnx_rewrite_operator_node(existing_names, node, sub_onx):
    """
    Replaces a node by a subgraph.

    :param existing_names: existing results names
    :param node: onnx node to replace
    :param sub_onx: onnx sub_graph to use as a replacement
    :return: new_initializer, new_nodes
    """
    if len(node.input) != len(sub_onx.graph.input):
        raise ValueError(  # pragma: no cover
            "Mismatch with the number of inputs for operator type %r. "
            "%d != %d." %
            (node.op_type, len(node.input), len(sub_onx.graph.nput)))
    if len(node.output) != len(sub_onx.graph.output):
        raise ValueError(  # pragma: no cover
            "Mismatch with the number of outputs for operator type %r. "
            "%d != %d." %
            (node.op_type, len(node.output), len(sub_onx.graph.output)))
    replaces = {}
    for inp, name in zip(sub_onx.graph.input, node.input):
        replaces[inp.name] = name
    for inp, name in zip(sub_onx.graph.output, node.output):
        replaces[inp.name] = name

    new_inits = []
    for init in sub_onx.graph.initializer:
        name = _unique_name(existing_names, init.name)
        replaces[init.name] = name
        tensor = from_array(to_array(init), name=name)
        new_inits.append(tensor)

    new_nodes = []
    for n in sub_onx.graph.node:
        new_node = NodeProto()
        new_node.op_type = n.op_type
        new_node.attribute.extend(n.attribute)  # pylint: disable=E1101
        new_node.input.extend(  # pylint: disable=E1101
            [replaces[i] for i in n.input])  # pylint: disable=E1101
        new_node.domain = n.domain
        new_out = []
        for o in n.output:
            if o in replaces:
                new_out.append(replaces[o])
            else:
                n = _unique_name(existing_names, o)
                new_out.append(n)
        new_node.output.extend(new_out)  # pylint: disable=E1101
        new_nodes.append(new_node)

    return new_inits, new_nodes
Пример #10
0
    def _common_caffe2_op_to_onnx_node(cls, op_def, shapes):
        node_def = NodeProto()
        node_def.name = op_def.name

        node_def.op_type = cls._renamed_operators.get(op_def.type, op_def.type)

        node_def.input.extend(op_def.input)
        node_def.output.extend(op_def.output)

        attrs = filter(None, [cls.caffe2_arg_to_onnx_attr(op_def, arg)
                              for arg in op_def.arg])
        node_def.attribute.extend(attrs)

        return node_def
Пример #11
0
    def _common_caffe2_op_to_onnx_node(cls, op_def, shapes):
        node_def = NodeProto()
        node_def.name = op_def.name

        node_def.op_type = cls._renamed_operators.get(op_def.type, op_def.type)

        node_def.input.extend(op_def.input)
        node_def.output.extend(op_def.output)

        attrs = filter(None, [cls.caffe2_arg_to_onnx_attr(op_def, arg)
                              for arg in op_def.arg])
        node_def.attribute.extend(attrs)

        return node_def
Пример #12
0
def load_node(input_str):
    """
    Return a node
    :param input_str:
    :return:
    """
    node_proto = NodeProto()
    text_format.Parse(input_str, node_proto)
    return node_proto
Пример #13
0
    def _common_singa_tensor_to_onnx_node(cls, op, op_t):
        """
        get a onnx node from a singa operator, prepare its type, inputs and outputs
        Args:
            op: a given operator
        Args:
            op: the tensor of the operator
        Returns: the onnx node
        """
        node_def = NodeProto()
        node_def.name = op.name

        optype = cls._get_singa_op_type(op)
        node_def.op_type = cls._rename_operators.get(optype, optype)

        inputs, outputs = cls._get_singa_op_inputs_outputs(op)
        node_def.input.extend(inputs)
        node_def.output.extend(outputs)

        return node_def
Пример #14
0
 def update_node_input(
     self, node: NodeProto, input_id: str, input_idx: Optional[int] = None
 ):
     """
     :param node: node to update the inputs of
     :param input_id: new input_id to attach to the node
     :param input_idx: optional index of the node input list to update,
         if none is given, the new input id will be appended to the input list
     """
     if input_idx is not None:
         if node in self._input_id_to_nodes[node.input[input_idx]]:
             self._input_id_to_nodes[node.input[input_idx]].remove(node)
         node.input[input_idx] = input_id
     else:
         node.input.append(input_id)
     self._input_id_to_nodes[input_id].append(node)
Пример #15
0
def create_node_from_schema(schema):
    """
    Convert an OpSchema to a NodeProto
    :param schema:  the input OpSchema
    :return: the equivalent NodeProto
    """

    node_proto = NodeProto()
    for attribute in schema.attributes:
        attr_value = schema.attributes[attribute]
        if attr_value.default_value.name == '':
            attr_value_new = onnx.helper.make_attribute(attr_value.name, '')
            attr_value_new.type = convert_attr_type_to_enum(attr_value)
            node_proto.attribute.append(attr_value_new)
        else:
            node_proto.attribute.append(attr_value.default_value)
    node_proto.op_type = schema.name
    node_proto.doc_string = schema.doc
    node_proto.name = schema.name
    for input_arr in schema.inputs:
        input_types = input_arr.types
        type_attr = onnx.helper.make_attribute(input_arr.name + '-types', [
            str(data_type).replace('tensor(', '').replace(')', '')
            for data_type in input_types
        ])
        node_proto.attribute.append(type_attr)

        if node_proto.input is None:
            node_proto.input = []
        node_proto.input.append(input_arr.name)
    for output_arr in schema.outputs:
        if node_proto.output is None:
            node_proto.output = []
            output_types = output_arr.types
            type_attr = onnx.helper.make_attribute(
                output_arr.name + '-types', [
                    str(data_type).replace('tensor(', '').replace(')', '')
                    for data_type in output_types
                ])
            node_proto.attribute.append(type_attr)
        node_proto.output.append(output_arr.name)
    return node_proto
Пример #16
0
def make_node(
    op_type,  # type: Text
    inputs,  # type: Sequence[Text]
    outputs,  # type: Sequence[Text]
    name=None,  # type: Optional[Text]
    doc_string=None,  # type: Optional[Text]
    domain=None,  # type: Optional[Text]
    _dtype=None,  # type: [np.float32, np.float64]
    **kwargs  # type: Any
):  # type: (...) -> NodeProto
    """Construct a NodeProto.

    Arguments:
        op_type (string): The name of the operator to construct
        inputs (list of string): list of input names
        outputs (list of string): list of output names
        name (string, default None): optional unique identifier for NodeProto
        doc_string (string, default None): optional documentation
            string for NodeProto
        dtype: dtype for double used to infer
        domain (string, default None): optional domain for NodeProto.
            If it's None, we will just use default domain (which is empty)
        **kwargs (dict): the attributes of the node.  The acceptable values
            are documented in :func:`make_attribute`.
    """
    if _dtype is None:
        raise ValueError("dtype cannot be None")
    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if domain is not None:
        node.domain = domain
    if kwargs:
        node.attribute.extend(
            make_attribute(key, value, dtype=_dtype, domain=domain)
            for key, value in sorted(kwargs.items()))
    return node
Пример #17
0
def make_node(
        op_type: Text,
        inputs: Sequence[Text],
        outputs: Sequence[Text],
        name: Optional[Text] = None,
        doc_string: Optional[Text] = None,
        domain: Optional[Text] = None,
        **kwargs: Any
) -> NodeProto:
    """Construct a NodeProto.

    Arguments:
        op_type (string): The name of the operator to construct
        inputs (list of string): list of input names
        outputs (list of string): list of output names
        name (string, default None): optional unique identifier for NodeProto
        doc_string (string, default None): optional documentation string for NodeProto
        domain (string, default None): optional domain for NodeProto.
            If it's None, we will just use default domain (which is empty)
        **kwargs (dict): the attributes of the node.  The acceptable values
            are documented in :func:`make_attribute`.
    Returns:
        NodeProto
    """

    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if domain is not None:
        node.domain = domain
    if kwargs:
        node.attribute.extend(
            make_attribute(key, value)
            for key, value in sorted(kwargs.items())
            if value is not None)
    return node
def _make_node(op_type,
               inputs,
               outputs,
               name=None,
               doc_string=None,
               domain=None,
               attributes=None):
    """
    Constructs a NodeProto.

    :param op_type: (string): The name of the operator to construct
    :param inputs: list of input names
    :param outputs: list of output names
    :param name: optional unique identifier for NodeProto
    :param doc_string: optional documentation
        string for NodeProto
    :param domain: optional domain for NodeProto.
        If it's None, we will just use default domain (which is empty)
    :param attributes: the attributes of the node.  The acceptable values
        are documented in :func:`make_attribute`.
    :return: node
    """
    node = NodeProto()
    node.op_type = op_type
    node.input.extend(inputs)
    node.output.extend(outputs)
    if name:
        node.name = name
    if doc_string:
        node.doc_string = doc_string
    if domain is not None:
        node.domain = domain
    if isinstance(attributes, dict):
        if len(attributes) > 0:
            node.attribute.extend(
                make_attribute(key, value)
                for key, value in sorted(attributes.items()))
    elif attributes:
        for att in attributes:
            node.attribute.extend([att])
    return node
Пример #19
0
 def convert(self, node: NodeProto):
     node = super().convert(node)
     node.prune = False
     return node