TensorFlow:使用 Tensorflow 2.2.0 将 SavedModel.pb

我一直在无休止地寻找一个 python 脚本或命令行函数来将 .pb 文件转换为 .tflite。我试过使用 tflite_convert,但它返回错误:


OSError:SavedModel 文件不存在于:C:/tensorflowTraining/export_dir/saved_model.pb/{saved_model.pbtxt|saved_model.pb}


我还尝试了一些脚本,例如:


import tensorflow as to


gf = tf.compat.v1.GraphDef()


m_file = open('saved_model.pb', 'rb')


gf.ParseFromString(m_file.read())


with open('somefile.txt', 'a') as the_file:


    for n in gf.node:


        the_file.write(n.name+'\n')


file = open('somefile.txt', 'r')


data = file.readlines()


print("output name = ")


print(data[len(data)-1])



print("Input name = ")


file.seek(0)


print(file.readline())

这将返回:


发生异常:DecodeError


意外的结束组标记。


这个错误发生在第 4 行:


 gf.ParseFromString(m_file.read())

如果有人可以提供工作脚本或命令行功能,那将非常有帮助,正如我研究过的那样返回错误或无法正常运行。


谢谢!


POPMUISE
浏览 124回答 1
1回答

温温酱

你可以尝试像下面这样的东西TF2.2。import tensorflow as tf graph_def_file = "./saved_model.pb"tflite_file = 'mytflite.tflite'input_arrays = ["input"]. # you need to change it based on your modeloutput_arrays = ["output"] # you need to change it based on your modelprint("{} -> {}".format(graph_def_file, tflite_file))converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(  graph_def_file=graph_def_file,  input_arrays=input_arrays,  output_arrays=output_arrays,input_shapes={'input_mel':[ 1, 50, 80]})# If there are multiple inputs, then update the dictionary abovetflite_model = converter.convert()open(tflite_file,'wb').write(tflite_model)在上面的代码中,您需要使用与您的模型相对应的input_arrays, output_arrays, 和。input_shapes
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python