预测失败:内容必须是标量 [英] Prediction failed: contents must be scalar

查看:366
本文介绍了预测失败:内容必须是标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功培训,导出并将我的'retrained_graph.pb'上传到ML引擎。我的导出脚本如下:

 从tensorflow.python.saved_model导入tensorflow作为tf 
导入signature_constants
从tensorflow.python.saved_model导入tag_constants $ b $从tensorflow.python.saved_model导入生成器作为saved_model_builder

input_graph ='retrained_graph.pb'
saved_model_dir ='my_model'

with tf.Graph()。as_default()as graph:
#在输出图
中读入tf.gfile.FastGFile(input_graph,'rb')as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name ='')

#定义SavedModel签名(输入和输出)
in_image = graph.get_tensor_by_name('DecodeJpeg / contents:0')
inputs = {'image_bytes':tf.saved_model.utils.build_tensor_info(in_image)}

out_classes = graph.get_tensor_by_name('final_result:0')
outputs = {'prediction_bytes':tf .saved_model.utils.build_tensor_info(out_classes)}

签名= tf.saved_model.signature_def_utils.build_signature_def(
输入=输入,
输出=输出,
method_name = 'tensorflow / serving / predict'


with tf.Session(graph = graph)as sess:
#保存SavedModel。
b = saved_model_builder.SavedModelBuilder(saved_model_dir)
b.add_meta_graph_and_variables(sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map = {'serving_default':signature})
b.save()

我使用以下方法构建我的预测Json:

 #将图像复制到本地磁盘。 
gsutil cp gs://cloud-ml-data/img/flower_photos/tulips/4520577328_a94c11e806_n.jpg flower.jpg

#以json格式创建请求消息。
python -c'import base64,sys,json; img = base64.b64encode(open(sys.argv [1],rb)。read());打印json.dumps({image_bytes:{b64:img}})'flower.jpg&> request.json

#调用预测服务API获取分类
gcloud ml-engine predict --model $ {MODEL_NAME} --json-instances request.json

然而,这会失败并返回:

 error:预测失败:模型执行期间出错:AbortionError(code = StatusCode.INVALID_ARGUMENT,details = \内容必须是标量,变形[1] \\\
\t [[节点:Deco
deJpeg = DecodeJpeg [_output_shapes = [[?,?,3]],acceptable_fraction = 1,channels = 3,dct_method = \\,fancy_upscaling = true,ratio = 1, try_recover_truncated = false,_device = \/ job:l
ocalhost / replica:0 / task:0 / device:CPU:0 \](_ arg_DecodeJpeg / contents_0_0)]] \)

$ / code>

任何帮助表示感谢,我如此接近我可以品尝它:D

解决方案

为什么你有这条线in_image = graph.get_tensor_by_name('DecodeJpeg / contents:0')? b

inputs = {'image_bytes':tf.saved_model.utils.build_tensor_info(in_image)}这里的形状是标量。你可以确保你创建形状输入[无]



https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/flowers/trainer/model.py#L364


I have successfully trained, exported and uploaded my 'retrained_graph.pb' to ML Engine. My export script is as follows:

import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import builder as saved_model_builder

input_graph = 'retrained_graph.pb'
saved_model_dir = 'my_model'

with tf.Graph().as_default() as graph:
  # Read in the export graph
  with tf.gfile.FastGFile(input_graph, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString(f.read())
      tf.import_graph_def(graph_def, name='')

  # Define SavedModel Signature (inputs and outputs)
  in_image = graph.get_tensor_by_name('DecodeJpeg/contents:0')
  inputs = {'image_bytes': tf.saved_model.utils.build_tensor_info(in_image)}

  out_classes = graph.get_tensor_by_name('final_result:0')
  outputs = {'prediction_bytes': tf.saved_model.utils.build_tensor_info(out_classes)}

  signature = tf.saved_model.signature_def_utils.build_signature_def(
      inputs=inputs,
      outputs=outputs,
      method_name='tensorflow/serving/predict'
  )

  with tf.Session(graph=graph) as sess:
    # Save out the SavedModel.
    b = saved_model_builder.SavedModelBuilder(saved_model_dir)
    b.add_meta_graph_and_variables(sess,
                               [tf.saved_model.tag_constants.SERVING],
                               signature_def_map={'serving_default': signature})
    b.save() 

I build my prediction Json using the following:

# Copy the image to local disk.
gsutil cp gs://cloud-ml-data/img/flower_photos/tulips/4520577328_a94c11e806_n.jpg flower.jpg

# Create request message in json format.
python -c 'import base64, sys, json; img = base64.b64encode(open(sys.argv[1], "rb").read()); print json.dumps({"image_bytes": {"b64": img}}) ' flower.jpg &> request.json

# Call prediction service API to get classifications
gcloud ml-engine predict --model ${MODEL_NAME} --json-instances request.json

However this fails with the response:

{
  "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"contents must be scalar, got shape [1]\n\t [[Node: Deco
deJpeg = DecodeJpeg[_output_shapes=[[?,?,3]], acceptable_fraction=1, channels=3, dct_method=\"\", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device=\"/job:l
ocalhost/replica:0/task:0/device:CPU:0\"](_arg_DecodeJpeg/contents_0_0)]]\")"
}

Any help appreciated, I'm so close I can taste it :D

解决方案

Why do you have this line in_image = graph.get_tensor_by_name('DecodeJpeg/contents:0')?

inputs = {'image_bytes': tf.saved_model.utils.build_tensor_info(in_image)} The shape here is scalar. Can you make sure you create input with shape [None]

https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/flowers/trainer/model.py#L364

这篇关于预测失败:内容必须是标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆