我制作了一个Tensorflow模型并将文件上传到GCP存储。
我试图从Google AI平台获得预测结果。但是它显示了错误。
我为具有以下角色的模型“ vgg_16”创建了一个serviceAccount。
roles/iam.securityAdmin
roles/iam.securityReviewer
roles/ml.admin
roles/ml.developer
roles/ml.modelOwner
roles/ml.modelUser
roles/ml.viewer
roles/owner
roles/viewer
此外,我的项目已激活计费。
我的代码:
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from oauth2client.service_account import ServiceAccountCredentials
import numpy
import base64
import logging
from IPython.display import display, Image
from google.cloud import storage
cropped_image = "8-android.widget.TextView.png"
display(Image(cropped_image))
PROJECT_ID = '6111116488'
MODEL_NAME = 'vgg_16'
VERSION_NAME ='v1'
def explicit():
# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json(
'D://setup//Development//sfsfc1111.json')
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)
def call_ml_service(img_str):
parent = 'projects/{}/models/{}/versions/{}'.format(PROJECT_ID, MODEL_NAME,VERSION_NAME)
pred = None
request_dict = {
"instances": [
{
"image": {
"b64": "%s"%img_str
}
}
]
}
try:
credentials = ServiceAccountCredentials.from_json_keyfile_name('D://setup//Development//sfsfc1111.json')
#credentials = GoogleCredentials.get_application_default()
cloudml_svc = discovery.build('ml', 'v1', credentials=credentials)
request = cloudml_svc.projects().predict(name=parent, body=request_dict)
response = request.execute()
print(response)
#pred = response['predictions'][0]['scores']
#pred = numpy.asarray(pred)
except:
logging.exception("Something went wrong!")
return pred
# base64 encode the same image
with open(cropped_image, 'rb') as image_file:
encoded_string = base64.b64encode(image_file.read())
#print(encoded_string)
explicit()
# See what ML Engine thinks
online_prediction = call_ml_service(encoded_string)
print(online_prediction)
但是403“无法访问模型。”发生了。 错误代码:
(tensorflow1.14) D:\work\models\research\slim>python inference_google_aiplatform.py
<IPython.core.display.Image object>
[<Bucket: android-ios-new-vcm>]
ERROR:root:Something went wrong!
Traceback (most recent call last):
File "inference_google_aiplatform.py", line 48, in call_ml_service
response = request.execute()
File "C:\Users\admin\Anaconda3\envs\tensorflow1.14\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\admin\Anaconda3\envs\tensorflow1.14\lib\site-packages\googleapiclient\http.py", line 907, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://ml.googleapis.com/v1/projects/6111116488/models/vgg_16/versions/v1:predict?alt=json returned "Access to model denied.">
None
有人可以帮助我吗?