【发布时间】:2018-05-13 11:21:03
【问题描述】:
我按照 codelab TensorFlow For Poets 使用 inception_v3 进行迁移学习。它生成 retrained_graph.pb 和 retrained_labels.txt 文件,可用于在本地进行预测(运行label_image.py)。
然后,我想将此模型部署到 Cloud ML Engine,以便进行在线预测。为此,我必须将 retrained_graph.pb 导出为 SavedModel 格式。我按照Flowers Cloud ML Engine Tutorial 中的this answer from Google's @rhaertel80 和this python file 中的指示设法做到了这一点。这是我的代码:
import tensorflow as tf
from tensorflow.contrib import layers
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import utils as saved_model_utils
export_dir = '../tf_files/saved7'
retrained_graph = '../tf_files/retrained_graph2.pb'
label_count = 5
def build_signature(inputs, outputs):
signature_inputs = { key: saved_model_utils.build_tensor_info(tensor) for key, tensor in inputs.items() }
signature_outputs = { key: saved_model_utils.build_tensor_info(tensor) for key, tensor in outputs.items() }
signature_def = signature_def_utils.build_signature_def(
signature_inputs,
signature_outputs,
signature_constants.PREDICT_METHOD_NAME
)
return signature_def
class GraphReferences(object):
def __init__(self):
self.examples = None
self.train = None
self.global_step = None
self.metric_updates = []
self.metric_values = []
self.keys = None
self.predictions = []
self.input_jpeg = None
class Model(object):
def __init__(self, label_count):
self.label_count = label_count
def build_image_str_tensor(self):
image_str_tensor = tf.placeholder(tf.string, shape=[None])
def decode_and_resize(image_str_tensor):
return image_str_tensor
image = tf.map_fn(
decode_and_resize,
image_str_tensor,
back_prop=False,
dtype=tf.string
)
return image_str_tensor
def build_prediction_graph(self, g):
tensors = GraphReferences()
tensors.examples = tf.placeholder(tf.string, name='input', shape=(None,))
tensors.input_jpeg = self.build_image_str_tensor()
keys_placeholder = tf.placeholder(tf.string, shape=[None])
inputs = {
'key': keys_placeholder,
'image_bytes': tensors.input_jpeg
}
keys = tf.identity(keys_placeholder)
outputs = {
'key': keys,
'prediction': g.get_tensor_by_name('final_result:0')
}
return inputs, outputs
def export(self, output_dir):
with tf.Session(graph=tf.Graph()) as sess:
with tf.gfile.GFile(retrained_graph, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name="")
g = tf.get_default_graph()
inputs, outputs = self.build_prediction_graph(g)
signature_def = build_signature(inputs=inputs, outputs=outputs)
signature_def_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
}
builder = saved_model_builder.SavedModelBuilder(output_dir)
builder.add_meta_graph_and_variables(
sess,
tags=[tag_constants.SERVING],
signature_def_map=signature_def_map
)
builder.save()
model = Model(label_count)
model.export(export_dir)
此代码生成一个 saved_model.pb 文件,然后我用它来创建 Cloud ML Engine 模型。我可以使用 gcloud ml-engine predict --model my_model_name --json-instances request.json 从这个模型中得到预测,其中 request.json 的内容是:
{ "key": "0", "image_bytes": { "b64": "jpeg_image_base64_encoded" } }
但是,无论我在请求中编码哪个 jpeg,我总是得到完全相同的错误预测:
我猜问题出在 CloudML Prediction API 将 base64 编码图像字节传递给 inception_v3 的输入张量“DecodeJpeg/contents:0”(前面代码中的“build_image_str_tensor()”方法)的方式上。关于如何解决此问题并让我在本地重新训练的模型在 Cloud ML Engine 上提供正确预测的任何线索?
(为了清楚起见,问题不在于 retrained_graph.pb,因为当我在本地运行它时它会做出正确的预测;也不在 request.json 中,因为相同的请求文件在跟随 Flowers 时没有问题上面提到的 Cloud ML Engine 教程。)
【问题讨论】:
-
你找到答案了吗?
-
从我对代码的阅读来看,输入似乎并没有真正连接到图表(尽管得到任何结果而不是错误消息有点令人惊讶)。我将在下面给出一个答案。
标签: machine-learning tensorflow computer-vision google-cloud-platform google-cloud-ml