批量预测 google automl 通过 python

batch predictions google automl via python

我是使用 Whosebug 以及使用 google 云平台的新手,所以如果我没有以正确的格式问这个问题,我深表歉意。我目前面临从我的模型中获取预测的问题。

我已经在 google 云平台上训练了一个多标签 automl 模型,现在我想使用该模型来计算新的数据条目。 由于该平台只允许同时输入一个条目,因此我想利用 python 进行批量预测。

我已将我的数据条目存储在 google 云存储桶上的单独 .txt 文件中,并创建了一个 .txt 文件,其中我列出了对这些文件的 gs:// 引用(就像他们推荐的那样在 documentation).

我已经从服务帐户导出了一个包含我的凭据的 .json 文件,并在我的代码中指定了 ID 和路径:

# import API credentials and specify model / path references
path = 'xxx.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = path

model_name = 'xxx'
model_id = 'TCN1234567890'
project_id = '1234567890'
model_full_id = f"https://eu-automl.googleapis.com/v1/projects/{project_id}/locations/eu/models/{model_id}"
input_uri = f"gs://bucket_name/{model_name}/file_list.txt"
output_uri = f"gs://bucket_name/{model_name}/outputs/"
prediction_client = automl.PredictionServiceClient()

然后我 运行 使用以下代码来获得预测:

# score batch of file_list
gcs_source = automl.GcsSource(input_uris=[input_uri])

input_config = automl.BatchPredictInputConfig(gcs_source=gcs_source)
gcs_destination = automl.GcsDestination(output_uri_prefix=output_uri)
output_config = automl.BatchPredictOutputConfig(
    gcs_destination=gcs_destination
)

response = prediction_client.batch_predict(
    name=model_full_id,
    input_config=input_config,
    output_config=output_config
)

print("Waiting for operation to complete...")
print(
    f"Batch Prediction results saved to Cloud Storage bucket. {response.result()}"
)

但是,我收到以下错误:InvalidArgument: 400 请求包含无效参数。

谁知道是什么导致了这个问题? 任何输入将不胜感激!谢谢!

找到问题了!

我需要先将客户端设置为 'eu' 环境:

options = ClientOptions(api_endpoint='eu-automl.googleapis.com')
prediction_client = automl.PredictionServiceClient(client_options=options)