预测为tensorflow savedModel且输入类型为字符串时收到“ Tensor占位符”错误

时间:2019-04-28 13:59:49

标签: tensorflow keras predict

我已将我的keras模型导出到tensorflow savedModel,并将其上传到云ML引擎以提供预测。当我发送json请求(输入为float)时,它可以正确地提供预测。但是当我发送json请求(带有图像的base64编码字符串)时,我得到了错误  “在图形中找不到在feed_devices或fetch_devices中指定的Tensor Placeholder_107:0”

K.clear_session()
sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(0)
model = load_model('modelName.h5')

image = tf.placeholder(shape=[None], dtype=tf.string)
export_path = 'path-name'

builder = saved_model_builder.SavedModelBuilder(export_path)

signature = predict_signature_def(inputs={'image_bytes': image},
                              outputs={'scores': model.output})

with K.get_session() as sess:
  builder.add_meta_graph_and_variables(sess=sess,
                                     tags=[tag_constants.SERVING],
                                     signature_def_map={
                                         'predict': signature})

builder.save()        

带有标签集:“ serve”的MetaGraphDef包含以下SignatureDef:

signature_def ['predict']:

给定的SavedModel SignatureDef包含以下输入:

      inputs['images'] tensor_info:
       dtype: DT_STRING
       shape: (-1)
       name: Placeholder_107:0                                     

给定的SavedModel SignatureDef包含以下输出:

   outputs['scores'] tensor_info:
    dtype: DT_FLOAT
    shape: (-1, 2)
    name: activation_14/Softmax:0

方法名称是:tensorflow / serving / predict

已传递图像数据以将其转换为json文件,以便可以将其发送到云ML引擎进行预测

from base64 import b64encode
from json import dumps
import json

with open(IMAGE_NAME, 'rb') as open_file:
   byte_content = open_file.read()
base64_bytes = b64encode(byte_content)                      
raw_data = {IMAGE_NAME: base64_bytes}
request_body= json.dumps({'images': base64_bytes})
with open('test_data.json', 'w') as outfile:
 outfile.write(request_body)

然后我将此json(图片的base64编码)传递给云ML引擎

  !gcloud ml-engine predict --model model-name --version version_3 --json-instances test_data.json

我收到以下错误

 ['{', '  "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\\"Tensor Placeholder_107:0, specified in either feed_devices or fetch_devices was not found in the Graph\\")"', '}']

我之前将模型输入发送为float,如下所示:

  signature = predict_signature_def(inputs={'image_bytes': image},
                              outputs={'scores': model.output})

当时

给定的SavedModel SignatureDef包含以下输入:

   inputs['images'] tensor_info:
     dtype: DT_FLOAT
     shape: (-1, 96, 96, 3)
     name: conv2d_6_input:0

我怀疑问题是由于此行引起的。 tf.placeholder(shape = [None],dtype = tf.string)。

一旦我将签名def更改为接受tf.placeholder,我就会遇到问题。我正在使用tf.placeholder使其接受输入作为字符串。

我尝试了很多方法来查看SO解决方案的一些输入,但没有帮助

请咨询

0 个答案:

没有答案