在GCP AI平台的培训脚本中部署新模型版本时的权限问题

时间:2019-10-09 09:16:49

标签: google-cloud-ml

我正在尝试自动化GCP AI平台中的模型部署。即,在成功训练模型之后,我将模型和源程序包打包并上传到GCS,然后将其部署为新版本并设置为默认版本。我的训练脚本结尾处有这个。我需要这个,因为模型会定期重新训练。

在训练脚本中,打包和上传(用gsutil调用subprocess)工作正常,但是在尝试部署新版本时遇到了权限问题。我尝试过

  • gcloud ai-platform呼叫subprocess
  • 直接从discovery.build('ml', 'v1').projects().models().versions().create()呼叫googlecloudapis
  • 调用执行上述操作的Cloud Function

我以任何方式收到错误

ResponseError: status=[403], code=[Forbidden], message=[Request had insufficient authentication scopes.]

我已经为AI平台(service-xxxxxxxxx@cloud-ml.google.com.iam.gserviceaccount.com,Google Cloud ML引擎服务代理)的服务帐户添加了足够的权限,但是没有用。

在训练实例中似乎使用了一个不同的帐户。 discovery.build('ml', 'v1')._http.credentials._service_account返回default而不是电子邮件。

在继续使用Cloud Function监视训练脚本的导出之前,我想问一下我是否错过了任何内容,或者是否还有其他选择?

谢谢。

1 个答案:

答案 0 :(得分:0)

我查看了服务帐户权限,并看到了Cloud ML Engine Admin,Developer和Viewer。

下面的一些示例代码:

enter image description here

来自here

    Usually, you'll create these credentials with one of the helper
    constructors. To create credentials using a Google service account
    private key JSON file::
        credentials = service_account.Credentials.from_service_account_file(
            'service-account.json')
    Or if you already have the service account file loaded::
        service_account_info = json.load(open('service_account.json'))
        credentials = service_account.Credentials.from_service_account_info(
            service_account_info)
    Both helper methods pass on arguments to the constructor, so you can
    specify additional scopes and a subject if necessary::
        credentials = service_account.Credentials.from_service_account_file(
            'service-account.json',
            scopes=['email'],
            subject='user@example.com')

来自here

def get_client(service_account_json):
    """Returns an authorized API client by discovering the Healthcare API and
    creating a service object using the service account credentials JSON."""
    api_scopes = ['https://www.googleapis.com/auth/cloud-platform']

    credentials = service_account.Credentials.from_service_account_file(
        service_account_json)
    scoped_credentials = credentials.with_scopes(api_scopes)

    return discovery.build(
        'ml',
        'v1',        
        credentials=scoped_credentials)