Exemplo n.º 1
0
    def onnx_graph_to_tensorflow_net(cls, graph_def, opset):
        model_graph = tf.Graph()
        with model_graph.as_default():
            # initializer: TensorProtos representing the values to initialize
            # a given tensor.
            # initialized: A list of names of the initialized tensors.
            if graph_def.initializer:
                input_dict_items = cls.onnx_initializer_to_input_dict_items(
                    graph_def.initializer)
                initialized = {init.name for init in graph_def.initializer}
            else:
                input_dict_items = []
                initialized = set()
            predict_net = TensorflowNet()
            predict_net.name = graph_def.name
            predict_net.graph = model_graph

            predict_net.external_input.extend(
                value_info.name for value_info in graph_def.input
                if value_info.name not in initialized)

            predict_net.external_output.extend(
                value_info.name for value_info in graph_def.output)

            # creating placeholders for currently unkown inputs
            for value_info in graph_def.input:
                if value_info.name in initialized:
                    continue
                shape = list(d.dim_value if d.dim_value >= 0 else None
                             for d in value_info.type.tensor_type.shape.dim)
                x = tf.placeholder(
                    TF_TYPE_ENUM[value_info.type.tensor_type.elem_type],
                    name=value_info.name,
                    shape=shape)
                input_dict_items.append([value_info.name, x])

            # tensor dict: this dictionary is a map from variable names
            # to the latest produced TF tensors of the given name.
            # This dictionary will get updated as we build the graph to
            # record the names of newly produced tensors.
            tensor_dict = dict(input_dict_items)
            # Since tensor dict may be updated, we need to keep a copy
            # of the original input dict where we track the earliest
            # defined tensors so we can have access to the placeholders
            # to feed in input tensors when we run the graph.
            input_dict = dict(input_dict_items)

            for node in graph_def.node:
                node = OnnxNode(node)

                output_ops = cls._onnx_node_to_tensorflow_op(node,
                                                             tensor_dict,
                                                             opset=opset)
                curr_node_output_map = list(zip(node.outputs, output_ops))
                tensor_dict = dict(
                    list(tensor_dict.items()) + curr_node_output_map)

            predict_net.tensor_dict = tensor_dict

        return predict_net
Exemplo n.º 2
0
    def onnx_graph_to_tensorflow_net(cls, graph_def):
        # initializer: TensorProtos representing the values to initialize
        # a given tensor.
        # initialized: A list of names of the initialized tensors.
        if graph_def.initializer:
            input_dict_items = cls.onnx_initializer_to_input_dict_items(
                graph_def.initializer)
            initialized = {init.name for init in graph_def.initializer}
        else:
            input_dict_items = []
            initialized = set()
        predict_net = TensorflowNet()
        predict_net.name = graph_def.name

        predict_net.external_input.extend(value_info.name
                                          for value_info in graph_def.input)
        predict_net.external_output.extend(value_info.name
                                           for value_info in graph_def.output)

        # creating placeholders for currently unkown inputs
        for value_info in graph_def.input:
            if value_info.name in initialized:
                continue

            shape = list(d.dim_value
                         for d in value_info.type.tensor_type.shape.dim)
            x = tf.placeholder(
                cls.tensor_type_enum[value_info.type.tensor_type.elem_type],
                name=value_info.name,
                shape=shape)
            input_dict_items.append([value_info.name, x])

        # input dict: this dictionary is a map from variable names
        # to the latest produced tensors of the given name.
        # This dictionary will get updated as build the graph because
        # some ops may produce a result tensor with the same name as
        # the input tensor. The input dict tracks the latest produced
        # tensors.
        input_dict = dict(input_dict_items)
        # Since input dict may be updated, we need to keep a copy
        # of the original input dict where we track the earliest
        # defined tensors so we can have access to the placeholders
        # to feed in input tensors when we run the graph.
        original_input_dict = dict(input_dict_items)
        output_dict = dict()

        for node in graph_def.node:
            node = OnnxNode(node)

            output_ops = cls._onnx_node_to_tensorflow_op(node, input_dict)
            curr_node_output_map = list(zip(node.outputs, output_ops))
            input_dict = dict(list(input_dict.items()) + curr_node_output_map)

            output_dict = dict(
                list(output_dict.items()) + curr_node_output_map)
            predict_net.op.extend(output_ops)
        predict_net.output_dict = output_dict
        return original_input_dict, predict_net