Search in sources :

Example 26 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class VisionClassificationPredict method predict.

static void predict(String projectId, String modelId, String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("score_threshold", // [0.0-1.0] Only produce results higher than this value
        "0.8").build();
        PredictResponse response = client.predict(predictRequest);
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
            System.out.format("Predicted class score: %.2f\n", annotationPayload.getClassification().getScore());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) Image(com.google.cloud.automl.v1.Image) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 27 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class VisionObjectDetectionDeployModelNodeCount method visionObjectDetectionDeployModelNodeCount.

// 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 {
    // 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");
    }
}
Also used : DeployModelRequest(com.google.cloud.automl.v1.DeployModelRequest) Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1.ModelName) ImageObjectDetectionModelDeploymentMetadata(com.google.cloud.automl.v1.ImageObjectDetectionModelDeploymentMetadata) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 28 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class VisionObjectDetectionPredict method predict.

static void predict(String projectId, String modelId, String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("score_threshold", // [0.0-1.0] Only produce results higher than this value
        "0.5").build();
        PredictResponse response = client.predict(predictRequest);
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
            System.out.format("Predicted class score: %.2f\n", annotationPayload.getImageObjectDetection().getScore());
            BoundingPoly boundingPoly = annotationPayload.getImageObjectDetection().getBoundingBox();
            System.out.println("Normalized Vertices:");
            for (NormalizedVertex vertex : boundingPoly.getNormalizedVerticesList()) {
                System.out.format("\tX: %.2f, Y: %.2f\n", vertex.getX(), vertex.getY());
            }
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) BoundingPoly(com.google.cloud.automl.v1.BoundingPoly) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) Image(com.google.cloud.automl.v1.Image) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload) NormalizedVertex(com.google.cloud.automl.v1.NormalizedVertex)

Example 29 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class ClassificationUndeployModel method classificationUndeployModel.

// Deploy a model
static void classificationUndeployModel(String projectId, String modelId) throws IOException, ExecutionException, InterruptedException {
    // 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);
        // Build deploy model request.
        UndeployModelRequest undeployModelRequest = UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
        // Deploy a model with the deploy model request.
        OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync(undeployModelRequest);
        future.get();
        // Display the deployment details of model.
        System.out.println("Model undeploy finished");
    }
}
Also used : Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1.ModelName) UndeployModelRequest(com.google.cloud.automl.v1.UndeployModelRequest) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 30 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class ClassificationDeployModel method classificationDeployModel.

// Deploy a model
static void classificationDeployModel(String projectId, String modelId) throws IOException, ExecutionException, InterruptedException {
    // 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);
        // Build deploy model request.
        DeployModelRequest deployModelRequest = DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
        // Deploy a model with the deploy model request.
        OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(deployModelRequest);
        future.get();
        // Display the deployment details of model.
        System.out.println("Model deployment finished");
    }
}
Also used : DeployModelRequest(com.google.cloud.automl.v1beta1.DeployModelRequest) Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1beta1.ModelName) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient)

Aggregations

ModelName (com.google.cloud.automl.v1.ModelName)24 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)16 ModelName (com.google.cloud.automl.v1beta1.ModelName)15 Empty (com.google.protobuf.Empty)14 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)11 GcsDestination (com.google.cloud.aiplatform.v1.GcsDestination)10 DeployModelRequest (com.google.cloud.automl.v1.DeployModelRequest)10 ModelName (com.google.cloud.aiplatform.v1.ModelName)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 PrintStream (java.io.PrintStream)9 Before (org.junit.Before)9 BatchPredictionJob (com.google.cloud.aiplatform.v1.BatchPredictionJob)8 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)8 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)8 LocationName (com.google.cloud.aiplatform.v1.LocationName)8 Model (com.google.cloud.automl.v1.Model)8 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)8 GcsSource (com.google.cloud.aiplatform.v1.GcsSource)7 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)7 PredictResponse (com.google.cloud.automl.v1.PredictResponse)7