tensorflow - 如何使用 tensorflow .pb 文件?

标签 tensorflow

我有一个 TensorFlow 文件 AlexNet.pb我正在尝试加载它,然后对我拥有的图像进行分类。我已经搜索了几个小时,但我仍然找不到加载它然后对图像进行分类的方法。是否如此明显,我只是如此愚蠢,因为似乎没有人有加载和运行 .pb 文件的简单示例。

最佳答案

这取决于如何创建 protobuf 文件。

如果 .pb 文件是以下结果:

    # Create a builder to export the model
    builder = tf.saved_model.builder.SavedModelBuilder("export")
    # Tag the model in order to be capable of restoring it specifying the tag set
    builder.add_meta_graph_and_variables(sess, ["tag"])
    builder.save()

您必须知道该模型是如何被标记的并使用 tf.saved_model.loader.load 方法将保存的图形加载到当前的空图形中。

如果模型已被卡住,则必须手动将二进制文件加载到内存中:
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

graph = tf.get_default_graph()
tf.import_graph_def(graph_def, name="prefix")

在这两种情况下,您都必须知道输入张量的名称和要执行的节点的名称:

例如,如果您的输入张量是一个名为 batch_ 的占位符而你要执行的节点是名为dense/BiasAdd:0的节点你必须
    batch = graph.get_tensor_by_name('batch:0')
    prediction = restored_graph.get_tensor_by_name('dense/BiasAdd:0')

    values = sess.run(prediction, feed_dict={
        batch: your_input_batch,
    })

关于tensorflow - 如何使用 tensorflow .pb 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299822/

相关文章:

tensorflow - 无法使用 tensorflow 创建数组

tensorflow - 如何解读Keras的模型结构?

python - 在 Keras 中将循环层与致密层合并

python - tensorflow 自动编码器损失不收敛

c++ - TensorFlow protobuf 版本不匹配

python - Tensorflow session 不执行函数

python - 导入 tensorflow "AlreadyExistsError: Another metric with the same name already exists."时出错

python - 推荐什么? tensorflow train_and_evaluate 或 estimator.train, estimator.evaluate

python - 验证准确性/损失随着每个连续的纪元线性上升和下降

python - tensorflow 中的条件图和访问张量大小的循环