简体   繁体   English

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

[英]Tensorflow: Resize Image Placeholder

I have a trained TF model that that operates on a serialized ( TFRecord ) input. 我有一个受过训练的TF模型,该模型可以处理序列化( TFRecord )输入。 The image data has variable shape and is converted to a 229x229x3 shape via tf.image.resize_images(...) . 图像数据具有可变的形状,并通过tf.image.resize_images(...)转换为229x229x3形状。 I would like to use the gcloud ml-engine predict platform similar to this , making sure to accept any size image as input. 我想用gcloud ml-engine predict 平台类似于 ,确保接受任何尺寸的图像作为输入。

I get my features tensor (which is passed to the prediction graph) from the following function: 我从以下函数获取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
  }

The tf.reshape at the end is because my prediction graph expects a tensor of shape [batch_size, 229, 229, 3] . 最后的tf.reshape是因为我的预测图期望形状为[batch_size, 229, 229, 3] tf.reshape的张量。 When I run this through the engine via 当我通过通过引擎运行

gcloud ml-engine local predict \
--model-dir=trained_model/export/ \
--json-instances=img.json

I get a PredictionError : 我收到一个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)'")

It looks to me like tf.reshape is being fed the output of tf.image.resize_images which should have the correct shape. 在我看来, tf.reshape正在馈送应该具有正确形状的tf.image.resize_images输出。 Any thoughts on what I'm doing wrong here? 对我在这里做错的任何想法吗? Thanks in advance! 提前致谢!

It looks like the error is caused by some code that feeds the "RESHAPE_PREDICT:0" tensor (ie the output of the tf.reshape() op, image ) rather than the "PREDICT_PLACEHOLDER:0" tensor (ie the input to the tf.image.resize_images() op, input_features ). 看来该错误是由于某些代码馈入了"RESHAPE_PREDICT:0"张量(即tf.reshape() op, image的输出)而不是"PREDICT_PLACEHOLDER:0"张量(即, tf.image.resize_images()的输入)引起的tf.image.resize_images() op, input_features )。

Without the whole source to your trained model, it's hard to say exactly what changes are necessary, but it might be as simple as changing the definition of inputs to: 如果没有经过培训的模型的全部资料,很难确切地说出需要进行哪些更改,但是可能就像将inputs的定义更改为以下那样简单:

inputs = {'image': input_features}

...so that the prediction service knows to feed values to that placeholder, rather than the fixed-shape output of tf.reshape() . ...以便预测服务知道将值提供给该占位符,而不是tf.reshape()的固定形状输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM