gpt4 book ai didi

python - 谷歌机器学习引擎 : Prediction failed: Error during model execution

转载 作者:行者123 更新时间:2023-11-28 22:19:38 24 4
gpt4 key购买 nike

我已使用 gcloud 命令行成功运行预测。我正在尝试运行 Python 脚本来运行预测。但是我遇到了错误。

Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] [[Node: map/while/decode_image/cond_jpeg/cond_png/cond_gif/Assert_1/Assert = Assert[T=[DT_STRING], summarize=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](map/while/decode_image/cond_jpeg/cond_png/cond_gif/is_bmp, map/while/decode_image/cond_jpeg/cond_png/cond_gif/Assert_1/Assert/data_0)]]")

from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
from googleapiclient import errors

PROJECTID = 'ai-assignment-185606'
projectID = 'projects/{}'.format(PROJECTID)
modelName = 'food_model'
modelID = '{}/models/{}/versions/{}'.format(projectID, modelName, 'v3')

scopes = ['https://www.googleapis.com/auth/cloud-platform']
credentials = GoogleCredentials.get_application_default()
ml = discovery.build('ml', 'v1', credentials=credentials)

with open('1.jpg', 'rb') as f:
b64_x = f.read()
import base64
import json

name = "7_5790100434_e2c3dbfdba.jpg";
with open("images/"+name, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
row = json.dumps({'inputs': {'b64': encoded_string}})

request_body = {"instances": row}

request = ml.projects().predict(name=modelID, body=request_body)
try:
response = request.execute()
except errors.HttpError as err:
print(err._get_reason())

if 'error' in response:
raise RuntimeError(response['error'])

print(response)

answer建议版本必须相同。我检查了 1.4 和 1.4.1 版本。

最佳答案

根据 https://cloud.google.com/ml-engine/docs/v1/predict-request ,该行应该是一个数据列表。每个数据可以是一个值、一个 JSON 对象或一个列表/嵌套列表:

{
"instances": [
<value>|<simple/nested list>|<object>,
...
]
}

相反,您的行是表示 JSON 的文本字符串(即接收者必须通过 json.loads(row) 才能获取 JSON)。试试这个:

instances = []
with open("images/"+name, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
instances.append({'b64': encoded_string})

request_body = {"instances": instances}

关于python - 谷歌机器学习引擎 : Prediction failed: Error during model execution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com