Tensorflow 服务 ml 引擎在线预测 json 文件格式
Tensorflow serving ml engine online prediction json file format
我想将 Tensorflow 模型保存到 GCP 上的 ml-engine,并进行在线预测。
我已经在 ml-engine 上成功创建了模型,但是,我正在努力将输入 JSON 字符串输入到模型中。
这是 code and data,感谢 Jose Portilla 在 Udemy 上的 Tensorflow 课程。
我已经使用 gcloud commend 进行预测:
gcloud ml-engine predict --model='lstm_test' --version 'v3' --json-instances ./test.json
test.json内容:
{"inputs":[1,2,3,4,5,6,7,8,9,10,11,12]}
我遇到的错误:
{
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"You must feed a value for placeholder tensor 'Placeholder_2' with dtype float and shape [?,12,1]\n\t [[Node: Placeholder_2 = Placeholder_output_shapes=[[?,12,1]], dtype=DT_FLOAT, shape=[?,12,1], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"]]\")"
}
一般来说,使用示例原型作为输入并不是使用 CloudML 服务的首选方法。相反,我们将直接使用占位符。
此外,一般来说,您应该创建一个 clean 服务图,因此我还建议进行以下更改:
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]]
这将发送一批两个实例(每行一个 instance/example),并假设 num_inputs
是 4,num_time_steps
是 3。
一个更重要的警告,如果您使用传统客户端发送请求(例如 JS、Python、curl 等),gcloud 的文件格式与您发送的请求的完整主体略有不同.).上面同一个文件对应的请求体为:
{
"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" 数组中的一个条目。
我想将 Tensorflow 模型保存到 GCP 上的 ml-engine,并进行在线预测。
我已经在 ml-engine 上成功创建了模型,但是,我正在努力将输入 JSON 字符串输入到模型中。
这是 code and data,感谢 Jose Portilla 在 Udemy 上的 Tensorflow 课程。
我已经使用 gcloud commend 进行预测:
gcloud ml-engine predict --model='lstm_test' --version 'v3' --json-instances ./test.json
test.json内容:
{"inputs":[1,2,3,4,5,6,7,8,9,10,11,12]}
我遇到的错误:
{ "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"You must feed a value for placeholder tensor 'Placeholder_2' with dtype float and shape [?,12,1]\n\t [[Node: Placeholder_2 = Placeholder_output_shapes=[[?,12,1]], dtype=DT_FLOAT, shape=[?,12,1], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"]]\")" }
一般来说,使用示例原型作为输入并不是使用 CloudML 服务的首选方法。相反,我们将直接使用占位符。
此外,一般来说,您应该创建一个 clean 服务图,因此我还建议进行以下更改:
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]]
这将发送一批两个实例(每行一个 instance/example),并假设 num_inputs
是 4,num_time_steps
是 3。
一个更重要的警告,如果您使用传统客户端发送请求(例如 JS、Python、curl 等),gcloud 的文件格式与您发送的请求的完整主体略有不同.).上面同一个文件对应的请求体为:
{
"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" 数组中的一个条目。