【问题标题】:tensorflow serving prediction not working with object detection pets example张量流服务预测不适用于对象检测宠物示例
【发布时间】:2017-07-28 13:40:00
【问题描述】:

我试图使用 tensorflow 对象检测宠物示例对 gcloud ml-engine 进行预测,但它不起作用。

我使用这个例子创建了一个检查点:https://github.com/tensorflow/models/blob/master/object_detection/g3doc/running_pets.md

在 tensorflow 团队的帮助下,我能够创建一个 saved_model 以上传到 gcloud ml-engine: https://github.com/tensorflow/models/issues/1811

现在,我可以将模型上传到 gcloud ml-engine。但不幸的是,我无法对模型进行正确的预测请求。每次我尝试预测时,都会得到同样的错误:

Input instances are not in JSON format.

我正在尝试使用

进行在线预测
gcloud ml-engine predict --model od_test --version v1 --json-instances prediction_test.json

我正在尝试使用

进行批量预测
gcloud ml-engine jobs submit prediction "prediction7" 
    --model od_test 
    --version v1 
    --data-format TEXT 
    --input-paths gs://ml_engine_test1/prediction_test.json 
    --output-path gs://ml_engine_test1/prediction_output 
    --region europe-west1

我想以 unit8 矩阵的形式提交图像列表,因此对于导出,我使用了输入类型 image_tensor

如文档中所述:https://cloud.google.com/ml-engine/docs/concepts/prediction-overview#prediction_input_data,输入 json 应具有特定格式。但是在线预测的格式和批量预测的格式都不起作用。我最近的测试是一个包含以下内容的文件:

{"instances": [{"values": [1, 2, 3, 4], "key": 1}]}

和内容:

{"images": [0.0, 0.3, 0.1], "key": 3}
{"images": [0.0, 0.7, 0.1], "key": 2}

他们都没有工作。谁能帮帮我,输入格式应该是怎样的?

编辑

批处理的错误是

{
    insertId:  "1a26yhdg2wpxvg6"   
    jsonPayload: {
        @type:  "type.googleapis.com/google.cloud.ml.api.v1beta1.PredictionLogEntry"    
        error_detail: {
            detail:  "No JSON object could be decoded"     
            input_snippet:  "Input snippet is unavailable."     
        }
        message:  "No JSON object could be decoded"    
    }
    logName:  "projects/tensorflow-test-1-168615/logs/worker"   
    payload: {
        @type:  "type.googleapis.com/google.cloud.ml.api.v1beta1.PredictionLogEntry"    
        error_detail: {
            detail:  "No JSON object could be decoded"     
            input_snippet:  "Input snippet is unavailable."     
        }
        message:  "No JSON object could be decoded"    
    }
    receiveTimestamp:  "2017-07-28T12:31:23.377623911Z"   
    resource: {
        labels: {
            job_id:  "prediction10"     
            project_id:  "tensorflow-test-1-168615"     
            task_name:  ""     
        }
        type:  "ml_job"    
    }
    severity:  "ERROR"   
    timestamp:  "2017-07-28T12:31:23.377623911Z"   
}

【问题讨论】:

  • 您报告的错误信息似乎来自gcloud ml-engine local predict,您能确认一下吗?如果有,服务返回的错误信息是什么?
  • 你是对的,错误似乎来自gcloud,而不是模型。
  • json.loads 引发ValueError 时会出现该错误消息。您介意提供我们输入文件链接的副本吗?
  • 你是说我用的那个文件?是的,没问题:drive.google.com/file/d/0B-LzDebt2EYGSmZ5d3l0NHplUW8/… 该文件是在 Windows 10 上使用 notepad++ 创建的。起初它的编码为 ANSI,在我第一次尝试后,我将编码更改为 UTF-8
  • 好的,所以你的文件还是有问题(前面有几个额外的字符):>>> open("prediction_test.json").read() '\xef\xbb\xbf{"instances": [{"inputs": [[[242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 23]]]}]}'我验证删除这些字符可以解析 json

标签: tensorflow gcloud tensorflow-serving google-cloud-ml-engine


【解决方案1】:

我相信特定模型需要二进制图像数据进行预测。

我希望您的请求类似于:

{
  instances: [
    { "images": { "b64": "image-bytes-base64-encoded" }, "key": 1 },
    ...
  ]
}

希望这有助于找到可行的解决方案。如果没有,请告诉我们,我会尽力为您提供更明确的信息。

【讨论】:

  • 我正在使用这种格式,但服务器一直拒绝。我在 C# 中使用 b64 转换。有什么帮助吗?错误“”{“错误”:“无法处理元素:0 键:\'instances\' 列表的图像。错误:无效参数:JSON 值:\“b64\”类型:字符串不是预期类型:float” }"
【解决方案2】:

如果您使用 gcloud 提交对gcloud ml-engine local predict 的请求以及批量预测,则您导出的模型接受如下输入以进行预测。

{"inputs": [[[242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 23]]]}
{"inputs": [[[232, 242, 219], [242, 240, 239], [242, 240, 239], [242, 242, 239], [242, 240, 123]]]}
...

如果您将请求直接发送到服务(即,不使用 gcloud),则请求的正文将如下所示:

{"instances": [{"inputs": [[[242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 239], [242, 240, 23]]]}]}
{"instances": [{"inputs": [[[232, 242, 219], [242, 240, 239], [242, 240, 239], [242, 242, 239], [242, 240, 123]]]}]}

输入张量名称应该是“inputs”,因为它在 signature.inputs 中是 what we've specified。从 here 可以看出,每个 JSON 对象的值都是一个 3-D 数组。外部维度为 None 以支持批量输入。不需要“实例”(除非您直接使用 http API)。请注意,您不能在输入中指定“键”,除非您修改图形以包含额外的占位符并使用 tf.identity 将其原样输出。

同样如the github issue中提到的,由于模型需要大内存,在线服务可能无法工作。我们正在努力。

【讨论】:

  • 不幸的是,这仍然不起作用。错误仍然是Input instances are not in JSON format. See "gcloud ml-engine predict --help" for details.。该错误显示得非常快,因此似乎 ml-engine 在模型加载之前就给出了这个错误。
  • 输入文件中是否有任何隐藏/不可见字符?你能运行 json 验证器来确保它是合法的 json 吗?
  • 在线 json 验证器说文件是正确的
  • 问题发现:用十六进制编辑器检查文件后,发现文件前面有三个字符。可能是因为从 ANSI 到 UTF-8 的转换,编辑器中没有显示 EF BB BF 这三个字符。非常感谢您的帮助。
  • FWIW 我建议导出模型以使用原始字节字符串。 JSON 中的 uint8 是一种相当低效的图像编码方式。
猜你喜欢
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 2017-12-21
  • 1970-01-01
  • 2019-07-12
  • 2020-02-14
  • 2020-07-06
相关资源
最近更新 更多