首页 文章

Tensorflow:调整图像占位符的大小

提问于
浏览
2

我有一个训练有素的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 的输出,它应具有正确的形状 . 对我在这里做错了什么的想法?提前致谢!

1 回答

  • 3

    看起来这个错误是由一些代码引起的,这些代码提供了 "RESHAPE_PREDICT:0" 张量(即 tf.reshape() op的输出, image ),而不是 "PREDICT_PLACEHOLDER:0" 张量(即 tf.image.resize_images() op的输入, input_features ) .

    如果没有经过训练的模型的完整来源,很难准确说出必要的更改,但可能只需将 inputs 的定义更改为:

    inputs = {'image': input_features}
    

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

相关问题