From 61a0bdc24110e8879b83ed2ffca763a5a98454c2 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 7 Oct 2019 14:52:36 -0600 Subject: [PATCH 1/2] Add missing samples for classification and rename object detection sample to add clarification --- .../automl/ClassificationDeployModel.java | 62 ++++++++++++++++ .../ClassificationDeployModelNodeCount.java | 62 ++++++++++++++++ .../automl/ClassificationUndeployModel.java | 62 ++++++++++++++++ ... ObjectDetectionDeployModelNodeCount.java} | 4 +- .../automl/ClassificationDeployModelIT.java | 71 +++++++++++++++++++ ...bjectDetectionDeployModelNodeCountIT.java} | 4 +- 6 files changed, 261 insertions(+), 4 deletions(-) create mode 100644 vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java create mode 100644 vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java create mode 100644 vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java rename vision/automl/src/main/java/com/google/cloud/vision/samples/automl/{DeployModelNodeCount.java => ObjectDetectionDeployModelNodeCount.java} (95%) create mode 100644 vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java rename vision/automl/src/test/java/com/google/cloud/vision/samples/automl/{DeployModelNodeCountIT.java => ObjectDetectionDeployModelNodeCountIT.java} (93%) diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java new file mode 100644 index 00000000000..4a0d2801b4d --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java @@ -0,0 +1,62 @@ +/* + * 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) { + // 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 future = + client.deployModelAsync(deployModelRequest); + + future.get(); + + // Display the deployment details of model. + System.out.println("Model deployment finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_deploy_model] diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java new file mode 100644 index 00000000000..f41e68d3829 --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java @@ -0,0 +1,62 @@ +/* + * 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) { + // 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 future = client.deployModelAsync(request); + future.get(); + System.out.println("Model deployment on 2 nodes finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_deploy_model_node_count] \ No newline at end of file diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java new file mode 100644 index 00000000000..aeae7907038 --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java @@ -0,0 +1,62 @@ +/* + * 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) { + // 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 future = + client.undeployModelAsync(undeployModelRequest); + + future.get(); + + // Display the deployment details of model. + System.out.println("Model undeploy finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_undeploy_model] diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java similarity index 95% rename from vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java rename to vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java index f686fa7e055..f0fea3defb7 100644 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java @@ -28,9 +28,9 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; -class DeployModelNodeCount { +class ObjectDetectionDeployModelNodeCount { - static void deployModelNodeCount(String projectId, String modelId) { + static void objectDetectionDeployModelNodeCount(String projectId, String modelId) { // String projectId = "YOUR_PROJECT_ID"; // String modelId = "YOUR_MODEL_ID"; diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java new file mode 100644 index 00000000000..b76bfc37c76 --- /dev/null +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java @@ -0,0 +1,71 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ClassificationDeployModelIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "ICN3125115511348658176"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployModelApi() { + ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID); + + String got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + + ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID); + + got = bout.toString(); + assertThat(got).contains("Model undeploy finished"); + } + + @Test + public void testDeployModelNodeCountApi() { + ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID); + + String got = bout.toString(); + assertThat(got).contains("Model deployment on 2 nodes finished"); + + ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID); + + got = bout.toString(); + assertThat(got).contains("Model undeploy finished"); + } +} diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java similarity index 93% rename from vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java rename to vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java index c0a3efd7269..dcd9a4fa879 100644 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java @@ -38,7 +38,7 @@ /** Tests for vision "Deploy Model Node Count" sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class DeployModelNodeCountIT { +public class ObjectDetectionDeployModelNodeCountIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String MODEL_ID = "IOD1854128448151224320"; private ByteArrayOutputStream bout; @@ -65,7 +65,7 @@ public void tearDown() throws IOException, InterruptedException, ExecutionExcept @Test public void testModelApi() { - DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID); + ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID); String got = bout.toString(); assertThat(got).contains("Model deployment on 2 nodes finished"); From e8e7814730cefe1a0f4ad2297914763c358ff67a Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 8 Oct 2019 09:20:14 -0600 Subject: [PATCH 2/2] Update test function names --- .../vision/samples/automl/ClassificationDeployModelIT.java | 4 ++-- .../samples/automl/ObjectDetectionDeployModelNodeCountIT.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java index b76bfc37c76..524ab5a503f 100644 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java @@ -44,7 +44,7 @@ public void tearDown() { } @Test - public void testDeployModelApi() { + public void testClassificationDeployModelApi() { ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID); String got = bout.toString(); @@ -57,7 +57,7 @@ public void testDeployModelApi() { } @Test - public void testDeployModelNodeCountApi() { + public void testClassificationDeployModelNodeCountApi() { ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID); String got = bout.toString(); diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java index dcd9a4fa879..17319e03a22 100644 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java @@ -64,7 +64,7 @@ public void tearDown() throws IOException, InterruptedException, ExecutionExcept } @Test - public void testModelApi() { + public void testObjectDetectionDeployModelNodeCountApi() { ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID); String got = bout.toString();