Python onnx.ValueInfoProto() Examples

The following are 19 code examples of onnx.ValueInfoProto(). 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: shape_inference_test.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def _assert_inferred(self, graph, vis):  # type: (GraphProto, List[ValueInfoProto]) -> None
        names_in_vis = set(x.name for x in vis)
        vis = list(x for x in graph.value_info if x.name not in names_in_vis) + vis
        inferred_model = self._inferred(graph)
        inferred_vis = list(inferred_model.graph.value_info)
        vis = list(sorted(vis, key=lambda x: x.name))
        inferred_vis = list(sorted(inferred_vis, key=lambda x: x.name))
        if vis == inferred_vis:
            return
        # otherwise some custom logic to give a nicer diff
        vis_names = set(x.name for x in vis)
        inferred_vis_names = set(x.name for x in inferred_vis)
        assert vis_names == inferred_vis_names, (vis_names, inferred_vis_names)
        for vi, inferred_vi in zip(vis, inferred_vis):
            assert vi == inferred_vi, '\n%s\n%s\n' % (vi, inferred_vi)
        assert False 
Example #2
Source File: onnx_script.py    From chainer-compiler with MIT License 6 votes vote down vote up
def _extract_value_info(arr, name):
    if isinstance(arr, list):
        assert arr
        assert not isinstance(arr[0], list)
        value_info_proto = onnx.ValueInfoProto()
        value_info_proto.name = name
        sequence_type_proto = value_info_proto.type.sequence_type
        nested = _extract_value_info(arr[0], name)
        tensor_type = sequence_type_proto.elem_type.tensor_type
        tensor_type.CopyFrom(nested.type.tensor_type)
        return value_info_proto
    else:
        return onnx.helper.make_tensor_value_info(
            name=name,
            elem_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[arr.dtype],
            shape=arr.shape) 
Example #3
Source File: value.py    From chainer-compiler with MIT License 5 votes vote down vote up
def __init__(self, value):
        if isinstance(value, Value):
            self.const_value = value.const_value
            value = value.value
        else:
            self.const_value = None
        self.value = value
        self.is_py = not isinstance(self.value, onnx.ValueInfoProto)
        if not self.is_py:
            assert self.is_tensor() or self.is_sequence()
            assert not (self.is_tensor() and self.is_sequence()) 
Example #4
Source File: _graph.py    From onnx2caffe with MIT License 5 votes vote down vote up
def _input_from_onnx_input(input):  # type: (ValueInfoProto) -> EdgeInfo
    name = input.name
    type = input.type.tensor_type.elem_type
    shape = tuple([d.dim_value for d in input.type.tensor_type.shape.dim])
    return (name, type, shape) 
Example #5
Source File: _test_utils.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _shape_from_onnx_value_info(
    v,
):  # type: (ValueInfoProto) -> Sequence[Tuple[int, ...]]
    return tuple([d.dim_value for d in v.type.tensor_type.shape.dim]) 
Example #6
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 #7
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 5 votes vote down vote up
def _data_type_caster(cls, protos, data_type_cast_map):
    """Cast to a new data type if node name is in data_type_cast_map.
    Be used to process protos to match ONNX type constraints.

    :param protos: Target protos.
      TensorProto for inputs and ValueInfoProto for consts.
    :param data_type_cast_map: A {node.name: new_data_type} dict.
    :return: Processed protos.
    """
    if not data_type_cast_map:
      return protos
    result = []
    for proto in protos:
      new_proto = proto
      if proto.name in data_type_cast_map:
        new_data_type = data_type_cast_map[proto.name]
        if type(proto) == TensorProto and proto.data_type != new_data_type:
          field = mapping.STORAGE_TENSOR_TYPE_TO_FIELD[
              mapping.TENSOR_TYPE_TO_STORAGE_TENSOR_TYPE[proto.data_type]]
          vals = getattr(proto, field)
          new_proto = make_tensor(
              name=proto.name,
              data_type=new_data_type,
              dims=proto.dims,
              vals=vals)
        elif type(
            proto
        ) == ValueInfoProto and proto.type.tensor_type.elem_type != new_data_type:
          new_proto.type.tensor_type.elem_type = new_data_type
      result.append(new_proto)
    return result 
Example #8
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 5 votes vote down vote up
def data_type_cast_map(self, data_type_cast_map):
    self._data_type_cast_map = data_type_cast_map

  # This list holds the protobuf objects of type ValueInfoProto
  # representing the all nodes' outputs to the converted ONNX graph. 
Example #9
Source File: pb_wrapper.py    From onnx-tensorflow with Apache License 2.0 5 votes vote down vote up
def _add_utility_constants(self):
    util_consts = {CONST_ONE_FP32: np.array([1.0]).astype(np.float32)}
    # Add a few useful utility constants:
    for name, value in util_consts.items():
      self.add_const_explicit(name=name, value=value)
      self.add_const_proto_explicit(
          name=name, value=value, np_dtype=value.dtype)
      self.add_input_proto_explicit(
          name=name, shape=value.shape, np_dtype=value.dtype)

  # This list holds the protobuf objects of type ValueInfoProto
  # representing the input to the converted ONNX graph. 
Example #10
Source File: _test_utils.py    From onnx-coreml with MIT License 5 votes vote down vote up
def _shape_from_onnx_value_info(v):  # type: (ValueInfoProto) -> Sequence[Tuple[int, ...]]
    return tuple([d.dim_value for d in v.type.tensor_type.shape.dim]) 
Example #11
Source File: _graph.py    From onnx-coreml with MIT License 5 votes vote down vote up
def _input_from_onnx_input(input):  # type: (ValueInfoProto) -> EdgeInfo
    name = input.name
    type = input.type.tensor_type.elem_type
    shape = tuple([d.dim_value for d in input.type.tensor_type.shape.dim])
    return (name, type, shape) 
Example #12
Source File: chainer2onnx.py    From chainer-compiler with MIT License 5 votes vote down vote up
def eval_ast(nast, env):
    for k, v in env.get_var_dict().items():
        assert not isinstance(v, onnx.ValueInfoProto), '%s %s' % (k, v)

    global _eval_ast_depth
    if not isinstance(nast, list):
        dprint('-' * _eval_ast_depth, gast.dump(nast), env.get_var_dict().keys())

    _eval_ast_depth += 1
    r = eval_ast_impl(nast, env)
    _eval_ast_depth -= 1
    return _value(r) 
Example #13
Source File: utils.py    From chainer-compiler with MIT License 5 votes vote down vote up
def istensor(x):
    return isinstance(x, onnx.ValueInfoProto) 
Example #14
Source File: value.py    From chainer-compiler with MIT License 5 votes vote down vote up
def to_sequence(self, env: 'utils.Env') -> onnx.ValueInfoProto:
        if self.is_py:
            self.const_value = Value(self.value)
            if not isinstance(self.value, collections.Iterable):
                raise TypeError('Expected a sequence: %s' % self.value)
            res = env.calc_seq(
                "SequenceConstruct",
                inputs=[],
            )
            for v in self.value:
                v = Value(v).to_tensor(env)
                res = env.calc_seq(
                    "SequenceInsert",
                    inputs=[res.name, v.name],
                )
            self.value = res
            self.is_py = False
        elif self.is_tensor():
            self.value = env.calc_seq(
                'SplitToSequence',
                inputs=[self.value.name],
                keepdims=False
            )

        assert self.is_sequence()
        return self.value 
Example #15
Source File: value.py    From chainer-compiler with MIT License 5 votes vote down vote up
def to_tensor(self, env: 'utils.Env',
                  dtype: type = None) -> onnx.ValueInfoProto:
        if self.is_py:
            self.const_value = Value(self.value)
            # TODO(hamaji): Rewrite `totensor` to convert a Python
            # list to a tensor.
            self.value = utils.totensor(self.value, env, dtype=dtype)
            self.is_py = False
        else:
            if self.is_sequence():
                self.value = env.calc('ConcatFromSequence',
                                      inputs=[self.value.name],
                                      axis=0,
                                      new_axis=True)
                self.is_py = False

            if dtype is not None:
                dt = utils.onnx_dtype(dtype)
                self.value = env.calc(
                    'Cast',
                    inputs=[self.value.name],
                    to=dt
                )
                self.value.type.tensor_type.elem_type = dt

        assert self.is_tensor()
        return self.value 
Example #16
Source File: value.py    From chainer-compiler with MIT License 5 votes vote down vote up
def to_value_info(self, env: 'utils.Env') -> onnx.ValueInfoProto:
        if self.is_py:
            if isinstance(self.value, collections.Iterable):
                return self.to_sequence(env)
            else:
                return self.to_tensor(env)
        return self.value 
Example #17
Source File: value.py    From chainer-compiler with MIT License 5 votes vote down vote up
def copy(self, env: 'utils.Env', name=None) -> 'Value':
        self.to_value_info(env)
        vi = self.value
        nvi = onnx.ValueInfoProto()
        if self.is_tensor():
            nvi.name = utils.gen_id(name, 'T')
        else:
            assert self.is_sequence(), self
            nvi.name = utils.gen_id(name, 'S')
        nvi.type.CopyFrom(vi.type)
        return Value(nvi) 
Example #18
Source File: _graph.py    From onnx2caffe with MIT License 4 votes vote down vote up
def from_onnx(graph):  # type: (GraphProto) -> Graph
        input_tensors = {
            t.name: numpy_helper.to_array(t) for t in graph.initializer
        }
        nodes_ = []
        nodes_by_input = {}  # type: Dict[Text, List[Node]]
        nodes_by_output = {}
        for node in graph.node:
            node_ = Node.from_onnx(node)
            for input_ in node_.inputs:
                if input_ in input_tensors:
                    node_.input_tensors[input_] = input_tensors[input_]
                else:
                    if input_ in nodes_by_input:
                        input_nodes = nodes_by_input[input_]
                    else:
                        input_nodes = []
                        nodes_by_input[input_] = input_nodes
                    input_nodes.append(node_)
            for output_ in node_.outputs:
                nodes_by_output[output_] = node_
            nodes_.append(node_)

        inputs = []
        for i in graph.input:
            if i.name not in input_tensors:
                inputs.append(_input_from_onnx_input(i))

        outputs = []
        for o in graph.output:
            outputs.append(_input_from_onnx_input(o))

        for node_ in nodes_:
            for input_ in node_.inputs:
                if input_ in nodes_by_output:
                    node_.parents.append(nodes_by_output[input_])
            for output_ in node_.outputs:
                if output_ in nodes_by_input:
                    node_.children.extend(nodes_by_input[output_])

        # Dictionary to hold the "value_info" field from ONNX graph
        shape_dict = {} # type: Dict[Text,Tuple[int,...]]

        def extract_value_info(shape_dict, # type: Dict[Text,Tuple[int,...]]
                               value_info, # type: ValueInfoProto[...]
                               ):
            # type: (...) -> None
            shape_dict[value_info.name] = tuple([int(dim.dim_value) for dim in value_info.type.tensor_type.shape.dim])

        for value_info in graph.value_info:
            extract_value_info(shape_dict, value_info)
        for value_info in graph.input:
            extract_value_info(shape_dict, value_info)
        for value_info in graph.output:
            extract_value_info(shape_dict, value_info)


        return Graph(nodes_, inputs, outputs, shape_dict) 
Example #19
Source File: _graph.py    From onnx-coreml with MIT License 4 votes vote down vote up
def from_onnx(graph, onnx_ir_version):  # type: (GraphProto) -> Graph
        input_tensors = {
            t.name: numpy_helper.to_array(t) for t in graph.initializer
        }
        nodes_ = []
        nodes_by_input = {}  # type: Dict[Text, List[Node]]
        nodes_by_output = {}
        for node in graph.node:
            node_ = Node.from_onnx(node)
            for input_ in node_.inputs:
                if input_ in input_tensors:
                    node_.input_tensors[input_] = input_tensors[input_]
                else:
                    if input_ in nodes_by_input:
                        input_nodes = nodes_by_input[input_]
                    else:
                        input_nodes = []
                        nodes_by_input[input_] = input_nodes
                    input_nodes.append(node_)
            for output_ in node_.outputs:
                nodes_by_output[output_] = node_
            nodes_.append(node_)

        inputs = []
        for i in graph.input:
            if i.name not in input_tensors:
                inputs.append(_input_from_onnx_input(i))

        outputs = []
        for o in graph.output:
            outputs.append(_input_from_onnx_input(o))

        for node_ in nodes_:
            for input_ in node_.inputs:
                if input_ in nodes_by_output:
                    node_.parents.append(nodes_by_output[input_])
            for output_ in node_.outputs:
                if output_ in nodes_by_input:
                    node_.children.extend(nodes_by_input[output_])

        # Dictionary to hold the "value_info" field from ONNX graph
        shape_dict = {} # type: Dict[Text,Tuple[int,...]]

        def extract_value_info(shape_dict, # type: Dict[Text,Tuple[int,...]]
                               value_info, # type: ValueInfoProto[...]
                               ):
            # type: (...) -> None
            t = tuple([int(dim.dim_value) for dim in value_info.type.tensor_type.shape.dim])
            if t:
                shape_dict[value_info.name] = t

        for value_info in graph.value_info:
            extract_value_info(shape_dict, value_info)
        for value_info in graph.input:
            extract_value_info(shape_dict, value_info)
        for value_info in graph.output:
            extract_value_info(shape_dict, value_info)


        return Graph(nodes_, inputs, outputs, shape_dict, onnx_ir_version)