Skip to content

Commit 82790c1

Browse files
authored
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
1 parent e402342 commit 82790c1

File tree

6 files changed

+262
-5
lines changed

6 files changed

+262
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class ClassificationDeployModel {
31+
32+
// Deploy a model
33+
static void classificationDeployModel(String projectId, String modelId) {
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+
} catch (IOException | InterruptedException | ExecutionException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
// [END automl_vision_classification_deploy_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
31+
class ClassificationDeployModelNodeCount {
32+
33+
// Deploy a model with a specified node count
34+
static void classificationDeployModelNodeCount(String projectId, String modelId) {
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 = DeployModelRequest.newBuilder()
50+
.setName(modelFullId.toString())
51+
.setImageClassificationModelDeploymentMetadata(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+
} catch (IOException | InterruptedException | ExecutionException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
// [END automl_vision_classification_deploy_model_node_count]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class ClassificationUndeployModel {
31+
32+
// Deploy a model
33+
static void classificationUndeployModel(String projectId, String modelId) {
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+
} catch (IOException | InterruptedException | ExecutionException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
// [END automl_vision_classification_undeploy_model]

vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java renamed to vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import java.io.IOException;
2929
import java.util.concurrent.ExecutionException;
3030

31-
class DeployModelNodeCount {
31+
class ObjectDetectionDeployModelNodeCount {
3232

33-
static void deployModelNodeCount(String projectId, String modelId) {
33+
static void objectDetectionDeployModelNodeCount(String projectId, String modelId) {
3434
// String projectId = "YOUR_PROJECT_ID";
3535
// String modelId = "YOUR_MODEL_ID";
3636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class ClassificationDeployModelIT {
29+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
30+
private static final String MODEL_ID = "ICN3125115511348658176";
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
34+
@Before
35+
public void setUp() {
36+
bout = new ByteArrayOutputStream();
37+
out = new PrintStream(bout);
38+
System.setOut(out);
39+
}
40+
41+
@After
42+
public void tearDown() {
43+
System.setOut(null);
44+
}
45+
46+
@Test
47+
public void testClassificationDeployModelApi() {
48+
ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID);
49+
50+
String got = bout.toString();
51+
assertThat(got).contains("Model deployment finished");
52+
53+
ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);
54+
55+
got = bout.toString();
56+
assertThat(got).contains("Model undeploy finished");
57+
}
58+
59+
@Test
60+
public void testClassificationDeployModelNodeCountApi() {
61+
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);
62+
63+
String got = bout.toString();
64+
assertThat(got).contains("Model deployment on 2 nodes finished");
65+
66+
ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);
67+
68+
got = bout.toString();
69+
assertThat(got).contains("Model undeploy finished");
70+
}
71+
}

vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java renamed to vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/** Tests for vision "Deploy Model Node Count" sample. */
3939
@RunWith(JUnit4.class)
4040
@SuppressWarnings("checkstyle:abbreviationaswordinname")
41-
public class DeployModelNodeCountIT {
41+
public class ObjectDetectionDeployModelNodeCountIT {
4242
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4343
private static final String MODEL_ID = "IOD1854128448151224320";
4444
private ByteArrayOutputStream bout;
@@ -64,8 +64,8 @@ public void tearDown() throws IOException, InterruptedException, ExecutionExcept
6464
}
6565

6666
@Test
67-
public void testModelApi() {
68-
DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID);
67+
public void testObjectDetectionDeployModelNodeCountApi() {
68+
ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID);
6969

7070
String got = bout.toString();
7171
assertThat(got).contains("Model deployment on 2 nodes finished");

0 commit comments

Comments
 (0)