如何获取GCP Vertex AI AutoML目标检测不同阈值下的逐类别评估指标
报错修复方案
1. AutoML API 位置不匹配报错
你使用eu-automl.googleapis.com端点时,传入的位置参数不能是具体的区域europe-west4,必须替换为统一的区域标识eu,修改model_full_id构造代码即可:
# 把原来的europe-west4替换为eu model_full_id = client.model_path(project_id, "eu", model_id)
注:更推荐使用Vertex AI统一SDK完成指标查询,无需混用旧版AutoML API,避免多套接口的适配问题
2. Vertex AI ModelService 无效evaluation_id报错
evaluation_id不能随意填写,需要先调用列表接口拉取当前模型下的所有评估记录ID,再传入查询接口:
from google.cloud import aiplatform def list_model_evaluations(project: str, model_id: str, location: str = "europe-west4"): api_endpoint = f"{location}-aiplatform.googleapis.com" client_options = {"api_endpoint": api_endpoint} client = aiplatform.gapic.ModelServiceClient(client_options=client_options) parent = client.model_path(project, location, model_id) # 拉取所有评估记录 evaluations = client.list_model_evaluations(parent=parent) eval_ids = [] for eval in evaluations: # 从评估资源名中提取evaluation_id eval_id = eval.name.split("/")[-1] eval_ids.append(eval_id) print(f"找到评估ID: {eval_id}, 评估时间: {eval.create_time}") return eval_ids
用上述接口拿到可用的evaluation_id后,再传入你原来的get_model_evaluation_image_object_detection_sample方法即可避免400报错。
逐类别多阈值评估指标获取方法
成功拉取到ModelEvaluation响应后,目标检测的阈值对应指标存放在metrics字段的boundingBoxMetrics数组中:
- 数组每个元素对应一组IoU阈值+置信度阈值的组合
- 每个组合下的
annotationMetrics数组为对应每个类别的指标,包含precision、recall、f1Score等字段 - 每个类别的
annotationSpecId对应你在数据集中定义的标签ID,可提前通过模型的标签列表映射为可读的标签名
完整的指标查询示例代码:
def get_per_class_threshold_metrics(project: str, model_id: str, evaluation_id: str, location: str = "europe-west4"): api_endpoint = f"{location}-aiplatform.googleapis.com" client_options = {"api_endpoint": api_endpoint} client = aiplatform.gapic.ModelServiceClient(client_options=client_options) eval_name = client.model_evaluation_path(project, location, model_id, evaluation_id) response = client.get_model_evaluation(name=eval_name) bbox_metrics = response.metrics.get("boundingBoxMetrics", []) for metric_entry in bbox_metrics: iou_thresh = metric_entry["iouThreshold"] conf_thresh = metric_entry["confidenceThreshold"] print(f"\n=== IoU阈值: {iou_thresh}, 置信度阈值: {conf_thresh} ===") # 遍历每个类别的指标 for class_metric in metric_entry["annotationMetrics"]: label_id = class_metric["annotationSpecId"] precision = class_metric.get("precision", 0) recall = class_metric.get("recall", 0) f1 = class_metric.get("f1Score", 0) print(f"类别ID {label_id}: 精确率={precision:.4f}, 召回率={recall:.4f}, F1={f1:.4f}")
内容的提问来源于stack exchange,提问作者TwinPenguins
相关产品推荐
相关产品推荐

