能動的学習用のデータラベル付けジョブを作成する

create_data_labeling_job メソッドを使用して、能動的学習用のデータラベル付けジョブを作成します。

コードサンプル

Java

このサンプルを試す前に、Vertex AI クライアント ライブラリをインストールするにある Java の設定手順を完了してください。詳細については、Vertex AI Java API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

import com.google.cloud.aiplatform.v1.ActiveLearningConfig;
import com.google.cloud.aiplatform.v1.DataLabelingJob;
import com.google.cloud.aiplatform.v1.DatasetName;
import com.google.cloud.aiplatform.v1.JobServiceClient;
import com.google.cloud.aiplatform.v1.JobServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;

public class CreateDataLabelingJobActiveLearningSample {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String project = "PROJECT";
    String displayName = "DISPLAY_NAME";
    String dataset = "DATASET";
    String instructionUri = "INSTRUCTION_URI";
    String inputsSchemaUri = "INPUTS_SCHEMA_URI";
    String annotationSpec = "ANNOTATION_SPEC";
    createDataLabelingJobActiveLearningSample(
        project, displayName, dataset, instructionUri, inputsSchemaUri, annotationSpec);
  }

  static void createDataLabelingJobActiveLearningSample(
      String project,
      String displayName,
      String dataset,
      String instructionUri,
      String inputsSchemaUri,
      String annotationSpec)
      throws IOException {
    JobServiceSettings settings =
        JobServiceSettings.newBuilder()
            .setEndpoint("us-central1-aiplatform.googleapis.com:443")
            .build();
    String location = "us-central1";

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (JobServiceClient client = JobServiceClient.create(settings)) {
      JsonArray jsonAnnotationSpecs = new JsonArray();
      jsonAnnotationSpecs.add(annotationSpec);
      JsonObject jsonInputs = new JsonObject();
      jsonInputs.add("annotation_specs", jsonAnnotationSpecs);
      Value.Builder inputsBuilder = Value.newBuilder();
      JsonFormat.parser().merge(jsonInputs.toString(), inputsBuilder);
      Value inputs = inputsBuilder.build();
      ActiveLearningConfig activeLearningConfig =
          ActiveLearningConfig.newBuilder().setMaxDataItemCount(1).build();

      String datasetName = DatasetName.of(project, location, dataset).toString();

      DataLabelingJob dataLabelingJob =
          DataLabelingJob.newBuilder()
              .setDisplayName(displayName)
              .addDatasets(datasetName)
              .setLabelerCount(1)
              .setInstructionUri(instructionUri)
              .setInputsSchemaUri(inputsSchemaUri)
              .setInputs(inputs)
              .putAnnotationLabels(
                  "aiplatform.googleapis.com/annotation_set_name",
                  "data_labeling_job_active_learning")
              .setActiveLearningConfig(activeLearningConfig)
              .build();
      LocationName parent = LocationName.of(project, location);
      DataLabelingJob response = client.createDataLabelingJob(parent, dataLabelingJob);
      System.out.format("response: %s\n", response);
      System.out.format("Name: %s\n", response.getName());
    }
  }
}

Python

このサンプルを試す前に、Vertex AI クライアント ライブラリをインストールするにある Python の設定手順を完了してください。詳細については、Vertex AI Python API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value

def create_data_labeling_job_active_learning_sample(
    project: str,
    display_name: str,
    dataset: str,
    instruction_uri: str,
    inputs_schema_uri: str,
    annotation_spec: 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.JobServiceClient(client_options=client_options)
    inputs_dict = {"annotation_specs": [annotation_spec]}
    inputs = json_format.ParseDict(inputs_dict, Value())

    active_learning_config = {"max_data_item_count": 1}

    data_labeling_job = {
        "display_name": display_name,
        # Full resource name: projects/{project}/locations/{location}/datasets/{dataset_id}
        "datasets": [dataset],
        "labeler_count": 1,
        "instruction_uri": instruction_uri,
        "inputs_schema_uri": inputs_schema_uri,
        "inputs": inputs,
        "annotation_labels": {
            "aiplatform.googleapis.com/annotation_set_name": "data_labeling_job_active_learning"
        },
        "active_learning_config": active_learning_config,
    }
    parent = f"projects/{project}/locations/{location}"
    response = client.create_data_labeling_job(
        parent=parent, data_labeling_job=data_labeling_job
    )
    print("response:", response)

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。