Search in sources :

Example 16 with Model

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

the class PredictCustomTrainedModelSample method predictCustomTrainedModel.

static void predictCustomTrainedModel(String project, String endpointId, String instance) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        ListValue.Builder listValue = ListValue.newBuilder();
        JsonFormat.parser().merge(instance, listValue);
        List<Value> instanceList = listValue.getValuesList();
        PredictRequest predictRequest = PredictRequest.newBuilder().setEndpoint(endpointName.toString()).addAllInstances(instanceList).build();
        PredictResponse predictResponse = predictionServiceClient.predict(predictRequest);
        System.out.println("Predict Custom Trained model Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            System.out.format("\tPrediction: %s\n", prediction);
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictRequest(com.google.cloud.aiplatform.v1.PredictRequest) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 17 with Model

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

the class PredictImageClassificationSample method predictImageClassification.

static void predictImageClassification(String project, String fileName, String endpointId) throws IOException {
    PredictionServiceSettings settings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(settings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        byte[] contents = Base64.encodeBase64(Files.readAllBytes(Paths.get(fileName)));
        String content = new String(contents, StandardCharsets.UTF_8);
        ImageClassificationPredictionInstance predictionInstance = ImageClassificationPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(predictionInstance));
        ImageClassificationPredictionParams predictionParams = ImageClassificationPredictionParams.newBuilder().setConfidenceThreshold((float) 0.5).setMaxPredictions(5).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(predictionParams));
        System.out.println("Predict Image Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            ClassificationPredictionResult.Builder resultBuilder = ClassificationPredictionResult.newBuilder();
            // Display names and confidences values correspond to
            // IDs in the ID list.
            ClassificationPredictionResult result = (ClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            int counter = 0;
            for (Long id : result.getIdsList()) {
                System.out.printf("Label ID: %d\n", id);
                System.out.printf("Label: %s\n", result.getDisplayNames(counter));
                System.out.printf("Confidence: %.4f\n", result.getConfidences(counter));
                counter++;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ImageClassificationPredictionParams(com.google.cloud.aiplatform.v1.schema.predict.params.ImageClassificationPredictionParams) Value(com.google.protobuf.Value) ImageClassificationPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance) ClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)

Example 18 with Model

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

the class PredictTabularClassificationSample method predictTabularClassification.

static void predictTabularClassification(String instance, String project, String endpointId) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        ListValue.Builder listValue = ListValue.newBuilder();
        JsonFormat.parser().merge(instance, listValue);
        List<Value> instanceList = listValue.getValuesList();
        Value parameters = Value.newBuilder().setListValue(listValue).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instanceList, parameters);
        System.out.println("Predict Tabular Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TabularClassificationPredictionResult.Builder resultBuilder = TabularClassificationPredictionResult.newBuilder();
            TabularClassificationPredictionResult result = (TabularClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getClassesCount(); i++) {
                System.out.printf("\tClass: %s", result.getClasses(i));
                System.out.printf("\tScore: %f", result.getScores(i));
            }
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) TabularClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularClassificationPredictionResult) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 19 with Model

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

the class GetModelEvaluationImageClassificationSample method getModelEvaluationImageClassificationSample.

static void getModelEvaluationImageClassificationSample(String project, String modelId, String evaluationId) throws IOException {
    ModelServiceSettings modelServiceSettings = ModelServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
        String location = "us-central1";
        ModelEvaluationName modelEvaluationName = ModelEvaluationName.of(project, location, modelId, evaluationId);
        ModelEvaluation modelEvaluation = modelServiceClient.getModelEvaluation(modelEvaluationName);
        System.out.println("Get Model Evaluation Image Classification Response");
        System.out.format("Model Name: %s\n", modelEvaluation.getName());
        System.out.format("Metrics Schema Uri: %s\n", modelEvaluation.getMetricsSchemaUri());
        System.out.format("Metrics: %s\n", modelEvaluation.getMetrics());
        System.out.format("Create Time: %s\n", modelEvaluation.getCreateTime());
        System.out.format("Slice Dimensions: %s\n", modelEvaluation.getSliceDimensionsList());
    }
}
Also used : ModelEvaluationName(com.google.cloud.aiplatform.v1.ModelEvaluationName) ModelEvaluation(com.google.cloud.aiplatform.v1.ModelEvaluation) ModelServiceSettings(com.google.cloud.aiplatform.v1.ModelServiceSettings) ModelServiceClient(com.google.cloud.aiplatform.v1.ModelServiceClient)

Example 20 with Model

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

the class GetModelEvaluationTabularClassificationSample method getModelEvaluationTabularClassification.

static void getModelEvaluationTabularClassification(String project, String modelId, String evaluationId) throws IOException {
    ModelServiceSettings modelServiceSettings = ModelServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
        String location = "us-central1";
        ModelEvaluationName modelEvaluationName = ModelEvaluationName.of(project, location, modelId, evaluationId);
        ModelEvaluation modelEvaluation = modelServiceClient.getModelEvaluation(modelEvaluationName);
        System.out.println("Get Model Evaluation Tabular Classification Response");
        System.out.format("\tName: %s\n", modelEvaluation.getName());
        System.out.format("\tMetrics Schema Uri: %s\n", modelEvaluation.getMetricsSchemaUri());
        System.out.format("\tMetrics: %s\n", modelEvaluation.getMetrics());
        System.out.format("\tCreate Time: %s\n", modelEvaluation.getCreateTime());
        System.out.format("\tSlice Dimensions: %s\n", modelEvaluation.getSliceDimensionsList());
    }
}
Also used : ModelEvaluationName(com.google.cloud.aiplatform.v1.ModelEvaluationName) ModelEvaluation(com.google.cloud.aiplatform.v1.ModelEvaluation) ModelServiceSettings(com.google.cloud.aiplatform.v1.ModelServiceSettings) ModelServiceClient(com.google.cloud.aiplatform.v1.ModelServiceClient)

Aggregations

Test (org.junit.Test)52 Model (org.eclipse.xtext.valueconverter.bug250313.Model)30 LocationName (com.google.cloud.aiplatform.v1.LocationName)17 ModelServiceClient (com.google.cloud.aiplatform.v1.ModelServiceClient)17 ModelServiceSettings (com.google.cloud.aiplatform.v1.ModelServiceSettings)17 Model (com.google.cloud.aiplatform.v1.Model)16 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)16 Model (com.google.cloud.automl.v1.Model)16 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)16 PipelineServiceClient (com.google.cloud.aiplatform.v1.PipelineServiceClient)14 PipelineServiceSettings (com.google.cloud.aiplatform.v1.PipelineServiceSettings)14 TrainingPipeline (com.google.cloud.aiplatform.v1.TrainingPipeline)14 Value (com.google.protobuf.Value)14 InputDataConfig (com.google.cloud.aiplatform.v1.InputDataConfig)13 ModelContainerSpec (com.google.cloud.aiplatform.v1.ModelContainerSpec)13 ArrayList (java.util.ArrayList)12 EndpointName (com.google.cloud.aiplatform.v1.EndpointName)11 FilterSplit (com.google.cloud.aiplatform.v1.FilterSplit)11 FractionSplit (com.google.cloud.aiplatform.v1.FractionSplit)11 PredefinedSplit (com.google.cloud.aiplatform.v1.PredefinedSplit)11