为了账号安全,请及时绑定邮箱和手机立即绑定

TensorFlow:使用 Tensorflow 2.2.0 将 SavedModel.pb

TensorFlow:使用 Tensorflow 2.2.0 将 SavedModel.pb

POPMUISE 2022-12-20 12:14:20
我一直在无休止地寻找一个 python 脚本或命令行函数来将 .pb 文件转换为 .tflite。我试过使用 tflite_convert,但它返回错误:OSError:SavedModel 文件不存在于:C:/tensorflowTraining/export_dir/saved_model.pb/{saved_model.pbtxt|saved_model.pb}我还尝试了一些脚本,例如:import tensorflow as togf = 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())如果有人可以提供工作脚本或命令行功能,那将非常有帮助,正如我研究过的那样返回错误或无法正常运行。谢谢!
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

你可以尝试像下面这样的东西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 model

output_arrays = ["output"] # you need to change it based on your model

print("{} -> {}".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 above

tflite_model = converter.convert()

open(tflite_file,'wb').write(tflite_model)

在上面的代码中,您需要使用与您的模型相对应的input_arrays, output_arrays, 和。input_shapes


查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信