Ran*_*lfo 6 python gcloud google-cloud-ml
我正在使用 google api python 客户端和在 google cloud 为我托管的模型在 google cloud machine learning API 上运行在线预测。当我预测发送一张图像时,服务器(包括所有流量)大约需要 40 秒。当我发送两个图像时,一段时间后,我收到消息:
timeout: The read operation timed out
Run Code Online (Sandbox Code Playgroud)
我想将超时设置为其他值,但我没有找到方法。
这是我的代码:
import base64
import io
import time
from PIL import Image
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
SERVICE_ACCOUNT_FILE = 'mycredentialsfile.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
ml = discovery.build('ml', 'v1', credentials=credentials)
projectID = 'projects/{}'.format('projectID') + '/models/{}'.format('modelID')
width = 640
height = 480
instances = []
for image in ["image5.jpg", "image6.jpg"]:
img = Image.open(image)
img = img.resize((width, height), Image.ANTIALIAS)
output_str = io.BytesIO()
img.save(output_str, "JPEG")
instance = {"b64": base64.b64encode(output_str.getvalue()).decode("utf-8") }
output_str.close()
instances.append(instance)
input_json = {"instances": instances }
request = ml.projects().predict(body=input_json, name=projectID)
print("Starting prediction")
start_time = time.time()
response = request.execute()
print("%s seconds" % (time.time() - start_time))
Run Code Online (Sandbox Code Playgroud)
我找到了一种从 github 上的 google api python 客户端研究样本并尝试相同更改的方法。
使用 httplib2 进行身份验证,您可以设置超时。
按照修改后的代码:
import base64
import io
import time
from PIL import Image
# Need: pip install google-api-python-client
import httplib2
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
# API & Services -> Credentials -> Create Credential -> service account key
SERVICE_ACCOUNT_FILE = 'mycredentialsfile.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
http = httplib2.Http(timeout=200)
http = credentials.authorize(http)
ml = discovery.build('ml', 'v1', http=http)
projectID = 'projects/{}'.format('projectID ') + '/models/{}'.format('modelID')
width = 640
height = 480
instances = []
for image in ["image5.jpg", "image6.jpg"]:
img = Image.open(image)
img = img.resize((width, height), Image.ANTIALIAS)
output_str = io.BytesIO()
img.save(output_str, "JPEG")
instance = {"b64": base64.b64encode(output_str.getvalue()).decode("utf-8") }
output_str.close()
instances.append(instance)
input_json = {"instances": instances }
request = ml.projects().predict(body=input_json, name=projectID)
print("Starting prediction")
start_time = time.time()
response = request.execute()
print("%s seconds" % (time.time() - start_time))
Run Code Online (Sandbox Code Playgroud)
我认为通过一些修改,您可以使用它为 python 客户端中的几乎所有谷歌云 API 设置超时。
我希望这有帮助。
是的。我同意上面 Shohei 的回答。我花了一段时间才找到这个简单而优雅的解决方案。您只需要在代码中添加以下内容
import socket
timeout_in_sec = 60*3 # 3 minutes timeout limit
socket.setdefaulttimeout(timeout_in_sec)
# then you could create your ML service object as usually, and it will have the extended timeout limit.
ml_service = discovery.build('ml', 'v1')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4314 次 |
| 最近记录: |