[英]ML Engine Online Prediction - Unexpected tensor name: values
I get the following error when trying to make an online prediction on my ML Engine model.尝试对我的 ML Engine 模型进行在线预测时,我收到以下错误。 The key "values" is not correct.
关键“值”不正确。 (See error on image.) enter image description here
(请参阅图像上的错误。)在此处输入图像描述
I already tested with RAW image data : {"image_bytes":{"b64": base64.b64encode(jpeg_data)}}
& Converted the data to a numpy array.我已经用 RAW 图像数据进行了测试:
{"image_bytes":{"b64": base64.b64encode(jpeg_data)}}
& 将数据转换为一个 numpy 数组。
Currently I have the following code:目前我有以下代码:
from googleapiclient import discovery
import base64
import os
from PIL import Image
import json
import numpy as np
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jacob/Desktop/******"
def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction.
Args:
project (str): project where the Cloud ML Engine Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']
savepath = 'upload/11277229_F.jpg'
img = Image.open('test/01011000/11277229_F.jpg')
test = img.resize((299, 299))
test.save(savepath)
img1 = open(savepath, "rb").read()
def load_image(filename):
with open(filename) as f:
return np.array(f.read())
predict_json('image-recognition-25***08', 'm500_200_waug', [{"values": str(base64.b64encode(img1).decode("utf-8")), "key": '87'}], 'v1')
The error message itself indicates (as you point out in the question), that the key "values" is not one of the inputs specified in the model.错误消息本身表明(正如您在问题中指出的那样),键“值”不是模型中指定的输入之一。 To inspect the model's input, use
saved_model_cli show --all --dir=/path/to/model
.要检查模型的输入,请使用
saved_model_cli show --all --dir=/path/to/model
。 That will show you a list of the names of the inputs.这将显示输入名称的列表。 You'll need to use the correct name.
您需要使用正确的名称。
That said, it appears there is another issue.也就是说,似乎还有另一个问题。 It's not clear from the question what type of input your model is expecting, though it's likely one of two things:
从问题中不清楚您的模型期望什么类型的输入,尽管它可能是以下两种情况之一:
The exact solution will depend on which of the above your exported model is using.确切的解决方案将取决于您的导出模型使用的是上述哪一个。
saved_model_cli
will help here, based on the type and shape of the input.根据输入的类型和形状,
saved_model_cli
将在这里有所帮助。 It will either be DT_FLOAT32
(or some other int/float type) and [NONE, 299, 299, CHANNELS]
or DT_STRING
and [NONE]
, respectively.它将分别是
DT_FLOAT32
(或其他一些 int/float 类型)和[NONE, 299, 299, CHANNELS]
或DT_STRING
和[NONE]
。
If your model is type (1), then you will need to send a matrix of ints/floats (which does not use base64 encoding):如果您的模型是类型 (1),那么您将需要发送一个整数/浮点数矩阵(不使用 base64 编码):
predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: load_image(savepath).tolist(), "key": '87'}], 'v1')
Note the use of tolist
to convert the numpy array to a list of lists.请注意使用
tolist
将 numpy 数组转换为列表列表。
In the case of type (2), you need to tell the service you have some base64 data by adding in {"b64": ...}:在类型 (2) 的情况下,您需要通过添加 {"b64": ...} 来告诉服务您有一些 base64 数据:
predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: {"b64": str(base64.b64encode(img1).decode("utf-8"))}, "key": '87'}], 'v1')
All of this, of course, depends on using the correct name for CORRECT_INPUT_NAME
.当然,所有这些都取决于使用正确的
CORRECT_INPUT_NAME
名称。
One final note, I'm assuming your model actually does have key
as an additional inputs since you included it in your request;最后一点,我假设您的模型确实有
key
作为附加输入,因为您将它包含在您的请求中; again, that can all be verified against the output of saved_model_cli show
.同样,这都可以根据
saved_model_cli show
的输出进行验证。
I used to get this errors too.我曾经也遇到过这个错误。 If anyone comes across this error, and using gcloud.
如果有人遇到此错误,并使用 gcloud。
Tensors are automatically called csv_rows.张量自动称为 csv_rows。 For example this works for me now
例如,这现在对我有用
"instances": [{
"csv_row": "STRING,7,4.02611534,9,14,0.66700000,0.17600000,0.00000000,0.00000000,1299.76500000,57",
"key": "0"
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.