python - 从谷歌云机器学习引擎本地加载保存的 tensorflow 模型.pb

标签 python tensorflow google-cloud-ml-engine

我想采用我在线训练的 tensorflow 模型,并使用我分发的 python 程序在本地运行它。

训练后,我得到一个目录/model,其中包含两个文件/saved_model.pb 和一个文件夹/variables。本地部署的最简单方法是什么?

我正在关注here用于部署卡住模型,但我无法完全阅读 .pb。我直接下载了saved_model.pb到我的工作中并尝试了

with tf.gfile.GFile("saved_model.pb", "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

google.protobuf.message.DecodeError: Truncated message.

正在寻找here ,他们建议了一条不同的路线。

with tf.gfile.GFile("saved_model.pb", "rb") as f:
    proto_b=f.read()
    graph_def = tf.GraphDef()
    text_format.Merge(proto_b, graph_def) 

builtins.TypeError: a bytes-like object is required, not 'str'

我觉得这很令人困惑

type(proto_b)
<class 'bytes'>
type(graph_def)
<class 'tensorflow.core.framework.graph_pb2.GraphDef'>

为什么会出现错误,字符串也不是?

部署云训练模型的最佳方法是什么?

完整代码

import tensorflow as tf
import sys
from google.protobuf import text_format


# change this as you see fit
#image_path = sys.argv[1]
image_path="test.jpg"

# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line 
               in tf.gfile.GFile("dict.txt")]

# Unpersists graph from file
with tf.gfile.GFile("saved_model.pb", "rb") as f:
    proto_b=f.read()
    graph_def = tf.GraphDef()
    text_format.Merge(proto_b, graph_def) 

with tf.Session() as sess:
    # Feed the image_data as input to the graph and get first prediction
    softmax_tensor = sess.graph.get_tensor_by_name('conv1/weights:0')

    predictions = sess.run(softmax_tensor, \
                           {'DecodeJpeg/contents:0': image_data})

    # Sort to show labels of first prediction in order of confidence
    top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

    for node_id in top_k:
        human_string = label_lines[node_id]
        score = predictions[0][node_id]
        print('%s (score = %.5f)' % (human_string, score))

最佳答案

您部署到 CloudML Engine 服务的模型的格式是 SavedModel 。使用 loader 在 Python 中加载 SavedModel 相当简单模块:

import tensorflow as tf

with tf.Session(graph=tf.Graph()) as sess:
   tf.saved_model.loader.load(
       sess,
       [tf.saved_model.tag_constants.SERVING],
       path_to_model)

为了执行推理,您的代码几乎是正确的;您需要确保向 session.run 提供一批数据,因此只需将 image_data 包装在列表中即可:

# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('conv1/weights:0')

predictions = sess.run(softmax_tensor, \
                       {'DecodeJpeg/contents:0': [image_data]})

# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

for node_id in top_k:
    human_string = label_lines[node_id]
    score = predictions[0][node_id]
    print('%s (score = %.5f)' % (human_string, score))

(请注意,根据您的图表,将 input_data 包装在列表中可能会增加您的预测张量的排名,并且您需要相应地调整代码)。

关于python - 从谷歌云机器学习引擎本地加载保存的 tensorflow 模型.pb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667018/

相关文章:

python - PyQt5 玛雅 2017

python - Pandas 与可变列数的比较

python - sess.run() 多个操作 vs 多个 sess.run()

python - Google ML 引擎和 Python 数据存储 API, 'Forbidden: 403 Request had insufficient authentication scopes.'

python - Tkinter 复选框问题

python - tensorflow :如何创建一个与占位符形状相同的常量张量

python - 使用 Keras 和 TensorFlow 找到最大化神经网络输出的输入

tensorflow - 限制 skflow 中的 GPU 内存分配

gcloud - Google Cloud ML 上的 "Attempted relative import in non-package"

machine-learning - Google Cloud 上使用 GPU 的 Tensorflow