【发布时间】:2017-03-21 21:46:50
【问题描述】:
我有一个训练有素的 TF 模型,它在序列化 (TFRecord) 输入上运行。图像数据具有可变形状,并通过tf.image.resize_images(...) 转换为 229x229x3 形状。我想使用类似于this 的gcloud ml-engine predict platform,确保接受任何尺寸的图像作为输入。
我从以下函数中获得了我的features 张量(它被传递到预测图):
def jpeg_serving_input_fn():
"""
Serve single jpeg feature to the prediction graph
:return: Image as a tensor
"""
input_features = tf.placeholder(dtype=tf.float32, shape=[None, None, 3],
name="PREDICT_PLACEHOLDER")
features_normalized = tf.image.resize_images(input_features, [229, 229])
image = tf.reshape(features_normalized, [1, 229, 229, 3], name="RESHAPE_PREDICT")
inputs = {
'image': image
}
最后的tf.reshape 是因为我的预测图需要一个形状为[batch_size, 229, 229, 3] 的张量。当我通过引擎运行它时
gcloud ml-engine local predict \
--model-dir=trained_model/export/ \
--json-instances=img.json
我收到了PredictionError:
predict_lib_beta.PredictionError: (4, "Exception during running the graph: Cannot feed value of shape (1, 1600, 2400, 3) for Tensor u'RESHAPE_PREDICT:0', which has shape '(1, 229, 229, 3)'")
在我看来,tf.reshape 正在接收tf.image.resize_images 的输出,它应该具有正确的形状。关于我在这里做错了什么有什么想法吗?提前致谢!
【问题讨论】:
-
Tensorflow 位在我看来是正确的; features_normalized 应该包含输出一个 (229, 229, 3) 形状的张量。您能否在您的函数中添加一些调试以验证它是否使用您认为用于预测的模型?我几周前才开始使用 Google Cloud ML,所以我很想看看这里的问题是什么。
-
添加
tf.logging.debug(features_normalized.get_shape())打印出我所期望的:229x229x3。但是,它是在保存模型之前在训练期间构建图形时执行此操作的。恢复模型进行预测时,形状不会重复。这显然是有道理的,但奇怪的是,当我查看tensorboard中的图表时,RESHAPE_PREDICT却无处可寻。
标签: python tensorflow gcloud google-prediction google-cloud-ml-engine