Skip to main content
 首页 » 编程设计

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

2024年08月20日28dflying

我有一个训练有素的 TF 模型,它在序列化 (TFRecord) 输入上运行。图像数据具有可变形状,并通过 tf.image.resize_images(...) 转换为 229x229x3 形状。我想使用 gcloud ml-engine predict platform类似于 this ,确保接受任何尺寸的图像作为输入。

我从以下函数获取我的特征张量(传递给预测图):

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

请您参考如下方法:

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

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

inputs = {'image': input_features} 

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