Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加载图形,然后使用它来构建tflite?_Python_Tensorflow_Deep Learning_Tensorflow Lite - Fatal编程技术网

Python 加载图形,然后使用它来构建tflite?

Python 加载图形,然后使用它来构建tflite?,python,tensorflow,deep-learning,tensorflow-lite,Python,Tensorflow,Deep Learning,Tensorflow Lite,我是tensorflow的新手,我正在尝试将.pb(proto buffer)文件转换为lite版本。但我面临一些问题。 导入时间、系统、警告、全局、随机、cv2、base64、json、csv、操作系统 import numpy as np import tensorflow as tf from collections import OrderedDict def load_graph(frozen_graph_filename): with tf.gfile.GFile(froze

我是tensorflow的新手,我正在尝试将.pb(proto buffer)文件转换为lite版本。但我面临一些问题。 导入时间、系统、警告、全局、随机、cv2、base64、json、csv、操作系统

import numpy as np
import tensorflow as tf
from collections import OrderedDict
def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def, 
            input_map=None, 
            return_elements=None, 
            name="prefix", 
            op_dict=None, 
            producer_op_list=None
        )
    return graph
此函数为我加载一个图形,现在我想将此图形转换为使用以下脚本的tflite。

CD_graph = load_graph("CD_Check_k.pb")
CD_input = CD_graph.get_tensor_by_name('prefix/input_node:0')
CD_output = CD_graph.get_tensor_by_name('prefix/output_node:0')
x_single = tf.placeholder(tf.float32, [1, 256 , 256, 3],
                              name="input_node")
with tf.Session() as sess:
  tflite_model = tf.contrib.lite.toco_convert(CD_graph, input_tensors=[x_single ], output_tensors=[CD_output])
  with open('./mnist.tflite', "wb") as f:
      f.write(tflite_model)
错误消息:

'Graph' object has no attribute 'SerializeToString'          

您可以使用
TocoConverter.from\u freezed\u graph()
API简化代码,这样您就不再需要读取冻结的图形。下面复制了来自的示例

从文件导出GraphDef 下面的示例演示如何在文件中存储TensorFlow GraphDef时将TensorFlow Lite FlatBuffer转换为TensorFlow。接受
.pb
.pbtxt
文件

该示例使用。该函数仅支持通过冻结的GraphDefs

import tensorflow as tf

graph_def_file = "/path/to/Downloads/mobilenet_v1_1.0_224/frozen_graph.pb"
input_arrays = ["input"]
output_arrays = ["MobilenetV1/Predictions/Softmax"]

converter = tf.contrib.lite.TocoConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)