且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从python中的.pb文件恢复Tensorflow模型?

更新时间:2023-12-02 19:03:58

以下代码将读取模型并打印出图中节点的名称.

The following code will read the model and print out the names of the nodes in the graph.

import tensorflow as tf
from tensorflow.python.platform import gfile
GRAPH_PB_PATH = './frozen_model.pb'
with tf.Session() as sess:
   print("load graph")
   with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f:
       graph_def = tf.GraphDef()
   graph_def.ParseFromString(f.read())
   sess.graph.as_default()
   tf.import_graph_def(graph_def, name='')
   graph_nodes=[n for n in graph_def.node]
   names = []
   for t in graph_nodes:
      names.append(t.name)
   print(names)

您正确冻结了图形,这就是为什么您得到不同结果的原因,基本上权重没有存储在模型中.您可以使用 freeze_graph.py (

You are freezing the graph properly that is why you are getting different results basically weights are not getting stored in your model. You can use the freeze_graph.py (link) for getting a correctly stored graph.