【问题标题】:Tensorflow serving ml engine online prediction json file formatTensorflow服务ml引擎在线预测json文件格式
【发布时间】:2018-09-14 13:39:52
【问题描述】:

我想将一个 Tensorflow 模型保存到 GCP 上的 ml-engine 中,并进行在线预测。
我已经在 ml 引擎上成功创建了模型,但是,我正在努力将输入的 JSON 字符串输入到模型中。 这是 codedata,感谢 Jose Portilla 在 Udemy 上的 Tensorflow 课程。

我已经使用 gcloud 推荐进行预测:

gcloud ml-engine predict --model='lstm_test' --version 'v3' --json-instances ./test.json

test.json 内容:

{“输入”:[1,2,3,4,5,6,7,8,9,10,11,12]}

我得到的错误:

{ "error": "预测失败:模型执行期间出错:AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"您必须使用 dtype float 和 shape [?,12,1] 为占位符张量 'Placeholder_2' 提供一个值\n \t [[节点:Placeholder_2 = Placeholder_output_shapes=[[?,12,1]], dtype=DT_FLOAT, shape=[?,12,1], _device=\"/job:localhost/replica:0/task:0 /设备:CPU:0\"]]\")" }

【问题讨论】:

    标签: tensorflow machine-learning lstm tensorflow-serving google-cloud-ml


    【解决方案1】:

    一般来说,使用示例原型作为输入并不是使用 CloudML 服务的首选方法。相反,我们将直接使用占位符。

    另外,一般来说,您应该创建一个干净服务图,所以我还建议进行以下更改:

    def build_graph(x):
      # All the code shared between training and prediction, given input x
      ...
    
      outputs = ...
    
      # Make sure they both have a Saver.    
      saver = tf.train.Saver()
    
      return outputs, saver
    
    # Do training
    with tf.Graph().as_default() as prediction_graph:
      x = tf.placeholder(tf.float32, [None, num_time_steps, num_inputs])
      outputs, saver = build_graph(x)
    
    with tf.Session(graph=prediction_graph) as sess:
      session.run([tf.local_variables_initializer(), tf.tables_initializer()])
      saver.restore(session, latest)
    
    # This is a much simpler interface for saving models.
    tf.saved_model.simple_save(
        sess,
        export_dir=SaveModel_folder,
        inputs={"x": x},
        outputs={"y": outputs}
    )
    

    现在,与gcloud 一起使用的文件应该如下所示:

    [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
    [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
    

    这会发送一批两个实例(每行一个实例/示例),并假设 num_inputs 为 4,num_time_steps 为 3。

    一个更重要的警告是,gcloud 的文件格式与您使用传统客户端发送请求(例如 JS、Python、curl 等)时发送的请求的完整正文略有不同。上面同一个文件对应的请求正文是:

    {
      "instances": [
        [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
        [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
      ]
    }
    

    基本上,gcloud 文件中的每一行都成为“instances”数组中的一个条目。

    【讨论】:

    • 非常感谢您的建议!我已经相应地更新了 github 上的代码,还修改了输入 JSON 文件,这有助于我走得更远。但是,仍然存在问题。我已将错误包含在我的 github 问题中。 @rhaertel80
    • 我认为问题可能是 ml-engine 运行时版本 (1.4) 与训练版本 (1.7) 不同,因为 simple_save 是 1.7 功能。该代码适用于 gcloud ml-engine local predict。 @rhaertel80,你知道在 ml-engine 上提供 runtime 1.7 的时间表是什么吗?
    • 也许你应该用新问题打开一个新问题
    • 不匹配的版本经常会导致问题。我相信 1.7 应该会在一周内非正式地推出;它可能已经可以使用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2017-12-08
    • 2019-08-27
    • 2018-07-21
    • 2019-10-05
    • 2019-04-30
    • 2021-04-08
    相关资源
    最近更新 更多