GCP AI Platform API - 类级别的对象检测指标 (Python)
Posted
技术标签:
【中文标题】GCP AI Platform API - 类级别的对象检测指标 (Python)【英文标题】:GCP AI Platform API - Object Detection Metrics at Class Level (Python) 【发布时间】:2021-12-09 01:25:55 【问题描述】:我已经在 Vertex AI(GCP 中 AI Platform 下的一项服务)中训练了一个 AutoML 对象检测模型。我正在尝试访问每个标签的模型评估指标(精度、召回率、准确性等),以获取不同的 Confidence Score Threshold 和 IoU Threshold。
但是,我被困在第一步,甚至是让模型的性能指标远远低于粒度级别的性能指标。我关注了this instruction,但我似乎无法弄清楚evaluation_id
是什么(另见官方示例代码sn-p here),即:
def get_model_evaluation_image_object_detection_sample(
project: str,
model_id: str,
evaluation_id: str,
location: str = "us-central1",
api_endpoint: str = "us-central1-aiplatform.googleapis.com",
):
# The AI Platform services require regional API endpoints.
client_options = "api_endpoint": api_endpoint
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.ModelServiceClient(client_options=client_options)
name = client.model_evaluation_path(
project=project, location=location, model=model_id, evaluation=evaluation_id
)
response = client.get_model_evaluation(name=name)
print("response:", response)
一段时间后,我发现对于在欧盟训练的模型,api_endpoint
应传递为:
location: str = "europe-west4"
api_endpoint: str = "europe-west4-aiplatform.googleapis.com"
但无论我为 evaluation_id
尝试什么都会导致以下错误:
InvalidArgument: 400 List of found errors: 1.Field: name; Message: Invalid ModelEvaluation resource name.
在它说的文档中(它似乎包含我需要的东西):
对于边界框度量,Vertex AI 返回一个度量数组 不同 IoU 阈值(介于 0 和 1 之间)的值和 置信度阈值(介于 0 和 1 之间)。例如,您可以 在 IoU 阈值为 0.85 和 置信度阈值为 0.8228。通过查看这些不同的阈值 值,您可以看到它们如何影响其他指标,例如精度 并回忆。
在不知道输出数组中包含的情况下,这对每个类如何工作?基本上,我需要为每个类提供不同 IoU 阈值和置信度阈值的模型指标。
我也尝试从 AutoML API 查询,例如:
client_options = 'api_endpoint': 'eu-automl.googleapis.com:443'
client = automl.AutoMlClient(client_options=client_options)
# Get the full path of the model.
model_full_id = client.model_path(project_id, "europe-west4", model_id)
print("List of model evaluations:")
for evaluation in client.list_model_evaluations(parent=model_full_id, filter=""):
print("Model evaluation name: ".format(evaluation.name))
print("Model annotation spec id: ".format(evaluation.annotation_spec_id))
print("Create Time: ".format(evaluation.create_time))
print("Evaluation example count: ".format(evaluation.evaluated_example_count))
print(
"Classification model evaluation metrics: ".format(
evaluation.classification_evaluation_metrics
)
)
毫不奇怪,这也不起作用,并导致:
InvalidArgument: 400 List of found errors: 1.Field: parent; Message: The provided location ID doesn't match the endpoint. For automl.googleapis.com, the valid location ID is `us-central1`. For eu-automl.googleapis.com, the valid location ID is `eu`.
【问题讨论】:
【参考方案1】:我能够使用 aiplatform_v1 获得模型评估的响应,这是有据可查的,这是从 Vertex AI reference page 链接的参考。
在此脚本上,我运行 list_model_evaluations() 来获取评估名称并将其用作 get_model_evaluation() 的输入,这将返回 Confidence Score Threshold、IoU Threshold 等的评估详细信息。
注意:我在 europe-west4
中没有经过训练的模型,所以我改用了 us-central1
。但是,如果您接受过europe-west4
的培训,您应该按照location document 使用https://europe-west4-aiplatform.googleapis.com
作为api_endpoint
。
from google.cloud import aiplatform_v1 as aiplatform
api_endpoint = 'us-central1-aiplatform.googleapis.com'
client_options = "api_endpoint": api_endpoint
client_model = aiplatform.services.model_service.ModelServiceClient(client_options=client_options)
project_id = 'your-project-id'
location = 'us-central1'
model_id = '999999999999'
model_name = f'projects/project_id/locations/location/models/model_id'
list_eval_request = aiplatform.types.ListModelEvaluationsRequest(parent=model_name)
list_eval = client_model.list_model_evaluations(request=list_eval_request)
eval_name=''
for val in list_eval:
eval_name = val.name
get_eval_request = aiplatform.types.GetModelEvaluationRequest(name=eval_name)
get_eval = client_model.get_model_evaluation(request=get_eval_request)
print(get_eval)
查看回复 sn-p:
name: "projects/xxxxxxxxx/locations/us-central1/models/999999999999/evaluations/1234567890"
metrics_schema_uri: "gs://google-cloud-aiplatform/schema/modelevaluation/image_object_detection_metrics_1.0.0.yaml"
metrics
struct_value
fields
key: "boundingBoxMeanAveragePrecision"
value
number_value: 0.20201288
fields
key: "boundingBoxMetrics"
value
list_value
values
struct_value
fields
key: "confidenceMetrics"
value
list_value
values
struct_value
fields
key: "confidenceThreshold"
value
number_value: 0.06579724
fields
key: "f1Score"
value
number_value: 0.15670435
fields
key: "precision"
value
number_value: 0.09326923
fields
key: "recall"
value
number_value: 0.48989898
values
struct_value
....
编辑 1:按类获取响应
要获取每个类的指标,您可以使用list_model_evaluation_slices() 获取每个类的名称,然后将名称用于get_model_evaluation_slice()。在这段代码中,我将名称推送到一个列表中,因为我有多个类。然后只需使用存储在数组中的值来获取每个类的指标。
在我的代码中,我使用label[0]
来从这个类中获得一个响应。
from google.cloud import aiplatform_v1 as aiplatform
api_endpoint = 'us-central1-aiplatform.googleapis.com'
client_options = "api_endpoint": api_endpoint
client_model = aiplatform.services.model_service.ModelServiceClient(client_options=client_options)
project_id = 'your-project-id'
location = 'us-central1'
model_id = '999999999999'
model_name = f'projects/project_id/locations/location/models/model_id'
list_eval_request = aiplatform.types.ListModelEvaluationsRequest(parent=model_name)
list_eval = client_model.list_model_evaluations(request=list_eval_request)
eval_name=''
for val in list_eval:
eval_name = val.name
label=[]
slice_eval_request = aiplatform.types.ListModelEvaluationSlicesRequest(parent=eval_name)
slice_eval = client_model.list_model_evaluation_slices(request=slice_eval_request)
for data in slice_eval:
label.append(data.name)
get_eval_slice_request = aiplatform.types.GetModelEvaluationSliceRequest(name=label[0])
get_eval_slice = client_model.get_model_evaluation_slice(request=get_eval_slice_request)
print(get_eval_slice)
打印所有类:
UI 中的类:
类的响应 sn-p:
name: "projects/xxxxxxxxx/locations/us-central1/models/999999999/evaluations/0000000000/slices/777777777"
slice_
dimension: "annotationSpec"
value: "Cheese"
metrics_schema_uri: "gs://google-cloud-aiplatform/schema/modelevaluation/image_object_detection_metrics_1.0.0.yaml"
metrics
struct_value
fields
key: "boundingBoxMeanAveragePrecision"
value
number_value: 0.14256561
fields
key: "boundingBoxMetrics"
value
list_value
values
struct_value
fields
key: "confidenceMetrics"
value
list_value
values
struct_value
fields
key: "confidenceThreshold"
value
number_value: 0.06579724
fields
key: "f1Score"
value
number_value: 0.10344828
fields
key: "precision"
value
number_value: 0.06198347
....
【讨论】:
感谢您的回复。它就像 aiplatform_v1 和您的导入的魅力!我现在得到了特定模型 ID 的评估 ID。它返回一个带有所有置信度阈值及其相应指标的长 json;不知道是每班还是!但是缺少 IoU 和类标签。我想获得每个班级不同阈值的指标。有什么想法吗? @TwinPenguins 我更新了我的答案以包含每个班级的指标。 哇,按预期工作。我看到了切片,但发现它们是类。想知道为什么事情不明确。谢谢以上是关于GCP AI Platform API - 类级别的对象检测指标 (Python)的主要内容,如果未能解决你的问题,请参考以下文章
您的应用包含公开的 Google Cloud Platform (GCP) API 密钥
您的应用包含公开的 Google Cloud Platform (GCP) API 密钥。有关详细信息,请参阅此 Google 帮助中心文章
Google Cloud Platform - AI Platform:为啥调用 API 时会得到不同的响应正文?
Google Cloud Platform Vertex AI 日志未显示在自定义作业中