【问题标题】:Tensorflow: Resize Image PlaceholderTensorflow:调整图像占位符的大小
【发布时间】:2017-03-21 21:46:50
【问题描述】:

我有一个训练有素的 TF 模型,它在序列化 (TFRecord) 输入上运行。图像数据具有可变形状,并通过tf.image.resize_images(...) 转换为 229x229x3 形状。我想使用类似于thisgcloud 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


【解决方案1】:

看起来该错误是由提供"RESHAPE_PREDICT:0"张量(即tf.reshape() op,image的输出)而不是"PREDICT_PLACEHOLDER:0"张量(即@987654327的输入)的代码引起的@op,input_features)。

如果没有您训练模型的完整来源,很难确切说明需要进行哪些更改,但可能就像将 inputs 的定义更改为:

inputs = {'image': input_features}

...以便预测服务知道将值提供给该占位符,而不是 tf.reshape() 的固定形状输出。

【讨论】:

  • 谢谢,这使我找到了解决方案,是的,问题在于如何在我的代码中的其他地方使用此代码。本质上,我的inputs 字典定义了用于预测的 JSON 输入文件的格式(见图 :-))。字典的值是张量,其形状被传递给tf.saved_model.signature_def_utils.build_signature_def(...)。这个模块有很好的教程或操作方法吗?文档看起来有点稀疏。无论如何,非常感谢@mrry
  • 我认为tf.saved_model 的唯一文档是这些:github.com/tensorflow/tensorflow/blob/master/tensorflow/python/…(加上代码本身的任何 cmets)。我相信团队正在编写一个使用它的教程,但可能值得通过打开GitHub issue 来请求更好的文档来提醒他们。
猜你喜欢
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 2014-03-13
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多