1

我有上序列化(TFRecord)输入操作训练有素的TF模型。图像数据具有可变的形状,并通过tf.image.resize_images(...)转换为229x229x3形状。我想用gcloud ml-engine predictplatform类似this,确保接受任何尺寸的图像作为输入。Tensorflow:调整图像占位符

我从下面的函数得到我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输出应该有正确的形状。有什么想法,我在做什么错在这里?提前致谢!

+0

的Tensorflow位看起来是正确的给我; features_normalized应包含输出(229,229,3)形张量。你可以在你的函数中添加一些调试来验证它是否使用了你认为用于预测的模型?几个星期前我刚开始使用Google Cloud ML,所以我很想看看这里有什么问题。 – SuperTetelman

+0

添加'tf.logging.debug(features_normalized.get_shape())'打印出我所期望的:'229x229x3'。但是,它在保存模型之前在训练期间构建图形时执行此操作。在恢复预测模型时,形状不会重复。这显然是有道理的,但奇怪的是,当我看着'tensorboard'中的图时,'RESHAPE_PREDICT'无处可寻。 – fenkerbb

回答

2

它看起来像错误是由一些代码,馈送"RESHAPE_PREDICT:0"张量(即tf.reshape() OP的输出,image)而不是"PREDICT_PLACEHOLDER:0"张量(即,输入到运算tf.image.resize_images()input_features)引起的。

没有整个源代码到你的训练模型,这是很难说什么的变化是必要的,但也可能是因为改变inputs定义为简单:

inputs = {'image': input_features} 

......从而使预测服务知道为该占位符提供值,而不是固定形状的输出tf.reshape()

+0

谢谢,这导致我的解决方案,是的问题是如何在代码中的其他地方使用此代码。本质上,我的“输入”字典定义了用于预测的JSON输入文件的格式(参见图:-))。字典的值是形状被传递给'tf.saved_model.signature_def_utils.build_signature_def(...)'的张量。这个模块有很好的教程或指导吗?文档看起来有点稀疏。在任何情况下,非常感谢@mrry – fenkerbb

+0

我认为'tf.saved_model'唯一文档是这些的:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md(加上代码本身的任何评论)。我相信球队正在使用此教程,但它可能是值得通过打开[GitHub的问题(https://github.com/tensorflow/tensorflow/issues),要求更好的文档来提醒他们。 – mrry