Anzahl der Modellknoten bereitstellen

Anzahl der Knotenknoten bereitstellen

Weitere Informationen

Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:

Codebeispiel

Go

Informationen zum Installieren und Verwenden der Clientbibliothek für die AutoML Vision-Objekterkennung finden Sie unter Clientbibliotheken für die AutoML Vision-Objekterkennung. Weitere Informationen finden Sie in der Referenzdokumentation zur AutoML Vision Object Detection Go API.

Richten Sie zur Authentifizierung bei der AutoML Vision-Objekterkennung die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	"cloud.google.com/go/automl/apiv1/automlpb"
)

// visionObjectDetectionDeployModelWithNodeCount deploys a model with node count.
func visionObjectDetectionDeployModelWithNodeCount(w io.Writer, projectID string, location string, modelID string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "IOD123456789..."

	ctx := context.Background()
	client, err := automl.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &automlpb.DeployModelRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		ModelDeploymentMetadata: &automlpb.DeployModelRequest_ImageObjectDetectionModelDeploymentMetadata{
			ImageObjectDetectionModelDeploymentMetadata: &automlpb.ImageObjectDetectionModelDeploymentMetadata{
				NodeCount: 2,
			},
		},
	}

	op, err := client.DeployModel(ctx, req)
	if err != nil {
		return fmt.Errorf("DeployModel: %w", err)
	}
	fmt.Fprintf(w, "Processing operation name: %q\n", op.Name())

	if err := op.Wait(ctx); err != nil {
		return fmt.Errorf("Wait: %w", err)
	}

	fmt.Fprintf(w, "Model deployed.\n")

	return nil
}

Java

Informationen zum Installieren und Verwenden der Clientbibliothek für die AutoML Vision-Objekterkennung finden Sie unter Clientbibliotheken für die AutoML Vision-Objekterkennung. Weitere Informationen finden Sie in der Referenzdokumentation zur AutoML Vision Object Detection Java API.

Richten Sie zur Authentifizierung bei der AutoML Vision-Objekterkennung die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DeployModelRequest;
import com.google.cloud.automl.v1.ImageObjectDetectionModelDeploymentMetadata;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class VisionObjectDetectionDeployModelNodeCount {

  static void visionObjectDetectionDeployModelNodeCount()
      throws InterruptedException, ExecutionException, IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    visionObjectDetectionDeployModelNodeCount(projectId, modelId);
  }

  // Deploy a model for prediction with a specified node count (can be used to redeploy a model)
  static void visionObjectDetectionDeployModelNodeCount(String projectId, String modelId)
      throws IOException, ExecutionException, InterruptedException {
    // 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 (AutoMlClient client = AutoMlClient.create()) {
      // Get the full path of the model.
      ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
      ImageObjectDetectionModelDeploymentMetadata metadata =
          ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
      DeployModelRequest request =
          DeployModelRequest.newBuilder()
              .setName(modelFullId.toString())
              .setImageObjectDetectionModelDeploymentMetadata(metadata)
              .build();
      OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);

      future.get();
      System.out.println("Model deployment finished");
    }
  }
}

Node.js

Informationen zum Installieren und Verwenden der Clientbibliothek für die AutoML Vision-Objekterkennung finden Sie unter Clientbibliotheken für die AutoML Vision-Objekterkennung. Weitere Informationen finden Sie in der Referenzdokumentation zur AutoML Vision Object Detection Node.js API.

Richten Sie zur Authentifizierung bei der AutoML Vision-Objekterkennung die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;

// Instantiates a client
const client = new AutoMlClient();

async function deployModelWithNodeCount() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
    imageObjectDetectionModelDeploymentMetadata: {
      nodeCount: 2,
    },
  };

  const [operation] = await client.deployModel(request);

  // Wait for operation to complete.
  const [response] = await operation.promise();
  console.log(`Model deployment finished. ${response}`);
}

deployModelWithNodeCount();

Python

Informationen zum Installieren und Verwenden der Clientbibliothek für die AutoML Vision-Objekterkennung finden Sie unter Clientbibliotheken für die AutoML Vision-Objekterkennung. Weitere Informationen finden Sie in der Referenzdokumentation zur AutoML Vision Object Detection Python API.

Richten Sie zur Authentifizierung bei der AutoML Vision-Objekterkennung die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# model_id = "YOUR_MODEL_ID"

client = automl.AutoMlClient()
# Get the full path of the model.
model_full_id = client.model_path(project_id, "us-central1", model_id)

# node count determines the number of nodes to deploy the model on.
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#imageobjectdetectionmodeldeploymentmetadata
metadata = automl.ImageObjectDetectionModelDeploymentMetadata(node_count=2)

request = automl.DeployModelRequest(
    name=model_full_id,
    image_object_detection_model_deployment_metadata=metadata,
)
response = client.deploy_model(request=request)

print(f"Model deployment finished. {response.result()}")

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.