创建标签集

标签集包含您希望人工标签添加者为您的图片添加的一组标签。举例来说,如果您想要对含有猫或狗的图片进行分类,则可以创建一个包含“Dog”和“Cat”这两个标签的标签集(实际上,您可能还需要使用表示“Neither”和“Both”的标签,如下所述)。 您的标签集最多可包含 100 个标签。

一个项目可以有多个标签集,每个标签集分别用于不同的数据标签服务请求。您可以获取可用的标签集列表,以及删除不再需要的标签集;如需了解详情,请参阅注解规范集资源页面

设计优质标签集

以下是创建优质标签集的几项准则。

  • 将每个标签的显示名设置为有意义的字词,例如“dog”“cat”或“building”。请勿使用“label1”和“label2”之类的抽象名称或冷僻的首字母缩写词。标签名称越有意义,人工标签添加者越容易准确应用标签名称并保持一致性。
  • 确保标签易于彼此区分。对于将单个标签应用到每个数据项的分类任务,请尽量不要使用含义重叠的标签。
  • 通常情况下,对于分类任务,最好包含名为“其他”或“无”的标签,以用于与其他标签不匹配的数据。例如,如果唯一可用的标签是“狗”和“猫”,则标签添加者必须使用这两个标签之一为每张图片加标签。如果自定义模型的训练数据中包含除狗或猫以外的图片,则该模型通常会更加完善。
  • 请注意,为使标签添加者能够以最佳的效率准确地进行工作,建议您将标签集内的标签数量维持在 20 个以内。

创建标签集资源

网页界面

  1. 打开数据标签服务界面

    标签集页面会显示之前为当前项目创建的标签集的状态。

    如需为其他项目添加标签集,请从标题栏右上角的下拉列表中选择项目。

  2. 点击标题栏中的创建按钮。

  3. 创建标签集页面上,为标签集输入名称和描述。

  4. 标签部分中,为您希望人工标签添加者应用的每个标签输入名称和描述。

    为标签输入名称和描述后,点击添加标签为其他标签添加一行。您最多可以添加 100 个标签。

  5. 点击创建以创建注解规范集。

    您会返回到标签集列表页面。

命令行

如需创建标签集资源,请以 JSON 格式列出所有标签,然后将其传递给数据标签服务。

以下示例会创建一个名为 code_sample_label_set 且含有两个标签的标签集。

保存新标签集的 "name"(来自响应),以用于其他操作(例如发送标签请求)。

curl -X POST \
     -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
     -H "Content-Type: application/json" \
     https://datalabeling.googleapis.com/v1beta1/projects/"${PROJECT_ID}"/annotationSpecSets \
     -d '{
       "annotationSpecSet": {
           "displayName": "code_sample_label_set",
           "description": "code sample general label set",
           "annotationSpecs": [
               {
                   "displayName": "dog",
                   "description": "label dog",
               },
               {
                   "displayName": "cat",
                   "description": "label cat",
               }
           ],
       },
    }'

您将看到如下所示的输出:

{
  "name": "projects/data-labeling-codelab/annotationSpecSets/5c73db2d_0000_2f46_983d_001a114a5d7c",
  "displayName": "code_sample_label_set",
  "description": "code sample general label set",
  "annotationSpecs": [
    {
      "displayName": "dog",
      "description": "label dog"
    },
    {
      "displayName": "cat",
      "description": "label cat"
    }
  ]
}

Python

在运行此代码示例之前,您必须先安装 Python 客户端库

def create_annotation_spec_set(project_id):
    """Creates a data labeling annotation spec set for the given
    Google Cloud project.
    """
    from google.cloud import datalabeling_v1beta1 as datalabeling

    client = datalabeling.DataLabelingServiceClient()

    project_path = f"projects/{project_id}"

    annotation_spec_1 = datalabeling.AnnotationSpec(
        display_name="label_1", description="label_description_1"
    )

    annotation_spec_2 = datalabeling.AnnotationSpec(
        display_name="label_2", description="label_description_2"
    )

    annotation_spec_set = datalabeling.AnnotationSpecSet(
        display_name="YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME",
        description="YOUR_DESCRIPTION",
        annotation_specs=[annotation_spec_1, annotation_spec_2],
    )

    response = client.create_annotation_spec_set(
        request={"parent": project_path, "annotation_spec_set": annotation_spec_set}
    )

    # The format of the resource name:
    # project_id/{project_id}/annotationSpecSets/{annotationSpecSets_id}
    print(f"The annotation_spec_set resource name: {response.name}")
    print(f"Display name: {response.display_name}")
    print(f"Description: {response.description}")
    print("Annotation specs:")
    for annotation_spec in response.annotation_specs:
        print(f"\tDisplay name: {annotation_spec.display_name}")
        print(f"\tDescription: {annotation_spec.description}\n")

    return response

Java

必须先安装 Java 客户端库,然后才能运行此代码示例。
import com.google.cloud.datalabeling.v1beta1.AnnotationSpec;
import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet;
import com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings;
import com.google.cloud.datalabeling.v1beta1.ProjectName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class CreateAnnotationSpecSet {

  // Create an annotation spec set.
  static void createAnnotationSpecSet(String projectId) throws IOException {
    // String projectId = "YOUR_PROJECT_ID";

    Map<String, String> annotationLabels = new HashMap<>();
    annotationLabels.put("label_1", "label_1_description");
    annotationLabels.put("label_2", "label_2_description");

    DataLabelingServiceSettings settings =
        DataLabelingServiceSettings.newBuilder()
            .build();
    try (DataLabelingServiceClient dataLabelingServiceClient =
        DataLabelingServiceClient.create(settings)) {
      ProjectName projectName = ProjectName.of(projectId);

      List<AnnotationSpec> annotationSpecs = new ArrayList<>();
      for (Entry<String, String> entry : annotationLabels.entrySet()) {
        AnnotationSpec annotationSpec =
            AnnotationSpec.newBuilder()
                .setDisplayName(entry.getKey())
                .setDescription(entry.getValue())
                .build();
        annotationSpecs.add(annotationSpec);
      }

      AnnotationSpecSet annotationSpecSet =
          AnnotationSpecSet.newBuilder()
              .setDisplayName("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")
              .setDescription("YOUR_DESCRIPTION")
              .addAllAnnotationSpecs(annotationSpecs)
              .build();

      CreateAnnotationSpecSetRequest request =
          CreateAnnotationSpecSetRequest.newBuilder()
              .setAnnotationSpecSet(annotationSpecSet)
              .setParent(projectName.toString())
              .build();

      AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request);

      System.out.format("Name: %s\n", result.getName());
      System.out.format("DisplayName: %s\n", result.getDisplayName());
      System.out.format("Description: %s\n", result.getDescription());
      System.out.format("Annotation Count: %d\n", result.getAnnotationSpecsCount());

      for (AnnotationSpec annotationSpec : result.getAnnotationSpecsList()) {
        System.out.format("\tDisplayName: %s\n", annotationSpec.getDisplayName());
        System.out.format("\tDescription: %s\n\n", annotationSpec.getDescription());
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

用于持续评估

创建评估作业时,您必须指定定义注解规范集的 CSV 文件:

  • 对于模型在预测期间可能输出的每个标签,该文件必须有一行数据。
  • 每行应为英文逗号分隔的一对数据,其中包含标签及其说明:LABEL_NAME,DESCRIPTION
  • 创建评估作业时,数据标签服务会使用该 CSV 文件的文件名作为它在后台创建的注解规范集的名称。

例如,如果您的模型是用来预测图片中含有的动物,您可以将以下规范写入名为 animals.csv 的文件:

bird,any animal in the class Aves - see https://en.wikipedia.org/wiki/Bird
cat,any animal in the species Felis catus (domestic cats, not wild cats) - see https://en.wikipedia.org/wiki/Cat
dog,any animal in the genus Canis (domestic dogs and close relatives) - see https://en.wikipedia.org/wiki/Canis
multiple,image contains more than one of the above
none,image contains none of the above

然后,您可以将此文件上传到持续评估作业所属的同一项目中的 Cloud Storage 存储桶