|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.vision; |
| 18 | + |
| 19 | +// Imports the Google Cloud client library |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.automl.v1beta1.AutoMlClient; |
| 22 | +import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics; |
| 23 | +import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics.ConfidenceMetricsEntry; |
| 24 | +import com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata; |
| 25 | +import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest; |
| 26 | +import com.google.cloud.automl.v1beta1.ListModelsRequest; |
| 27 | +import com.google.cloud.automl.v1beta1.LocationName; |
| 28 | +import com.google.cloud.automl.v1beta1.Model; |
| 29 | +import com.google.cloud.automl.v1beta1.ModelEvaluation; |
| 30 | +import com.google.cloud.automl.v1beta1.ModelEvaluationName; |
| 31 | +import com.google.cloud.automl.v1beta1.ModelName; |
| 32 | +import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 33 | +import com.google.longrunning.Operation; |
| 34 | +import com.google.protobuf.Empty; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.ExecutionException; |
| 38 | +import net.sourceforge.argparse4j.ArgumentParsers; |
| 39 | +import net.sourceforge.argparse4j.inf.ArgumentParser; |
| 40 | +import net.sourceforge.argparse4j.inf.ArgumentParserException; |
| 41 | +import net.sourceforge.argparse4j.inf.Namespace; |
| 42 | +import net.sourceforge.argparse4j.inf.Subparser; |
| 43 | +import net.sourceforge.argparse4j.inf.Subparsers; |
| 44 | + |
| 45 | +/** |
| 46 | + * Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java |
| 47 | + * -Dexec.mainClass ='com.google.cloud.vision.samples.automl.ModelApi' -Dexec.args='create_model |
| 48 | + * [datasetId] test_model' |
| 49 | + */ |
| 50 | +public class ModelApi { |
| 51 | + |
| 52 | + // [START automl_vision_create_model] |
| 53 | + /** |
| 54 | + * Demonstrates using the AutoML client to create a model. |
| 55 | + * |
| 56 | + * @param projectId the Id of the project. |
| 57 | + * @param computeRegion the Region name. |
| 58 | + * @param dataSetId the Id of the dataset to which model is created. |
| 59 | + * @param modelName the Name of the model. |
| 60 | + * @param trainBudget the Budget for training the model. |
| 61 | + */ |
| 62 | + static void createModel( |
| 63 | + String projectId, |
| 64 | + String computeRegion, |
| 65 | + String dataSetId, |
| 66 | + String modelName, |
| 67 | + String trainBudget) { |
| 68 | + // Instantiates a client |
| 69 | + try (AutoMlClient client = AutoMlClient.create()) { |
| 70 | + |
| 71 | + // A resource that represents Google Cloud Platform location. |
| 72 | + LocationName projectLocation = LocationName.of(projectId, computeRegion); |
| 73 | + |
| 74 | + // Set model metadata. |
| 75 | + ImageClassificationModelMetadata imageClassificationModelMetadata = |
| 76 | + Long.valueOf(trainBudget) == 0 |
| 77 | + ? ImageClassificationModelMetadata.newBuilder().build() |
| 78 | + : ImageClassificationModelMetadata.newBuilder() |
| 79 | + .setTrainBudget(Long.valueOf(trainBudget)) |
| 80 | + .build(); |
| 81 | + |
| 82 | + // Set model name and model metadata for the image dataset. |
| 83 | + Model myModel = |
| 84 | + Model.newBuilder() |
| 85 | + .setDisplayName(modelName) |
| 86 | + .setDatasetId(dataSetId) |
| 87 | + .setImageClassificationModelMetadata(imageClassificationModelMetadata) |
| 88 | + .build(); |
| 89 | + |
| 90 | + // Create a model with the model metadata in the region. |
| 91 | + OperationFuture<Model, OperationMetadata> response = |
| 92 | + client.createModelAsync(projectLocation, myModel); |
| 93 | + |
| 94 | + System.out.println( |
| 95 | + String.format( |
| 96 | + "Training operation name: %s", response.getInitialFuture().get().getName())); |
| 97 | + System.out.println("Training started..."); |
| 98 | + } catch (IOException | ExecutionException | InterruptedException e) { |
| 99 | + e.printStackTrace(); |
| 100 | + } |
| 101 | + } |
| 102 | + // [END automl_vision_create_model] |
| 103 | + |
| 104 | + public static void main(String[] args) { |
| 105 | + argsHelper(args); |
| 106 | + } |
| 107 | + |
| 108 | + static void argsHelper(String[] args) { |
| 109 | + ArgumentParser parser = |
| 110 | + ArgumentParsers.newFor("ModelApi") |
| 111 | + .build() |
| 112 | + .defaultHelp(true) |
| 113 | + .description("Model API operations."); |
| 114 | + Subparsers subparsers = parser.addSubparsers().dest("command"); |
| 115 | + |
| 116 | + Subparser createModelParser = subparsers.addParser("create_model"); |
| 117 | + createModelParser.addArgument("datasetId"); |
| 118 | + createModelParser.addArgument("modelName"); |
| 119 | + createModelParser.addArgument("trainBudget"); |
| 120 | + |
| 121 | + String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 122 | + String computeRegion = System.getenv("REGION_NAME"); |
| 123 | + |
| 124 | + if (projectId == null || computeRegion == null) { |
| 125 | + System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README"); |
| 126 | + System.exit(-1); |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + Namespace ns = parser.parseArgs(args); |
| 131 | + if (ns.get("command").equals("create_model")) { |
| 132 | + createModel( |
| 133 | + projectId, |
| 134 | + computeRegion, |
| 135 | + ns.getString("datasetId"), |
| 136 | + ns.getString("modelName"), |
| 137 | + ns.getString("trainBudget")); |
| 138 | + } |
| 139 | + } catch (ArgumentParserException e) { |
| 140 | + parser.handleError(e); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments