Search in sources :

Example 1 with ModelName

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

the class BatchPredict method batchPredict.

static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
    // 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);
        // Configure the source of the file from a GCS bucket
        GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
        BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        // Build the request that will be sent to the API
        BatchPredictRequest request = BatchPredictRequest.newBuilder().setName(name.toString()).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
        // Start an asynchronous request
        OperationFuture<BatchPredictResult, OperationMetadata> future = client.batchPredictAsync(request);
        System.out.println("Waiting for operation to complete...");
        BatchPredictResult response = future.get();
        System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
    }
}
Also used : BatchPredictRequest(com.google.cloud.automl.v1beta1.BatchPredictRequest) ModelName(com.google.cloud.automl.v1beta1.ModelName) GcsSource(com.google.cloud.automl.v1beta1.GcsSource) BatchPredictInputConfig(com.google.cloud.automl.v1beta1.BatchPredictInputConfig) BatchPredictOutputConfig(com.google.cloud.automl.v1beta1.BatchPredictOutputConfig) BatchPredictResult(com.google.cloud.automl.v1beta1.BatchPredictResult) GcsDestination(com.google.cloud.automl.v1beta1.GcsDestination) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) PredictionServiceClient(com.google.cloud.automl.v1beta1.PredictionServiceClient)

Example 2 with ModelName

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

the class DeleteModel method deleteModel.

// Delete a model
static void deleteModel(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);
        // Delete a model.
        Empty response = client.deleteModelAsync(modelFullId).get();
        System.out.println("Model deletion started...");
        System.out.println(String.format("Model deleted. %s", response));
    }
}
Also used : Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1beta1.ModelName) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient)

Example 3 with ModelName

use of com.google.cloud.automl.v1beta1.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) throws IOException {
    // 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());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.automl.v1.Image) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 4 with ModelName

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

the class ModelApi method createModel.

// [START automl_vision_create_model]
/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param dataSetId the Id of the dataset to which model is created.
 * @param modelName the Name of the model.
 * @param trainBudget the Budget for training the model.
 */
static void createModel(String projectId, String computeRegion, String dataSetId, String modelName, String trainBudget) {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, computeRegion);
        // Set model metadata.
        ImageClassificationModelMetadata imageClassificationModelMetadata = Long.valueOf(trainBudget) == 0 ? ImageClassificationModelMetadata.newBuilder().build() : ImageClassificationModelMetadata.newBuilder().setTrainBudget(Long.valueOf(trainBudget)).build();
        // Set model name and model metadata for the image dataset.
        Model myModel = Model.newBuilder().setDisplayName(modelName).setDatasetId(dataSetId).setImageClassificationModelMetadata(imageClassificationModelMetadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> response = client.createModelAsync(projectLocation, myModel);
        System.out.println(String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
        System.out.println("Training started...");
    } catch (IOException | ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ImageClassificationModelMetadata(com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata) Model(com.google.cloud.automl.v1beta1.Model) IOException(java.io.IOException) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) ExecutionException(java.util.concurrent.ExecutionException) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName)

Example 5 with ModelName

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

the class DeployModel method deployModel.

// Deploy a model for prediction
static void deployModel(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);
        DeployModelRequest request = DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
        OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
        future.get();
        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)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