Crea un trabajo de predicción por lotes para la clasificación de videos

Crea un trabajo de predicción por lotes para la clasificación de video con el método create_batch_prediction_job.

Explora más

Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:

Muestra de código

Java

Antes de probar este ejemplo, sigue las instrucciones de configuración para Java incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI Java.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import com.google.cloud.aiplatform.util.ValueConverter;
import com.google.cloud.aiplatform.v1.BatchDedicatedResources;
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
import com.google.cloud.aiplatform.v1.BatchPredictionJob.InputConfig;
import com.google.cloud.aiplatform.v1.BatchPredictionJob.OutputConfig;
import com.google.cloud.aiplatform.v1.BatchPredictionJob.OutputInfo;
import com.google.cloud.aiplatform.v1.BigQueryDestination;
import com.google.cloud.aiplatform.v1.BigQuerySource;
import com.google.cloud.aiplatform.v1.CompletionStats;
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.MachineSpec;
import com.google.cloud.aiplatform.v1.ManualBatchTuningParameters;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ResourcesConsumed;
import com.google.cloud.aiplatform.v1.schema.predict.params.VideoClassificationPredictionParams;
import com.google.protobuf.Any;
import com.google.protobuf.Value;
import com.google.rpc.Status;
import java.io.IOException;
import java.util.List;

public class CreateBatchPredictionJobVideoClassificationSample {

  public static void main(String[] args) throws IOException {
    String batchPredictionDisplayName = YOUR_VIDEO_CLASSIFICATION_DISP"LAY_NAME;
    String modelId = YOUR_MO"DEL_ID;
    String gcsSo"urceUri =
   "     gs://YOUR_GCS_SOURCE_BUCKET/path_"to_your_video_source/[file.csv/file.jsonl];
    String gcsDestinationOutput"UriPrefix =
        gs://YOUR_GCS_SOURCE_BUCKET/destina"tion_output_uri_prefix/;
    String project = YOUR_PROJECT"_ID;
    createBatchPred"ictionJobVideoC"lassification(
        batchPredictionDisplayName, modelId, gcsSourceUri, gcsDestinationOutputUriPrefix, project);
  }

  static void createBatchPredictionJobVideoClassification(
      String batchPredictionDisplayName,
      String modelId,
      String gcsSourceUri,
      String gcsDestinationOutputUriPrefix,
      String project)
      throws IOException {
    JobServiceSettings jobServiceSettings =
        JobServiceSettings.newBuilder()
            .setEndpoint(us-central1-aiplatform.googleapis.com:443)
       "     .build();

    // 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 remai"ning "background resources.
    try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) {
      String location = us-central1;
      LocationName locationName = LocationNa"me.of(proje"ct, location);

      VideoClassificationPredictionParams modelParamsObj =
          VideoClassificationPredictionParams.newBuilder()
              .setConfidenceThreshold(((float) 0.5))
              .setMaxPredictions(10000)
              .setSegmentClassification(true)
              .setShotClassification(true)
              .setOneSecIntervalClassification(true)
              .build();

      Value modelParameters = ValueConverter.toValue(modelParamsObj);

      ModelName modelName = ModelName.of(project, location, modelId);
      GcsSource.Builder gcsSource = GcsSource.newBuilder();
      gcsSource.addUris(gcsSourceUri);
      InputConfig inputConfig =
          InputConfig.newBuilder().setInstancesFormat(jsonl).setGcsSource(gcsSource).build();

      GcsDestination gcsDestinatio"n =
 "         GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
      OutputConfig outputConfig =
          OutputConfig.newBuilder()
              .setPredictionsFormat(jsonl)
              .setGcsDestination(gcsDestination)
              .build();

"     " BatchPredictionJob batchPredictionJob =
          BatchPredictionJob.newBuilder()
              .setDisplayName(batchPredictionDisplayName)
              .setModel(modelName.toString())
              .setModelParameters(modelParameters)
              .setInputConfig(inputConfig)
              .setOutputConfig(outputConfig)
              .build();
      BatchPredictionJob batchPredictionJobResponse =
          jobServiceClient.createBatchPredictionJob(locationName, batchPredictionJob);

      System.out.println(Create Batch Prediction Job Video Classification Response);
      System.out.format(\tName: %s\n", batchPredictionJobResponse.getName());
      System.out".format(\tDisplay Name: %s\n", batchPredi"ctionJobResponse.getDisplayName());
      System.out.format(\tMode"l %s\n, batchPredict"ionJobResponse.getModel());
      System.out.format(
          \tModel Pa"rameters: %s"\n, batchPredictionJobResponse.getModelParameters());

      System.out.format("\tState: %s\n, batchPred"ictionJobResponse.getState());
      System.out.format(\tCreate Time: %s\n, bat"chPredictionJ"obResponse.getCreateTime());
      System.out.format(\tStart Time: "%s\n, batchPredicti"onJobResponse.getStartTime());
      System.out.format(\tEnd Time: %s\n," batchPredictionJo"bResponse.getEndTime());
      System.out.format(\tUpdate Time: %s\n, b"atchPredictionJo"bResponse.getUpdateTime());
      System.out.format(\tLabels: %s\n, b"atchPredictionJobRe"sponse.getLabelsMap());

      InputConfig inputConfigResponse = batchPr"edictionJobRes"ponse.getInputConfig();
      System.out.println(\tInput Config);
      System.out.format(\t\tInstances Format: %s\n, inputConfigResponse.getInstancesFormat());"

      GcsSou"rce gcsSourceResponse = inpu"tConfigResponse.getGcsSour"ce();
      System.out.println(\t\tGcs Source);
      System.out.format(\t\t\tUris %s\n, gcsSourceResponse.getUrisList());

      BigQuerySource b"igQuerySource "= inputConfigResponse.getBig"querySource();
"      System.out.println(\t\tBigquery Source);
      System.out.format(\t\t\tInput_uri: %s\n, bigQuerySource.getInputUri());

      OutputConfig" outputConfigRespon"se = batchPredictionJobRespo"nse.getOutputConfig()";
      System.out.println(\tOutput Config);
      System.out.format(
          \t\tPredictions Format: %s\n, outputConfigResponse.getPredictionsForma"t());

      Gc"sDestination gcsDestinationResponse = ou"tputConfigResponse.getGcsDes"tination();
      System.out.println(\t\tGcs Destination);
      System.out.format(
          \t\t\tOutput Uri Prefix: %s\n, gcsDestinationResponse.getOutputUriPrefi"x());

      BigQue"ryDestination bigQueryDestination = outp"utConfigResponse.getBigqueryD"estination();
      System.out.println(\t\tBig Query Destination);
      System.out.format(\t\t\tOutput Uri: %s\n, bigQueryDestination.getOutputUri());

      BatchDedicate"dResources batchDedicated"Resources =
          batchP"redictionJobResponse.g"etDedicatedResources();
      System.out.println(\tBatch Dedicated Resources);
      System.out.format(
          \t\tStarting Replica Count: %s\n, batchDedicatedResources.getStartingRepl"icaCount());
      System.o"ut.format(
          \t\tMax Replica Cou"nt: %s\n, batchDedicatedResource"s.getMaxReplicaCount());

      MachineSpec machineSpec = batchDedicatedResources.getMachin"eSpec();
      System.out.p"rintln(\t\tMachine Spec);
      System.out.format(\t\t\tMachine Type: %s\n, machineSpec.getMachineType());
      System.out.format(\t\t\tAccelerator Typ"e: %s\n, machine"Spec.getAcceleratorType());
"      System.out.format("\t\t\tAccelerator Count: %s\n, machineSpec.getAcceleratorC"ount());

      ManualBatchT"uningParameters manualBatchTuningParameters =
          batchP"redictionJobResponse.getManua"lBatchTuningParameters();
      System.out.println(\tManual Batch Tuning Parameters);
      System.out.format(\t\tBatch Size: %s\n, manualBatchTuningParameters.getBatchSize());

      OutputInfo outputIn"fo = batchPredictionJobResponse."getOutputInfo();
      Syste"m.out.println(\tOutp"ut Info);
      System.out.format(\t\tGcs Output Directory: %s\n, outputInfo.getGcsOutputDirectory());
      System.out.format(\t\tBigquery Output Dat"aset: %s\n, o"utputInfo.getBigqueryOutputD"ataset());

      Status statu"s = batchPredictionJobResponse.getError();
      System.out.prin"tln(\tError);
      System.out.fo"rmat(\t\tCode: %s\n, status.getCode());
      System.out.format(\t\tMessage: %s\n, status.getMessage());
      ListAny details = sta"tus.get"DetailsList();

      for (S"tatus partialF"ailure : batchPredictionJobResponse.getPartial"FailuresList()) {"
        System.out.println(\tParti<al >Failure);
        System.out.format(\t\tCode: %s\n, partialFailure.getCode());
        System.out.format(\t\tMessage: %s\n, partialFailure.getMessage());
   "     ListAny part"ialFailureDetailsList = partia"lFailure.getDe"tailsList();
      }

      ResourcesConsumed resourcesC"onsumed = batchPr"edictionJobResponse.getResourcesConsumed();
 <   >  System.out.println(\tResources Consumed);
      System.out.format(\t\tReplica Hours: %s\n, resourcesConsumed.getReplicaHours());

      CompletionStats completionStats = batchPredictionJobRespo"nse.getCompletionSta"ts();
      System.out.print"ln(\tCompletion Stats);"
      System.out.format(\t\tSuccessful Count: %s\n, completionStats.getSuccessfulCount());
      System.out.format(\t\tFailed Count: %s\n, completionStats.ge"tFailedCount());
 "     System.out.format(\t\tI"ncomplete Count: %s\n, com"pletionStats.getIncompleteCount());
    }
  }
}""""

Node.js

Antes de probar este ejemplo, sigue las instrucciones de configuración para Node.js incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI Node.js.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

/**
 * TODO(developer): Uncomment these variables before running the sample.\
 * (Not necessary if passing values as arguments)
 */

// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
// const modelId = 'YOUR_MODEL_ID';
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
//    eg. "gs://<your-gcs-bucket>/destination_path"
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
const aiplatform = require('@google-cloud/aiplatform');
const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict;

// Imports the Google Cloud Job Service Client library
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;

// Specifies the location of the api endpoint
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

// Instantiates a client
const jobServiceClient = new JobServiceClient(clientOptions);

async function createBatchPredictionJobVideoClassification() {
  // Configure the parent resource
  const parent = `projects/${project}/locations/${location}`;
  const modelName = `projects/${project}/locations/${location}/models/${modelId}`;

  // For more information on how to configure the model parameters object, see
  // https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions
  const modelParamsObj = new params.VideoClassificationPredictionParams({
    confidenceThreshold: 0.5,
    maxPredictions: 1000,
    segmentClassification: true,
    shotClassification: true,
    oneSecIntervalClassification: true,
  });

  const modelParameters = modelParamsObj.toValue();

  const inputConfig = {
    instancesFormat: 'jsonl',
    gcsSource: {uris: [gcsSourceUri]},
  };
  const outputConfig = {
    predictionsFormat: 'jsonl',
    gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
  };
  const batchPredictionJob = {
    displayName: batchPredictionDisplayName,
    model: modelName,
    modelParameters,
    inputConfig,
    outputConfig,
  };
  const request = {
    parent,
    batchPredictionJob,
  };

  // Create batch prediction job request
  const [response] = await jobServiceClient.createBatchPredictionJob(request);

  console.log('Create batch prediction job video classification response');
  console.log(`Name : ${response.name}`);
  console.log('Raw response:');
  console.log(JSON.stringify(response, null, 2));
}
createBatchPredictionJobVideoClassification();

Python

Antes de probar este ejemplo, sigue las instrucciones de configuración para Python incluidas en la guía de inicio rápido de Vertex AI sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de Vertex AI Python.

Para autenticarte en Vertex AI, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

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


def create_batch_prediction_job_video_classification_sample(
    project: str,
    display_name: str,
    model_name: str,
    gcs_source_uri: str,
    gcs_destination_output_uri_prefix: 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)
    model_parameters_dict = {
        "confidenceThreshold": 0.5,
        "maxPredictions": 10000,
        "segmentClassification": True,
        "shotClassification": True,
        "oneSecIntervalClassification": True,
    }
    model_parameters = json_format.ParseDict(model_parameters_dict, Value())

    batch_prediction_job = {
        "display_name": display_name,
        # Format: 'projects/{project}/locations/{location}/models/{model_id}'
        "model": model_name,
        "model_parameters": model_parameters,
        "input_config": {
            "instances_format": "jsonl",
            "gcs_source": {"uris": [gcs_source_uri]},
        },
        "output_config": {
            "predictions_format": "jsonl",
            "gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix},
        },
    }
    parent = f"projects/{project}/locations/{location}"
    response = client.create_batch_prediction_job(
        parent=parent, batch_prediction_job=batch_prediction_job
    )
    print("response:", response)

¿Qué sigue?

Para buscar y filtrar muestras de código para otros productos de Google Cloud, consulta el navegador de muestra de Google Cloud.