Search in sources :

Example 21 with ModelName

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

the class ListModelEvaluations method listModelEvaluations.

// List model evaluations
static void listModelEvaluations(String projectId, String modelId) throws IOException {
    // 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);
        ListModelEvaluationsRequest modelEvaluationsrequest = ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build();
        // List all the model evaluations in the model by applying filter.
        System.out.println("List of model evaluations:");
        for (ModelEvaluation modelEvaluation : client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) {
            System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName());
            System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId());
            System.out.println("Create Time:");
            System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds());
            System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9);
            System.out.format("Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
            // [END automl_language_sentiment_analysis_list_model_evaluations]
            // [END automl_language_text_classification_list_model_evaluations]
            // [END automl_translate_list_model_evaluations]
            // [END automl_vision_classification_list_model_evaluations]
            // [END automl_vision_object_detection_list_model_evaluations]
            System.out.format("Entity Extraction Model Evaluation Metrics: %s\n", modelEvaluation.getTextExtractionEvaluationMetrics());
            // [END automl_language_entity_extraction_list_model_evaluations]
            // [START automl_language_sentiment_analysis_list_model_evaluations]
            System.out.format("Sentiment Analysis Model Evaluation Metrics: %s\n", modelEvaluation.getTextSentimentEvaluationMetrics());
            // [END automl_language_sentiment_analysis_list_model_evaluations]
            // [START automl_language_text_classification_list_model_evaluations]
            // [START automl_vision_classification_list_model_evaluations]
            System.out.format("Classification Model Evaluation Metrics: %s\n", modelEvaluation.getClassificationEvaluationMetrics());
            // [END automl_language_text_classification_list_model_evaluations]
            // [END automl_vision_classification_list_model_evaluations]
            // [START automl_translate_list_model_evaluations]
            System.out.format("Translate Model Evaluation Metrics: %s\n", modelEvaluation.getTranslationEvaluationMetrics());
            // [END automl_translate_list_model_evaluations]
            // [START automl_vision_object_detection_list_model_evaluations]
            System.out.format("Object Detection Model Evaluation Metrics: %s\n", modelEvaluation.getImageObjectDetectionEvaluationMetrics());
        // [START automl_language_entity_extraction_list_model_evaluations]
        // [START automl_language_sentiment_analysis_list_model_evaluations]
        // [START automl_language_text_classification_list_model_evaluations]
        // [START automl_translate_list_model_evaluations]
        // [START automl_vision_classification_list_model_evaluations]
        }
    }
}
Also used : ModelEvaluation(com.google.cloud.automl.v1.ModelEvaluation) ModelName(com.google.cloud.automl.v1.ModelName) ListModelEvaluationsRequest(com.google.cloud.automl.v1.ListModelEvaluationsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 22 with ModelName

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

the class UndeployModel method undeployModel.

// Undeploy a model from prediction
static void undeployModel(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);
        UndeployModelRequest request = UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
        OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync(request);
        future.get();
        System.out.println("Model undeployment 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 23 with ModelName

use of com.google.cloud.automl.v1beta1.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 24 with ModelName

use of com.google.cloud.automl.v1beta1.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 25 with ModelName

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

the class ClassificationDeployModelNodeCount method classificationDeployModelNodeCount.

// Deploy a model with a specified node count
static void classificationDeployModelNodeCount(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);
        // Set how many nodes the model is deployed on
        ImageClassificationModelDeploymentMetadata deploymentMetadata = ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
        DeployModelRequest request = DeployModelRequest.newBuilder().setName(modelFullId.toString()).setImageClassificationModelDeploymentMetadata(deploymentMetadata).build();
        // Deploy the model
        OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
        future.get();
        System.out.println("Model deployment on 2 nodes 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) ImageClassificationModelDeploymentMetadata(com.google.cloud.automl.v1beta1.ImageClassificationModelDeploymentMetadata) 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)13 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 DeployModelRequest (com.google.cloud.automl.v1.DeployModelRequest)10 OperationMetadata (com.google.cloud.automl.v1beta1.OperationMetadata)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 PrintStream (java.io.PrintStream)9 Before (org.junit.Before)9 Model (com.google.cloud.automl.v1.Model)8 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)8 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)7 PredictResponse (com.google.cloud.automl.v1.PredictResponse)7 AnnotationPayload (com.google.cloud.automl.v1.AnnotationPayload)6 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)6 PredictRequest (com.google.cloud.automl.v1.PredictRequest)6 DeployModelRequest (com.google.cloud.automl.v1beta1.DeployModelRequest)5 TextSnippet (com.google.cloud.automl.v1.TextSnippet)4 Model (com.google.cloud.automl.v1beta1.Model)4