Tensorflow 如何仅从savedmodel.pb创建冻结推理图?我没有任何检查点文件

Tensorflow 如何仅从savedmodel.pb创建冻结推理图?我没有任何检查点文件,tensorflow,google-cloud-automl,Tensorflow,Google Cloud Automl,我已经从Google AutoML图像分类导出了我的模型。我只有一个保存的_model.pb,没有检查点或元文件。有没有办法将保存的_model.pb转换为冻结的_inferenc_图形您可以通过以下方法仅使用.pb文件来实现这一点 import tensorflow as tf from tensorflow.python.platform import gfile GRAPH_PB_PATH = './model/model_file.pb' #path to your .pb file

我已经从Google AutoML图像分类导出了我的模型。我只有一个保存的_model.pb,没有检查点或元文件。有没有办法将保存的_model.pb转换为冻结的_inferenc_图形

您可以通过以下方法仅使用.pb文件来实现这一点

import tensorflow as tf
from tensorflow.python.platform import gfile

GRAPH_PB_PATH = './model/model_file.pb' #path to your .pb file
with tf.Session(config=config) 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]
现在,当您将图形冻结到.pb文件时,您的变量将转换为常量类型,并且作为可训练变量的权重也将作为常量存储在.pb文件中。graph_节点包含图形中的所有节点。但我们对所有Const类型的节点都感兴趣

wts = [n for n in graph_nodes if n.op=='Const']
wts的每个元素都是NodeDef类型。它有几个属性,如name、op等。可以按如下方式提取值-

from tensorflow.python.framework import tensor_util

for n in wts:
    print "Name of the node - %s" % n.name
    print "Value - " 
    print tensor_util.MakeNdarray(n.attr['value'].tensor)

希望这能解决您的问题。

您只需使用.pb文件,就可以通过以下方法实现这一点

import tensorflow as tf
from tensorflow.python.platform import gfile

GRAPH_PB_PATH = './model/model_file.pb' #path to your .pb file
with tf.Session(config=config) 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]
现在,当您将图形冻结到.pb文件时,您的变量将转换为常量类型,并且作为可训练变量的权重也将作为常量存储在.pb文件中。graph_节点包含图形中的所有节点。但我们对所有Const类型的节点都感兴趣

wts = [n for n in graph_nodes if n.op=='Const']
wts的每个元素都是NodeDef类型。它有几个属性,如name、op等。可以按如下方式提取值-

from tensorflow.python.framework import tensor_util

for n in wts:
    print "Name of the node - %s" % n.name
    print "Value - " 
    print tensor_util.MakeNdarray(n.attr['value'].tensor)

希望这能解决你的问题。

@Amudha Sethuraman-如果答案能回答你的问题,请你接受并投票。谢谢。@Amudha Sethuraman-如果答案能回答您的问题,请您接受并投票表决。非常感谢。