AI开发平台ModelArts-创建并完成图像分类的智能标注任务:操作步骤

时间:2025-02-12 15:15:44

操作步骤

  1. 调用认证鉴权接口获取用户的Token。
    1. 请求消息体:

      URI格式:POST https://{iam_endpoint}/v3/auth/tokens

      请求消息头:Content-Type →application/json

      请求Body:
      {  "auth": {    "identity": {      "methods": ["password"],      "password": {        "user": {          "name": "user_name",           "password": "user_password",          "domain": {            "name": "domain_name"            }        }      }    },    "scope": {      "project": {        "name": "cn-north-1"        }    }  }}
      其中,加粗的斜体字段需要根据实际值填写:
      • iam_endpoint IAM 的终端节点。
      • user_name为IAM用户名。
      • user_password为用户登录密码。
      • domain_name为用户所属的账号名。
      • cn-north-1为项目名,代表服务的部署区域。
    2. 返回状态码“201 Created”,在响应Header中获取“X-Subject-Token”的值即为Token,如下所示:
      x-subject-token →MIIZmgYJKoZIhvcNAQcCoIIZizCCGYcCAQExDTALBglghkgBZQMEAgEwgXXXXXX...
  2. 调用启动智能任务接口给图像分类的数据集创建一个智能标注任务。
    1. 请求消息体:

      URI格式:POST https://{ma_endpoint}/v2/{project_id}/datasets/{dataset_id}/tasks

      请求消息头:
      • X-auth-Token →MIIZmgYJKoZIhvcNAQcCoIIZizCCGYcCAQExDTALBglghkgBZQMEAgEwgXXXXXX...
      • Content-Type →application/json

      其中,加粗的斜体字段需要根据实际值填写:

      • ma_endpoint为ModelArts的终端节点。
      • project_id为用户的项目ID。
      • dataset_id为待启动智能标注任务的数据集ID。
      • X-auth-Token的值为获取到的Token值。
      请求body:
      {  "task_type" : "auto-label",  "collect_key_sample" : true,  "config" : {    "algorithm_type" : "fast"}}

      只有“task_type”填写“auto-label”才表示创建的是智能标注任务。

    2. 返回状态码为“200 OK”,响应Body如下所示:
      {    "task_id": "7eAtpiNSaI1mCHJK3SJ"}

      “task_id”为创建的智能标注任务ID,记录该值用于后续步骤。

  3. 调用获取智能任务的信息接口根据智能标注的任务ID查询任务详情。
    1. 请求消息体:

      URI格式:GET https://{ma_endpoint}/v2/{project_id}/datasets/{dataset_id}/tasks/{task_id}

      请求消息头:X-auth-Token →MIIZmgYJKoZIhvcNAQcCoIIZizCCGYcCAQExDTALBglghkgBZQMEAgEwgXXXXXX...

      其中,加粗的斜体字段需要根据实际值填写,task_id为上一步记录的任务ID。

    2. 返回状态码为“200 OK”,响应Body如下所示:
      {    "resource_id": "6mHUGe7ETlhayb4qDMN",    "create_time": "2021-06-18 19:14:55",    "progress": 30,    "status": 1,    "message": "Submit auto labeling task succeed, waiting for auto labeling task to finish. ",    "code": "ModelArts.4914",    "elapsed_time": 55,    "result": {        "total_sample_count": 30,        "annotated_sample_count": 28,        "events": [            {                "name": "start",                "create_time": 1624014895314,                "elapsed_time": 21,                "ordinal": 1,                "level": 1,                "status": "success"            },            {                "name": "init",                "create_time": 1624014916770,                "elapsed_time": 0,                "ordinal": 2,                "level": 1,                "status": "success"            },            {                "name": "labeling",                "create_time": 1624014916770,                "ordinal": 3,                "level": 1,                "events": [                    {                        "name": "jobstat_init",                        "create_time": 1624014927183,                        "elapsed_time": 0,                        "ordinal": 1,                        "level": 2,                        "status": "success"                    },                    {                        "name": "jobstat_waiting",                        "create_time": 1624014927184,                        "elapsed_time": 0,                        "ordinal": 2,                        "level": 2,                        "status": "success"                    },                    {                        "name": "jobstat_running",                        "create_time": 1624014927184,                        "elapsed_time": 20,                        "ordinal": 3,                        "level": 2,                        "status": "running"                    },                    {                        "name": "jobstat_completed",                        "ordinal": 4,                        "level": 2,                        "status": "waiting"                    }                ],                "status": "running"            },            {                "name": "result_processing",                "ordinal": 4,                "level": 1,                "status": "waiting"            },            {                "name": "end",                "ordinal": 5,                "level": 1,                "status": "waiting"            }        ],        "train_job_name": "AUTO_TRAINING_cls_6mHUGe7ETlhayb4qDMN_7eAtpiNSaI",        "train_job_id": "932777",        "version_id": "1777740",        "continuity": false    },    "export_type": 0,    "config": {        "ambiguity": false,        "name": "ouIsrAuG226MgMknu9j",        "worker_server_num": 0,        "collect_sample": false,        "confidence_scope": "0.0-0.5",        "algorithm_type": "fast",        "image_brightness": false,        "image_colorfulness": false    }}

      根据响应可以了解智能标注任务详情,其中“progress”“30”表示当前任务进度为30%,“status”“1”表示任务状态为在运行中。

  4. 待智能标注任务完成后,调用查询智能标注的样本列表接口可以查看标注结果。
    1. 请求消息体:

      URI格式:GET https://{ma_endpoint}/v2/{project_id}/datasets/{dataset_id}/auto-annotations/samples

      请求消息头:X-auth-Token →MIIZmgYJKoZIhvcNAQcCoIIZizCCGYcCAQExDTALBglghkgBZQMEAgEwgXXXXXX...

      其中,加粗的斜体字段需要根据实际值填写。

    2. 返回状态码为“200 OK”,响应Body如下所示:
      {    "sample_count": 2,    "samples": [        {            "sample_id": "44827d0dc5fd6ff5be6dd0e14103121e",            "sample_type": 0,            "labels": [                {                    "name": "rose",                    "type": 0,                    "id": "1",                    "property": {                        "@modelarts:hard_coefficient": "0.0",                        "@modelarts:hard": "false"                    },                    "score": 1                }            ],            "source": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/145862135_ab710de93c_n.jpg......",            "preview": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/145862135_ab710de93c_n.jpg......",            "metadata": {                "@modelarts:new_source": "s3://test/DATASETS/input/145862135_ab710de93c_n.jpg",                "@modelarts:hard_coefficient": 1,                "@modelarts:origin_source": "s3://test/DATASETS/input/145862135_ab710de93c_n.jpg",                "@modelarts:hard": true,                "@modelarts:source_image_info": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/145862135_ab710de93c_n.jpg......",                "@modelarts:data_checked": true,                "@modelarts:origin_source_id": "44827d0dc5fd6ff5be6dd0e14103121e",                "@modelarts:import_origin": 0,                "@modelarts:size": [                    320,                    238,                    3                ],                "@modelarts:check_result": [                    {                        "language": "en-us",                        "result_property": 0,                        "result_descriptions": []                    },                    {                        "language": "zh-cn",                        "result_property": 0,                        "result_descriptions": []                    }                ],                "@modelarts:hard_reasons": [                    3                ]            },            "sample_time": 1623922030282,            "sample_status": "UN_ANNOTATION",            "annotated_by": ""        },        {            "sample_id": "1b87c42574ddc1faf05580d2e3a7bd4f",            "sample_type": 0,            "labels": [                {                    "name": "dasiy",                    "type": 0,                    "id": "1",                    "property": {                        "@modelarts:hard_coefficient": "0.0",                        "@modelarts:hard": "false"                    },                    "score": 1                }            ],            "source": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/153210866_03cc9f2f36.jpg?......",            "preview": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/153210866_03cc9f2f36.jpg?......",            "metadata": {                "@modelarts:new_source": "s3://test/DATASETS/input/153210866_03cc9f2f36.jpg",                "@modelarts:hard_coefficient": 0,                "@modelarts:origin_source": "s3://test/DATASETS/input/153210866_03cc9f2f36.jpg",                "@modelarts:hard": false,                "@modelarts:source_image_info": "https://test.obs.{ma_endpoint}.com:443/DATASETS/input/153210866_03cc9f2f36.jpg?......",                "@modelarts:data_checked": true,                "@modelarts:origin_source_id": "1b87c42574ddc1faf05580d2e3a7bd4f",                "@modelarts:import_origin": 0,                "@modelarts:size": [                    500,                    375,                    3                ],                "@modelarts:check_result": [                    {                        "language": "en-us",                        "result_property": 0,                        "result_descriptions": []                    },                    {                        "language": "zh-cn",                        "result_property": 0,                        "result_descriptions": []                    }                ],                "@modelarts:hard_reasons": []            },            "sample_time": 1623922028739,            "sample_status": "UN_ANNOTATION",            "annotated_by": ""        }    ]}

      根据响应可以了解智能标注的样本数量和样本列表,由“sample_count”参数可知总共标注了2个样本,从“@modelarts:hard”参数可知有一个难例样本。

  5. 调用批量更新样本标签接口根据上一步获取的智能标注样本列表确认智能标注结果。
    1. 请求消息体:

      URI格式:PUT https://{ma_endpoint}/v2/{project_id}/datasets/{dataset_id}/data-annotations/samples

      请求消息头:

      • X-auth-Token →MIIZmgYJKoZIhvcNAQcCoIIZizCCGYcCAQExDTALBglghkgBZQMEAgEwgXXXXXX...
      • Content-Type →application/json

      其中,加粗的字段需要根据实际值填写。

      复制上一步的响应body,只保留“sample_id”“labels”字段直接作为请求body,如下所示:
      {    "samples": [        {            "sample_id": "44827d0dc5fd6ff5be6dd0e14103121e",            "labels": [                {                    "name": "rose",                    "type": 0,                    "id": "1",                    "property": {                        "@modelarts:hard_coefficient": "0.0",                        "@modelarts:hard": "false"                    },                    "score": 1                }            ],        },        {            "sample_id": "1b87c42574ddc1faf05580d2e3a7bd4f",            "labels": [                {                    "name": "dasiy",                    "type": 0,                    "id": "1",                    "property": {                        "@modelarts:hard_coefficient": "0.0",                        "@modelarts:hard": "false"                    },                    "score": 1                }            ],        }    ]}
    2. 返回状态码为“200 OK”表示确认完成,响应Body如下所示:
      {  "success" : true}
support.huaweicloud.com/api-modelarts/modelarts_03_0403.html