2016-12-01 88 views

回答

5

我會告訴你如何來驗證您的生產環境中使用CloudML網上預報名。 CloudML快速入門使用gcloud通過用戶名,密碼等認證最終用戶。gcloud不能很好地適應100臺機器啓動和停止的環境。下面,我將引導您完成創建Cloud 服務帳戶並生成私鑰的步驟,以便您的生產實例向Google服務器標識自己。請參閱驗證文檔here

這裏有一個你可以使用的配方。

PROJECT= 
MODEL_NAME= 
SERVICE_ACCOUNT_PREFIX=cloud-ml-predict 
SERVICE_ACCOUNT="${SERVICE_ACCOUNT_PREFIX}@${PROJECT}.iam.gserviceaccount.com" 

這些步驟只能做一次,並且會爲您創建一個服務帳戶和私鑰。

# Make a new service account 
gcloud iam service-accounts create ${SERVICE_ACCOUNT_PREFIX} \ 
    --display-name ${SERVICE_ACCOUNT_PREFIX} 

# Provide correct role to service account permissions: 
gcloud projects add-iam-policy-binding $PROJECT \ 
    --member "serviceAccount:$SERVICE_ACCOUNT" --role roles/viewer 

# Create private key for the service account: 
gcloud iam service-accounts keys create --iam-account \ 
    $SERVICE_ACCOUNT private_key.json 

現在我們有一個私鑰(在private_key.json),我們可以稱之爲從具有googleapiclient Python庫的任何計算機預測API。現在從任何機器帶或不帶gcloud你只需要包括以下行通過HTTP

scopes = ['https://www.googleapis.com/auth/cloud-platform'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_filename, scopes=scopes) 
ml_service = discovery.build('ml', 'v1beta1', credentials=credentials) 

訪問CloudML預測服務最後,這裏有一個工作例子假設你從quickstarts部署的MNIST模型。

cat > key_pair_cloud_ml_serve.py <<EOD 
from googleapiclient import discovery 
import json 
from oauth2client.service_account import ServiceAccountCredentials 
import sys 

def get_mnist_prediction(ml_service, project, model_name, instance): 
    parent = 'projects/{}/models/{}'.format(project, model_name) 
    request_dict = {'instances': [json.loads(instance)]} 

    request = ml_service.projects().predict(name=parent, body=request_dict) 
    print request.execute() # waits till request is returned 

if __name__ == '__main__': 
    usage_str = 'usage: python prog private_key.json MODEL_NAME data/predict*json' 
    assert len(sys.argv) == 4, usage_str 

    key_file = sys.argv[1] 
    model_name = sys.argv[2] 
    data_file = sys.argv[3] 

    scopes = ['https://www.googleapis.com/auth/cloud-platform'] 
    credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file, 
scopes=scopes) 
    ml_service = discovery.build('ml', 'v1beta1', credentials=credentials) 
    with open(key_file) as ff: 
    project = json.load(ff)['project_id'] 


    with open(data_file) as ff: 
    for ii, instance in enumerate(ff): 
     get_mnist_prediction(ml_service, project, model_name, instance) 
EOD 

而且從我們所說的我們的代碼Cloud ML samplesmnist/deployable文件夾中...

python key_pair_cloud_ml_serve.py private_key.json \ 
    $MODEL_NAME data/predict_sample.tensor.json 


{u'predictions': [{u'prediction': 5, u'key': 0, u'scores': [0.04025577753782272, 0.00042669562390074134, 0.005919951014220715, 0.4221051335334778, 2.2986243493505754e-05, 0.5084351897239685, 0.0007824163185432553, 0.01125132292509079, 0.008616944774985313, 0.0021835025399923325]}]} 

瞧!我們使用私鑰,並且從不需要使用gcloud進行身份驗證或查詢我們的預測模型!

+0

你如何選擇哪個項目的服務帳戶將與步'相關gcloud IAM服務帳戶創建$ {SERVICE_ACCOUNT_PREFIX} \ --display名$ {SERVICE_ACCOUNT_PREFIX}' – Andrew

+0

安德魯,我不認爲它很重要。擁有該模型的項目仍將被記帳,並且仍然必須允許服務帳戶訪問(無論哪個項目擁有該項目)。擁有服務帳戶的項目僅保留刪除服務帳戶的能力。 – JoshGC

相關問題