Search in sources :

Example 31 with ModelName

use of com.google.cloud.aiplatform.v1.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)

Example 32 with ModelName

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

the class PredictionApi method predict.

// [START automl_vision_predict]
/**
 * Demonstrates using the AutoML client to predict an image.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param modelId the Id of the model which will be used for text classification.
 * @param filePath the Local text file path of the content to be classified.
 * @param scoreThreshold the Confidence score. Only classifications with confidence score above
 *     scoreThreshold are displayed.
 */
static void predict(String projectId, String computeRegion, String modelId, String filePath, String scoreThreshold) {
    // Instantiate client for prediction service.
    try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, computeRegion, modelId);
        // Read the image and assign to payload.
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload examplePayload = ExamplePayload.newBuilder().setImage(image).build();
        // Additional parameters that can be provided for prediction e.g. Score Threshold
        Map<String, String> params = new HashMap<>();
        if (scoreThreshold != null) {
            params.put("score_threshold", scoreThreshold);
        }
        // Perform the AutoML Prediction request
        PredictResponse response = predictionClient.predict(name, examplePayload, params);
        System.out.println("Prediction results:");
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.println("Predicted class name :" + annotationPayload.getDisplayName());
            System.out.println("Predicted class score :" + annotationPayload.getClassification().getScore());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ModelName(com.google.cloud.automl.v1beta1.ModelName) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1beta1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1beta1.ExamplePayload) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) Image(com.google.cloud.automl.v1beta1.Image) PredictionServiceClient(com.google.cloud.automl.v1beta1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1beta1.AnnotationPayload)

Example 33 with ModelName

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

the class GetModelEvaluationTest method setUp.

@Before
public void setUp() throws IOException {
    // Get a model evaluation ID from the List request first to be used in the Get call
    try (AutoMlClient client = AutoMlClient.create()) {
        ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID);
        ListModelEvaluationsRequest modelEvaluationsrequest = ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build();
        ModelEvaluation modelEvaluation = client.listModelEvaluations(modelEvaluationsrequest).getPage().getValues().iterator().next();
        modelEvaluationId = modelEvaluation.getName().split("/modelEvaluations/")[1];
    }
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);
}
Also used : PrintStream(java.io.PrintStream) ModelEvaluation(com.google.cloud.automl.v1.ModelEvaluation) ModelName(com.google.cloud.automl.v1.ModelName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ListModelEvaluationsRequest(com.google.cloud.automl.v1.ListModelEvaluationsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) Before(org.junit.Before)

Example 34 with ModelName

use of com.google.cloud.aiplatform.v1.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 35 with ModelName

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

the class TranslatePredict 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);
        String content = new String(Files.readAllBytes(Paths.get(filePath)));
        TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build();
        ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
        PredictResponse response = client.predict(predictRequest);
        TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent();
        System.out.format("Translated Content: %s\n", translatedContent.getContent());
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) TextSnippet(com.google.cloud.automl.v1.TextSnippet) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient)

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