0

我已经在 Vertex AI(GCP 中 AI Platform 下的一项服务)中训练了一个 AutoML 对象检测模型。我正在尝试访问每个标签的模型评估指标(精度、召回率、准确性等),以获取不同的置信度分数阈值和 IoU 阈值。

然而,我被困在第一步,甚至让模型的性能指标远远低于粒度级别的性能指标。我已按照此说明进行操作,但我似乎无法弄清楚是什么(另请参阅此处evaluation_id的官方示例代码片段),即:

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`.
4

1 回答 1

2

我能够使用有据可查的aiplatform_v1获得模型评估的响应,这是从Vertex AI 参考页面链接的参考。

在此脚本上,我运行list_model_evaluations()来获取评估名称并将其用作get_model_evaluation()的输入,这将返回 Confidence Score Threshold、IoU Threshold 等的评估详细信息。

注意:我没有训练有素的模型,europe-west4所以我us-central1改用了。但是,如果您接受过培训,则应按位置europe-west4文件使用https://europe-west4-aiplatform.googleapis.comapi_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)

查看响应片段:

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中的类: 在此处输入图像描述

类的响应片段:

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
                          }
                        }
....
于 2021-10-25T02:40:16.533 回答