Skip to content

Commit 2f559d2

Browse files
chingor13nirupa-kumarnnegreyandrewferlitschdpebot
authored
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]>
1 parent eb735d8 commit 2f559d2

9 files changed

+755
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.samples.automl;
18+
19+
// [START automl_vision_classification_deploy_model]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23+
import com.google.cloud.automl.v1beta1.ModelName;
24+
import com.google.cloud.automl.v1beta1.OperationMetadata;
25+
import com.google.protobuf.Empty;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
class ClassificationDeployModel {
30+
31+
// Deploy a model
32+
static void classificationDeployModel(String projectId, String modelId)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String modelId = "YOUR_MODEL_ID";
36+
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (AutoMlClient client = AutoMlClient.create()) {
41+
42+
// Get the full path of the model.
43+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
44+
45+
// Build deploy model request.
46+
DeployModelRequest deployModelRequest =
47+
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
48+
49+
// Deploy a model with the deploy model request.
50+
OperationFuture<Empty, OperationMetadata> future =
51+
client.deployModelAsync(deployModelRequest);
52+
53+
future.get();
54+
55+
// Display the deployment details of model.
56+
System.out.println("Model deployment finished");
57+
}
58+
}
59+
}
60+
// [END automl_vision_classification_deploy_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.samples.automl;
18+
19+
// [START automl_vision_classification_deploy_model_node_count]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23+
import com.google.cloud.automl.v1beta1.ImageClassificationModelDeploymentMetadata;
24+
import com.google.cloud.automl.v1beta1.ModelName;
25+
import com.google.cloud.automl.v1beta1.OperationMetadata;
26+
import com.google.protobuf.Empty;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class ClassificationDeployModelNodeCount {
31+
32+
// Deploy a model with a specified node count
33+
static void classificationDeployModelNodeCount(String projectId, String modelId)
34+
throws IOException, ExecutionException, InterruptedException {
35+
// String projectId = "YOUR_PROJECT_ID";
36+
// String modelId = "YOUR_MODEL_ID";
37+
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// Get the full path of the model.
43+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
44+
45+
// Set how many nodes the model is deployed on
46+
ImageClassificationModelDeploymentMetadata deploymentMetadata =
47+
ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
48+
49+
DeployModelRequest request =
50+
DeployModelRequest.newBuilder()
51+
.setName(modelFullId.toString())
52+
.setImageClassificationModelDeploymentMetadata(deploymentMetadata)
53+
.build();
54+
// Deploy the model
55+
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
56+
future.get();
57+
System.out.println("Model deployment on 2 nodes finished");
58+
}
59+
}
60+
}
61+
// [END automl_vision_classification_deploy_model_node_count]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.samples.automl;
18+
19+
// [START automl_vision_classification_undeploy_model]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.ModelName;
23+
import com.google.cloud.automl.v1beta1.OperationMetadata;
24+
import com.google.cloud.automl.v1beta1.UndeployModelRequest;
25+
import com.google.protobuf.Empty;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
class ClassificationUndeployModel {
30+
31+
// Deploy a model
32+
static void classificationUndeployModel(String projectId, String modelId)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String modelId = "YOUR_MODEL_ID";
36+
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (AutoMlClient client = AutoMlClient.create()) {
41+
42+
// Get the full path of the model.
43+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
44+
45+
// Build deploy model request.
46+
UndeployModelRequest undeployModelRequest =
47+
UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
48+
49+
// Deploy a model with the deploy model request.
50+
OperationFuture<Empty, OperationMetadata> future =
51+
client.undeployModelAsync(undeployModelRequest);
52+
53+
future.get();
54+
55+
// Display the deployment details of model.
56+
System.out.println("Model undeploy finished");
57+
}
58+
}
59+
}
60+
// [END automl_vision_classification_undeploy_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.samples.automl;
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.samples.automl;
18+
19+
// [START automl_vision_object_detection_deploy_model_node_count]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23+
import com.google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata;
24+
import com.google.cloud.automl.v1beta1.ModelName;
25+
import com.google.cloud.automl.v1beta1.OperationMetadata;
26+
import com.google.protobuf.Empty;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class ObjectDetectionDeployModelNodeCount {
31+
32+
static void objectDetectionDeployModelNodeCount(String projectId, String modelId)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String modelId = "YOUR_MODEL_ID";
36+
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (AutoMlClient client = AutoMlClient.create()) {
41+
// Get the full path of the model.
42+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
43+
44+
// Set how many nodes the model is deployed on
45+
ImageObjectDetectionModelDeploymentMetadata deploymentMetadata =
46+
ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
47+
48+
DeployModelRequest request =
49+
DeployModelRequest.newBuilder()
50+
.setName(modelFullId.toString())
51+
.setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata)
52+
.build();
53+
// Deploy the model
54+
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
55+
future.get();
56+
System.out.println("Model deployment on 2 nodes finished");
57+
}
58+
}
59+
}
60+
// [END automl_vision_object_detection_deploy_model_node_count]

0 commit comments

Comments
 (0)