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

标签 python tensorflow gcloud google-prediction google-cloud-ml-engine

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

我从以下函数获取我的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

我收到一个预测错误:

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() 的固定形状输出。

关于python - Tensorflow:调整图像占位符大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938728/

相关文章:

python - 将非时间数据上采样为整数米

python - 随机打乱每行 numpy 数组中的项目

python - 即使已安装 Python 模型(使用 gcloud)也会出现导入错误

python - 导入错误: module object detection not found

tensorflow - 如何在 Tensorflow 中使用光流扭曲图像?

linux - 如何制作连接到 VM 并执行命令的 shell 脚本?

python - 如何在云中为 python 覆盖 GOOGLE_APPLICATION_CREDENTIALS

Python设置字符串的差异

python - 使用点符号字符串 "a.b.c.d.e"检查嵌套字典,自动创建缺失级别

python - 无法在 Google COLAB 上将我的 Dataframe 保存/导出为 CSV