Vorhersagen aus einem Modell zur Videoaktionserkennung abrufen

Auf dieser Seite erfahren Sie, wie Sie mithilfe der Google Cloud Console oder der Vertex AI API Batchvorhersagen aus Ihren Modellen zur Videoaktionserkennung abrufen. Batchvorhersagen sind asynchrone Anfragen. Sie können Batchvorhersagen direkt von der Modellressource anfordern, ohne das Modell auf einem Endpunkt bereitstellen zu müssen.

AutoML-Videomodelle unterstützen keine Onlinevorhersagen.

Batchvorhersagen abrufen

Für eine Batchvorhersage geben Sie eine Eingabequelle und einen Ausgabeort an, an dem Vertex AI Vorhersageergebnisse speichert.

Anforderungen an Eingabedaten

Die Eingabe für Batchanfragen gibt die Elemente an, die zur Vorhersage an Ihr Modell gesendet werden sollen. Batchvorhersagen für den AutoML-Videomodelltyp verwenden eine JSON Lines-Datei, um eine Liste von Videos für Vorhersagen anzugeben. Anschließend wird die JSON Lines-Datei in einem Cloud Storage-Bucket gespeichert. Sie können Infinity für das Feld timeSegmentEnd angeben, um das Ende des Videos anzugeben. Das folgende Beispiel zeigt eine einzelne Zeile in einer JSON Lines-Eingabedatei.

{'content': 'gs://sourcebucket/datasets/videos/source_video.mp4', 'mimeType': 'video/mp4', 'timeSegmentStart': '0.0s', 'timeSegmentEnd': '2.366667s'}

Eine Batchvorhersage anfordern

Für Batchvorhersageanfragen können Sie die Google Cloud Console oder die Vertex AI API verwenden. Abhängig von der Anzahl der Eingabeelemente, die Sie eingereicht haben, kann die Batchvorhersage eine Weile dauern.

Google Cloud Console

Verwenden Sie die Google Cloud Console, um eine Batchvorhersage anzufordern.

  1. Rufen Sie in der Google Cloud Console im Abschnitt Vertex AI die Seite Batchvorhersagen auf.

    Zur Seite "Batchvorhersagen"

  2. Klicken Sie auf Erstellen, um das Fenster Neue Batchvorhersage zu öffnen, und führen Sie die folgenden Schritte aus:

    1. Geben Sie einen Namen für die Batchvorhersage ein.
    2. Wählen Sie für Modellname den Namen des Modells aus, das für diese Batchvorhersage verwendet werden soll.
    3. Geben Sie unter Quellpfad den Cloud Storage-Speicherort an, in dem sich Ihre JSON Lines-Eingabedatei befindet.
    4. Geben Sie als Zielpfad einen Cloud Storage-Speicherort an, an dem die Ergebnisse der Batchvorhersage gespeichert werden. Das Format der Ausgabe wird durch das Ziel des Modells bestimmt. AutoML-Modelle für Bildziele geben JSON Lines-Ausgabedateien aus.

API

Verwenden Sie die Vertex AI API zum Senden von Batchvorhersageanfragen.

REST

Ersetzen Sie dabei folgende Werte für die Anfragedaten:

  • LOCATION_ID: Region, in der das Modell gespeichert ist .und der Batchvorhersagejob ausgeführt wird. Beispiel: us-central1.
  • PROJECT_ID: Ihre Projekt-ID
  • BATCH_JOB_NAME: Anzeigename für den Batchjob
  • MODEL_ID: Die ID für das Modell, das für Vorhersagen verwendet werden soll
  • THRESHOLD_VALUE (optional): Das Modell gibt nur Vorhersagen mit Konfidenzwerten mit mindestens diesem Wert zurück
  • URI: Der Cloud Storage-URI, in dem sich die JSON Lines-Eingabedatei befindet
  • BUCKET: Ihr Cloud Storage-Bucket
  • PROJECT_NUMBER: Die automatisch generierte Projektnummer Ihres Projekts.

HTTP-Methode und URL:

POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/batchPredictionJobs

JSON-Text der Anfrage:

{
    "displayName": "BATCH_JOB_NAME",
    "model": "projects/PROJECT_ID/locations/LOCATION_ID/models/MODEL_ID",
    "modelParameters": {
      "confidenceThreshold": THRESHOLD_VALUE,
    },
    "inputConfig": {
        "instancesFormat": "jsonl",
        "gcsSource": {
            "uris": ["URI"],
        },
    },
    "outputConfig": {
        "predictionsFormat": "jsonl",
        "gcsDestination": {
            "outputUriPrefix": "OUTPUT_BUCKET",
        },
    },
}

Wenn Sie die Anfrage senden möchten, wählen Sie eine der folgenden Optionen aus:

curl

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

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

PowerShell

Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

$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_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/batchPredictionJobs" | Select-Object -Expand Content

Sie sollten in etwa folgende JSON-Antwort erhalten:

{
  "name": "projects/PROJECT_NUMBER/locations/us-central1/batchPredictionJobs/BATCH_JOB_ID",
  "displayName": "BATCH_JOB_NAME",
  "model": "projects/PROJECT_NUMBER/locations/us-central1/models/MODEL_ID",
  "inputConfig": {
    "instancesFormat": "jsonl",
    "gcsSource": {
      "uris": [
        "CONTENT"
      ]
    }
  },
  "outputConfig": {
    "predictionsFormat": "jsonl",
    "gcsDestination": {
      "outputUriPrefix": "BUCKET"
    }
  },
  "state": "JOB_STATE_PENDING",
  "createTime": "2020-05-30T02:58:44.341643Z",
  "updateTime": "2020-05-30T02:58:44.341643Z",
  "modelDisplayName": "MODEL_NAME",
  "modelObjective": "MODEL_OBJECTIVE"
}

Sie können den Status des Batch-Jobs mit BATCH_JOB_ID abfragen, bis der Job state den Wert JOB_STATE_SUCCEEDED hat.

Java

Bevor Sie dieses Beispiel anwenden, folgen Sie den Java-Einrichtungsschritten in der Vertex AI-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Informationen finden Sie in der Referenzdokumentation zur Vertex AI Java API.

Richten Sie zur Authentifizierung bei Vertex AI Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import com.google.cloud.aiplatform.util.ValueConverter;
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.GcsSource;
import com.google.cloud.aiplatform.v1.JobServiceClient;
import com.google.cloud.aiplatform.v1.JobServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.protobuf.Value;
import java.io.IOException;

public class CreateBatchPredictionJobVideoActionRecognitionSample {

  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 model = "MODEL";
    String gcsSourceUri = "GCS_SOURCE_URI";
    String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
    createBatchPredictionJobVideoActionRecognitionSample(
        project, displayName, model, gcsSourceUri, gcsDestinationOutputUriPrefix);
  }

  static void createBatchPredictionJobVideoActionRecognitionSample(
      String project,
      String displayName,
      String model,
      String gcsSourceUri,
      String gcsDestinationOutputUriPrefix)
      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)) {
      Value modelParameters = ValueConverter.EMPTY_VALUE;
      GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
      BatchPredictionJob.InputConfig inputConfig =
          BatchPredictionJob.InputConfig.newBuilder()
              .setInstancesFormat("jsonl")
              .setGcsSource(gcsSource)
              .build();
      GcsDestination gcsDestination =
          GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
      BatchPredictionJob.OutputConfig outputConfig =
          BatchPredictionJob.OutputConfig.newBuilder()
              .setPredictionsFormat("jsonl")
              .setGcsDestination(gcsDestination)
              .build();

      String modelName = ModelName.of(project, location, model).toString();

      BatchPredictionJob batchPredictionJob =
          BatchPredictionJob.newBuilder()
              .setDisplayName(displayName)
              .setModel(modelName)
              .setModelParameters(modelParameters)
              .setInputConfig(inputConfig)
              .setOutputConfig(outputConfig)
              .build();
      LocationName parent = LocationName.of(project, location);
      BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
      System.out.format("response: %s\n", response);
      System.out.format("\tName: %s\n", response.getName());
    }
  }
}

Python

Informationen zum Installieren oder Aktualisieren von Python finden Sie unter Vertex AI SDK für Python installieren. Weitere Informationen finden Sie in der Referenzdokumentation zur Python API.

def create_batch_prediction_job_sample(
    project: str,
    location: str,
    model_resource_name: str,
    job_display_name: str,
    gcs_source: Union[str, Sequence[str]],
    gcs_destination: str,
    sync: bool = True,
):
    aiplatform.init(project=project, location=location)

    my_model = aiplatform.Model(model_resource_name)

    batch_prediction_job = my_model.batch_predict(
        job_display_name=job_display_name,
        gcs_source=gcs_source,
        gcs_destination_prefix=gcs_destination,
        sync=sync,
    )

    batch_prediction_job.wait()

    print(batch_prediction_job.display_name)
    print(batch_prediction_job.resource_name)
    print(batch_prediction_job.state)
    return batch_prediction_job

Batchvorhersageergebnisse abrufen

Vertex AI sendet eine Batchvorhersageausgabe an das angegebene Ziel.

Wenn eine Batchvorhersage abgeschlossen ist, wird die Ausgabe der Vorhersage in dem Cloud Storage-Bucket gespeichert, den Sie in der Anfrage angegeben haben.

Beispielergebnisse für Batchvorhersagen

Das folgende Beispiel zeigt die Batchvorhersage aus einem Modell zur Videoaktionserkennung.

{
  "instance": {
   "content": "gs://bucket/video.mp4",
    "mimeType": "video/mp4",
    "timeSegmentStart": "1s",
    "timeSegmentEnd": "5s"
  }
  "prediction": [{
    "id": "1",
    "displayName": "swing",
    "timeSegmentStart": "1.2s",
    "timeSegmentEnd": "1.2s",
    "confidence": 0.7
  }, {
    "id": "2",
    "displayName": "jump",
    "timeSegmentStart": "3.4s",
    "timeSegmentEnd": "3.4s",
    "confidence": 0.5
  }]
}