訓練影片動作辨識模型

本頁面說明如何使用 Google Cloud 控制台或 Vertex AI API,從影片資料集訓練 AutoML 動作辨識模型。

訓練 AutoML 模型

Google Cloud 控制台

  1. 在 Google Cloud 控制台的 Vertex AI 專區中,前往「Datasets」頁面。

    前往「資料集」頁面

  2. 按一下要用來訓練模型的資料集名稱,開啟資料集詳細資料頁面。

  3. 按一下「訓練新模型」

  4. 輸入新模式的顯示名稱。

  5. 如要手動設定訓練資料的分割方式,請展開「進階選項」,然後選取資料分割選項。瞭解詳情

  6. 按一下「繼續」

  7. 選取模型訓練方法。

    • AutoML 適用於多種用途。
    • Seq2seq+ 是進行實驗的好選擇。由於架構較簡單,且使用較小的搜尋空間,因此演算法可能會比 AutoML 更快收斂。實驗結果顯示,Seq2Seq+ 在時間預算較少的情況下,以及在大小小於 1 GB 的資料集上,表現良好。
    點按「繼續」

  8. 按一下「開始訓練」

    模型訓練作業可能需要花費數小時,視資料的大小和複雜度以及您指定的訓練預算而定。您可以關閉這個分頁,稍後再返回查看。模型訓練完成後,您會收到電子郵件通知。

    訓練開始後幾分鐘,您可以查看模型的資源資訊,瞭解訓練節點小時估計值。如果您取消訓練,目前的產品就不會產生費用。

API

REST

使用任何要求資料之前,請先替換以下項目:

  • PROJECT:您的專案 ID
  • LOCATION:資料集所在的區域,以及建立模型的位置。例如:us-central1
  • TRAINING_PIPELINE_DISPLAY_NAME:必填。TrainingPipeline 的顯示名稱。
  • DATASET_ID:訓練資料集 ID。
  • TRAINING_FRACTIONTEST_FRACTIONfractionSplit 物件為選用項目,可用於控制資料分割。如要進一步瞭解如何控制資料分割,請參閱「AutoML 模型資料分割簡介」。例如:
    • {"trainingFraction": "0.8","validationFraction": "0","testFraction": "0.2"}
  • MODEL_DISPLAY_NAME:已訓練模型的顯示名稱。
  • MODEL_DESCRIPTION:模型的說明。
  • MODEL_LABELS:任何一組鍵/值組合,用於整理模型。舉例來說:
    • "env": "prod"
    • "tier": "backend"
  • EDGE_MODEL_TYPE
    • MOBILE_VERSATILE_1:一般用途
  • PROJECT_NUMBER:系統自動產生的專案編號

HTTP 方法和網址:

POST https://LOCATION-aiplatform.googleapis.com/beta1/projects/PROJECT/locations/LOCATION/trainingPipelines

JSON 要求主體:

{
  "displayName": "TRAINING_PIPELINE_DISPLAY_NAME",
  "inputDataConfig": {
    "datasetId": "DATASET_ID",
    "fractionSplit": {
      "trainingFraction": "TRAINING_FRACTION",
      "validationFraction": "0",
      "testFraction": "TEST_FRACTION"
    }
  },
  "modelToUpload": {
    "displayName": "MODEL_DISPLAY_NAME",
    "description": "MODEL_DESCRIPTION",
    "labels": {
      "KEY": "VALUE"
    }
  },
  "trainingTaskDefinition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_object_tracking_1.0.0.yaml",
  "trainingTaskInputs": {
    "modelType": ["EDGE_MODEL_TYPE"],
  }
}

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/beta1/projects/PROJECT/locations/LOCATION/trainingPipelines"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/beta1/projects/PROJECT/locations/LOCATION/trainingPipelines" | Select-Object -Expand Content

回應內容包含規格和 TRAININGPIPELINE_ID 相關資訊。

Java

在試用這個範例之前,請先按照 Vertex AI 快速入門:使用用戶端程式庫中的操作說明設定 Java。詳情請參閱 Vertex AI Java API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import com.google.cloud.aiplatform.util.ValueConverter;
import com.google.cloud.aiplatform.v1.InputDataConfig;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.cloud.aiplatform.v1.Model;
import com.google.cloud.aiplatform.v1.PipelineServiceClient;
import com.google.cloud.aiplatform.v1.PipelineServiceSettings;
import com.google.cloud.aiplatform.v1.TrainingPipeline;
import com.google.cloud.aiplatform.v1.schema.trainingjob.definition.AutoMlVideoActionRecognitionInputs;
import com.google.cloud.aiplatform.v1.schema.trainingjob.definition.AutoMlVideoActionRecognitionInputs.ModelType;
import java.io.IOException;

public class CreateTrainingPipelineVideoActionRecognitionSample {

  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 datasetId = "DATASET_ID";
    String modelDisplayName = "MODEL_DISPLAY_NAME";
    createTrainingPipelineVideoActionRecognitionSample(
        project, displayName, datasetId, modelDisplayName);
  }

  static void createTrainingPipelineVideoActionRecognitionSample(
      String project, String displayName, String datasetId, String modelDisplayName)
      throws IOException {
    PipelineServiceSettings settings =
        PipelineServiceSettings.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 (PipelineServiceClient client = PipelineServiceClient.create(settings)) {
      AutoMlVideoActionRecognitionInputs trainingTaskInputs =
          AutoMlVideoActionRecognitionInputs.newBuilder().setModelType(ModelType.CLOUD).build();

      InputDataConfig inputDataConfig =
          InputDataConfig.newBuilder().setDatasetId(datasetId).build();
      Model modelToUpload = Model.newBuilder().setDisplayName(modelDisplayName).build();
      TrainingPipeline trainingPipeline =
          TrainingPipeline.newBuilder()
              .setDisplayName(displayName)
              .setTrainingTaskDefinition(
                  "gs://google-cloud-aiplatform/schema/trainingjob/definition/"
                      + "automl_video_action_recognition_1.0.0.yaml")
              .setTrainingTaskInputs(ValueConverter.toValue(trainingTaskInputs))
              .setInputDataConfig(inputDataConfig)
              .setModelToUpload(modelToUpload)
              .build();
      LocationName parent = LocationName.of(project, location);
      TrainingPipeline response = client.createTrainingPipeline(parent, trainingPipeline);
      System.out.format("response: %s\n", response);
      System.out.format("Name: %s\n", response.getName());
    }
  }
}

Python 適用的 Vertex AI SDK

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件

from google.cloud import aiplatform
from google.cloud.aiplatform.gapic.schema import trainingjob


def create_training_pipeline_video_action_recognition_sample(
    project: str,
    display_name: str,
    dataset_id: str,
    model_display_name: str,
    model_type: 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.PipelineServiceClient(client_options=client_options)
    training_task_inputs = trainingjob.definition.AutoMlVideoActionRecognitionInputs(
        # modelType can be either 'CLOUD' or 'MOBILE_VERSATILE_1'
        model_type=model_type,
    ).to_value()

    training_pipeline = {
        "display_name": display_name,
        "training_task_definition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_action_recognition_1.0.0.yaml",
        "training_task_inputs": training_task_inputs,
        "input_data_config": {"dataset_id": dataset_id},
        "model_to_upload": {"display_name": model_display_name},
    }
    parent = f"projects/{project}/locations/{location}"
    response = client.create_training_pipeline(
        parent=parent, training_pipeline=training_pipeline
    )
    print("response:", response)

使用 REST 控管資料分割

您可以控制訓練資料如何在訓練集、驗證集和測試集之間分割。使用 Vertex AI API 時,請使用 Split 物件來決定資料分割方式。Split 物件可納入 InputConfig 物件,做為多種物件類型之一,每種物件都提供不同的訓練資料分割方式。您只能選取一種方法。

  • FractionSplit
    • TRAINING_FRACTION:訓練資料用於訓練集的比例。
    • VALIDATION_FRACTION:用於驗證集的訓練資料比例。不適用於影片資料。
    • TEST_FRACTION:要用於測試集的訓練資料比例。

    如果指定任何分數,則必須指定所有分數。分數總和必須為 1.0。分數的預設值會因資料類型而異。瞭解詳情

    "fractionSplit": {
      "trainingFraction": TRAINING_FRACTION,
      "validationFraction": VALIDATION_FRACTION,
      "testFraction": TEST_FRACTION
    },
    
  • FilterSplit
    • TRAINING_FILTER:系統會使用符合這項篩選條件的資料項目來建立訓練集。
    • VALIDATION_FILTER:符合此篩選條件的資料項目會用於驗證集。影片資料必須為「-」。
    • TEST_FILTER:系統會使用符合這項篩選條件的資料項目來建立測試集。

    這些篩選器可搭配 ml_use 標籤或您套用至資料的任何標籤使用。進一步瞭解如何使用ml-use 標籤其他標籤篩選資料。

    以下範例說明如何使用 filterSplit 物件搭配 ml_use 標籤,並納入驗證集:

    "filterSplit": {
    "trainingFilter": "labels.aiplatform.googleapis.com/ml_use=training",
    "validationFilter": "labels.aiplatform.googleapis.com/ml_use=validation",
    "testFilter": "labels.aiplatform.googleapis.com/ml_use=test"
    }