python - Tensorflow:导入预训练模型(mobilenet、.pb、.ckpt)

标签 python tensorflow

我一直在研究在 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/

相关文章:

python - 如何下载文件并将其传递给函数

python - 如何找到之前列定义的每个子集中存在的 pandas 单元格值?

tensorflow - Channels first vs Channels last——这是什么意思?

python - Tensorflow 嵌入层词汇量大小

machine-learning - 重置 tensorflow 元图中输入占位符的形状

python - 更改模型 : <name> given automatically by keras in model. summary() 输出

python - 无法使用 Python 3.6 在 Mac 上安装 python-levenshtein 包

python - 如何在 boto 中设置 `aws` 的凭据?

python - 使用 python 在 unix 中获取正在运行的进程列表的最佳方法是什么?

tensorflow - 如何清除 Tensorflow-Keras GPU 内存?