我一直在研究在 tensorflow 中导入预训练模型的检查点。 这样做的目的是让我可以检查它的结构,并将其用于图像 分类。
具体来说,mobilenet 模型 found here .我找不到任何 从各种 *.ckpt.* 文件中导入模型的合理方法,以及 一些论坛嗅探我发现了 Github 用户 StanislawAntol 写的要点 据称将所述文件转换为卡住模型 ProtoBuf (.pb) 文件。这 要点是 here
运行脚本会给我一堆 .pb 文件,我希望我能工作 和。事实上,this SO question似乎回应了我的祈祷。
我一直在尝试以下代码的变体,但无济于事。任何物体
tf.import_graph_def
返回的似乎是 None 类型。
import tensorflow as tf
from tensorflow.python.platform import gfile
model_filename = LOCATION_OF_PB_FILE
with gfile.FastGFile(model_filename,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def, name='')
print(g_in)
我在这里缺少什么吗?整个转换为 .pb 的过程是否有误?
最佳答案
tf.import_graph_def
不返回图形,它填充作用域中的“默认图形”。参见 documentation for tf.import_graph_def
有关返回值的详细信息。
在您的情况下,您可以使用 tf.get_default_graph()
检查图形。例如:
with gfile.FastGFile(model_filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
g = tf.get_default_graph()
print(len(g.get_operations()))
参见 documentation for tf.Graph
有关“默认图”和范围界定概念的更多详细信息。
希望对您有所帮助。
关于python - Tensorflow:导入预训练模型(mobilenet、.pb、.ckpt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48157649/