-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: migrate samples from GoogleCloudPlatform/java-docs-samples /…
…vision/automl (#261) * samples: Automl (#1158) * Test push * Vision AutoML * Vision AutoML updates + Translate AutoML * Translate README fixes * Fixing Kokoro failure issue * Language AutoML * Vision AutoML * Translate AutoML files added * Triggering tests * Triggering tests * samples: Automl (#1162) * Test push * Vision AutoML * Vision AutoML updates + Translate AutoML * Translate README fixes * Fixing Kokoro failure issue * Language AutoML * Vision AutoML * Translate AutoML files added * Triggering tests * Triggering tests * Updates based on comments * Updates after review comments * Fixed build issue * samples: Clean up the code and fix the sample tests (#1463) * samples: Prevent Name collisions on tests (#1466) * samples: fix misspelling reported by user (#1451) * samples: Auto-update dependencies. (#1467) * Auto-update dependencies. * Rollback * Auto-update dependencies. * Fix merge errors * Rollback * Fix tests * Fix test * samples: Add new sample for deploying a model with a node count (#1601) * samples: Add missing samples for classification and rename object detection sa… (#1604) * Add missing samples for classification and rename object detection sample to add clarification * Update test function names * samples: automl: fix old beta snippet tests (#1994) * samples: docs: update tests that are failing or not cleaning up resources * samples: update shared config (#2443) * update shared config * Update to 1.0.13 * lint * Fix linting * lint * fix imports Co-authored-by: Les Vogel <[email protected]> * samples: bug: fix importData sample in order to bump libraries-bom version (#3011) Fixes #2943 * samples: automl: remove vision samples no longer on cgc (#2844) - [ x] `pom.xml` parent set to latest `shared-configuration` - [ in progress] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [see below] Tests pass (`mvn -P lint clean verify`) - [x ] Please **merge** this PR for me once it is approved. Need to take a look still at modelApi tests (it looks like the entire file is obviated, but could be refactored to test model creation?) Could use a second set of eyes here. * samples: samples: increased wait time for undeployed model prediction (#3286) * samples: increased wait time for undeployed model prediction * fixed the lint issue Co-authored-by: Nirupa Anantha Kumar <[email protected]> Co-authored-by: Noah Negrey <[email protected]> Co-authored-by: Andrew Ferlitsch <[email protected]> Co-authored-by: DPEBot <[email protected]> Co-authored-by: nnegrey <[email protected]> Co-authored-by: Averi Kitsch <[email protected]> Co-authored-by: Les Vogel <[email protected]> Co-authored-by: Stephanie Wang <[email protected]> Co-authored-by: Anthony <[email protected]> Co-authored-by: Mike <[email protected]>
- Loading branch information
1 parent
eb735d8
commit 2f559d2
Showing
9 changed files
with
755 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...ppets/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.vision.samples.automl; | ||
|
||
// [START automl_vision_classification_deploy_model] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.DeployModelRequest; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class ClassificationDeployModel { | ||
|
||
// Deploy a model | ||
static void classificationDeployModel(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// String projectId = "YOUR_PROJECT_ID"; | ||
// String modelId = "YOUR_MODEL_ID"; | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// 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); | ||
|
||
// Build deploy model request. | ||
DeployModelRequest deployModelRequest = | ||
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); | ||
|
||
// Deploy a model with the deploy model request. | ||
OperationFuture<Empty, OperationMetadata> future = | ||
client.deployModelAsync(deployModelRequest); | ||
|
||
future.get(); | ||
|
||
// Display the deployment details of model. | ||
System.out.println("Model deployment finished"); | ||
} | ||
} | ||
} | ||
// [END automl_vision_classification_deploy_model] |
61 changes: 61 additions & 0 deletions
61
.../main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.vision.samples.automl; | ||
|
||
// [START automl_vision_classification_deploy_model_node_count] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.DeployModelRequest; | ||
import com.google.cloud.automl.v1beta1.ImageClassificationModelDeploymentMetadata; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class ClassificationDeployModelNodeCount { | ||
|
||
// Deploy a model with a specified node count | ||
static void classificationDeployModelNodeCount(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// String projectId = "YOUR_PROJECT_ID"; | ||
// String modelId = "YOUR_MODEL_ID"; | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// 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"); | ||
} | ||
} | ||
} | ||
// [END automl_vision_classification_deploy_model_node_count] |
60 changes: 60 additions & 0 deletions
60
...ets/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.vision.samples.automl; | ||
|
||
// [START automl_vision_classification_undeploy_model] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.cloud.automl.v1beta1.UndeployModelRequest; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class ClassificationUndeployModel { | ||
|
||
// Deploy a model | ||
static void classificationUndeployModel(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// String projectId = "YOUR_PROJECT_ID"; | ||
// String modelId = "YOUR_MODEL_ID"; | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// 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); | ||
|
||
// Build deploy model request. | ||
UndeployModelRequest undeployModelRequest = | ||
UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); | ||
|
||
// Deploy a model with the deploy model request. | ||
OperationFuture<Empty, OperationMetadata> future = | ||
client.undeployModelAsync(undeployModelRequest); | ||
|
||
future.get(); | ||
|
||
// Display the deployment details of model. | ||
System.out.println("Model undeploy finished"); | ||
} | ||
} | ||
} | ||
// [END automl_vision_classification_undeploy_model] |
143 changes: 143 additions & 0 deletions
143
automl/snippets/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.vision.samples.automl; | ||
|
||
// Imports the Google Cloud client library | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics; | ||
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics.ConfidenceMetricsEntry; | ||
import com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata; | ||
import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest; | ||
import com.google.cloud.automl.v1beta1.ListModelsRequest; | ||
import com.google.cloud.automl.v1beta1.LocationName; | ||
import com.google.cloud.automl.v1beta1.Model; | ||
import com.google.cloud.automl.v1beta1.ModelEvaluation; | ||
import com.google.cloud.automl.v1beta1.ModelEvaluationName; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.longrunning.Operation; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
import net.sourceforge.argparse4j.inf.Subparser; | ||
import net.sourceforge.argparse4j.inf.Subparsers; | ||
|
||
/** | ||
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java | ||
* -Dexec.mainClass ='com.google.cloud.vision.samples.automl.ModelApi' -Dexec.args='create_model | ||
* [datasetId] test_model' | ||
*/ | ||
public class ModelApi { | ||
|
||
// [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(); | ||
} | ||
} | ||
// [END automl_vision_create_model] | ||
|
||
public static void main(String[] args) { | ||
argsHelper(args); | ||
} | ||
|
||
static void argsHelper(String[] args) { | ||
ArgumentParser parser = | ||
ArgumentParsers.newFor("ModelApi") | ||
.build() | ||
.defaultHelp(true) | ||
.description("Model API operations."); | ||
Subparsers subparsers = parser.addSubparsers().dest("command"); | ||
|
||
Subparser createModelParser = subparsers.addParser("create_model"); | ||
createModelParser.addArgument("datasetId"); | ||
createModelParser.addArgument("modelName"); | ||
createModelParser.addArgument("trainBudget"); | ||
|
||
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
String computeRegion = System.getenv("REGION_NAME"); | ||
|
||
if (projectId == null || computeRegion == null) { | ||
System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README"); | ||
System.exit(-1); | ||
} | ||
|
||
try { | ||
Namespace ns = parser.parseArgs(args); | ||
if (ns.get("command").equals("create_model")) { | ||
createModel( | ||
projectId, | ||
computeRegion, | ||
ns.getString("datasetId"), | ||
ns.getString("modelName"), | ||
ns.getString("trainBudget")); | ||
} | ||
} catch (ArgumentParserException e) { | ||
parser.handleError(e); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.vision.samples.automl; | ||
|
||
// [START automl_vision_object_detection_deploy_model_node_count] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.DeployModelRequest; | ||
import com.google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class ObjectDetectionDeployModelNodeCount { | ||
|
||
static void objectDetectionDeployModelNodeCount(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// String projectId = "YOUR_PROJECT_ID"; | ||
// String modelId = "YOUR_MODEL_ID"; | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// 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 | ||
ImageObjectDetectionModelDeploymentMetadata deploymentMetadata = | ||
ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); | ||
|
||
DeployModelRequest request = | ||
DeployModelRequest.newBuilder() | ||
.setName(modelFullId.toString()) | ||
.setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata) | ||
.build(); | ||
// Deploy the model | ||
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request); | ||
future.get(); | ||
System.out.println("Model deployment on 2 nodes finished"); | ||
} | ||
} | ||
} | ||
// [END automl_vision_object_detection_deploy_model_node_count] |
Oops, something went wrong.