我有一个经过训练的 TF 模型,该模型在序列化 (TFRecord
) 输入上运行。图像数据具有可变形状,并通过 tf.image.resize_images(...) 转换为 229x229x3 形状。我想使用 gcloud ml-engine Predict
platform类似于 this ,确保接受任何尺寸的图像作为输入。
我从以下函数获取我的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
我收到一个预测错误
:
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
的输出,它应该具有正确的形状。对我在这里做错了什么有什么想法吗?提前致谢!
最佳答案
看起来该错误是由某些提供 "RESHAPE_PREDICT:0"
张量的代码引起的(即 tf.reshape()
操作的输出,image
)而不是 "PREDICT_PLACEHOLDER:0"
张量(即 tf.image.resize_images()
操作的输入,input_features
)。
如果没有经过训练的模型的完整源代码,很难确切地说出需要进行哪些更改,但可能就像将 inputs
的定义更改为:
inputs = {'image': input_features}
...以便预测服务知道向该占位符提供值,而不是 tf.reshape()
的固定形状输出。
关于python - Tensorflow:调整图像占位符大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938728/