将图形原型(pb/pbtxt)转换为SavedModel以在TensorFlow Serving或Cloud ML Engine中使用 [英] Convert a graph proto (pb/pbtxt) to a SavedModel for use in TensorFlow Serving or Cloud ML Engine
问题描述
我一直在关注 TensorFlow for Poets 2 我已经训练过的模型上的代码实验室,并用嵌入的权重创建了一个冻结的量化图.它被捕获在一个文件中-例如my_quant_graph.pb
.
I've been following the TensorFlow for Poets 2 codelab on a model I've trained, and have created a frozen, quantized graph with embedded weights. It's captured in a single file - say my_quant_graph.pb
.
因为我可以使用该图与 TensorFlow Android推理库进行推理很好,我以为我可以使用Cloud ML Engine做同样的事情,但似乎只适用于SavedModel模型.
Since I can use that graph for inference with the TensorFlow Android inference library just fine, I thought I could do the same with Cloud ML Engine, but it seems it only works on a SavedModel model.
如何简单地将冻结/量化的图形转换为单个PB文件以在ML引擎上使用?
How can I simply convert a frozen/quantized graph in a single pb file to use on ML engine?
推荐答案
事实证明,SavedModel提供了一些有关已保存图形的额外信息.假设冻结的图不需要资产,则只需要指定一个服务签名.
It turns out that a SavedModel provides some extra info around a saved graph. Assuming a frozen graph doesn't need assets, then it needs only a serving signature specified.
这是我运行的python代码,用于将图形转换为Cloud ML引擎接受的格式.注意,我只有一对输入/输出张量.
Here's the python code I ran to convert my graph to a format that Cloud ML engine accepted. Note I only have a single pair of input/output tensors.
import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
export_dir = './saved'
graph_pb = 'my_quant_graph.pb'
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.gfile.GFile(graph_pb, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sigs = {}
with tf.Session(graph=tf.Graph()) as sess:
# name="" is important to ensure we don't get spurious prefixing
tf.import_graph_def(graph_def, name="")
g = tf.get_default_graph()
inp = g.get_tensor_by_name("real_A_and_B_images:0")
out = g.get_tensor_by_name("generator/Tanh:0")
sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
tf.saved_model.signature_def_utils.predict_signature_def(
{"in": inp}, {"out": out})
builder.add_meta_graph_and_variables(sess,
[tag_constants.SERVING],
signature_def_map=sigs)
builder.save()
这篇关于将图形原型(pb/pbtxt)转换为SavedModel以在TensorFlow Serving或Cloud ML Engine中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!